Halnex
Halnex

Reputation: 4526

Scale background image on container hover

I have a background image with a background color overlay that I'm trying to scale using transform: scale3d(1.1,1.1,1); when I hover on its container.

Even though the code appears in the inspector, it is not taking effect.

This is the CSS that is supposed to scale the background image whenever I hover over the item container

.item:hover .img-container {
    transform: scale3d(1.1,1.1,1);
    -webkit-transform: scale3d(1.1,1.1,1);
    -moz-transform: scale3d(1.1,1.1,1);
    opacity: .2;
}

DEMO https://jsfiddle.net/xaw3vcL1/

HTML

<article class="item" style="background: url('https://images.pexels.com/photos/235470/pexels-photo-235470.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb') center center / cover;">
  <div class="item-content">
    <a href="#" class="img-container"></a>
  </div>
  <div class="content-container">
    Title here
  </div>
</article>

CSS

.item {
    height: 450px;
    position: relative;
    overflow: hidden;
}

.item:before {
    display: block;
    content: " ";
    width: 100%;
    padding-top: 68.17%;
}

.item-content {
    position: absolute!important;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
}

.img-container {
    webkit-background-size: cover!important;
    -moz-background-size: cover!important;
    -o-background-size: cover!important;
    background-size: cover!important;
    position: absolute;
    top: -1px;
    left: -2px;
    right: -2px;
    bottom: -1px;
    -webkit-transition: all .5s;
    -moz-transition: all .5s;
    transition: all .5s;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -moz-transform-origin: 0 0;
    transform: translate3d(0,0,0);
}

.img-container:after {
    opacity: .5;
    background: #26D0CE;
    background: -moz-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: -webkit-gradient(left bottom,right top,color-stop(0,#1A2980),color-stop(100%,#26D0CE));
    background: -webkit-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: -o-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: -ms-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    -webkit-transition: all .35s ease;
    -moz-transition: all .35s ease;
    -o-transition: all .35s ease;
    transition: all .35s ease;
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.item:hover .img-container {
    transform: scale3d(1.1,1.1,1);
    -webkit-transform: scale3d(1.1,1.1,1);
    -moz-transform: scale3d(1.1,1.1,1);
    opacity: .2;
}

.content-container {
    position: absolute;
    bottom: 15px;
    left: 20px;
    right: 20px;
    padding: 0;
    max-height: 75%;
    overflow: hidden;
    pointer-events: none;
    transform: translate3d(0,0,0);
    -webkit-backface-visibility: hidden;
}

Upvotes: 0

Views: 1482

Answers (2)

Mihai T
Mihai T

Reputation: 17687

it doesn't work because the background-image is not set to the img-container but to the article.item .

you should put the background-image to the right container if you want your code to work.

and, remove opacity:.2 from .item:hover .img-container and add .item:hover .img-container:after {opacity:.2}

see snippet or jsFiddle

.item {
    height: 450px;
    position: relative;
    overflow: hidden;
}

.item:before {
    display: block;
    content: " ";
    width: 100%;
    padding-top: 68.17%;
}

.item-content {
    position: absolute!important;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
}

.img-container {
    webkit-background-size: cover!important;
    -moz-background-size: cover!important;
    -o-background-size: cover!important;
    background-size: cover!important;
    position: absolute;
    top: -1px;
    left: -2px;
    right: -2px;
    bottom: -1px;
    -webkit-transition: all .5s;
    -moz-transition: all .5s;
    transition: all .5s;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -moz-transform-origin: 0 0;
    transform: translate3d(0,0,0);
}

.img-container:after {
    opacity: .5;
    background: #26D0CE;
    background: -moz-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: -webkit-gradient(left bottom,right top,color-stop(0,#1A2980),color-stop(100%,#26D0CE));
    background: -webkit-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: -o-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: -ms-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    background: linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
    -webkit-transition: all .35s ease;
    -moz-transition: all .35s ease;
    -o-transition: all .35s ease;
    transition: all .35s ease;
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.item:hover .img-container {
    transform: scale3d(1.1,1.1,1);
    -webkit-transform: scale3d(1.1,1.1,1);
    -moz-transform: scale3d(1.1,1.1,1);
}
.item:hover .img-container:after {
	opacity:.2
}

.content-container {
    position: absolute;
    bottom: 15px;
    left: 20px;
    right: 20px;
    padding: 0;
    max-height: 75%;
    overflow: hidden;
    pointer-events: none;
    transform: translate3d(0,0,0);
    -webkit-backface-visibility: hidden;
}
<article class="item" >
  <div class="item-content">
    <a href="#" class="img-container" style="background: url('https://images.pexels.com/photos/235470/pexels-photo-235470.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb') center center / cover;"></a>
  </div>
  <div class="content-container">
    Title here
  </div>
</article>

Upvotes: 2

Vishnu prasad
Vishnu prasad

Reputation: 11

add this class to your css

article:hover{transform: scale3d(1.1,1.1,1);
-webkit-transform: scale3d(1.1,1.1,1);
-moz-transform: scale3d(1.1,1.1,1);
opacity:0.2;}

Upvotes: -1

Related Questions