How can I maintain the hover state even when the mouse is not hovering over it in CSS?

I have a lineup of 5 div boxes, and whenever you hover over one, it expands to fill the width of the screen.

The default width of the blue div is set to cover the whole screen, allowing for the application of CSS to shrink it when hovering over the other divs as it is placed last in the HTML:

#green{
    width:20px;
}
#blue{
    width:100%;
}
#green:hover{
    width:100%
}
#green:hover ~ #blue{
    width:20px;   
}

Is there a method to keep a div box expanded after it is no longer being hovered over? For instance, if the orange div was open and then the cursor moved off it without going to another colored div, could it retain its full width? Currently, when the cursor moves away, the divs revert back to blue.

Furthermore, can this be achieved without using any scripting language?

Answer №1

Unfortunately, achieving this without JavaScript is not possible. However, here is a snippet of sample code that you may find useful:

<div id="green" onmouseover="green()">foo</div>

<div id="blue" onmouseover="blue()">more foo</div>

<div id="pink" onmouseover="pink()">even more foo</div>

And here is the accompanying script:

function green() {
    $('div').css('width','20px');
    $('#green').css('width','100%');
}

function blue() {
    $('div').css('width','20px');
    $('#blue').css('width','100%');
}

function pink() {
    $('div').css('width','20px');
    $('#pink').css('width','100%');
}

I hope this information proves helpful to you!

Answer №2

Achieving this effect without a script is feasible with CSS Animations. Check out

While not as dependable as using a script, it is doable -- even maintaining it after hover, which poses the real challenge.

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

I prefer to prevent the page from refreshing once the PHP code is executed

<?php $sendTo = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="45203d242835292005203d24283529206b2b29">[email protected]</a>"; //Receiver mail $name = ""; //Sender's Name $email = ""; //Sender' ...

What is the method to alter the color of the browse button?

I am currently utilizing a custom file input feature from Bootstrap: https://i.sstatic.net/ybCYl.png Is there a way to modify the color of the "Browse" button? My attempts so far: Added a color tag to custom-file-input Added a color tag to custom-file- ...

Create a dynamic onClick event script and integrate it into Google Optimize

I need to incorporate a button element into my website using Google Optimize for an experiment. This button needs to trigger a specific script depending on the variation of the experiment. I have attempted two different methods: <button id="my-button" ...

CSS transitions/transforms are failing to impact the necessary elements

I'm currently grappling with advanced transitions and transforms, and encountering some challenges with a section I'm developing. I've managed to get everything working as needed, except for the fact that there is an h5 element positioned ov ...

The position of the scroll bar remains consistent as you navigate between different websites

Is it possible for me to click on a link within my HTML website and have the other website load with me in the exact same position? For example, if I'm halfway down a webpage and click a link, can I be taken to that same point on the new page? If so, ...

What is the best way to showcase nested array JSON data in an HTML Table?

https://i.stack.imgur.com/OHL0A.png I attempted to access the following link http://jsfiddle.net/jlspake/v2L1ny8r/7/ but without any success. This is my TypeScript code: var viewModel = function(data){ var self = this; self.orders = ko.observableArr ...

Snapping a photo from the webcam for your profile picture

Is there a way to capture images using a webcam and upload them to a server in a PHP & Mysql application? I've been searching on Google but only find outdated code that is not supported in all browsers. Here are some links you can check out for more ...

"Fixing index.html with the power of Bootstrap 4: A step-by

*I am facing an issue where the default bootstrap navbar style appears before my custom styles get applied every time I reload the page. Any assistance would be greatly appreciated* - Whenever I reload the page, the default bootstrap navbar style appears ...

Make sure to add CSS styles to the "ul" class first, followed by

I'm looking to update the color of the links in my footer using CSS. I want to change the color of all a items that have the same classes, but I'm unsure of how to do this with CSS. Here's the code snippet: <div class="col-lg-3 ...

Utilizing KnockoutJS to Apply CSS Binding Based on Dropdown Selection

Check out the live example here: http://jsfiddle.net/0gmbbv5w/ In my application, I have an object retrieved from the database that I bind to a <select> var NoticeType = function (noticeType) { this.NoticeTypeId = ko.observable(noticeType.Notic ...

Transforming a two-digit year into a four-digit year

Our entry form provides users with a calendar drop-down option, but they are also able to manually type in dates. We recently discovered that if someone enters a 2-digit year, it automatically changes to 1912 instead of 2012 (as dates cannot be in the past ...

The multiple-choice selection tool is not displaying any choices

I am having an issue with my ng-repeat code in conjunction with the jquery multiple-select plugin. Despite using this plugin for a multiple select functionality, the options generated by ng-repeat are not visible. Below is the code snippet in question: & ...

Are there any alternatives similar to the reverse sr-only feature in Bootstrap 4?

I implemented the use of .sr-only to ensure compatibility with text-based browsers such as Lynx. This allows for the display of an ASCII logo in the header for those using standart GUI browsers like Firefox, while displaying the normal graphical logo for o ...

Having trouble implementing responsive image with fixed aspect ratio code

I've tried several tutorials online to make this image responsive with a fixed aspect ratio, but nothing seems to work. The image is set at 900x400px and I want it to remain fully visible on smaller screens as well. Here's the codepen link < ...

Struggling to concentrate on a text field following navigation in an AngularJS application

My current project involves working with an AngularJS application where I am facing a challenge in setting the cursor or focus on a specific text box when routing to a page. Despite my attempts using various methods such as setFocus() in JavaScript, autofo ...

Style Guide for optimum class arrangement in Bootstrap framework

I've come across various Style Guides that discuss declaration and attribute ordering, but surprisingly none that specifically address class ordering. For instance, one developer's code looks like this: <div class="btn btn-primary btn-lg mt- ...

Basic email subscriber form using PHP and HTML

Seeking assistance in creating a basic subscriber email form where users can input their email addresses. Upon submission, I would receive an email notification indicating a new subscription and the client would be informed that we will get in touch with t ...

Difficulty aligning headings in HTML

I'm puzzled by a strange 1px gap on the left of my h2 heading, which sits atop an h3. The font sizes are set to 40px for h2 and 12px for h3. Can anyone help me solve this mystery? Any assistance would be greatly appreciated! Thank you body { pa ...

HTML page with a Bootstrap video banner displaying incorrectly

I'm currently working on modifying code sourced from Video Header snippet to suit my needs. The video header I've implemented is taking over my entire page, and I'd like to adjust its size to a width of 100% across the page and a specific h ...

Navigational Menu in PHP

My goal is to have one link that retrieves data from a table with a specific value and another identical link that pulls data from the same table but with a different value. However, both dropdowns are displaying in the link. <div class="col-sm-9"> ...