Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying transform:scale to just the background image

I am trying to achieve a classy zoom in effect on a div's background image with background-size:cover using transform:scale.

The problem I am facing is that the scale seems to enlarge the div as the background-image zooms in.

Is there a way to only apply the effect to the background image? Or am I doing something wrong?

.despre-noi-image {
  width: 40%;
  height: 500px;
  background: url('https://static.independent.co.uk/s3fs-public/styles/story_large/public/thumbnails/image/2017/04/18/16/wine-evening.jpg') no-repeat center center;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  text-align: left;
  background-position-x: 50%;
  background-position-y: 0%;
  -webkit-animation: zoomin 10s linear;
  animation: zoomin 10s linear;
  animation-fill-mode: forwards;
}

@-webkit-keyframes zoomin {
  0% {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  100% {
    -webkit-transform: scale(2);
    transform: scale(2);
  }
}
<div class="despre-noi-image">
  &nbsp;
</div>
<!-- despre-noi-image -->
like image 201
raduzrc Avatar asked Nov 10 '17 04:11

raduzrc


People also ask

How do I automatically resize a background image?

Use background-size property to cover the entire viewport The CSS background-size property can have the value of cover . The cover value tells the browser to automatically and proportionally scale the background image's width and height so that they are always equal to, or greater than, the viewport's width/height.

Does object fit work with background image?

Adding a Background to an Image With object-fit: contain # We would benefit from that when also using object-fit: contain . In the example below, we have a grid of images. When the aspect ratios of the image and the container are different, the background color will appear.

How do I stretch a background image in CSS?

You can use the CSS background-size: cover; to stretch and scale an image in the background with CSS only. This scales the image as large as possible in such a way that the background area is completely covered by the background image, while preserving its intrinsic aspect ratio.

How do I stretch a background image to fit?

When you work with background images, you may want an image to stretch to fit the page despite the wide range of devices and screen sizes. The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover.


1 Answers

CSS is very simple. You can change background size with a transition on hover instead of scaling.

.despre-noi-image {
    width: 40%;
    height: 500px;
    background: url('https://static.independent.co.uk/s3fs-public/styles/story_large/public/thumbnails/image/2017/04/18/16/wine-evening.jpg');
    background-repeat: no-repeat;
    background-position: center;
    background-size: 100%;
    transition: all .7s;
}
.despre-noi-image:hover {
    transition: all 1s;
    background-size: 110%;
}
like image 155
Ram Babu Avatar answered Oct 23 '22 16:10

Ram Babu