CSS Navigation Menu - Submenu displaying above main link

<div id="header">
<div class="cont">
<div id="banner">

  <div id="nav">

  <ul>
    <li><a href="index.htm">home</a></li>

    <li><a href="work.html">Works»</a>
    <ul>
      <li><a href="paintings.html">Paintings</a></li>
      <li><a href="#">Watercolour</a></li>
      <li><a href="#">Ink</a></li>
    </ul>
    </li>
    <li style="

 "><a href="about.html">About</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
  </div>
</div>
  <div id="greeting" style="
clear: both;
  ">

Navbar CSS (Check out the full CSS in the JSfiddle link provided below)

#nav
{ float:right;
position:relative;
width:100%;
text-align:center;
font-family: monospace, serif;
list-style-type: none;
height:150px;
}
#nav ul
{float: center;
clear: left;
font-family: monospace, serif;
list-style-type: none; 
text-align:center;
padding:0;
}

To see an illustration of the issue, visit this JSfiddle link.

The problem arises when hovering over the "Works" link as shown in the jsfiddle. Despite my attempts to rectify it, I have not been successful. I seek your assistance in understanding why the drop-down menu appears above the parent link instead of below. Previously, it was functioning correctly below the main link until I made some CSS adjustments that caused this unexpected behavior. Your help and explanation would be greatly appreciated.

Answer №1

#nav ul li:hover ul {
   display: block;
    opacity: 1;
    position: absolute;
   text-align: center;
   visibility: visible;
}

Answer №2

To ensure proper alignment, remember to assign a vertical-align property to your li elements:

#navigation ul li {
    ...
    vertical-align: top;
}

Check out this JSFiddle example.

Answer №3

To adjust the placement of #nav ul li:hover ul, simply set it to absolute:

#nav ul li:hover ul {
    display:block;
    opacity: 1;
    visibility: visible;
    position:absolute;
    text-align:center;
}

This will position the ul relative to the closest parent with a relative positioning, in this case, the #nav div.

Check out the JSFiddle example

Answer №4

Although it's not directly related to positioning, remember to close the CSS statement for: #nav ul li:hover ul li a:hover. Failing to do so could potentially cause issues in the future.

Answer №5

Eliminate the use of position: relative in the style for #nav ul li:hover ul

You can see a demonstration here http://jsfiddle.net/6n3WK/3/

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

Leveraging the :checked state in CSS to trigger click actions

Is there a way to revert back to the unchecked or normal state when clicking elsewhere in the window, after using the :checked state to define the action for the clicked event? ...

Display One Div at a Time in Sequence on Page Load Using jQuery

I have come across this question multiple times: How to Fade In images on page load using JavaScript one after the other? Fade in divs sequentially Using jQuery .fadeIn() on page load? Despite trying various recommended methods, I'm still unable t ...

When the mouse hovers over, alter the background color with React

Currently attempting to adjust the background color of a Grid section when the mouse enters. I've made some progress, but now I seem to be stuck. My current approach involves calling a function to update the background color on mouse enter. const [isS ...

Getting the Title value from Selenium in Python: A guide

Can someone provide guidance on how to retrieve the title value from this element using Selenium? I have tried the following code but it is returning an empty string. items = driver.find_elements_by_tag_name("li") for item in items: followe ...

Innovative CSS trick for styling checkboxes alongside non-related content

The CSS checkbox hack demonstrated below assumes that the content is a sibling of the checkbox. By clicking on the label, the content will toggle. Check out the DEMO here <input id="checkbox" type="checkbox" /> <label for="checkbox"> Toggle ...

Ways to hide the value from being shown

I have integrated jscolor (from jscolor) to allow users to pick a color with an input field. However, I am facing issues in styling it as I'm unable to remove the hex value of the selected color and couldn't find any relevant documentation on av ...

How can I open a PDF file on the frontend using the Django framework?

I am attempting to extract content from a PDF file using the Django framework. Here is what I have tried so far: def upload(request): try: if request.method == 'POST': if request.FILES['document'].name.split ...

Linked Dropdownlists Connected to a Single Dropdownlist

I am struggling to find the best way to phrase my question, but I need some help. I want to connect three dropdown lists together in a specific way. Dropdown list 2 and 3 should be disabled so the user cannot interact with them, while dropdown list 1 remai ...

Guide to displaying database results in an HTML dropdown menu by utilizing AJAX technology

Greetings! As a newcomer to the realms of PHP and AJAX, I am currently on a quest to showcase the outcomes of a query within an HTML dropdown using AJAX. Let's delve into my PHP code: $pro1 = mysqli_query("select email from groups"); I made an attemp ...

Tips for successfully passing PHP variables to a JavaScript function

Having trouble passing the selected values of two comboboxes to another PHP file using AJAX. How can I pass both values in one function? <script> function showUser(str,str2) { if (str == "" || str2 =="") { document.getElementById("tx ...

What's the best way to ensure that a select and button have consistent heights in Bootstrap 5?

Can I make a button and select element in the same row with Bootstrap 5 have the same height? https://i.sstatic.net/3S13Q.png <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" dat ...

Utilizing tags within the pre tag to incorporate additional styles

Using pre tags to display code works well, but I need to style certain parts of the code in green. However, when I wrap the desired code in a <div> tag with styling, it affects formatting by adding newlines and swallowing spacing. How can I achieve t ...

Troublesome alignment with bootstrap grid system

I require some assistance with the following: Specifically, I need help regarding the structure shown below: <div class="row"><div class="col-md-4"> <a class="individ" href="index.php?p=x" style="margin:5px 5px;"> ...

Retrieve the full directory path that has been chosen in an HTML form

I am facing a similar issue in my web application where I am using PHP, HTML, and JavaScript. What I want is to be able to select a folder and obtain the full path of that folder for backing up data. For example, when a user enters backup.php, they should ...

Remove any unnecessary white space and new lines from the HTML code

Is there a way to remove double whitespaces and new lines from my HTML output without altering the ones within textarea and pre tags using PHP? ...

Error: Property 'onclick' cannot be set on a null object

JavaScript isn't my strong suit, so I'm struggling to solve this issue. The console is showing me the following error message: Uncaught TypeError: Cannot set property 'onclick' of null <script> var modal = document.getE ...

XPath //*[text()] is not picking up the desired text

Using the XPath 1.0 expression //*[text()] does not capture the phrase "now revenue of kfc is" https://i.stack.imgur.com/GeFCY.png However, with the XPath 2.0 expression //text(), it can be selected. https://i.stack.imgur.com/uTSqW.png Unfortunately, S ...

Is SqliteDataReader not compatible with C#?

Currently, I am facing an issue with displaying data from a SQLite database on a web page using C#. Despite my efforts to find a solution and only coming across the console.writeline method, the SqliteDataReader function is not functioning properly. Below ...

When the down key is pressed in a textarea, choose the list item

I have HTML similar to the following <div class="row"> <textarea id="txtArea" ng-model="input" ng-change="handleOnChange(input);getSearchField(input);" ng-click="search(input)" ng-focus="search(input);" ...

Tips for adjusting the close date based on the selected date in a dropdown datepicker

Here is a sample code snippet: <input id="date1" name="start_date" id="dpd1"/> <select class="form_line_only form-control" name="ho_night"> <option selected> 0 </option> <option ...