Hover transitions with absolute elements - html

I have an image with an absolutely positioned link centered on top of it. I have transition effects when the image is hovered over, but they are lost when the link is hovered over.
HTML
<div class="imagediv">
<img src="#">
Text
</div>
CSS
.imagediv img {
transition: transform 0.2s linear;
}
.imagediv img:hover{
transform: scale(1.2);
filter: grayscale(50%);
}
.imagediv a {
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
How can I make it so my effects on image hover are retained on link hover?

Change your CSS selector to trigger when the .imagediv is hovered not the img itself.
.imagediv img {
transition: transform 0.2s linear;
}
.imagediv:hover img {
transform: scale(1.2);
filter: grayscale(50%);
}

If you are able to switch the order of the img text elements, you could use the general sibling selector, but it only works with folowing siblings
a:hover ~ img {
transform: scale(1.2);
filter: grayscale(50%);
}
.imagediv img {
transition: transform 0.2s linear;
}
.imagediv img:hover{
transform: scale(1.2);
filter: grayscale(50%);
}
.imagediv a {
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
a:hover ~ img {
transform: scale(1.2);
filter: grayscale(50%);
}
<div class="imagediv">
Text
<img src="#">
</div>
.imagediv img {
transition: transform 0.2s linear;
}
.imagediv img:hover{
transform: scale(1.2);
filter: grayscale(50%);
}
.imagediv a {
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

A couple of things to fix:
You should move the :hover style to container.
The container should be set to relative position, so that the absolute link can be centered inside, otherwise the offsets will be relative to the viewport.
.imagediv {
display: inline-block;
position: relative;
}
.imagediv img {
transition: transform 0.2s linear;
}
.imagediv:hover img {
transform: scale(1.2);
filter: grayscale(50%);
}
.imagediv a {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="imagediv">
<img src="//unsplash.it/200">
Text
</div>
If you need the entire block to be clickable, you can use flexbox.
.imagediv {
display: inline-block;
position: relative;
}
.imagediv img {
transition: transform 0.2s linear;
}
.imagediv:hover img {
transform: scale(1.2);
filter: grayscale(50%);
}
.imagediv a {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
}
<div class="imagediv">
<img src="//unsplash.it/200">
Text
</div>

Use :hover on the imagediv
.imagediv {
position: relative; /* added so link position relates to imagediv */
}
.imagediv img {
transition: transform 0.2s linear;
}
.imagediv:hover img {
transform: scale(1.2);
filter: grayscale(50%);
}
.imagediv a {
position: absolute;
color: white;
z-index: 2;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="imagediv">
<img src="http://placehold.it/600x100/f00">
Text
</div>

Related

Css Grow on Hover is moving text vertically across the div

I have a css transition so that when someone hovers over an image, the h2 will grow. It does kind of work, but the in addition to growing the h2 is also moving across the div. This has never happened to me before and I can't figure out why. You can see it's behavior here, if you scroll to the bottom of the page and hover over Our Story and Our Team: https://katherinemade.com/staging/mission-vision-values/
Here is my html:
<div class="img-relative-position">
<h2 class="over-image-text">Our Story</h2>
<img />
</div>
And my css:
.img-relative-position {
position: relative;
}
.over-image-text {
position: absolute;
top: 45%;
left: 50%;
transform: translate(-50%, -50%);
}
.img-relative-position h2 {
transition: all .2s ease-in-out;
}
.img-relative-position:hover h2 {
transform: scale(1.5);
}
Does anyone know what could be causing my h2 to move vertically across the div, and how I can keep it center but still grow?
You can do like this
.img-relative-position {
position: relative;
}
.over-image-text {
position: absolute;
top: 45%;
left: 50%;
transform: translate(-50%, -50%);
}
.img-relative-position h2 {
transition: all .2s ease-in-out;
}
.img-relative-position:hover h2 {
transform: scale(1.5);
}
<div class="img-relative-position">
<div class = "over-image-text"> //new div
<h2 class="over-image-text-cstm">Our Story</h2>
</div>
<img />
</div>
i think you should scale a "parent" Container so if you create something like this
<div class="img-relative-position"> // <-- the Image
<div class="scale-this-on-hover"> // <-- new container this one has to be scaled
<h2 class="over-image-text">Our Story</h2>
<img />
</div>
</div>
No need To add scale property to increase text size
.img-relative-position:hover h2 {
/* transform: scale(1.5); */ // Remove This Line
font-size: 60px; // Add font-size
}
Thanks
I think you are missing transform-origin: center on h2. I have made a snippet for you. Have a look.
Thanks me later.
* {
box-sizing: border-box;
}
.wrap {
margin: 40px;
width: 300px;
height: 200px;
line-height: 200px;
background: green;
color: #fff;
text-align: center;
}
.wrap h2 {
transition: .3s ease;
transform-origin: center center;
}
.wrap:hover h2 {
transform: scale(1.5);
}
<div class="wrap">
<h2>Hello Test</h2>
</div>
For Extra Hover Effect You Can use This Css I Hope You Like It :)
.img-relative-position {
position: relative;
overflow: hidden;
}
.over-image-text {
position: absolute;
top: 45%;
left: 50%;
transform: translate(-50%,-50%);
z-index: 2;
transition: all 0.5s ease-in-out;
}
.img-relative-position:hover h2 {
font-size: 60px;
}
.img-relative-position a {
display: block;
}
.img-relative-position img {
transition: all 0.5s ease;
}
.img-relative-position:hover img {
transform: scale(1.2) rotate(-5deg);
}
Thanks

Image overlay on a slideshow puts the overlay on the next slide

This is what each slide looks like:
.slideImage {
opacity: 1;
display: block;
width: 100%;
height: auto;
transition: .5s ease;
backface-visibility: hidden;
}
.slideOverlay {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
justify-content: center;
}
.slideImage:hover {
opacity: 0.3;
}
.slideOverlay:hover {
opacity: 1;
}
<div className="each-slide">
<div className="slideImage" style={{ 'backgroundImage': `url(${slideImages[0]})`}}>
</div>
<div className="slideOverlay">
<p>OverlayText</p>
</div>
</div>
There's three slides. The image overlay text only appears on the 2nd slide and the 2nd slide doesn't fade out. Can someone tell me why that is?
The answer is .each-slide's position needed to be relative

Stopping img:hover transform from scaling outside its box

On hover, the scale transform starts to overlap the other pictures in it's row. I'm not sure how to prevent this and just have the scale stay within it's own picture.
HTML
<div id="pictures">
<img src="/pic1.jpg"/>
<img src="/pic2.jpg"/>
<img src="/pic3.jpeg"/>
</div>
CSS
#pictures {
text-align: justify;
overflow: hidden;
}
#pictures img {
display: inline-block;
width: 350px;
height: 350px;
}
#pictures:after {
content: '';
display: inline-block;
width: 100%;
}
#pictures img {
-webkit-transform: scale(1);
transform: scale(1);
-webkit-transition: .9s ease-in-out;
transition: .9s ease-in-out;
}
#pictures img:hover {
-webkit-transform: scale(1.5);
transform: scale(1.5);
}

How is possible to get 2d transformation css vertical way

I have this with the following CSS 2d Transforms code that built a h2 in horizontal way from the Left side of the Content to the Right.
I'd need the same effect but in the "vertical way", from bottom to top.
Furthermore, I' like to have a gradient color effect from green to Red.
You can find the demo here Link EFFECT 12
.hovereffect {
width: 100%;
height: 100%;
float: left;
overflow: hidden;
position: relative;
text-align: center;
cursor: default;
background: #42b078;
}
.hovereffect .overlay {
width: 100%;
height: 100%;
position: absolute;
overflow: hidden;
top: 0;
left: 0;
padding: 50px 20px;
}
.hovereffect img {
display: block;
position: relative;
max-width: none;
width: calc(100% + 20px);
-webkit-transition: opacity 0.35s, -webkit-transform 0.35s;
transition: opacity 0.35s, transform 0.35s;
-webkit-transform: translate3d(-10px,0,0);
transform: translate3d(-10px,0,0);
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.hovereffect:hover img {
opacity: 0.4;
filter: alpha(opacity=40);
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}
.hovereffect h2 {
text-transform: uppercase;
color: #fff;
text-align: center;
position: relative;
font-size: 17px;
overflow: hidden;
padding: 0.5em 0;
background-color: transparent;
}
.hovereffect h2:after {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
background: #fff;
content: '';
-webkit-transition: -webkit-transform 0.35s;
transition: transform 0.35s;
-webkit-transform: translate3d(-100%,0,0);
transform: translate3d(-100%,0,0);
}
.hovereffect:hover h2:after {
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}
.hovereffect a, .hovereffect p {
color: #FFF;
opacity: 0;
filter: alpha(opacity=0);
-webkit-transition: opacity 0.35s, -webkit-transform 0.35s;
transition: opacity 0.35s, transform 0.35s;
-webkit-transform: translate3d(100%,0,0);
transform: translate3d(100%,0,0);
}
.hovereffect:hover a, .hovereffect:hover p {
opacity: 1;
filter: alpha(opacity=100);
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="hovereffect">
<img class="img-responsive" src="http://placehold.it/350x250" alt="">
<div class="overlay">
<h2>Effect 12</h2>
<p>
LINK HERE
</p>
</div>
</div>
</div>
If you are looking to create a vertical variation of your design, please consider the below effect:
.hovereffect,
.hovereffect img {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
.overlay {
z-index: 10;
position: relative;
text-align: center;
}
.overlay p {
position: relative;
transform: translateY(-100vh);
opacity: 0;
transition: all 0.4s;
}
.hovereffect h2 {
position: relative;
}
.hovereffect h2:after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 0;
height: 5px;
background: tomato;
transform: translateY(100vh);
opacity: 0;
transition: all 0.4s;
}
.hovereffect:hover p,
.hovereffect:hover h2:after {
transform: translateY(0);
opacity: 1;
}
.hovereffect:hover h2:after{left:0;width:100%;}
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="hovereffect">
<img class="img-responsive" src="http://placehold.it/350x250" alt="">
<div class="overlay">
<h2>Effect 12</h2>
<p>
LINK HERE
</p>
</div>
</div>
</div>

How to let underline fade in beneath link on hover?

How can I fade in an underline on hover over a link like in the video below?
I found this CSS by using the developer console, but I can't figure out how it works.
.mainContent p>a:hover::after, .mainContent li>a:hover::after, .mainContent .text a:hover::after, .mainContent p>a:focus::after, .mainContent li>a:focus::after, .mainContent .text a:focus::after {
opacity: 1;
-webkit-transform: translateY(-2px);
-moz-transform: translateY(-2px);
transform: translateY(-2px);
}
.mainContent p>a::after, .mainContent li>a::after, .mainContent .text a::after {
position: absolute;
top: 100%;
left: 0;
width: 100%;
height: 1px;
background: rgba(226,0,26,1);
content: '';
opacity: 0;
-webkit-transition: opacity 0.15s,-webkit-transform 0.2s;
-moz-transition: opacity 0.15s,-moz-transform 0.2s;
transition: opacity 0.15s,transform 0.2s;
-webkit-transform: translateY(4px);
-moz-transform: translateY(4px);
transform: translateY(4px);
}
HTML
Hallenplan
How does this work and is there a even better way?
To do this effect you need two things:
Give the a a position attribute
Animate its after element by placing it at the bottom through position: absolute and then transforming it.
One possible way is:
a {
position: relative;
text-decoration: none;
}
a:hover::after, a:focus::after {
opacity: 1;
transform: translateY(-2px);
}
a::after {
position: absolute;
top: 100%;
left: 0;
width: 100%;
height: 1px;
background: black;
content: '';
opacity: 0;
transition: opacity 0.2s, transform 0.2s;
transform: translateY(4px);
}
<a href="http://www.stackoverflow.com">
StackOverflow
</a>