I am trying to get a <p> element to appear upon hovering over an image. As I have it, nothing happens when I hover over the image.
Any idea what I'm doing wrong?
img {
height: 400px;
width: 400px;
position: relative;
}
p {
position: absolute;
color: #b6a9a9;
background-color: rgb(5, 5, 5);
bottom: 50%;
left: 1em;
opacity: .68;
transition: .5s ease-in;
transform: scale(1);
height: 25px;
width: 25%;
}
img:hover .stuff p {
transform: scale(2);
transform-origin: center;
}
<img src="image.jpg">
<div class="stuff">
<p>here's some text that will go on top of ths image ... I hope...</p>
</div>
You are almost there, you just need to add the + selector to the css definition
The + sign selector is used to select the elements that are placed immediately after the specified element but not inside.
So: img:hover + .stuff p, means: when hover over img, select it's "brother" with class .stuff and then select the p that is inside.
img {
height: 400px;
width: 400px;
position: relative;
}
p {
position: absolute;
color: #b6a9a9;
background-color: rgb(5, 5, 5);
bottom: 50%;
left: 1em;
opacity: .68;
transition: .5s ease-in;
transform: scale(1);
height: 25px;
width: 25%;
}
img:hover + .stuff p {
transform: scale(2);
transform-origin: center;
}
<img src="https://via.placeholder.com/400">
<div class="stuff">
<p>here's some text that will go on top of ths image ... I hope...</p>
</div>
Related
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
I've been trying to figure out how I can make a block of text appear to the side of an image when hovered over instead of appearing on top of it, however I can't seem to find any explanations for anything other than having the text fade in on the actual image itself. This is what I've been experimenting with(adapted from w3schools), as of right now it only has the text on the image. If anyone could edit it so that the text comes to the side that would be incredibly helpful.
.container {
position: relative;
width: 50%;
}
.image {
opacity: 1;
display: block;
width: 100%;
height: auto;
transition: .5s ease;
backface-visibility: hidden;
}
.middle {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}
.container:hover .image {
opacity: 0.3;
}
.container:hover .middle {
opacity: 1;
}
.text {
color: black;
font-size: 16px;
padding: 16px 32px;
}
<div class="container">
<img src="https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg" alt="placeholder" class="image" style="width:100%">
<div class="middle">
<div class="text">This should be to the side of the image</div>
</div>
</div>
If I understand correctly, you want the text to be outside of the image.
You are using position: relative on the .container, that's why the .middle will stay inside of it, removing that will solve the issue:
.container {
/* position: relative; */
width: 50%;
}
.image {
opacity: 1;
display: block;
width: 100%;
height: auto;
transition: .5s ease;
backface-visibility: hidden;
}
.middle {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 0;
left: 50%;
/* transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center; */
}
.container:hover .image {
opacity: 0.3;
}
.container:hover .middle {
opacity: 1;
}
.text {
color: black;
font-size: 16px;
padding: 16px 32px;
}
<div class="container">
<img src="https://via.placeholder.com/350x150" alt="placeholder" class="image" style="width:100%">
<div class="middle">
<div class="text">This should be to the side of the image</div>
</div>
</div>
Check if that's what you need (made myself a quick example) :
.container {
display: flex;
}
.leftBlock {
width: 50%;
height: auto;
transition: all 0.3s ease;
}
.leftBlock:hover {
opacity: 0.5;
}
.leftBlock:hover + .rightBlock {
opacity: 1;
}
.rightBlock {
right: 0;
background-color: #222;
flex-grow: 1;
text-align: center;
vertical-align: middle;
opacity: 0;
transition: all 0.4s ease;
color: #fff;
}
<div class="container">
<img src="https://images.pexels.com/photos/3683056/pexels-photo-3683056.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" class="leftBlock">
<div class="rightBlock">Text Goes Here</div>
</div>
I want to achive a simple animation on hover in css, but while hovering off, the animation jumps and makes it look silly. Is there a way to avoid that?
My wish would be that it is colapsing the same way it originaly transformed.
Codepen: https://codepen.io/misah/pen/abzRXvL
.item {
width: 20%;
height: 0;
padding-top: 20%;
background-color: blue;
position: relative;
transition: 800ms;
}
.item::after {
position: absolute;
bottom: 0;
height: 30%;
width: 100%;
content: "";
background-color: grey;
transition: transform 800ms ease-in-out;
}
.item:hover::after {
transform: scaleY(2);
transform-origin: bottom;
}
<div class="item">
</div>
This happens because the transform-origin: bottom; is only applied when the item is hovered. When not hovered it falls back to the default - center. Move the rule to the declaration of the ::after.
.item {
width: 20%;
height: 0;
padding-top: 20%;
background-color: blue;
position: relative;
transition: 800ms;
}
.item::after {
position: absolute;
bottom: 0;
height: 30%;
width: 100%;
content: "";
background-color: grey;
transition: transform 800ms ease-in-out;
transform-origin: bottom;
}
.item:hover::after {
transform: scaleY(2);
}
<div class="item"></div>
I'm trying to build a card scheme where when you hover the mouse over the movie cover, some informations are displayed. The problem is: my star emoji doesn't reapear on hover.
I used this same visibility: hidden/visible with the center class, but with center-img it's not working.
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
.container {
margin: 20px;
width: 300px;
height: 424px;
background-color: black;
position: relative;
}
.fundo {
width: 300px;
height: 424px;
opacity: 1;
}
.center {
position: absolute;
top: 65%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 30px;
font-weight: 900;
font-family: "montserrat", sans-serif;
background: linear-gradient(to right,#ff8a00,#da1b60);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
visibility: hidden;
}
.center-img {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
width: 80px;
height: auto;
visibility: hidden;
}
.fundo:hover {
transition: .4s ease;
opacity: 0.5
}
.fundo:hover + .center {
visibility: visible;
}
.fundo:hover + .center-img {
visibility: visible;
}
</style>
</head>
<body>
<div class="container">
<img class="fundo" src="https://www.downgraf.com/wp-content/uploads/2015/06/Classic-Movie-Posters-8.jpg" alt="Cinque Terre">
<div class="center">10/10</div>
<img class="center-img" src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/160/samsung/161/white-medium-star_2b50.png">
</div>
</body>
</html>
I would like to see this shitty star to reapear on hover, if you guys can help me. Thanks in advance.
.container {
margin: 20px;
width: 300px;
height: 424px;
background-color: black;
position: relative;
}
.hover {
visibility: hidden
}
.fundo {
width: 300px;
height: 424px;
opacity: 1;
}
.center {
position: absolute;
top: 65%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 30px;
font-weight: 900;
font-family: "montserrat", sans-serif;
background: linear-gradient(to right,#ff8a00,#da1b60);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.center-img {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
width: 80px;
height: auto;
}
.container:hover .fundo {
transition: .2s ease;
opacity: 0.5
}
.container:hover .hover {
visibility: visible;
}
You have to switch your selector from .fundo:hover + .center-img to .fundo:hover ~ .center-img because <img class="center-img"> is not a direct sibling of <img class="fundo">.
The ~ selector will enable you to select any sibling of <img class="fundo"> that has the class of .center-img.
Using opacity transition
CodePen using opacity transition
.center-img {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
width: 80px;
height: auto;
opacity: 0; /* Add this */
}
.fundo:hover ~ .center-img { /* Change selector to this */
transition: 1.2s ease; /* Add this */
opacity: 1; /* Add this */
}
Using visibility property
CodePen using visibility property
.center-img {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
width: 80px;
height: auto;
visibility: hidden;
}
.fundo:hover ~ .center-img { /* Change selector to this */
visibility: visible;
}
Solving the flicker effect when you hover over the star or score by using an overlay
CodePen implementation of overlay
The idea is to create an invisible overlay that will cover the entire movie image and apply the hover effects onto the overlay.
The flickering was occurring in your solution because the hover effects were placed on the movie image, which was behind the star and score on the z-axis.
Once your mouse hovered over either the star or score, the visibility effect was essentially being toggled because your mouse was hovering over the star/score instead of the movie image. This resulted in the hover effects on the star/score would be taken off. Once the effects were taken off and their visibility was hidden, you were back to hovering over the movie image. This cycle is what created the flickering effect.
.fundo__overlay { /* Created new selector */
position: absolute;
z-index: 999; /* Give the overlay a higher stacking content */
top: 0;
left: 0;
width: 100%; /* Get it to match the movie image's dimensions */
height: 100%;
background: black;
opacity: 0; /* Used for hover transition */
}
/* Transferred hover effects */
.fundo__overlay:hover { /* Changed selector */
transition: opacity .4s ease;
opacity: 0.5
}
.fundo__overlay:hover ~ .center { /* Changed selector */
visibility: visible;
}
.fundo__overlay:hover ~ .center-img { /* Changed selector */
visibility: visible;
}
<div class="container">
<div class="fundo__overlay"></div> <!-- Added new element -->
<img class="fundo" src="https://www.downgraf.com/wp-content/uploads/2015/06/Classic-Movie-Posters-8.jpg" alt="Cinque Terre">
<div class="center">10/10</div>
<img class="center-img" src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/160/samsung/161/white-medium-star_2b50.png">
</div>
I'm a blogger and I'm trying my best to learn to code certain things for everyday use. Today I'm trying to get a picture in which when you hover over it it fades in with a text over it and I want that text to have a drop down menu.
Ex: I have a picture of books. When hovered over the word Books appears in the middle of the picture and the photo has a transparent overlay. When you hover over the word Books a drop down menu says Hauls, Reviews, and Inspired By. I would also like to be able to just click the word Books and have it take me to all post labeled books.
Here is the code I have so far:
.container {
position: relative;
width: 50%;
}
.image {
opacity: 1;
display: block;
width: 100%;
height: auto;
transition: .5s ease;
backface-visibility: hidden;
}
.middle {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%)
}
.container:hover .image {
opacity: 0.3;
}
.container:hover .middle {
opacity: 1;
}
.text {
background-color: #000000;
color: white;
font-size: 16px;
padding: 16px 32px;
}
<div class="container">
<img src="https://farm5.staticflickr.com/4397/35532470254_614bf14a8b_b.jpg" alt="Avatar" class="image" style="width:100%">
<div class="middle">
<div class="text">Books</div>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Hauls
Reviews
Inspired By
</div>
</div>
I hope this makes sense! Thanks so much for the help!
First, move the dropdown-content inside middle content, because CSS hover can't access if it's not the parent element, you can change HTML markup like this.
.container {
position: relative;
width: 50%;
}
.image {
opacity: 1;
display: block;
width: 100%;
height: auto;
transition: .5s ease;
-webkit-transition: .5s ease;
backface-visibility: hidden;
}
.middle {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
}
.container:hover .image {
opacity: 0.3;
}
.container:hover .middle {
opacity: 1;
}
.text {
background-color: #000000;
display: inline-block;
color: white;
font-size: 16px;
padding: 16px 32px;
}
/* this for show dropdown */
.middle .dropdown-content {
display: none;
position: absolute;
top: 100%;
}
.middle:hover .dropdown-content {
display: block
}
<div class="container">
<img src="https://farm5.staticflickr.com/4397/35532470254_614bf14a8b_b.jpg" class="image" alt="" />
<div class="middle">
Books
<div class="dropdown-content">
Hauls
Reviews
Inspired By
</div>
</div>
</div>
I think it works like you want.. Dropdown content always shows if middle hovered.