It is generally not recommended to set the width, especially max-width
, on the body
tag due to various reasons.
In this scenario, the issue lies in defining the width using the body
tag, causing confusion and problems.
You can refer to my code example on jsfiddle: http://jsfiddle.net/abc123/25/
To achieve the desired effect, you will need a #container
div (CSS cannot directly target the body
tag).
To address the request for the image to flow 100% of the container size, simply add width: 100%
to the image. Use a unique class to prevent this from affecting all <img>
elements.
Here is the necessary HTML:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<div id="container">
<img src="http://www.example.com/image.jpg">
</div>
</body>
</html>
And the corresponding CSS:
body
{
}
#container {
padding: 15px;
max-width: 250px;
overflow: hidden;
background: blue;
}
img {
width: 100%
}