The issue of the horizontal navigation bar not functioning properly persists when a specific width

I am currently working on building a horizontal navigation bar using CSS.

What puzzles me is that when I specify a width for my ol tag, the horizontal float function does not seem to cooperate. However, if I remove the width property, everything works just fine.

Can anyone shed some light on what might be causing this issue?

Here is the CSS code:

nav {
    position: absolute;
    height: 40px;
    width: 100%;
    top: 240px;
    background-color: #8F8E93;
    float: left;
}

nav ol {
    width: 300px;
}


nav ol li {
    float: left;
}

nav>ol>li>a {
    display: block;
}

And here's the HTML snippet:

<nav>
  <ol>
    <li><a href="reiseziele.html">Reiseziele</a></li>
    <li><a href="kontakt.html">Kontakt</a>
      <ol>
        <li><a href="kontakt_mail.html">E-Mail</a></li>
        <li><a href="kontakt_formular.html">Formular</a></li>
      </ol>
    </li>
  </ol>
</nav>

Answer №1

Make sure to assign a width to the li element, but be cautious when using CSS selectors to avoid affecting nested li elements.

To prevent floating left on inner li elements and make them act as sub menus, apply the following CSS:

nav ol li ol li{
  float:none !important;;
}

For the outer ol li elements, use the following CSS:

nav ol li {
    float: left;
    width:50%;
}

I have overridden the float:none property for inner child elements to enable them to function in a dropdown menu.

View the Demo

Note: Always remember to use clear:both after using float to prevent white gap issues if the width is not sufficient.

Answer №2

If you're looking to simplify things, consider using the flexbox approach for your layout.

Also, I noticed that you have nested sub-navigation using the details and summary tags.

Here's an example of the HTML structure:

<nav>
  <a href="reiseziele.html">Reiseziele</a>

  <details id="container">
    <summary><a href="kontakt.html">Kontakt</a></summary>
    <a href="kontakt_mail.html">E-Mail</a>
    <a href="kontakt_formular.html">Formular</a>
  </details>

</nav>

And here's some CSS to style it:

nav {
    display: flex;
    background-color: #8F8E93;
    justify-content: space-around;
    align-items: flex-start;
}

nav ol {
    margin: 0;
}

Check out this demo for a live example: http://codepen.io/to7be/pen/XNjRLG

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

What is the best way to prevent the content within a div from being obscured by a sticky element affixed to the bottom of the div?

I am struggling with ensuring that the content in my wrapper does not get cut off by the "view more" div attached to the bottom. The default wrapper cuts off the children, and even when expanded, the issue persists due to CSS problems. In React, the gener ...

Display a tooltip upon the page's initial load and automatically hide it afterwards

On page load, I want to display a tooltip for a few seconds or hide it when another tooltip is hovered over. I have been trying to achieve this but because my tooltips are nested in multiple divs due to using an Elementor Addon, I am facing some confusion ...

turning every input field's border to red if none of them were filled out at least once

I struggle with javascript and need some help. I have a form with multiple input fields, and I want to ensure that the user fills in at least one of them. I found code that triggers an alert message if the user does not fill in any fields, but I would pref ...

Utilizing the text field feature within Twitter Bootstrap 3.0 in-line styling

I am attempting to create a horizontal form with an inline text field split into two sections: one on the left and one on the right. The left side will contain horizontal text fields, some of which are inline, while the right side will have a mix of inline ...

The menu will showcase all currently active sub-category items whenever an item below is selected (using Joomla/Mosets Tree)

I am currently managing a Joomla website that utilizes Mosets Tree for a business directory. One issue I have noticed is that on the right side of the site, all the categories are listed. When you expand sub-categories and then roll over to another menu it ...

Is it feasible to incorporate two distinct images into a single element using CSS layering?

I have a question about setting a background image for a web page using CSS. Currently, I have the following code: body { font-size: 62.5%; /* Resets 1em to 10px */ font-family: Verdana, Arial, Sans-Serif; background-color: #9D5922; color: #000; margin ...

Converting plain text to a JSON object using JavaScript: A step-by-step guide

Having trouble converting plain text into JSON value in Javascript. Need some assistance. Actual string obtained from a command line executed through Javascript: Net State: OFFLINE Net Err: Net Time: 0 Current Sample: 0 Sample #0 Sample Role: O ...

The functionality of z-index is not applying when using position fixed

I have the following CSS code: #one{ position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 999; } #two{ position: relative; z-index: 9; width: 200px; height: 200px; background: red; } #link { position: ...

Troubleshooting Bootstrap's Margin Problem

Currently, I am delving into the basics of bootstrap and encountering some challenges with using margin-auto. Specifically, I am working on positioning a navbar by using ml-auto to separate it from the brand, pushing the navbar to the right side of the p ...

Switching the theme color from drab grey to vibrant blue

How can I change the default placeholder color in md-input-container from grey to Material Blue? I have followed the instructions in the documentation and created my own theme, but none of the code snippets seems to work. What am I doing wrong? mainApp. ...

Having trouble implementing font css file in Reactjs

When working with Reactjs (Nextjs), every time I try to incorporate "css" into my project, I encounter the following error on my screen: Module not found: Can't resolve '../fonts/fontawesome-webfont.eot?v=4.7.0' How can I solve this issue? ...

Tips for incorporating a background color into a specific section

Code: .headline { font: 150 50px/0.8 'Pacifico', Helvetica, sans-serif; color: #2b2b2b; text-shadow: 3px 3px 0px rgba(0, 0, 0, 0.1), 7px 7px 0px rgba(0, 0, 0, 0.05); text-align: center; } <link href='http://fonts.googleapis.com ...

How can I retrieve the span html element without using a class with ajax?

Trying to work with Ajax syntax for the first time, I have this HTML code. <div class="select2-container select2" id="s2id_ServiceID-2-1" style="width: 100%;"> <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabind ...

What is the best way to implement this design using CSS flexbox?

I'm working on designing a simple layout that looks like this: https://i.stack.imgur.com/oCzyV.png Currently, I have the following HTML structure with some variations in class names and additional markup within each element: <div class="site ...

Whenever I try to include something within the `componentWillUnmount` function,

I'm currently learning React on my own. I've been trying to save session data in my componentWillUnmount method. However, when I add code inside componentWillUnmount, nothing seems to happen. I tried debugging by adding console logs and debugger ...

Achieving a full width vertical navigation on the left side using Bootstrap's grid system: a step-by

I am looking to design an admin panel using bootstrap. To start off, I want to create a navigation menu on the left side. However, I encountered an issue where my navigation menu with a red background does not stretch the full width of the left column. Her ...

Unable to identify the Xpath selector for the "Account settings button" and "find my iPhone" features within iCloud

When trying to access my iCloud account settings and locate the "Find my iPhone" button on iCloud.com, I am encountering issues with my selectors. It seems like there are no frames on the page, but the Xpaths I'm using are not matching. Here are the ...

What is the best way to dynamically swap out an image within an element?

Is there a way to dynamically replace this image without deleting the HTML SVG element and re-rendering it? xlink:href="http://missosology.info/forum/download/file.php?avatar=11666_1307312313.jpg" CODE: https://jsfiddle.net/bfv17f0e/ <svg class="clip ...

Guide to creating a CSS horizontal line with square elements on either side of a title

I am currently working on how to create a horizontal line with small squares on each side of an h1 title. I have managed to achieve the horizontal line using code from a similar question, but I am struggling to add the square elements to the design. https ...

Creating a universal header for all webpages in Angular JS by dynamically adding elements using JavaScript DOM manipulation techniques

I have successfully created a json File containing an array of "learnobjects", each including an id, name, and link. Check out my plnkr example var myMessage = { "learnobjects": [{ "id": "1", "name": "Animation-Basics", "link": "animation_bas ...