Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set background-size to "cover", plus a little more in CSS?

Tags:

html

css

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?

like image 287
Sam D20 Avatar asked Jun 14 '14 20:06

Sam D20


People also ask

How do I limit a background image in CSS?

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 ...

Can we specify the background-size in percentage in CSS?

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.

Can we specify the background-size in percentage?

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.

What is background-size cover in CSS?

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.


2 Answers

Simply using background-size: auto 110%; did the trick.

like image 158
Sam D20 Avatar answered Oct 05 '22 10:10

Sam D20


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>  
like image 45
Hubbs Avatar answered Oct 05 '22 08:10

Hubbs