Reputation: 7
I have a responsive site and am trying to setup the background image so that the height is always 100%. The width doesn't matter so much, but there is elements at the bottom of the picture that always need to show. I can't quite get my coding right though:
CSS:
body.cms-home .main-container {
margin-top: 20px;
background-image: url("../images/landing.jpg");
background-repeat: no-repeat;
height: 100%;
background-position: center;
width: auto;
HTML:
<div class="main-container">
</div>
My website is through Magneto, but I don't think that should make any different to the CSS.
What am I missing?
UPDATE: Just to confirm as it seems everyone is confused. I want my background image to always be 100% (no shorter). So if the browser is made shorter the image scales down to ensure that the full height of the image is always shown. So I guess the image size is dominated by the height of the browser.
Upvotes: 0
Views: 1700
Reputation: 24375
You could use the vh
sizing element. It stands for viewport height
, and it's supported in most modern browsers. Apply it to your .main-container
element so that it is always given a full height and width of the browsers window.
body.cms-home .main-container {
background-image: url("../images/landing.jpg");
background-repeat: no-repeat;
height: 100vh;
width: 100%;
background-size: cover;
background-position: center;
}
Note that you may also want to give it a position: fixed
if your page is scrollable.
Upvotes: 0
Reputation: 231
body.cms-home .main-container {
margin-top: 20px;
background-image: url("../images/landing.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
Upvotes: 2