I am unable to understand the purpose of this, I need the help of an expert to figure it out

I recently came across a code snippet on a tutorial website that explained how to create a pure CSS horizontal menu. Just when I thought I had grasped the concept, I encountered an issue that has left me reflecting on all that I have learned about CSS. The code in question is as follows:

#nav, .nav, #nav .nav li { margin:0px; padding:0px; }
#nav li {float:left; display:inline; cursor:pointer; list-style:none; padding:0px 10px 0px 10px; border:1px #000 solid; 
position:relative;}
#nav li ul.first {left:-1px; top:100%;}

li, li a {color:#000; text-decoration:none;}
#nav .nav li { width:100%; text-indent:10px; line-height:30px; margin-right:10px; border-top:1px #000 solid; 
border-bottom:1px #000 solid; border-left:none; border-right:none; background:#fff;}
#nav li a {display:block; width:inherit; height:inherit;}

ul.nav { display:none; }
#nav li:hover > a, #nav li:hover { color:#fff; background:#000; }
li:hover > .nav { display:block; position:absolute; width:200px; top:-2px; left:50%; z-index:1000; border:1px #000 solid; }
li:hover { position:relative; z-index:2000; }

And here is the corresponding HTML:

<ul id="nav">
    <li>Menu 1
        <ul class="nav first">
            <li><a href="#">Menu 1</a></li>
            <li>Menu 2</li>
            <li>Menu 3</li>
            <li>Menu 4</li>
        </ul>
    </li>
    <li>Menu 2</li>
    <li>Menu 3</li>
    <li>Menu 4</li>
</ul>

However, I ran into an issue while working on this code. Specifically, on the 9th line of the CSS where it says li:hover > .nav. When I tried changing it to #nav li:hover > .nav, the drop down menu did not behave as expected and seemed to mess up. I've been trying to understand why simply adding "#nav" could disrupt the layout. Despite coming up with some theories, they seem to contradict what I've learned so far. If anyone can offer an explanation, I would greatly appreciate it. Thank you!

Answer №1

You currently have the following CSS code:

#nav li ul.first {left:-1px; top:100%;}

Additionally, you have this CSS code:

li:hover > .nav { display:block; position:absolute; width:200px; top:-2px; left:50%; z-index:1000; border:1px #f00 solid; }

Because the first line is more specific, its left and top values will take precedence over those of the latter line. By modifying the latter line to read like this:

#nav li:hover > .nav { display:block; position:absolute; width:200px; top:-2px; left:50%; z-index:1000; border:1px #f00 solid; }

This new modification becomes more specific than the original and gains priority over it, causing a change in positioning.

To learn more about CSS specificity, you can search for "css precedence" on the web, such as at this link:

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

Cufon text isn't displaying on Internet Explorer 8

The website functions properly in browsers like Chrome and IE9, however, there are some issues when viewed in IE8. Interestingly, it seems to work fine in IE7. The main problem lies in the top navigation menu which uses a custom font with Cufon. While th ...

Encoding in Spring MVC for HTML with UTF-8 does not properly render Russian characters

I have been experimenting with an idea and have configured the settings as follows: https://i.sstatic.net/LYteF.png and also adjusted the settings to: https://i.sstatic.net/qQXB9.png After all these changes, the outcome is displayed as: https://i.sstatic ...

What is the reason behind div elements shifting when hovering over a particular element?

Currently, I have floated all my div elements (icons) to the left and margin-lefted them to create space in between. I've displayed them inline as well. However, when I hover over one element (icon), the rest of the elements move. Can you please help ...

What is the best way to position the footer at the bottom of the page without obstructing any content?

I am currently working on a project that utilizes Bootstrap, and I am facing an issue with positioning the footer. Ideally, I want the footer to stay fixed at the bottom of the screen when there is not enough content to fill the page. However, when the con ...

I am having issues with my Django application where I am unable to check

I have an HTML page where I want to only display the search bar if the table object passed in is not empty. However, my check doesn't seem to be working correctly. Here's my code: <!-- We'll show the search bar only if the user has acces ...

<!--[if IE]> does not function properly on any web browser

Hello, I am experiencing a perplexing issue and despite searching extensively on Google, I have been unable to find a solution! I am trying to change the body background color to red only in Internet Explorer, but it is not working as expected. Here is t ...

What is the best way to adjust the dimensions of a textfield in Vuetify by altering its width and height?

Is there a way to adjust both the width and height of the <v-text-field>tag? I attempted to modify it using .css, but it seems to only affect certain parameters, leaving large white spaces on my page. This is the method I have tried so far: <te ...

Implementing universal styles to injected HTML (v-html)

I am facing an issue while injecting HTML content from my database using Vue. The problem is that although I can successfully inject the HTML, the framework stylesheet (Vuetify) does not seem to be applied to the injected content. The rest of the page ha ...

Border for status input like on Facebook

I tried to investigate with Firebug, but struggled to find a solution. How does Facebook wrap the border around the autosize input in the status section? I am particularly curious about the small triangle attached to the border. While using Firebug, I loca ...

The function provided to jQuery's one() method will only be executed the first time the event is triggered

Greetings from a newbie in the world of javascript! I am currently experimenting with creating custom menu transitions using some basic jquery code. The concept is to have a menu that is visible initially, and when the user clicks "close," the menu will ...

The function of modal in JavaScript is not working properly

I am encountering a problem with my web page that is running on Wildfly 12. It is a simple Java EE project that I developed in Eclipse Neon. The issue arises when I try to use Bootstrap modals, as every time I attempt to open or use the methods in a JavaSc ...

Is there a way to have just the data table automatically update every 2 minutes without affecting the rest of the

I'm working on a project where I need to create an inbox mail that automatically refreshes every 2 minutes to display any new mail in the table. Can anyone help me with refreshing my datatable? ...

Bootstrap flex: on a single row, one column spans the entire height while the other contains elements that are vertically aligned in the center

I'm having trouble with Bootstrap 4 as I try to vertically align the elements of the right column and create a full-height column on the left with just a background color. I've come across many posts discussing similar issues, but none specific t ...

Changing text on a button with JavaScript toggle functionality: A step-by-step guide

I am looking to implement a feature where I can hide and show overflow content in a div. When I click on the expand button, it expands the content. However, I want this button to toggle and change text as well. Thank you! function descriptionHeightMo ...

The JSON is not displaying in the expected sequence

I am currently working with a json file named places.json which contains data on various cities around the world. Here is a snippet of the file: { "Europe": [ { "name": "London", "country": "England" }, ...

Tips for Making JQuery UI Droppable Work with Multiple Elements

I've been tasked with modifying a piece of code that currently allows users to drag one row from a table to a div. The new requirement is to enable users to select multiple rows and drag them all to the div. I'm struggling to tweak the existing c ...

Using jQuery Ajax and PHP to retrieve data from a database based on multiple checkboxes

my code currently only selects one checkbox at a time. What I am trying to achieve is when clicking on the first checkbox, it should display a result. Then, if the second or third checkbox is clicked, all checkboxes should be submitted together to the proc ...

"Subtle Website Background Fade Effect When Menu is Hovered Over

Click on the following link to observe a transition effect that occurs on the body of the website when interacting with the main menu system: Here is the website Do you know what this effect is called and how it can be integrated into a website? ...

Issues with padding in the h1 title of a mobile website

I am currently working on creating a mobile-friendly version of a website. However, I am facing an issue where the background image set for an H1 title is only taking into account the "text height" and not the actual picture height specified in the CSS. A ...

Querying with jQuery to Retrieve HTML from Elements with the Same Class

Greetings Everyone! Please take a look at my code... <font class="detDesc" > Uploaded 07-11&nbsp;2012, Size 105.79&nbsp;MiB, ULed by <a class="detDesc" href="/somelink" title="somelink title">somelink</a> </font> ...