Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale a background image to cover, times an extra scaling factor?

I have a background image which I would like to cover the element, using background-size: cover; However, I'd also like to scale it up 110% in both directions, beyond what cover does, so that I can subsequently move it around with background-position.

Put another way, I'd like background-size: cover to treat the surrounding div as if it were 110% larger in both directions.

Is there a way to do this purely in CSS?

If I understand correctly, this would be a way to do it, but max() is not standard CSS3:

background-size: max(auto 110%) max(auto 110%);
like image 714
ptomato Avatar asked Dec 02 '15 20:12

ptomato


People also ask

How do I make the background image fit my screen size?

Using CSS, you can set the background-size property for the image to fit the screen (viewport). The background-size property has a value of cover . It instructs browsers to automatically scale the width and height of a responsive background image to be the same or bigger than the viewport.

What scales the background image as large as possible?

The background-size CSS property scale the background image to be as large as possible so that the background area is completely covered by the background image.

How do I resize a background image in CSS?

The background-size property is used to set the background image size using CSS. Use height and width property to set the size of the background image.


2 Answers

I have created a Fiddle for you to check out. I believe I understood the question correctly but please let me know if I am off base.

I wrapped the div that has the background-image in another div like so:

<div class="hero-container">
    <div class="row" id="hero"></div>
</div>

and applied the styles like so:

.hero-container {
    overflow: hidden;
    width: 100%;
    height: 100vh;
}
#hero {
    background: url('https://i.ytimg.com/vi/ReF6iQ7M5_A/maxresdefault.jpg') no-repeat center center scroll;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    position: relative;
    width: 100%;
    height: 110vh;
    margin-bottom: 0px;
    right: 0;
}

Play around with the fiddle by changing the height: 110vh and let me know if this is what you were looking for, or if I am at least on the right track.

Hope this helps!

EDIT*: I removed the transition and the padding as these are not necessary.

If you would like to do this with a container that has a fixed height you can change the .hero-container height to 500px or something and then just use 110% instead of 110vh in the height for the #hero div.

like image 57
Matthew Avatar answered Oct 25 '22 07:10

Matthew


If I do understood your question correctly, I guess you can try this below:

.box {
  width: 40vw;
  height: 40vh;
  background: url('https://i.ytimg.com/vi/ReF6iQ7M5_A/maxresdefault.jpg') no-repeat center;
  background-size: cover;
}

.box:hover {
  background-size: 140%;
}
<div class="box"></div>
like image 44
Megrax Avatar answered Oct 25 '22 05:10

Megrax