I am working on a responsive page, and have a large background image that I wish to keep in proportion no matter what size device a user is on. When I use an img
tag for this it stretches way beyond the bottom of the screen, so I am using the background-img
property instead . This is working, but I began by using an img
tag and couldn't replicate the same behavior as the background-img
.
Based on this SO Post I believe that I should be implement the same effect using either img
or background-img
From my reading it appears that I should be able to use either and create the same experience for the user, so would love to know how to replicate the background-img
properties from the code below into the code with my img
tag.
Please see the code below.
With the background-image
property set. This works as I like.
HTML
<div id="container">
<div class="main-image">
</div>
</div>
CSS
html, body {
width: 100%;
margin: 0;
}
.main-image {
background: url(http://i2.cdn.turner.com/cnnnext/dam/assets/151009172735-02-tbt-bob-dylan-restricted-super-169.jpg) center center no-repeat;
background-size: cover;
max-height: 90%;
}
With <img>
attribute. What would be needed to be done to replicate the same behavior as the css above? I have tried playing around with all attributes but cannot figure this out.
HTML
<div id="container">
<div class="main-image">
<img src="http://i2.cdn.turner.com/cnnnext/dam/assets/151009172735-02-tbt-bob-dylan-restricted-super-169.jpg">
</div>
</div>
CSS
html, body {
width: 100%;
margin: 0;
}
.main-image {
width: 100%;
max-height: 90%;
}
img {
width: 100%;
}