I have a container with a fixed height of 850px. I want a background-image that will keep the entire background covered at all levels of zoom. I would use background-size: cover
however, I need it to be a little bit bigger than cover
will make it. I want some of the background to be bleed out of the container and be unseen, for the purpose of creating a parallax.
Basically how can I use background-size: cover
, and increase the size a little bit on top of that?
edit: Forgot to mention I'm also using background-attachment: fixed
, so if I'm not mistaken, its actually calculated via the size of the browser window(?) and not the 850px tall container. How would I add that extra bleed in this case?
There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated ...
If you only provide one value (e.g. background-size: 400px ) it counts for the width, and the height is set to auto . You can use any CSS size units you like, including pixels, percentages, ems, viewport units, etc.
When we set background-size: 113% , it means the the width can be max 113% of the container's width, which would be, 63.28px and this is roughly 50% of the original image's width.
The background-size CSS property sets the size of the element's background image. The image can be left to its natural size, stretched, or constrained to fit the available space.
Simply using background-size: auto 110%;
did the trick.
This is an old thread but I found a solution to this so thought I'd share.
Using background-size: auto 110%;
doesn't really work because you lose the cover
functionality. For responsive and more dynamic elements, you can end up with undesired blank spaces.
Instead, create a wrapping div with position:relative; overflow:hidden;
. Then within that create your background div with position:absolute;
and some additional settings. See below.
.banner-wrapper {
position: relative;
min-height: 340px;
width: 100%;
overflow: hidden;
border: 1px solid #ccc;
}
.banner {
position: absolute;
left: -40px;
right: -40px;
top: -40px;
bottom: -40px;
width: calc(100% + 40px);
background: url(https://upload.wikimedia.org/wikipedia/commons/2/25/Lang%27s_short_tail_blue_%28Leptotes_pirithous%29_male_underside.jpg) center center no-repeat;
background-size: cover;
}
<div class="banner-wrapper">
<div class="banner">
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With