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

Clicking the button to send the form using JavaScript

Currently, I am dealing with a shopping cart plugin that relies on a basic form on the product page to send values such as price and product name. However, I am facing an issue where this plugin uses a standard submit button to transmit the values, and I w ...

Barba.js Revolutionizes Page Reloading for Initial Links

On my Wordpress site, I am using Barba js v.2 for page transitions. However, I have encountered an issue where I need to click on a link twice in order to successfully change the page and make the transition work. Interestingly, the first time I click on t ...

Place the object into a vacant area with the help of jQuery Sortable

I am using jQuery Sortable and it's functioning well. However, I have encountered a small issue that I need help with.    I have two blocks where elements from one block can be moved to the other. The problem arises when all the elem ...

Struggling to extract a substring from a lengthy multi-line string in Swift

After extracting some HTML code and storing it in a string named "html", I needed to retrieve the number of stars from this HTML code. Initially, my approach was to search for the word "stars" within the html string itself. Here's the code snippet I u ...

Designs for Creating Webpages

As a beginner in the coding world, I have little experience with HTML and Python. My goal is to create a personal webpage, starting with a template and customizing it to fit my needs. Any suggestions on where to find free templates or where to begin my s ...

Display the field names from a MySQL table on a web page

Is there a way to show all column names from a MySQL table on a webpage if only the table name is known? ...

The SVG syntax underwent alterations following its transmission via email

Can someone please help me fix this code? My visual studio code interpreter seems to be having trouble recognizing the style code within it. <?xml version="1.0" encoding="utf-8"?> <!-- (c) ammap.com | SVG weather icons --> <svg vers ...

406 error is triggered by GET parameters

Here is a PHP script I have: <!DOCTYPE html> <html lang="es-ES" xmlns="http://www.w3.org/1999/xhtml" xml:lang="es-ES"> <head> <meta charset="iso-8859-1" /> </head> <body><p><?php if(isset($_GET['echo& ...

Encountering a problem with Selenium when dealing with tabular data displayed in a DIV format on a website, where each row is encapsulated within its own DIV element

While creating Selenium automation tests for a website with multiple rows contained within a main DIV element, each row represented by a separate DIV. For example, if there are 5 dynamically generated rows, the HTML code structure would look like this: < ...

Get the nearest offspring of the guardian

I have a main 'container' div with multiple subsections. Each subsection contains 1 or 2 sub-subsections. Let's say I have a variable that holds one of the subsections, specifically the 4th subsection. This is the structure:container > s ...

Creating a triangle shape using Bootstrap to style a div element, similar to the image provided

Trying to achieve the look of the attached image with a few divs. I know I can use a background image like this: background:url(image.png) no-repeat left top; But I'm curious if there's another way to achieve this without using a background ima ...

The functionality of Ng-Show is acting up

$scope.stay = function() { alert("Inside Keep me In") $scope.timed = false; $scope.isLogStatus = true; } $scope.displayAlert = function() { $scope.timed = true; alert("inside display") } function idleTimer() { var t; $window.o ...

Dividing a sentence by spaces to isolate individual words

Recently, I encountered a challenging question that has me stuck. I am working on creating an HTML text box where the submitted text is processed by a function to check for any links. If a link is detected, it should be wrapped in anchor tags to make it cl ...

Is it possible to load a route first, and then proceed to load the static files from the public folder in a Node.js application?

When running the app.js file for Node.js, I am trying to ensure that the '/' route is loaded first. This route handles authentication and then redirects to the index.html file. However, I am encountering an error because the program cannot find ...

Converting Selenium Webdriver Xpath value to CSS: A Step-by-Step

We are looking to transform the lengthy HTML Xpath values in the existing code into a shorter CSS value: driver.findElement(By.cssSelector(".form button")).click(); ...

Utilize AJAX or another advanced technology to refine a pre-existing list

I am trying to implement a searchable list of buttons on my website, but I haven't been able to find a solution that fits exactly what I have in mind. Currently, I have a list of buttons coded as inputs and I want to add a search field that will filte ...

Mysterious CSS "manifestation" perplexes Chrome while evading Firefox's gaze

There seems to be an issue with the jQuery plugin on this demo page: If you access it using Firefox, you'll notice that the social sharing buttons are visible and functioning properly. However, in Chrome, there appears to be a peculiar CSS computatio ...

Assistance needed with CSS floating properties

Despite seeing many inquiries about float, I still can't pinpoint what's causing my issue. My objective is simple: GREETINGS FRIEND I aim for it to align just to the left of the unordered list. Referencing this solution, I attempted adding cl ...

What is the best way to search for a specific term in Visual Studio Code/IDE without including another term in the search results?

Is it feasible to search for instances in my code where the term "<img" appears without being accompanied by the term "alt="? This would help me locate all image tags that do not have the alt attribute. Can you provide guidance on how this can be achiev ...

Adjust the appearance of a div according to the input value

When a user inputs the correct value (a number) into an input of type "number," I want a button to appear. I attempted var check=document.getElementById("buttonID").value == "1" followed by an if statement, but it seems I made a mistake somewhere. Here&ap ...