How to make CSS hover effect stay even after the mouse is no longer hovering

I've come across a challenge that is new to me in my journey as a web designer. As a beginner, I recently incorporated the CSS hover feature on a div element within my webpage. Upon hovering over this div, an additional image is revealed; however, when moving the cursor away from the div, the new image disappears, and I would like it to remain visible.

Below is an illustration of the code I am currently using:

#about {
height: 25px;
width: 84px;
background-image: url('about.png');
position: absolute;
top: 200px;
left: 0px;
}

#onabout {
height: 200px;
width: 940px;
position: absolute;
top: 60px;
left: 0px;
color: #fff;
font-family: 'Lato', sans-serif;
font-size: 15px;
font-weight: 300;
display: none;
}

#about:hover #onabout {
display: block;
}

Is there a way to resolve this issue solely using CSS? Up until now, I have avoided using JavaScript and feel less confident with it.

In any case, I am open to all suggestions for resolving this matter. Thank you in advance!

Answer №1

Exploring a new CSS concept!

One interesting approach is to utilize transition-delay; http://jsfiddle.net/nAg7W/

div img {
    position:absolute;
    opacity:0;
    transition:0s 180s;
}
div:hover img {
    opacity:1;
    transition:0s;
}
div {
    line-height:1.2em;
    font-size:1em;
    color:black;
    transition:0s 180s;
}
div:hover {
    line-height:0;
    font-size:0;
    color:transparent;
    transition:0;
}

Here's the markup:

<div>some text to hover to see an image which is hidden as you read this
<img src="http://placehold.it/200x200&text=zi image" />

You could also make it disappear on click. http://jsfiddle.net/nAg7W/1/

div:hover img:focus {/* includes tabindex in html tag for img */
   opacity:0;
   transition:3s;
   -webkit-transform:rotate(-360deg) scale(0.23);
   -webkit-transform:rotate(-360deg) scale(0.23);
   -moz-transform:rotate(-360deg) scale(0.23);
   -o-transform:rotate(-360deg) scale(0.23);
   -ms-transform:rotate(-360deg) scale(0.23);
   transform:rotate(-360deg) scale(0.23);
}

Answer №2

According to @Leo Pflug, implementing CSS and HTML on and after click can be done confidently using the techniques outlined in this resource.

By incorporating a bit of JavaScript, achieving this effect is not only possible but also straightforward and reliable, as demonstrated by @Wind Shear.

However, accomplishing Pure CSS on and after hover is significantly more challenging and may not be currently feasible for production due to browser compatibility issues. Modifying elements with the :hover pseudo-class selector poses limitations when dealing with elements that are no longer being hovered over.

The most viable approach using pure CSS would involve CSS3 Animations. As an example, I have developed a demonstration here (v2 w/FF & IE) utilizing CSS animations to transition to a hover state upon hovering and maintaining that state until the page is refreshed.

@keyframes hover {
  0% {
    // normal styles
  }
  100% {
    // hover styles
  }
}

.class {
  animation-name: hover;
  animation-duration: 1ms;
  animation-fill-mode: backwards;
  animation-play-state: paused;      
}

.class:active,
.class:focus,
.class:hover {
  animation-fill-mode: forwards;
  animation-play-state: running;
}

I conducted tests in Chrome latest, Firefox latest, and IE 10, and it functioned correctly on all three browsers.

Please note that there were display issues on CodePen with the latest code updates, so I have included a fresh link here.

For additional information on stopping CSS3 animations at specific frames, refer to this post and this thread.

UPDATE: Lea Verou has recently showcased this technique in her article titled Smooth state animations with animation-play-state.

Answer №3

Hover state in CSS is not a permanent solution as it only dictates the styling rules for various elements. To maintain a hover state indefinitely, Javascript is required.

Personally, I am a big advocate of using the jQuery library. Hence, here's my approach to tackle this issue.

Styling with CSS

ul li ul {
    display: none;
}
ul li.permahover ul {
    display: block;
}

Implementing Javascript with jQuery

$("#onabout").one("mouseover", function() {
  $("#onabout").addClass('permahover');
});

The one event binds a handler to an event that will execute only once per element.

Check out the Demo

http://jsfiddle.net/jlratwil/w83BW/

Answer №4

To achieve this functionality, JavaScript is required. By utilizing jQuery, you can effectively update the style permanently in this manner:

$(#onabout).onmouseover().css("display","block");

Answer №5

Unique CSS Concept

Exploring the idea of creating a "stay visible" effect using pure CSS can lead to various approaches with potential unintended consequences. Achieving a purely permanent solution might require implementing JavaScript, as suggested by others.

Extended Visibility But Not Permanence

If your goal is to keep an element visible while hovering over another specific area, a simple adjustment like the following in your code could work:

#about:hover #onabout,
#onabout:hover {
   display: block;
}

Sustained Visibility Approach

An alternative method involves encasing the #onabout element within a wrapper with a fixed position that reveals itself upon hover. By setting this wrapper to cover all sides of the screen and employing its :hover state to maintain visibility of #onabout, you may achieve a more persistent effect. However, this approach may interfere with other interactions on the page. Consider customizing this idea based on your specific project needs. The code snippet for this technique would look something like this:

#permaHoverWrap {
   position: fixed;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   display: none;
}

#about:hover #permaHoverWrap,
#permaHoverWrap:hover {
   display: block;
}

In this scenario, the visibility of #onabout is maintained through the fixed wrapper's display property toggled via hover interaction. While this concept demonstrates a CSS-only solution, it may have adverse effects depending on the context. Experiment with caution to determine its suitability for your application.

Answer №6

One effective solution involves manipulating classes on an element, which can be achieved using JQUERY.

#about {
height: 25px;
width: 84px;
background-image: url('about.png');
position: absolute;
top: 200px;
left: 0px;
}

#onabout {
height: 200px;
width: 940px;
position: absolute;
top: 60px;
left: 0px;
color: #fff;
font-family: 'Lato', sans-serif;
font-size: 15px;
font-weight: 300;
display: none;
}

#about.hover #onabout {
display: block;
}

It's worth noting the change from :hover to .hover (Class Name) in the code.

$("#about").hover(function(){
   $(this).addClass("hover");
});

Answer №7

It's unlikely that you can achieve this effect using only CSS and the :hover selector. One alternative is to use a click event instead. By changing the code so that the user has to click on the div, you can make the other one appear permanently.

<style>
.onabout
{
    display: none;
}

input#activate
{
    display: none;
}

input#activate:checked + .onabout
{
    display: block;
}
</style>

<label for="activate">
    <div class="about">Click me</div>
</label>
<input id="activate" type="checkbox"></input>
<div class="onabout">Visible while checkbox:checked true or Radiobutton:checked true</div>

If you want the ability to hide the element by clicking again, you can use a checkbox. Alternatively, if you prefer the element to stay visible until another option is clicked, consider using a radio input type.

Answer №8

To solve this issue, it seems that utilizing CSS animation with an eternal delay could be beneficial. Take a look at the following article for more information...

Answer №9

Just stumbled upon the solution to your query. Check out the sample css code below:

.Main_title {
margin-top: 20px;
margin-left: 15px;
font-family: 'Open Sans';
font-size: 14pt;
color: #4E7AA1;
transition: 800ms 2s;
}

or

.Subtitle_text {
margin-top: 6px;
width: 200px;
margin-left: 20px;
font-family: 'Open Sans';
font-size: 10pt;
color: #8C6239;
position: relative;
top: -25px;
left: -180px;
opacity: 0;
transition: opacity 3s 2s, left 800ms 2s, top 1.5s 2s;
}

The second value in the transition attribute functions as a delay parameter. This means that your hover effects will persist for the specified duration before returning to their original state (e.g., transition: 800ms 2s; - In this case, the hover effect will remain active for 2 seconds after you move your cursor away).

Hope this clears things up.

S.D.R

Answer №10

Forget about Javascript – CSS is all you need to achieve this effect. Here's a way to implement it using CSS without any scripting:

(Remember, visibility is the key here instead of display)

.menu-wrapper .dropdown-content {
  display: inline-flex;
  transition: all 0s ease 1s; /*delay 1s*/
  visibility: hidden;
}
.menu-wrapper:hover .dropdown-content {
  transition-delay: 0s;
  visibility: visible;
}

Answer №11

Check out this jQuery trick that allows a second level menu item to display its child third level contents when hovered over, and maintains visibility until another second level item is selected.

 $("ul.second > li.dropdown").hover(function() {
    // Remove the 'show' class if it's already applied
    $('ul.third').removeClass('show');
    // Apply the 'show' class only to the child of this element
    $(this).find('.third').addClass('show');
  });

Answer №12

Try replacing this:

#section:hover #onsection {
 display: block;
}

Insert the following line:

#onsection:hover {
 display: block;
}

Your updated code should look like this:

#section:hover #onsection,
#onsection:hover {
 display: block;
}

Ensure that no margin is added in the process.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I position a floating label within a form-control in Bootstrap 5, while also setting its width to

I am currently utilizing Bootstrap 5 and attempting to incorporate floating labels with centered form controls that have a width set to auto, as opposed to the default 100% in Bootstrap. My challenge lies in getting the floating labels to remain inside the ...

Pattern for Ajax callback in Javascript

I'm facing an issue with the code snippet below. var bar = { ajaxcall : function(){ var _obj = {}; $.ajax({ headers: { 'Content-Type': "application/json; charset=utf-8", 'da ...

encountering difficulties when inserting a div in between slick.js slides

Incorporating slick.js with Angular, I am facing a challenge in adding a custom div with a dashed border after each image in my slick slider. This div should be positioned between the gaps of two slides. The slick slider is implemented with vertical scroll ...

"Ajax has the ability to alter the vertical scroll position

I have a quick query for you. Can anyone assist me in understanding why the page scroll resets to the top after an Ajax request? I suspect it has something to do with JQuery. There isn't much information available online, so I'm reaching out for ...

It appears that JavaScript has the capability to automatically remove event listeners from textareas

Greetings to all members of the community, I have developed a web application (see code below) that, when double-clicked, inserts a text area into the frame. Upon double-clicking the text area, it changes its color to green, and with another double-click ...

Is it true that event.stopPropagation does not function for pseudoelements?

I am facing an issue with event handling in my ul element. The ul has three li children elements, and the ul itself has a useCapture event handler for click. In the click event handler, I successfully stop the event using event.stopPropagation(), and every ...

Stylish News Carousel using CSS and HTML

I've been working on creating a news slider for my website, but I'm encountering an issue. Despite completing the HTML and CSS, I'm struggling to make the 'showcase' rotate using JQuery. I've tried various guides and instructi ...

increasing the size of the input thumb compared to the others

Hello fellow React developers, I'm currently learning by coding and I have a question about a slider. I'm trying to make the thumb of the slider bigger, but when I increase its size it doesn't fully display within the input area (as you can ...

switch the visibility of the p tag based on its content

It seems like solving this shouldn't be too challenging, but I'm still learning when it comes to Javascript or JQuery. HTML: <p><span id="AddLine1Summary"></span>,</p> <p><span id="AddLine2Summary"></span& ...

Creating a dual-column checkbox design with CSS only

I'm working with a dynamically generated form element that can only be styled using CSS. I need help achieving a specific layout without making any HTML changes, except for the classes. The "element" and "formField" classes are common and cannot be mo ...

Group in group, glitch in outdated browser

Facing an issue with IE6 while writing HTML/CSS code. I am looking to create dynamic divs using classes: Here is a simple example (not real-project code): .top {width: 50px;} .top.selected {background: #f00;} .mid {width: 114px;} .mid.selected {backgrou ...

the frame on the page is distorting

I am trying to implement a frame around my website that overlaps elements. desired appearance Although I created a styled div for the frame, it seems to have a bug and looks like this: current appearance Below is the React code I am using: class About ...

Why is the Bootstrap template working on index.html but not on any other React components?

Greetings to all who stumble upon this post! Hello, I have been diligently working on a React application and am eager to integrate a Bootstrap template. However, everything seems to be in order except for the responsiveness of the template. It lacks anim ...

Adjusting table font dynamically using Angular and CSS

I am currently working on an Angular project and facing a challenge with setting the font size dynamically in a table. I have created a function to address this issue, which includes the following code snippet: $('.td').css({ 'font-size ...

"Stylish symbols displayed atop the bootstrap navigation bar

Looking to add some icons (Facebook, Instagram, YouTube, WhatsApp) above my navbar elements. Here is an example of what I'm trying to achieve: https://i.sstatic.net/Ojio6.png Below is the code I am currently using: <nav class="navbar fixed-top na ...

Adding an id attribute to your HTML <tr> element can make it more

Is there a way to assign a unique ID (such as 1, 2, 3, etc.) to each new row generated by my script? FIDDLE CODE: function deleteRow(tableID) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; ...

Issues with submenus not displaying properly in the hover menu

I am encountering an issue with my vertical hover menu. Even when I don't actually hover over any buttons, but the cursor is slightly to the right of them, the submenu pops up. Additionally, when there isn't a submenu, the main menu button starts ...

template not being loaded with form

My Django form is not showing up when I try to implement it. Even after checking the browser's inspector, there is no sign of it being loaded at all. If more information is required, please let me know. Here is the project structure: /beerad url ...

Attempting to fill a collection of JSON entities through a GET call?

When handling a GET request that contains a list of JSON objects, I want to display the data in an input field on the screen using ng-model for data binding. The structure of the JSON get request is as follows: [{"make":"Mahindra","vin":"1987","model":"XU ...

Image floated to the left, text aligned in the center. Following the image, the links are not aligned

UPDATED: I recently made some adjustments to my webpage layout. There is an image at the top of the page that is floated left, and next to it, there is a list of links centered with text-align: center. However, the last link is centered within the div but ...