Achieve zero vertical space for an inline-block element

My menu is evenly distributed and looks like this:

HTML

<nav>
    <ul>
        <li><a href="">Home</a>
        </li>
        <li><a href="">About</a>
        </li>
        <li><a href="">Contact</a>
        </li>
        <li><a href="">Blog</a>
        </li>
    </ul>
</nav>

CSS

nav ul {
    padding:0;
    margin:0;
    text-align: justify;
    text-transform: uppercase;
    background-color: #000;
}
nav ul:after {
    content:'';
    width: 100%;
    display: inline-block;
    height: 0;
    line-height: 0;
    font-size: 0px;
}
nav ul li {
    display: inline-block;
}
nav ul li a {
    color: #fff;
}

The navigation menu items fill the entire width of the bar, as shown in this fiddle: http://jsfiddle.net/SjDEX/.

However, there seems to be extra space below the menu items due to the ::after element increasing the height of the ul.

Is there a way to prevent the after element from occupying vertical space? Setting its height to 0 does not resolve the issue, and changing its display property to block or other values disrupts the layout.

Answer №1

Instead of changing the :after element's height, adjust the height of the actual ul element like this:

nav ul {
   height: 20px;
}

Check out the demo here

You can simplify the code further by using the following:

nav ul:after {
    content:'';
    width: 100%;
    display: inline-block;
}

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

Place a gap at a specific spot within the boundary line

Here is a CSS code snippet that displays a horizontal line: .horizontalLineBottom { border-bottom:solid #6E6A6B; border-width:1px; } Is it possible to add space at a specific position on this line? For example, _________________________________ ...

:valid CSS remains visible even if $valid is set to false

Here is the code I've been working on. I have implemented a custom directive to check if two passwords match. Below is my HTML: <!-- confirm password --> <div class="row"> <div class="col-xs-10 col-centered"> ...

What is the best way to adjust the margins of my div based on the width of the webpage?

I have a website where I use two columns with modules to display information like this: https://i.sstatic.net/Jp97V.png However, I work on vertical monitors and when I switch to a more landscape format, the layout looks like this: https://i.sstatic.net/D ...

Modifying the Vue page background directly from a page: a step-by-step guide

UPDATE: I recently discovered that I need to modify the body background-color, but I am unsure of how to accomplish this from within the style of this page based on the wrapper's class. The class is determined by the data values. I aim to change the ...

What could be the reason my nth-of-type pseudo-class is not targeting the first "a" element, but instead identifying it as the fourth one?

Question: I am facing an issue where .main-index a:nth-child(4) works on my first a link, but not .main-index a:nth-child(1). I have been unable to identify the problem or figure out what I might be doing incorrectly. The current method works, but I ackn ...

Arranging text within a button in perfect alignment

Trying to create buttons with icons positioned on the left side, I stumbled upon a useful example online. However, no matter how much I tweak the code, I can't seem to get the text inside the buttons to align vertically. Working within Bootstrap 4 fra ...

Substitute using JQuery

Hello, I am attempting to update some html using Jquery, here is my current progress. $(".old").html($(".new").html()); It's almost working. The problem is that the .new content is being copied instead of replaced. I want to Cut/Paste instead of Co ...

The Bootstrap dropdown menu seems to have a mind of its own, stubbornly sticking to the top left corner of the screen with no intention

Currently, I am creating a music streaming service similar to Deezer. However, I am encountering an issue with my dropdown menu placement. Despite attempting various adjustments such as changing the margins and using absolute positioning with specific coor ...

Creating a visible block in ReactJS: Step-by-step guide

Hello and thank you for all the hard work you do on this platform. I am relatively new to working with ReactJS and have been encountering some difficulties trying to integrate it with sandbox environments like JSFiddle. I have a div element named "app-con ...

How can I prevent an image from appearing to float on the page while using the img-fluid class?

While testing the responsiveness of our website built with Bootstrap, we noticed an issue where an image appears to be "floating" on top of the page when viewed in iPad mode using the browser's device toolbar. Despite this, the site functions correctl ...

Tips for preventing a span from disappearing when a hidden div is displayed during onmouseover

, there is a concern about the disappearing behavior of the span with ID "topnavbar" when the display property changes from none to block on a hidden div using onmouseover event. The query is how to ensure that the span remains visible at all times. Additi ...

Guide to ensuring child elements inherit parent's width in Bootstrap 4

Want to ensure that a child element inherits the parent's width in Bootstrap 4? Trying to adjust the width of the download_resume block. Currently using two key Javascript scripts: 1. The first function changes the image with a hover effect 2. The ...

Verify the presence of a mouse before attempting to hover

Is there a way to determine if the user has a cursor to hover over elements using JavaScript, while also utilizing Bootstrap? if (hasCursor()) { doCode(); } I initially attempted to check if it was a touch device, but faced limitations testing on my 2- ...

Make sure the header spans the full width of the body

I am trying to create a header that spans the entire width of the body, but I am encountering some issues with white space on both sides. I initially attempted to set the width to 100% on the header div, but this did not eliminate the white space. I then e ...

The dropdown menu in the navigation bar is overlapping with the datatable, creating a transparency effect

Working on a website layout that features a navbar at the top and a datatable below. However, when hovering over the navbar to reveal subitems, I notice a transparency issue where the navbar overlaps with the datatable. Below is a simplified version of my ...

Ways to address the horizontal shift of an element caused by the Cumulative Layout Shift issue

My header is styled with a max-width and margin to position it in the center of the page horizontally. Despite this, running a lighthouse report reveals a horizontal shift issue. If anyone has faced this problem before and knows how to fix the horizontal ...

Adjusting the arrow direction upon clicking in a Select option

Is there a way to make the arrow move upwards when I open the select dropdown option? <select class="form-select" aria-label="Default select example"> <option selected>Choose an option</option> <option value="1">One</optio ...

Arranging elements in a single line using Bootstrap 4

I need to align links in a row at the bottom, such as "Link 1 left bottom", "Link 2 center bottom", "Link 3 right bottom" but "Link 2" is not centered on PC and mobile devices. HTML: <div> <a id="contact" hre ...

Experimenting with combining a CSS class alongside the Nth-child selector

Hello, I've been researching various sources including Stackoverflow on how to effectively use an Nth child selector and a Class together but have not had much success so far. In essence, my menu consists of Main categories (class = cat) and subcateg ...

Instructions for adding a unique custom external CSS link, such as Bootstrap CSS, to a specific REACT component

Is it possible to incorporate custom CSS from the Bootstrap website into a React component? Since this CSS file cannot be imported directly, what steps should I take to include it? <link href="https://getbootstrap.com/docs/4.5/dist/css/bootstrap. ...