How to display a div when hovering over an image using CSS/HTML + Transitions?

Psacey

I'm trying to achieve two things I can't figure out:
1) How to display a div when I hover over an image, ideally with a transition effect.
2) How to make the div stay up when the user shifts the mouse from the image to the div itself.

Here's my code so far; it has no transition effect and unless the div is directly next to the image, it doesn't stay up when I mouse over to it.

<style>
#Picture {
position: fixed; left: 0px; right: 0px; top: 0px; bottom: 0px; margin: auto;
width: 375px;
height: 375px;
}

#content {
display: none;
position: fixed; left: -800px; right: 0px; top: 0px; bottom: 0px; margin: auto;
width: 300px;
height: 300px;
background-color: #7377a8;
}

#Picture:hover + #content {display: block;}

#content:hover {display:block;}
</style>
<body>
<img src="" alt="Picture" id="Picture" />
<div id="Content">
Something goes here
</div>
</body>

P.S. I am sorry if I formatted anything incorrectly; I am brand new to the site.

Rene van der Lende

The hover effect is not mobile-friendly (though there are more and more 'hover-sensitive' devices). To accomodate most devices I often use both :hover and :focus to 'dropdown' things. However, this requires 'focusable' elements, for which I usually use the <a>nchor tag.

But first: The point in your code is consistency as you are mix-matching lowercase and uppercase in #content and id="Content". That is why it does not work anyway.

Answering your questions:

1) make upper/lowercase consistent!

2) To create a hover with persistency, trigger the display of 'content' with a focusable 'trigger' element

On hover/click the outer <a> stays focused and therefore its sibling #content visible. On hover .shorttext its sibling .longtext will show.

On click .shorttext (actually anywhere in #content) the content box will close again as the outer <a> loses focus again.

FYI-1, attribute display is not animatable, so you will need an alternative when you need a transition on some element. In this case opacity going from 0 to 1 is used (optionally combined with width and height, from 0 to 300px).

FYI-2, using href="javascript:void(0)" instead of href="#" will prevent browers from adding an entry in their history log each click.

FYI-3 final, use CSS classes by default, these are generic making it a lot easier to copy the same behaviour in your HTML, without repeating CSS each time. IDs are specific and require you to copy equal CSS over and over.

a {
  color: currentColor;
  text-decoration: none
}

.picture {
  position: fixed;
  left: 0px;
  right: 0px;
  top: 0px;
  bottom: 0px;
  margin: auto;
  width: 375px;
  height: 375px;
}

.content {
  /*  display: none;  remove */
  opacity: 0; /* add */
  transition: all 150ms ease-in-out; /* add */
  position: fixed;
  left: -800px;
  right: 0px;
  top: 0px;
  bottom: 0px;
  margin: auto;
  width: 0; /* [OPTIONAL] modify from 300px */
  height: 0; /* ditto */
  background-color: #7377a8;
}

.trigger:hover+.content,
.trigger:focus+.content {
  /* add, for persistent display of content. click elsewhere to close again */
  /*  display: block; remove */
  opacity: 1; /* add */
  width: 300px; /* [OPTIONAL] add, see above */
  height: 300px;
}

.shorttext { /* eye-candy only */
  width: 100%;
  text-align: center
}

.longtext {
  display: none
}

.shorttext:hover+.longtext {
  display: block;
}

/* little debug helper */

[outlines="1"] * {
  outline: 1px dashed purple
}
<body outlines="0">
<a class="trigger" href="javascript:void(0)"><img src="https://picsum.photos/300?random=1" alt="Picture" class="picture" /></a>
<div class="content">
    <h3 class="shorttext">short intro text, hover me</h3>

    <p class="longtext">Lorem ipsum dolor sit amet, exerci dolorem est ad. Sumo rebum prompta vim ad. Legendos expetendis id sed. Ex ius quem accusamus, pri et
        deleniti copiosae.</p>
</div>
</body>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to display a div when hovering over another div

unable to display text when hovering over image

How to get EpicGame's navbar transitions when hovering over link?

How to display different infos when hovering over an image with mouse in matlab guide?

Use css to display menu when hovering over an image

swap image when hovering over its container div

Expand image only when hovering over containing div

using css display <button /> when cursor hovering over

How can I change a sites background image when hovering over a button using css?

How to implement a pop-up bubble when hovering the cursor over the image rendered in the table using R shiny?

How to change class of separate div when hovering over link using JQuery

How to display a button over a picture only when hovering it?

How to display an annotation and exploding a wedge when hovering over pie chart?

How to Create an HTML Popup Window Next to Image when Hovering Over It?

How to change menu image when hovering over different links?

How to set an image as a cursor when hovering over a link

How to change the color of the SVG when hovering over the image?

How do I stop sound on mouseout when hovering over an image?

Show text over a image when hovering over it

How to trigger div visibility when hovering over list item in CSS?

How to scroll up a div when hovering over another element

Hovering over image and displaying text in another div

Highstocks - Display year when hovering over a point

DIsplay information when hovering over chart - chartjs

Button reveal when hovering over image

Bug in image map when hovering over area

Bootstrap 4 Popover when hovering over image

Hide image when hovering over a parent element

Creating a text overlay when hovering over image

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive