Does this extensive combination of pseudo classes hold true?

Here's the code snippet I am working with:

.fontmenu .fontlist{
        position: absolute;
        bottom:30px;
        display:none;

    }

.fontbutton button:hover .fontmenu .fontlist{
        display:block;
    }
<div class="fontmenu">
    <div class="fontbutton">
        <button>fonts</button>
    </div>
    <div class="fontlist">  
        <div onclick="font(1)">Arial</div>
        <div onclick="font(2)">Courier</div>
        <div onclick="font(3)">Verdana</div>
        <div onclick="font(4)">sans</div>
    </div>
</div>

I'm facing an issue where the CSS doesn't seem to work as expected. The font list is not visible when I hover over the button. I need some clarification on whether the

.fontbutton button:hover .fontmenu .fontlist{}
syntax is correct or not.

Answer №1

Here is the code you have utilized:

.fontbutton button:hover .fontmenu .fontlist{ }

Why It Won't Work

Interested in knowing why this won't work? Continue reading on as I provide an explanation. But first, let's explore what will work.


Let's experiment with different selectors:

.fontbutton:hover + .fontlist {}

This WILL be effective

Witness it in action below:

.fontmenu .fontlist {
  bottom: 30px;
  display: none;
}

.fontbutton:hover + .fontlist {
  display: block;
}
/* Avoid targeting the parent fontmenu div and instead target siblings like fontbutton and fontlist.
Use the + selector to avoid the browser mistaking fontlist for a child of fontbutton */
<div class="fontmenu">
  <div class="fontbutton">
    <button>fonts</button>
  </div>
  <div class="fontlist">
    <div onclick="font(1)">Arial</div>
    <div onclick="font(2)">Courier</div>
    <div onclick="font(3)">Verdana</div>
    <div onclick="font(4)">sans</div>
  </div>
</div>

Note how the list appears even by hovering to the right of the button. This occurs because we are targeting the div element fontbutton, not the <button> element. As a result, the browser displays the list when we hover over the div, not the button.

Solution

A slight HTML adjustment is required.

.fontmenu .fontlist {
  display: none;
}

button:hover + .fontlist {
  display: block;
}
<div class="fontmenu">
  <button>fonts</button>
  <div class="fontlist">
    <div onclick="font(1)">Arial</div>
    <div onclick="font(2)">Courier</div>
    <div onclick="font(3)">Verdana</div>
    <div onclick="font(4)">sans</div>
  </div>
</div>

By eliminating the .fontbutton class and positioning the <button> as a sibling of .fontlist, the list will now only be visible when hovering over the button.


You may suggest adding more selectors to your CSS. However, this wouldn't work as there's no direct way to target <button> and then move down to the separate .fontlist within a different div.

.fontbutton > button:hover ? .fontmenu > .fontlist{ }

The issue lies with the ? placeholder.

  1. We initially descend to .button.
  2. We then ascend to .fontbutton.
  3. Add a + selector and transition to .fontmenu.
  4. Finally, reach .fontlist by descending further.

Once we've descended to .button, we cannot return upwards to .fontbutton.

Lack of Parent Selector in CSS

Hence, such targeting methods are not feasible using CSS.

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

The performance of the Ajax Jquery remove function leaves something to be desired

My table has items with a delete button for each row. To achieve this, I created the following Ajax function: $(document).ready(function() { $(".destroy-device").click(function(e) { e.preventDefault(); var id = $(this).attr("data-id"); $.aj ...

A guide on dynamically sending data to a PHP script when an input is changed and displaying the results in a

I am trying to implement a feature where the data inputted into a text field is sent to posttothis.php and then the result is displayed in a content div. However, I am encountering difficulties in making it work. testscript.html <html> <head> ...

"Enhancing the appearance of sorted divs using CSS3 animations in jQuery

Looking to enhance the sorting functionality of divs using jQuery by incorporating CSS3 transition animations for smoother transitions. Encountering an issue where the CSS3 transition animation is not functioning as expected, currently investigating the r ...

Transform a Microsoft Word table into HTML code

My boss has provided me with a very detailed table in an MS Word document filled with various colors, shapes, and lots of information that I need to incorporate into a website. I attempted saving the Word file as HTML and then transferring the code over, ...

Setting default selections for mat-select component in Angular 6

I've been attempting to preselect multiple options in a mat-select element, but I haven't been successful so far. Below is the snippet of HTML code: <mat-dialog-content [formGroup]="form"> <mat-form-field> <mat-select pla ...

A guide on accessing information from a post form using an express.js server

Issue: Whenever the client submits a form using a post request to the server, the express server receives an empty body (req.body = {}). Objective: My goal is to retrieve req.body.username and req.body.password on a post request from the client (using the ...

JavaScript encounters an Access Denied error when attempting to read data from a JSON file in

My code is experiencing issues on IE browser and Chrome, but works perfectly on FireFox. var currentPage = 1; var max = 0; var myList = []; var links = []; $.ajax({ cache: false, type : 'GET', crossDomain: true, ...

What is the process for uploading an image and entering text into the same row in Google Sheets?

Hello, I am currently working on a Google Script web app that enables users to upload 10 photos along with comments for each photo. This information will then be inserted into a Google Sheet when the user clicks the 'upload' button on the web app ...

I'm having trouble getting things to line up properly in Bootstrap5. Can anyone help me figure out why?

When observing this layout, there seems to be a vertical misalignment. The button slightly overflows, while the input field does not. https://i.sstatic.net/pEw2U.png What could be causing this issue? I am currently utilizing Bootstrap 5.2.3 and have incl ...

Retrieving ng-repeat object in Angular

How can I retrieve the current object from an ng-repeat on ng-click without using $index? The $index method is giving me the wrong index due to my use of orderBy. Ideally, I would like to be able to click on the object (thumbnail) and have $scope.activePer ...

Before beginning my selenium scripts, I need to figure out how to set an item using Ruby in the browser's localStorage

Before running my selenium scripts, I am attempting to store an item in the browser's localStorage. I attempted to clear the local storage using this command: driver.get('javascript:localStorage.clear();') This successfully cleared the lo ...

What is the best way to restrict the selection of specific days of the week on an HTML form date input using a combination of JavaScript, React, and HTML?

I need help customizing my Forms Date Input to only allow selection of Thursdays, Fridays, and Saturdays. I've searched for a solution but haven't been successful so far. Is there any JavaScript or HTML code that can help me achieve this? Below ...

The HTML code as content

While working on an AJAX project, I ran into this issue: http://jsbin.com/iriquf/1 The data variable contains a simple HTML string. After making the AJAX call, I noticed that the returned string sometimes includes extra whitespaces. I attempted to locat ...

Tips for effectively handling repetitive bootstrap CSS code

As part of my coding routine, I often utilize the following code snippet to center specific content: <!-- horizontal --> <div class="d-flex align-items-center justify-content-center h-100"> .. </div> <!-- vertical --> & ...

Increased wait time during initial execution

Currently facing an issue with delaying the first run of a function. I've developed a basic slideshow that is causing problems due to this delay in the initial run. My goal is to have the first run wait for 10 seconds and then maintain a 4-second del ...

What is the best way to showcase my React App.js in an HTML document?

Is there a way to display my React app file (App.Js) within my Index.html file? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/fav ...

Change all lowercase characters to uppercase in the font file

: I recently created a .ttf font file, but unfortunately only included capital characters. Is there a tool or simple method I could use to automatically create lowercase versions of each letter? For instance, when typing "hello" the font should display ...

Pull HTML code from element using XPath in Python Selenium

I want to extract HTML codes from a specific element using XPath. Here is the XPath: //*[@id=":nn"]/div[1] Additionally, I need to download an image from the same element. What is the best way to retrieve these HTML codes? ...

Customize your Outlook emails with HTML and CSS to right-align content for a polished look

My current project involves generating a report to be sent via email, with Outlook being the mandatory application in our system. The report's content is HTML-based and utilizes a table layout. Part of the content needs to appear right-aligned within ...

"Interactive table feature allowing for scrolling, with anchored headers and columns, as well as the option to fix

I'm in need of a table that has certain requirements, as shown in the image. https://i.stack.imgur.com/Z6DMx.png The specific criteria include: The header row (initially blurred) should remain fixed when scrolling down the table The first column s ...