Weird CSS image hover - css

I'm creating a hover effect on an image with SCSS on a WordPress site (see gif: https://gyazo.com/1a35247e40d74b5fc756d508de4231eb)
As you see the image gets a "distorted" after hovering over it, maybe the ease-in property is wrong, or I'm not doing the hover effect properly. I'm wondering what I'm doing wrong in the code when it behaves like this.
This is the code that is working:
(left out some SCSS because it was so wast but the & is used to use parent class)
Edit: The HTML & SCSS
<div class="project_container">
<div class="project_content">
Test event
<br>
2018
</div>
<img src="http://testsite.com/wp-content/uploads/2018/12/img.jpg" class="attachment-full aligncenter">
</div>
-
&_container {
position: relative;
height: 300px;
width: 300px;
&:hover {
& > img {
opacity: .2;
}
& > .project_content {
opacity: 1;
}
}
& img {
position: absolute;
width: 100%;
height: 100%;
/*
object-fit: none;
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
*/
// Hover tranisiton
transition: opacity .5s;
}
}
&_content {
opacity: 0;
transition: opacity .5s;
// Center Position
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: $purple;
z-index: 2;
text-transform: uppercase;
font-size: 20px;
font-weight: 500;
text-align: center;
}
}
So using :hover; on the &_container gives the project_content: opacity: 1;. Then it also blurs the background image with the opacity: .2;, and the effect is achieved with a transition;
Thank you!

I rewrote everything with the hover effect so now I use an overlay class which has opacity: 0; on it (and a transition that takes care of the effect on :hover)
This is a simple version of the HTML:
<div class="project_container">
<div class="project_content">
<p>Content</p>
</div>
<img src="#" alt="test">
<div class="project_overlay"></div>
</div>
and the simplified SCSS:
.project {
&_container {
position: relative;
height: 300px;
width: 300px;
&:hover .project_overlay,
&:hover .project_content {
opacity: 1;
transition: opacity .5s;
}
}
& img {
position: absolute;
width: 100%;
height: 100%;
}
&_overlay {
background-color: rgba(255, 255, 255, .5);
position: absolute;
height: 100%;
width: 100%;
opacity: 0;
transition: opacity .5s;
}
}

Related

How to do this image hover effect in CSS : dark overlay + image grow + title on it?

Does anyone knows how to do in css the following effect on hover on a image (dark overlay + image grow + name which appears on it) : https://drive.google.com/file/d/1zP5gRnI7BZIO-ajHSrZg2LnNhZCJ_f5F/view?usp=sharing
Thank you very much :)
My practice is to create wrapper element around img tag
<div class="img-container">
<img src="./images/img.png" alt="foo" />
</div>
and then applying styles like
.img-container {
position: relative;
border-radius: 10px;
overflow: hidden;
}
.img-container:hover .overlay {
opacity: 1;
}
.img-container:hover img{
transform: scale(1.2);
}
.img-container::before {
content: "Hello";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
font-size: 20px;
text-transform: uppercase;
font-weight: bold;
line-height: 25px;
color: #fff;
text-align: center;
background-color: rgba(175, 175, 175, 0.425);
pointer-events: none;
opacity: 0;
transition: opacity 0.3s;
}
img {
transition: transform 0.3s;
}
Hope this help ;)
You can either create a parent element appending the image element inside or use the ::after selector:
img {
position: relative;
}
img:hover {
/* grow image here */
}
img:hover::after {
contents: "FLUO";
display: flex;
background-color: rgba(0, 0, 0, 50%);
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
justify-content: center;
align-items: center;
}
Or use the background-image property to grow your image:
img {
background-image: url("/path/to/your/image.png");
background-position: center;
background-repeat: no-repeat;
background-size: contain;
position: relative;
}
img:hover {
background-size: 120%;
}

Transform: scale() not transforming from center

When I hover, it scales perfectly but the logo moves up a little bit. I tried transform-origin: center (even though that is the default) and nothing changed.
HTML
<div class="portfolio-projects">
<div class="project">
<img src="#" alt="">
</div>
</div>
CSS
portfolio-projects {
display: grid;
grid-gap: 50px;
margin: 50px 0;
max-width: 1050px;
.project {
position: relative;
cursor: pointer;
background-color: $gray;
max-width: 500px;
height: 325px;
overflow: hidden;
}
img {
position: absolute;
top: 35%;
left: 50%;
transform: translate(-50%,-35%) scale(.8);
transition: .2s;
}
.project:hover {
img {
transform: translate(-50%,-50%) scale(.9);
}
}
Wow. So I just needed to change it to this
.project:hover {
img {
transform: translate(-50%,-35%) scale(.9);
}
}
I forgot to adjust the hover after adjust the original value.

Changing background-position-x on :hover

I am trying to get a little hover animation going by moving the background image on the x-axis. However, I can't seem to find how to capture the element:hover. Does anyone has an idea what is wrong? Is the selector below valid?
Thank you for your help
div.card:hover .cardbg {
}
.card {
border-radius:15px;
border:none;
height: 280px;
position: relative;
background: linear-gradient(to bottom right,#ffec99 0,#ffb549 100%);
z-index: -1;
font-family: 'Arial';
}
.cardbg {
width: 100%;
height: 100%;
background-image: url('images/atom-original.svg');
opacity: 0.2;
background-size: cover;
background-repeat: no-repeat;
background-position-x: 100px;
transition: .2s ease all;
position: absolute;
top: 0;
left: 0;
}
div.card:hover .cardbg {
background-position-x: 10px;
background-color: red;
}
<div class="card">
<div class="cardbg">
</div>
</div>
Try removing the z-index: -1 from the .card class. That worked for me.

"position: fixed" not woking when parent has the "transform" CSS property

In my project I have screen which should ease-in from right side of the screen so for that thing I have used transform: translateX(100%) and then changing that to transform: translateX(0%). it works fine I able to achieve the ease-in effect but in that screen I have action button which has css property of Position: Fixed;Bottom: 0px; but this is not working I mean its not sticking in the bottom of the screen.
Here is my JSfiddle : https://jsfiddle.net/sureshpattu/a1seze4x/
Html:
<header>
Heading
</header>
<div class="page__popup page__popup--ease-in-right page__action-btn--visible" style="height: 382px;">
<div class="container">
</div>
<button class="js-action-btn">
SELECT ROOMS
</button>
</div>
Css:
header {
background: #fff;
height: 60px;
position: fixed;
top: 0;
left: 0;
right: 0;
width: 100%;
z-index: 10;
box-shadow: 2px 2px 10px #000;
}
.container {
height: 382px;
}
.page__popup {
position: absolute;
top: 100vh;
z-index: 8;
display: block;
overflow-y: scroll;
max-height: 100vh;
width: 100%;
height: 0;
background: #ffffff;
.js-action-btn {
position: relative;
bottom: -50px;
transition: all 0.25s ease-in-out;
}
//Themes
&--ease-in-bottom {
&.visible {
transition: height 0.25s ease-in-out;
top: 54px;
right: 0;
bottom: 0;
left: 0;
}
}
&--ease-in-right {
transform: translateX(100%);
height: 100vh;
top: 60px;
&.visible {
transition: transform 0.25s ease-in-out;
transform: translateX(0);
}
}
}
.page__action-btn--visible {
.js-action-btn {
background: red;
width: 100%;
height: 30px;
position: fixed;
bottom: 0;
z-index: 10;
box-shadow: 0 7px 4px 10px rgba(0, 0, 0, .12);
}
}
This is not a bug.
Take a look at the spec: The Transform Rendering Model
Specifying a value other than ‘none’ for the ‘transform’ property
establishes a new local coordinate system at the element that it is
applied to.
So according to the spec: the element with fixed positioning will become relative to the element with the transform - not the viewport
As a workaround you could:
1) Use transitions (eg. on the left property) instead of transform (translateX)
2) Remove the position:fixed button from the container which uses transforms

Distortion is present when transitioning img with CSS object fit

I checked it in Chrome and Firefox and in both browsers when I hover over the image, the transition seems to forget that the object-fit rule is applied.
Is this a bug or is there something wrong?
EDIT: I found it looks good if I put the img inside a div and animate that div. But I wouldn't like to modify my html for css reasons.
* {
padding: 0;
margin: 0;
}
figure {
width: 400px;
height: 150px;
overflow: hidden;
}
figcaption {
position: absolute;
z-index: 1;
color: white;
font-family: Arial;
top: 10px;
left: 10px;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center center;
transition: transform 0.5s ease;
}
img:hover {
transform: scale(1.2);
}
<figure>
<figcaption>Title</figcaption>
<img src="http://www.fondox.net/wallpapers/un-gato-bebe-433.jpg" alt="" />
</figure>
You have to set height as auto:
* {
padding: 0;
margin: 0;
}
figure {
width: 400px;
height: 150px;
overflow: hidden;
}
figcaption {
position: absolute;
z-index: 1;
color: white;
font-family: Arial;
top: 10px;
left: 10px;
}
img {
width: 100%;
height: auto;
object-fit: cover;
object-position: center center;
transition: transform 0.5s ease;
}
img:hover {
transform: scale(1.2);
}
<figure>
<figcaption>Title</figcaption>
<img src="http://www.fondox.net/wallpapers/un-gato-bebe-433.jpg" alt="" />
</figure>

Resources