Pure CSS: Show image2 above/over/on top image1 on hover - html

SOLVED FIDDLE:
I can't figure out how to display an image(two) above/over my current visible image(one) when hovered.
Html:
<img class="two" src="img/2.png" alt="bulb">
<img class="one" src="img/1.png" alt="me">
CSS:
.one {
display: block;
margin: 0 auto;
padding-top: 50px;
}
.two {
opacity: 0;
}
.one:hover .two {
opacity: 1;
}
What am I doing wrong?

Edit: If you want to show another image below the first image, than you need to change your DOM, as CSS cannot select the previous element, but it can select adjacent image, what you are using is .one:hover .two which will select an element having class .two nested under element having class .one on hover but since the elements are adjacent, you can use the selector below, but note that you need to change the element order in the DOM.
.one:hover + .two {
opacity: 1;
}
Demo
I assume Above means on hover you want to swap the image, so if that's what you want than use CSS Positioning techniques by setting your images to position: absolute; which are nested under position: relative; container.
Demo
div {
position: relative;
}
div img { /* Setting images to absolute */
position: absolute;
top: 0;
}
div img:nth-of-type(2) { /* Initially hiding image 2 */
opacity: 0;
}
div:hover img:nth-of-type(1) { /* On hover of div we hide 1st image */
opacity: 0;
}
div:hover img:nth-of-type(2) { /* On hover of div we show 2nd image */
opacity: 1;
}
You can also add transition property to smoothly swap the image on hover
Demo
div img {
position: absolute;
top: 0;
-moz-transition: all .5s;
-webkit-transition: all .5s;
transition: all .5s;
}

Try using position:absolute and wrap your images with a div
div.images{
border:solid green 4px;
height:120px;
width:120px;;
margin-top: 50px;
position:relative;
}
.images img{
position:absolute;
top:0;
left:0
}
.two {
opacity:0;
z-index:1;
}
.images:hover .two {
opacity: 10;
}
DEMO

if it does not matter which one of your images come first you can change your html like this :
<img class="one" src="img/1.png" alt="me">
<img class="two" src="img/2.png" alt="bulb">
and use this CSS:
img.one:hover + img.two {
opacity: 1;
}
because there is no backward in CSS.

Related

Trying to make a div appear when hovering over an image [duplicate]

This question already has answers here:
How to overlay images
(11 answers)
Closed 3 years ago.
I'm trying to make a div with some information about an image appear when I hover over the image, my code doesn't seem to work.
HTML:
< div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.< /div>
< img class="batmobile" src="images/batmobile.png" alt="batmobile" >
CSS:
.batmobile{
position:relative;
width:500px;
height:200px;
top:350px;
left:200px;
}
.batmobile-info{
visibility:hidden;
}
.batmobile:hover .batmobile-info{
background-color:white;
text-align:center;
width:290px;
height:40px;
position:absolute;
top:500px;
left:700px;
visibility: visible;
}
If you reverse the order of your elements, so the img comes before the div to give you:
<img class="batmobile" src="images/batmobile.png" alt="batmobile">
<div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.</div>
Then you could use the following CSS to show the div on hover:
.batmobile:hover + .batmobile-info {
visibility: visible;
}
The + is an adjacent sibling selector - a super useful selector!
One way to make this work is to use container where you'll put your img and div with information.
I used opacity instead of visibility because opactity is the property you can use in transitions.
* {
box-sizing: border-box;
}
html, body {
margin: 0;
}
/* contiainer to center my content, not relevant to question*/
.main-container {
margin-top: 2rem;
height: calc(100vh - 2rem);
display: flex;
align-items: center;
justify-content: space-around;
}
/* image and info container */
.img-container {
display: inline-block;
position: relative;
border: 2px solid #333;
}
/* style for div with information */
.img-info {
position: absolute;
background-color: rgba(0, 0, 0, 0.8);
color: #eee;
transition: opacity .3s ease-in-out;
opacity: 0;
display: flex;
align-items: center;
justify-content: center;
padding: .5rem;
}
/* information positioning styles */
.img-info-overlay {
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.img-info-above {
left: -2px;
top: -2rem;
height: 2rem;
width: calc(100% + 4px);
}
/* this part show information on hover */
.img-container:hover .img-info {
opacity: 1;
}
<div class="main-container">
<div class="img-container">
<img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
<div class="img-info img-info-overlay">Some info about this image</div>
</div>
<div class="img-container">
<img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
<div class="img-info img-info-above">Some info about this image</div>
</div>
</div>
The problem is in both - the HTML and CSS, you need to put the image and text in one parent block and start styling from it. I'd avoid absolute positioning and also try using opacity CSS property in case you will need to add some extra transition effects to your text block .batmobile:hover state. Check out the code and run the snippet:
body {background:#ccc}
.batmobile {
position: relative;
width: 500px;
height: 200px
}
.batmobile div p {
visibility: hidden;
background-color: white;
width: 300px;
height: 40px;
margin: 0px auto; /* Think you wanted to center the block as width is less than parent */
text-align: center /*In case you need to center text only */
}
.batmobile:hover div p {
visibility: visible;
}
.batmobile img {width: 100%}
<div class="batmobile">
<div>
<p>An excelent drone - Ryze Tello with price under $100</p>
</div>
<img src="https://bestdroneforkids.com/wp-content/uploads/2019/09/ryze-tello-drone-forkids.jpg" alt="Ryze tello drone in the air">
</div>

Transparent layer over img when hover ++ text

I want to add an transparent layer over my img on a card when I hover over it, I have done that part but I want it to be cut to the img and not cover the footer on the card. If that makes sence?
this is the card with Hover. As u can see on the card, the img just covers like 90% of the card, I want the hover overlay to do the same
Card when not hover IMG
Card when hover IMG
.card {
position:relative;
width: 350px;
height: 335px;
background-size: contain;
background-repeat: no-repeat;
background-color: #fff;
border-radius: 5px;
margin: 30px;
float: left;
}
#card_oslo{
background-image: url(img/oslo.jpg);
}
#card_oslo:hover{
box-shadow: inset 0 0 0 1000px rgba(0,0,0,.7);
transition: .5s;
}
You should use a pseudo-element for this. Use :after or :before and set it as full size also set the parent with position:relative; then change the opacity of the pseudo element on hover.
Working Demo.
.box {
position:relative;
}
.box:after {
content:"";
/* Set the element as full-size */
position:absolute;
left:0;
top:0;
width:100%;
height:100%;
/* Set bg and hide the element + animation */
background-color:#000;
opacity:0;
transition: all 0.5s ease 0s;
}
.box:hover:after {
/* Show the overlay on hover */
opacity:0.5;
}
/* For the demo */
.box {
width:200px;
height:200px;
background-color:red;
}
<div class="box"></div>
You can set the overlay to
position: absolute;
top: 0;
bottom: XXpx;
left: 0;
right: 0;
where XX is the footer height, then it will cover the whole card and leave the bottom x pixels free. You can also use % values instead of px.
If you want the overlay to contain text you need to put it into an extra div that you can then use as overlay.
I made a simplified version here https://jsfiddle.net/0L9fL1pj/
Been looking for a similar solution and since this thread never got a proper answer (neither proposed answer got me where I wanted and I doubt) but I got some important clues here and I thought I'd provide my current solution so anyone who has a similar problem can benefit.
I made a simple demo here: https://jsfiddle.net/Tdesign/2ynuajk0/14/
HTML:
<div id="imgBox2" class="shade">
<img id="img2" src="https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg" width="350" height="335" loading="lazy" >
</div>
CSS:
#imgBox2 {
position: relative;
width: 500px;
}
.shade:hover::after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 500px;
background-color: rgba(0,0,0,0.5);
}

hover on an <img> to make a new div

I'm trying to have it so that when I hover on an <img> tag, a div will appear over it. I want it to be a white overlay with text inside of it.
I cannot make the image a background-image, as much as I would like to. My code uses width:percent/max-width:pixels and height:auto/max-height:pixels, so without the img there, nothing would show up. And to my knowledge, there is no solution to that issue.
I attempted to give the image a unique id and apply a id:hover .class to have the div appear, but it didn't respond to any coding I gave it, let alone work right. I then tried putting the id on a div of its own over putting it on the picture with still no yield.
I also tried to make a div with the image as a background pic and made the hover as desired. I tried to make the div not implode by putting in another div that has the image constraints, but because of the height:auto, it didn't work.
I refuse to set height/width as pixels, as it would mess up the rest of my coding and one of the major reasons I'm coding what I am. So, if it's not possible because of this, that's fine; Just tell me.
My CSS is as follows:
#logo {
text-align: center;
width:100%;
max-width:769px;
height:auto;
overflow:hidden;
}
#bannerpic {
max-width:769px;
max-height:300px;
width:100%;
height:auto;
}
#bannerpic .logobody {
display:none;
}
#bannerpic:hover .logobody {
display:inline;
background-color:rgba(255,255,255,0.5);
width:100%;
height:100%;
}
My HTML is this:
<div id="logo">
<img id="bannerpic" src="https://dl.dropboxusercontent.com/u/67673862/logoTEMP.png">
<div class="logobody">text</div>
</img>
</div>
I don't know if you need to be able to click the image, but you can overlay text with absolute positioning.
#logo {
text-align: center;
height: auto;
overflow: hidden;
position: relative;
max-width: 469px;
}
#bannerpic {
width: 100%;
height: auto;
}
.logobody {
position: absolute;
background-color: pink;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 0;
-webkit-transition: opacity .3s;
transition: opacity .3s;
}
.logobody:hover {
opacity: 1;
}
<div id="logo">
<img id="bannerpic" src="https://dl.dropboxusercontent.com/u/67673862/logoTEMP.png" />
<div class="logobody">text</div>
</div>
How about this:
#logo {
position: absolute;
z-index: 1;
left: 0;
top: 0;
}
.logobody {
position: absolute;
z-index: 2;
left: 0;
top: 0;
background-color: rgba(255,255,255,0.75);
visibility: hidden;
height: 100%;
width: 100%;
}
#logo:hover .logobody{
visibility: visible;
}
#bannerpic {
max-width:769px;
max-height:300px;
width:100%;
height:auto;
}
Here's a JSFiddle link for it:
https://jsfiddle.net/zVBDc/790/embedded/result/
Note: My example is using 0.75 alpha for the background. 0.5 seemed too low. Set it to whatever you prefer, though.

CSS hover on dot to reveal image - not hovering on image

I'm creating a merchandising page where there will be several products photoshopped onto the background image. I want customers to be able to hover on a dot positioned on the product to reveal it's information and contain a link similar to http://www.wineenthusiast.com/custom-cellars/ but I want to do it in pure CSS. I only want the info and link to appear when a user hovers on the dot and then on the containing div. The issue I keep running into is that since both elements are contained in the same div, the corresponding image is displayed. This will be too messy when there are 15 products on this page. Still a noob coder so any help would be greatly appreciated, thanks!
Here's the fiddle: http://jsfiddle.net/jakvisualdesign/hvb77m8L/2/
and the code:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<link rel="stylesheet" href="style.css">
<title>JS Bin</title>
</head>
<body>
<div id="container">
<div class="base">
<img src="https://s3.amazonaws.com/steinersports/CMS+Pages/Yankees+Man+Cave/background.jpg" />
</div>
<div class="pop-container-a">
<img src="https://s3.amazonaws.com/steinersports/CMS+Pages/Yankees+Man+Cave/background.jpg">
<div class="dot"></div>
</div>
</div>
</body>
</html>
and the CSS:
#container {
position:relative;
width: 960px;
margin-left: auto;
margin-right: auto;
}
.base {
width: 100%;
height: 100%;
position: absolute;
}
#container.pop-container-a img {
position: absolute;
}
.dot {
width: 10px;
height: 10px;
position: absolute;
background-color: red;
z-index: 10;
border-radius: 50%;
}
.pop-container-a img {
position:absolute;
opacity: 0;
width: 150px;
transition: opacity .5s ease;
}
.pop-container-a:hover .dot, .pop-container-a:hover img {
opacity: 1;
}
.pop-container-a {
position: absolute;
top: 100px;
left: 50px;
display: inline-block;
}
Fixed that up for you: http://jsfiddle.net/hvb77m8L/3/
The trick is using the adjacent sibling selector + to allow hovering on the dot to affect the image next to it. So, this:
.pop-container-a:hover .dot, .pop-container-a:hover img {
opacity: 1;
}
becomes this:
.dot:hover + img {
opacity: 1;
}
Edit: and since that selects an element immediately following the targeted one, also note that I changed this:
<img src="https://s3.amazonaws.com/steinersports/CMS+Pages/Yankees+Man+Cave/background.jpg">
<div class="dot"></div>
to this:
<div class="dot"></div>
<img src="https://s3.amazonaws.com/steinersports/CMS+Pages/Yankees+Man+Cave/background.jpg">
Edit 2: To make the images remain when hovered on, you can make them inactive by default by setting their width to 0:
.pop-container-a img {
opacity: 0;
width: 0; // in addition to being invisible, the image will not respond to hovering
position: absolute;
transition: opacity .5s ease;
}
Then, when the dot is hovered, return the images to normal width and normal opacity:
.dot:hover + img {
width: 150px;
opacity: 1;
}
And when the image is in this state, it can now remain there with a hover effect:
.dot + img:hover {
width: 150px;
opacity: 1;
}
A new fiddle to demonstrate this: http://jsfiddle.net/hvb77m8L/6/

Center a Font Awesome icon over the image on hover

I'm trying to center a font awesome icon over an image when the mouse is hovering the image.
Here's my HTML:
<div class="profile-img-container">
<img src="http://s3.amazonaws.com/37assets/svn/765-default-avatar.png" class="img-thumbnail img-circle img-responsive" />
<i class="fa fa-upload fa-5x"></i>
</div>
And my CSS:
.profile-img-container {
position: relative;
}
.profile-img-container img:hover {
opacity: 0.5;
}
.profile-img-container img:hover + i {
display: block;
z-index: 500;
}
.profile-img-container i {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
However the font awesome icon is somewhy displayed all the way to the left and the icon keeps flickering when I hover the image.
Here's my JSFiddle:
http://jsfiddle.net/fns8byfj/1/
The usage here is important to consider. This is a trigger, so I would use a link inside here. I would not display:none since IOS will not work on the actions inside this when the state on the selector was display:none or visibility:hidden even if the :hover changes this state. Use opacity and position to "hide it".
VERY IMPORTANT:
A parent is not the size of the child image inside it unless that div is the child of something that constrains its width or the div is floated or inline-block. If you put this in a column inside the grid and the image is, at any viewport width, as wide as that column, then you can remove the "inline-block" on the .profile-img-container however if you use it just stand alone you have to float it or put an .inline-block on it but then you have to change the responsiveness of the image if the parent is an inline-block max-width:100% doesn't work (just like it doesn't work if inside a table-cell).
:hover is not a good idea, I would use jQuery to make this a click by toggling the parent's class.
DEMO: http://jsfiddle.net/prtkqb44/
CSS:
.profile-img-container {
position: relative;
display: inline-block; /* added */
overflow: hidden; /* added */
}
.profile-img-container img {width:100%;} /* remove if using in grid system */
.profile-img-container img:hover {
opacity: 0.5
}
.profile-img-container:hover a {
opacity: 1; /* added */
top: 0; /* added */
z-index: 500;
}
/* added */
.profile-img-container:hover a span {
top: 50%;
position: absolute;
left: 0;
right: 0;
transform: translateY(-50%);
}
/* added */
.profile-img-container a {
display: block;
position: absolute;
top: -100%;
opacity: 0;
left: 0;
bottom: 0;
right: 0;
text-align: center;
color: inherit;
}
HTML:
<div class="profile-img-container">
<img src="http://s3.amazonaws.com/37assets/svn/765-default-avatar.png" class="img-thumbnail img-circle img-responsive" />
<span class="fa fa-upload fa-5x"></span>
</div>
You can center it both horizontally and vertically using percentage widths, but this implies that you know approximately the width in percentages of the element you are trying to position, in this case the font-awesome one. Note that I aligned it approximately, positioning it to the left and top by 45%.
I've updated your code with the above-mentioned part, and by also applying the hover effect on the containing DIV such that the font-awesome icon does not flicker. It flickered because when you were hovering over it, the hover over the image was being lost.
The HTML remains, the same, only the style differs:
.profile-img-container {
position: relative;
}
.profile-img-container i {
position: absolute;
top: 45%;
left: 45%;
transform: translate(-45%, -45%);
display: none;
}
.profile-img-container:hover img {
opacity: 0.5;
}
.profile-img-container:hover i {
display: block;
z-index: 500;
}
Your updated JSFiddle.
The following solution should work, as long as you can afford to have fixed widths in the topmost container (in the example, 300px) or otherwise manage to have a line-height value which is always equal to the rendered height of the image.
The solution exploits the line-height and text-align properties to achieve vertical and horizontal positioning, respectively.
<div class="profile-img-container">
<img src="http://s3.amazonaws.com/37assets/svn/765-default-avatar.png" class="img-thumbnail img-circle img-responsive" />
<div class="profile-img-i-container">
<i class="fa fa-upload fa-5x"></i>
</div>
</div>
.profile-img-container {
height: 300px;
width: 300px;
line-height: 300px;
position:relative;
}
.profile-img-container:hover img {
opacity: 0.5;
}
.profile-img-container:not(:hover) .profile-img-i-container {
display: none;
}
.profile-img-i-container {
position: absolute;
top: 0px;
left: 0px;
display: block;
height: 100%;
width: 100%;
z-index: 500;
text-align:center;
}
.profile-img-i-container i {
display:block;
line-height: inherit;
}
Fiddle
About the flickering:
Be careful with :hover. In your example you had the following snippet:
.profile-img-container img:hover + i {
display: block;
...
}
This causes the i element to appear when you hover the image. Then the i element is placed on top of the image, so you're not longer hovering the image, but the i element. The icon hides again, and you are again hovering the image. This is what caused the flickering effect. The solution is to work with the :hover property of the topmost element.
.profile-img-container:hover img + i {
display: block;
...
}
I have changed the position of .profile-img-container to absolute and set it's width to 50% (you can adjust the width to change the image size).
.profile-img-container {
position: absolute;
width:50%;
}
because of the flickering effect, you must set your img z-index higher than the z-index of i.
.profile-img-container img:hover {
opacity: 0.5;
z-index: 501;
}
.profile-img-container img:hover + i {
display: block;
z-index: 500;
}
I have changed the position of i to absolute and centered it using margin-left and margin-top
.profile-img-container i {
display: none;
position: absolute;
margin-left:43%;
margin-top:40%;
}
And finally I changed the position of img to absolute
.profile-img-container img {
position:absolute;
}
Try this code: http://jsfiddle.net/fns8byfj/16/