Strange new alignment when using float

Looking to arrange a series of menus using the float and clearfix method, but encountering some issues. Specifically, trying to align the menu group to the right without setting a uniform width due to varying text lengths. However, adding padding causes the last element to drop down to a new line, indicating an unexpected behavior in the floating container.

If forcibly assigning a large width to the container, it eliminates the extra line, but leads to uncontrolled whitespace on the right. On the other hand, allowing children to float right without setting the parent container's float property achieves desired results, albeit requiring <li> to be written in reverse order.

The HTML code snippet is as follows:

<ul class="clearfix" id="top-navigation-container">
                <li class="top-navigation">12345678</li>
                <li class="top-navigation">2345</li>
                <li class="top-navigation">3</li>
                <li class="top-navigation">LOG IN</li>
            </ul>

And the accompanying SCSS code:

#top-navigation-container {
    float: right; // Unexpected behavior here.
    white-space: nowrap;
//    width: 75%;
    text-align: center;
    top: 25px;
    right: 25px;

    .top-navigation {
//      width: 25%;
      padding-left: 2%;
      padding-right: 2%;

      float: left;
      display: inline-block;

    }
  }

.clearfix::after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}

Seeking clarity on why the new line occurs and how toggling float or white-space properties in Chrome DevTools affects the rendering unpredictably, sometimes eliminating extra lines or introducing excess whitespace on the right.

Are there any more effective solutions available to address these challenges?

Answer №1

Make sure to position your </li> and <li> elements next to each other.

<li class="top-navigation">12345678</li><li class="top-navigation">2345</li>

This issue arises from using the inline-block property on your <li> elements, which adds extra whitespace. Take a look at this resource for more information.

Answer №2

This problem arises when the inline-block property is used with the <li> elements. An easy fix for this is to set the font-size to zero using font-size:0 for the #top-navigation-container and then specify the desired font-size for the <li> elements.

Here is an example of CSS code:

#top-navigation-container {font-size:0;}
#top-navigation-container .top-navigation {font-size:20px;}

View the working example on jsfiddle 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

I am puzzled as to why this particular contact form is not functioning properly

I'm having trouble figuring out why my form isn't functioning properly. I receive a success message, but nothing is being delivered to my inbox. I even checked my spam folder to no avail. After waiting patiently for a few minutes, still no sign o ...

Having trouble getting the CSS class to work in MVC4 with jQuery implementation

I recently created a view page where I implemented some jQuery code: <script type="text/javascript"> $(document).ready(function () { var hdrname = $('#headername').text(); alert(hdrname); if (hdrname == "Pending Assignment") { ...

Avoid using CSS browser prefixes when selecting plugins or addons

I tried searching on Google for a solution, but I couldn't find anything! Is there a trick or library available to avoid repeating CSS extensions for browsers? Instead of using -webkit-, -moz-, and -o- prefixes? -webkit-animation: NAME-YOUR-ANIMATI ...

The WordPress website showcases inline CSS code

I'm currently working on a project at and upon reviewing the source code, I noticed the following snippet. This inline code is part of the Theme, and I'm wondering if there's a way to extract this code into a separate stylesheet within the ...

Tips on gathering information from an HTML for:

After encountering countless programming obstacles, I believe that the solution to my current issue is likely a simple fix related to syntax. However, despite numerous attempts, I have been unable to resolve it thus far. I recently created a contact form ...

What methods can be used to create a universal JS function for popup windows?

How can I create a more flexible code structure? I have successfully implemented the functionality using the following approach: let popup1 = document.getElementById("popup-1"); function openPopup1() { popup1.classList.add("open-popup& ...

When a link is clicked, it quickly blinks or flashes on the screen

https://i.sstatic.net/cCFmX.gif <div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2"> <%= link_to 'FARDIN KHANJANI', root_path, class: 'navbar-brand logo' %> </div> Upon viewing the gif I include ...

Maintaining my navigation menu as you scroll through the page

I've been working on creating a website for my business but I'm facing a challenge. My goal is to have a fixed navigation bar that stays in place as people scroll down the page, similar to what you can see on this website: (where the navigat ...

Issues with IE 11: SVG Map Not Triggering Mouseenter or Mouseleave Events

I've been grappling with this issue for the past couple of days and despite trying numerous solutions, nothing seems to be working. My SVG map of the US has jQuery mouseenter and mouseleave events that function properly in browsers other than IE 11 ( ...

Struggling to make background image behave as desired in CSS

After spending hours scouring forums, I still haven't been able to solve this issue. I am trying to make one of my divs serve as the background for my entire website. Additionally, I want it to start from a specific point and not cover the entire pag ...

Is there a method for me to retrieve the current value of top from the animation function provided here?

$(".container").animate({ top: '300px', height: '60px', width: '250px',},5000).fadeOut("fast").hide("fast"); ...

Creating a gradient border around a div using CSS3

Can a border like this be achieved using only CSS, or will I need to use an image? ...

Challenge with VueJS structure

I've been working on making nestable DOM elements draggable with VueJS and I've made some progress. However, after dragging and dropping, it seems like some elements are disappearing. Could someone please review my code and point out what might ...

Web server decides on "Multiple Options" while utilizing %3F instead of "?" in the URL

I have been using the Lazarus-IDE for a project and encountered an issue with one of its components that doesn't accept "?" in the URL for online help info. I attempted to encode "?" as "%3F" in the URL, thinking it was the correct encoding, but both ...

Is it possible to modify the value on the src img tag prior to scraping the data

Hey everyone, check out this piece of code I have: foreach ($image as $snimka){ $url = $snimka->src; $ch = curl_init($snimka->src); $fp = fopen('images/' . basename($url), 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp ...

Tips for positioning the search form in the navbar header

My brand image and list with 2 glyph-icons & a search form in the navbar header are not aligning properly. The search form button keeps dropping to the line below. Does anyone know a CSS trick to keep it on the same line as the rest of the links? All the ...

What is the best way to display HTML and JavaScript content using Express?

Currently, I am working on a simple express app which comprises of two routes. One route is responsible for rendering html, while the other route is supposed to render a JavaScript file. To keep things organized, I plan to store the html files in a ./views ...

Display React elements on the web page

Seeking assistance from anyone! I've been grappling with a problem and can't seem to figure out a solution. Here's the scenario: My current setup involves a sidebar and a top bar for navigation in React. Check out my app so far in this imag ...

Using two different colors in text with CSS

Is it possible to have text in two different colors like this: https://i.stack.imgur.com/R40W1.png In terms of HTML, I tried looking it up but found answers related to:- https://i.stack.imgur.com/GRAfI.png ...

Seeking guidance on selecting text using XPath

My HTML document contains the following HTML code: <!DOCTYPE html> <html> <head> <title>page XYZ</title> </head> <body> <p><b>bold text</b> some more text </p> <p>< ...