Space separating the text and underline

Can anyone explain why padding-bottom or margin-bottom does not seem to have any effect on the underline in the li > a:before section? I tried adding padding-bottom but it doesn't create any space. Any suggestions?

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    text-align: right;
    line-height: 10vh;
    margin-right: 10vh;
    animation: fadein 5s;
    -moz-animation: fadein 5s; /* Firefox */
    -webkit-animation: fadein 5s; /* Safari and Chrome */
    -o-animation: fadein 5s; /* Opera */
    font-family: verdana;
}

li {
    display: inline;
    margin-left: 2.5vh;
    margin-right: 5vh;
    text-transform: uppercase;
}

a {
    text-decoration:none;
    color: white;
}

li > a {
    position: relative;
    color: black;
    text-decoration: none;
}

li > a:hover {
    color: black;
}

li > a:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 2px;
    padding-bottom: 3px;
    bottom: 0;
    left: 0;
    background-color: black;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.7s ease-in-out 0s;
    transition: all 0.7s ease-in-out 0s;
}

li > a:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
}
<nav id="nav">
<ul>
  <li id="about"><a href="#">About</a></li>
<li id="work"><a href="#">Work</a></li>
<li id="contact"><a href="#">Contact</a></li>
</ul>
</nav>

Answer №1

To achieve the desired effect, simply update bottom: 0 to bottom: -3px;:

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    text-align: right;
    line-height: 10vh;
    margin-right: 10vh;
    animation: fadein 5s;
    -moz-animation: fadein 5s; /* Firefox */
    -webkit-animation: fadein 5s; /* Safari and Chrome */
    -o-animation: fadein 5s; /* Opera */
    font-family: verdana;
}

li {
    display: inline;
    margin-left: 2.5vh;
    margin-right: 5vh;
    text-transform: uppercase;
}

a {
    text-decoration:none;
    color: white;
}

li > a {
    position: relative;
    color: black;
    text-decoration: none;
}

li > a:hover {
    color: black;
}

li > a:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 2px;
    padding-bottom: 3px;
    bottom: -3px;
    left: 0;
    background-color: black;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.7s ease-in-out 0s;
    transition: all 0.7s ease-in-out 0s;
}

li > a:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
}
<nav id="nav">
<ul>
  <li id="about"><a href="#">About</a></li>
<li id="work"><a href="#">Work</a></li>
<li id="contact"><a href="#">Contact</a></li>
</ul>
</nav>

Answer №2

Alternatively, you can include "padding-bottom" in the "li > a" selector. It works perfectly. :)

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    text-align: right;
    line-height: 10vh;
    margin-right: 10vh;
    animation: fadein 5s;
    -moz-animation: fadein 5s; /* Firefox */
    -webkit-animation: fadein 5s; /* Safari and Chrome */
    -o-animation: fadein 5s; /* Opera */
    font-family: verdana;
}

li {
    display: inline;
    margin-left: 2.5vh;
    margin-right: 5vh;
    text-transform: uppercase;
}

a {
    text-decoration:none;
    color: white;
}

li > a {
    position: relative;
    color: black;
    text-decoration: none;
    padding-bottom: 10px;
}

li > a:hover {
    color: black;
}

li > a:after {
    content: "";
    position: absolute;
    width: 100%;
    height: 2px;
    padding-bottom: 3px;
    bottom: 0;
    left: 0;
    background-color: black;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.7s ease-in-out 0s;
    transition: all 0.7s ease-in-out 0s;
}

li > a:hover:after {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
}
<nav id="nav">
<ul>
<li id="about"><a href="#">About</a></li>
<li id="work"><a href="#">Work</a></li>
<li id="contact"><a href="#">Contact</a></li>
</ul>
</nav>

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

Center the span in CSS by setting its position to absolute

I am trying to create a span element positioned as absolute inside a div. The div has top and left values set. When the user inputs text, the span expands with the given text towards the right to contain it. However, I would like it to grow symmetrically o ...

Is there a way to set a custom background image for a specific Vaadin view?

I am developing a view in Vaadin that looks something like this... public class PosAppPickerView extends VerticalLayout implements View { Everything is functioning smoothly, but I am interested in incorporating an image as the background for the body/mai ...

Various designs for various arrangements in vuejs applications

In my project, I have two distinct layouts that function as separate parent routes with multiple nested components. I want to apply different global styles to each layout, but when I import styles for one layout, it affects the other as well: <style ty ...

basic file not functioning properly

So I am currently developing a straightforward script to assign the value " ...php?answer=1" only if the java is enabled. It so happens that I have made some progress on this... <script language="text/javascript"> document.form.answer.value=1; < ...

Blank area located at the bottom of the document

I'm having trouble designing a webpage without a scroll bar because there isn't much content to display on the page. I've tried searching for solutions and following steps to fix the issue, but I haven't had any success. If anyone can a ...

Encountering the error message "Undefined variable '$'" in a script that is loaded after jQuery

My code is simple - it loads jQuery first, followed by a script that uses the $ syntax. However, I keep encountering the error: ReferenceError: $ is not defined Initially, I suspected that the async attribute on the script tag was the issue, but changi ...

Achieving perfect alignment of two images within a block and maintaining their center position even when resizing

While working within a Wordpress post, my goal is to insert two images side by side with the block of images centered, one aligned left and the other aligned right (which I have achieved). As the page size decreases, I want the images to stack on top of e ...

Issues with JavaScript scripts functionality on my live server

Hey everyone, I'm new to this community and having some trouble with my index.html file. Everything works fine except for one script that only runs when I open the file directly with live server in VSCode. Can someone help me out? The script causing i ...

Is there a way to create tabs in JavaScript without having to edit the <head> section?

I am in need of JavaScript for creating tabs without the necessity of editing the <head> (as it is not possible). My requirement involves having three links and three divs. Whenever a link is clicked, one specific div should be displayed while the o ...

Using Three.js to display a CSS3D-rendered DIV in full-screen mode

After extensively experimenting with the three.js library, I was able to establish two distinct scenes – one canvas-rendered and the other css3d-rendered – both displayed using the same perspective camera. Note: Despite my efforts, I am constrained to ...

Is there a way to execute jQuery code only after AngularJS has finished rendering all its components?

My goal is to display or hide my chart div based on different pages (Add, Edit, Save As). This functionality should be controlled by a checkbox - when the checkbox is checked, the div is displayed, and when it is unchecked, the div is hidden. On the Add ...

How to position button components at the center in a NextJS application?

Incorporating a button within my nextjs page has been a challenge as I am striving to position it in the center of the page for optimal viewing on both PCs and mobile devices. Despite various attempts, the button remains fixed on the far left side of the p ...

Extract the content of an element and incorporate it into a script (AddThis)

HTML: <div id="file-section"> <div class="middle"> <p class="description"> Lorem ipsum... </p> </div> </div> jQuery: var share_on_addthis = { description: $('#file-section ...

AngularJS - Changing Views with Templates

At the moment, I am changing my directives in the following manner. Within my directive: (function(){ 'use strict'; var controller = ['$scope', function ($scope) { }]; angular .module('moduleName' ...

Struggling to align an element in HTML when used in columns?

<div class="row"> <div class="col-lg-3 " style="border: groove;"> <p class="circle col-xs-1 center-block">01</p> <h3>trending courses</h3> ...

Tips for incorporating an alt attribute into image tags using jquery

I recently inserted a Google map into a webpage and noticed that it generated multiple img tags without the alt attribute. I'm looking for a way to dynamically add the alt attribute to each img tag within a div using jQuery. Can someone suggest an alt ...

What is the best way to customize the appearance of only one of two identical tables using HTML and CSS?

I have two tables that look identical: <html> <head> <style> td, th { text-align: left; padding: 28px; } </style> </head> <body> <table> <tr> ...

Aligning the right menu of Ant Design from the Layout component

How can I align the menu to the right in antd? https://i.sstatic.net/xmh6E.png Please Note: The image has been edited using Photoshop and is not real. I attempted to declare a className = "Menu" and set .Menu {align-items: right;} but it didn't wor ...

Various JSON Tag values occurring more than once

Upon receiving a JSON Object in HTML, I am passing it to a JavaScript function defined below. The issue arises when the same folderName appears repeatedly on the HTML page. JSON Object: { "folderName": "arjunram-test", "objects": [ { ...

Clicking on the modal button causes the code to run multiple times in JQuery and JavaScript

Hello, I'm experiencing an issue where the code inside a modal is being executed multiple times when the modal button is clicked. For example, if I click the modal button once, the code runs once; if I click it twice, the code runs twice, and so on. ...