Fade in a box text overlay with a hoverable dropdown - html

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.

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

Fade color and display text on image hover

I'm trying to make thumbnails that on hover fades into a color and display text centered in the image. The images and text are different so I want them to display text specific to that image and not to the parent element. This is what I've got so far:
<div id="latest_stuff">
<img src="image2.jpg" alt="Video Thumbnail">
<img src="image2.jpg" alt="Video Thumbnail">
<img src="image2.jpg" alt="Video Thumbnail">
</div>
#latest_stuff a{
transition: opacity 0.25s;
}
#latest_stuff a:hover{
opacity: 0.5;
}
Sorry if you don't understand, English is not my first language.
I have created for one card you can follow for others
#latest_stuff {
position: relative;
width: 50%;
height: 50vh;
overflow: hidden;
}
#latest_stuff a{
transition: opacity 0.25s;
}
img {
width: 100%;
height: 100%;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
}
#latest_stuff:hover .overlay {
opacity: 0.5;
}
.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
<div id="latest_stuff">
<a href="www.example.com/video1">
<img src="https://i.picsum.photos/id/1035/5854/3903.jpg?hmac=DV0AS2MyjW6ddofvSIU9TVjj1kewfh7J3WEOvflY8TM"
alt="Video Thumbnail">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</a>
</div>
Here, I placed some dummy images.
#latest_stuff a {
position: relative;
display: inline-block;
transition: opacity 0.25s;
}
.img-caption {
position: absolute;
width: 100%;
height: 100%;
top: 50%; /* 50% from top */
left: 50%; /* 50% from left */
display: flex;
align-items: center;
justify-content: center;
text-align: center;
transform: translate(-50%, -50%) scale(0); /* to center the text, scale(0) to doesn't show text */
transform-origin: center;
transition: 0.25s;
}
#latest_stuff a:hover .img-caption {
transform: translate(-50%, -50%) scale(1); /* to center the text, scale(1) to show text */
transform-origin: center;
transition: 0.25s;
}
#latest_stuff a:hover img {
opacity: 0.5;
}
<div id="latest_stuff">
<a href="www.example.com/video1">
<img src="https://placehold.jp/150x150.png" alt="Video Thumbnail">
<div class="img-caption">
Some dummy text
</div>
</a>
<a href="www.example.com/video2"><img src="https://placehold.jp/150x150.png" alt="Video Thumbnail">
<div class="img-caption">
Some dummy text
</div>
</a>
<a href="www.example.com/video3"><img src="https://placehold.jp/150x150.png" alt="Video Thumbnail">
<div class="img-caption">
Some dummy text
</div>
</a>
</div>

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

CSS how can I make text appear next to an image on hover instead of on it?

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>

CSS hover effect to modify another element is not working

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>