Menu options spreading out horizontally across multiple layers

https://jsfiddle.net/ccaf8msu/1/

<nav class="navbar navbar-default">
<ul class="nav navbar-nav navbar-right">
<li class="first leaf"><a href="/front">Frontpage</a></li>
<li class="leaf"><a href="/library" class="container-one">Library</a></li>
<li class="expanded active-trail active"><a href="/om-websitet" class="active-trail active">About the website</a>
  <ul class="nav navbar-nav navbar-right">
    <li class="first leaf"><a href="/en-sode">About the book</a></li>
      <li class="last leaf"><a href="/en-sode">The authors</a></li>
  </ul>
</li>
<li class="leaf"><a href="/categories" class="container-two">Categories</a></li>
<li class="last leaf webshop-link"><a href="http://google.dk">Webshop</a></li>
</ul>
</nav>

In my design, I have a two-level menu that I want to display horizontally for both levels. However, when the second level is active, the menu item 'about the website' displays with a lot of whitespace after it.

The goal is to eliminate the extra whitespace and align the 2nd level menu items to the right for proper positioning.

Answer №1

To enhance the appearance of list items, apply the following styles: set position:relative to li elements, and specify position:absolute for nested ul tags.

nav.navbar-default {
    width: 100%;  
}
ul {
    list-style: none;
}
ul li {
  float: left;
  position: relative;
}
li ul {
    position:absolute
}
li a {
  display: block;
  height: 55px;
  margin: 0 20px;
}

Answer №2

There's a gap in the layout because your dropdown is positioned 1) after the <li> in the DOM order, causing it to still take up space before moving to the next <li>.

To fix this issue, you should set your dropdowns to have position: absolute so they don't disrupt the layout of other elements.

It's also worth noting that in 2016, utilizing floats for layout design may not be optimal and using flexbox would provide more flexibility and control over your layouts.

Below is an example code snippet without white space:

CSS:

nav.navbar-default {
    width: 100%; 
}

ul {
    list-style: none;
    display: flex;
}

ul li {
    justify-content: flex-start;
    position: relative;
}

.navbar-right {
    position: absolute;
    top: 100px;
    left: 0;
}

li a {
    display: block;
    height: 55px;
    margin: 0 20px;
}

For a live demonstration, you can view the Fiddle.

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

How come these social buttons are refusing to align in a responsive layout?

Our website incorporates Twitter, Facebook, and G+ buttons using a responsive design in an ajax portfolio. While the buttons align perfectly with each other, we are facing an issue where we want all three buttons to be centered regardless of the size of th ...

When resizing the window, the width change in JQuery always resets to 0

I've been trying to adjust the width of a div element when the window is resized, but for some reason, the width always ends up being 0 after the resize event. function resizeUploadField() { var uploadInputField = $('.input-append&apo ...

Customize Bootstrap Vue dropdown without any predefined styling options

After reviewing the documentation, I created a sample example utilizing the b-dropdown component: You can view the example here: https://codesandbox.io/s/6lhk6?file=/src/components/GenericItem.vue However, when I implemented the component in the code: &l ...

Creating a circular shape that adjusts to different screen sizes using only CSS

Is there a way to create perfect circles using only CSS without them turning into ovals when using percentage values for height? ...

Utilizing Media Queries with Dynamic Vue Properties

On one of my website pages, I always have a Div element with a Background Image that changes based on Media Queries (for example, I fetch a smaller resolution from my CDN on mobile phones). However, since I retrieve the Image URL on Page Load, I need to s ...

How can I make an element stick at the bottom of another element using the top position

I have two stacking elements of varying heights within a scrolling container. When the lower element extends below the bottom of the container, I want to display a part of it affixed to the bottom. To achieve this, I applied position: sticky to position ...

Customizing the color of a select dropdown in Bootstrap using CSS styling

I am having difficulty changing the default blue color for Bootstrap select dropdowns. Despite trying :hover and :active selectors on both option and select, I have not been successful in making the desired changes. Could anyone provide insight regarding ...

Changing the background color of a fixed header in AngularJS based on the current page

I designed my homepage with a full-height div featuring a video. The header is fixed in position and has a transparent background with white text when positioned over the video. As I scroll down, I dynamically adjust the background to white with black text ...

What technique works best for changing the visibility of an image without causing other elements to shift position?

On my webpage, I have a table cell with an image that should only display when hovering over the cell. The problem is that when the image is shown, it shifts the other text to the left because its default state is "display:none". I'm searching for a s ...

svg viewbox cannot be adjusted in size

Struggling with resizing an SVG to 20px by 20px. The original code size of the SVG is quite large at 0 0 35.41 35.61: <!doctype html> <html> <head> <meta charset="utf-8"> <title>SVG</title> ...

What is the best way to place text alongside a shape?

I'm attempting to place text beside the circular shape. HTML <div class="row"> <div class="col-sm-2"> <span><div class="circle"></div></span><p>available</p> </div> </div> C ...

Do not execute the script if the window's width is below a certain threshold

I have a unique website layout that adjusts to different screen sizes, and I've incorporated some jQuery functionality. Here is a snippet of the code: <script> $(document).ready(function(){ $("#D1000C36LPB3").click(function(){$("#D ...

Customizing styles in ZK based on theme

I am currently working on a ZK application that has been styled using CSS, specifically the colors from the Sapphire theme. My goal is to have environment-dependent themes - Sapphire for production and Breeze for stage. While I have successfully implemente ...

Creating a mobile-friendly layout with 3 columns: Tips and tricks

I attempted to solve the issue independently but didn't have much success. My goal is to create a proper version for mobile users using bootstrap, however, it currently looks terrible on mobile devices. I suspect the problem lies with the div classes ...

Utilizing CSS for showcasing XSTL table

I've hit a roadblock with my initial XML project and I need some help. My goal is to create an address book using an XML file containing contact data, which is then transformed in XSLT. While both the XML and XSLT files function correctly, I encounter ...

What is the best way to adjust the responsiveness of my AppDrag page for mobile optimization?

My landing page is all set up, but I'm looking to tweak the font sizes and spacing for mobile users. Can these changes be made using design tools instead of digging into the code? ...

Intersection Observer is functioning properly as elements vanish once they finish appearing

New to this forum and looking forward to receiving assistance and making some fantastic connections! Currently working on my personal portfolio with the goal of getting it online for job hunting purposes, but encountering some challenges along the way. T ...

Is there a way to merge attribute selectors for elements that satisfy one condition or the other?

Is there a way to combine selectors effectively? <input type=number> <input type=date> I was able to successfully hide the arrows in a number field with the following code: input[type=number]::-webkit-inner-spin-button, input[type=number]::- ...

margin-top: automatic adjustment, with a minimum of 50 pixels

I am trying to add a minimum of 50px margin to the top of my footer tag using CSS, but I haven't been successful with the min() function. I'm not sure if I am overlooking something or if there is another correct approach to achieve this. * { ...

Tips for concealing overlay when the cursor hovers

Can anyone help me with a code issue I'm having? I want to hide an overlay after mouse hover, but currently it remains active until I remove the mouse from the image. Here is the code: .upper {position: absolute; top: 50%; bottom: 0; left: 50%; tra ...