Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically resize an image for a mobile site?

Tags:

html

css

image

I tried a Google search on this and still cannot figure out how to resize an image by its width for various mobile devices. Here is my attempt:

CSS:

img.test {     width: 100%;     height: auto; } 

HTML:

<html>     <head>         <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">         <meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0;   user-scalable=0;" name="viewport">         <link rel="stylesheet" href="../includes/style.css">     </head>     <img class="test"  src="../includes/foo.jpg"> 

But the image is still being loaded with its original scaling. I am a css and html newb so any help would be great!

Edit:

Thanks for the responses. Here is a revised version that now works.

HTML:

   <html>     <head>         <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">         <meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0;  user-scalable=0;" name="viewport">         <link rel="stylesheet" href="../includes/style.css">     </head>     <body width = "device-width">         <img class="test"  src="../includes/buoy_map.jpg">     </body> 
like image 796
Richard Avatar asked Oct 05 '11 13:10

Richard


People also ask

How do I resize an image automatically?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

How do I make my image fit responsive?

To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.


1 Answers

img {   max-width: 100%; } 

Should set the image to take up 100% of its containing element.

like image 99
Doozer Blake Avatar answered Oct 17 '22 02:10

Doozer Blake