Adding spacing to Bootstrap Navbar

My bootstrap top navigation bar appears as shown below: https://i.sstatic.net/llqGh.png When I hover over each of the items labeled LOREM, it changes to this: https://i.sstatic.net/yGAd8.png How can I adjust my code so that the spacing above and below the LOREM items is equal in the navbar, like this:

https://i.sstatic.net/ioyW9.png

Please keep the underline intact. I just need the LOREM items to have consistent spacing when not hovered. Below you will find my HTML and CSS code for reference:

<!DOCTYPE html>
            <html>
               <head>
                   <title>Bootstrap Resize</title>
                   <meta charset="utf-8"></meta>
                   <meta name="viewport" content="width=device-width, initial-scale=1" ></meta>
                   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
                   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
               </head>

               <style type="text/css">
                .navbar-custom { 
                background-color: #484848;
                }

                ul.ml-auto > li > a > span{
                font: 15px Roboto,sans-serif;
                font-weight: 500;
                position: relative;
                display: inline-block;
                color: #FFFFFF80;
                overflow: hidden;
                padding-bottom: 5px;
                }
                ul.ml-auto > li > a > span::before {
                position: absolute;
                content: attr(data-content);
                color: #00FFFF;
                clip-path: polygon(0 0, 0 0, 0% 100%, 0 100%);
                transition: clip-path 275ms ease;
                display: block;
                border-bottom: 2px solid #00FFFF;
                padding-bottom: 4px;
                }
                ul.ml-auto > li > a > span:hover::before {
                clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
                }
               </style>

               <body>                 
                <nav class="navbar navbar-custom navbar-expand-lg navbar-dark fixed-top py-2" id="mainNav">
                  <div class="container">
                    <a  style="font: 20px Arial,sans-serif" class="navbar-brand js-scroll-trigger" href="#">HOME</a>
                    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                      <span class="navbar-toggler-icon"></span>
                    </button>

                    <div class="collapse navbar-collapse" id="navbarResponsive">
                      <ul style="font: 15px Roboto,sans-serif" class="navbar-nav ml-auto">
                        <li class="nav-item">
                          <a class="nav-link js-scroll-trigger" href="#about"><span data-content="LOREM">LOREM</span></a>
                        </li>
                        <li class="nav-item">
                          <a class="nav-link js-scroll-trigger" href="#expertise"><span data-content="LOREM">LOREM</span></a>
                        </li>
                        <li class="nav-item">
                          <a class="nav-link js-scroll-trigger" href="#portfolio"><span data-content="LOREM">LOREM</span></a>
                        </li>
                        <li class="nav-item">
                          <a class="nav-link js-scroll-trigger" href="#contact"><span data-content="LOREM">LOREM</span></a>
                        </li>
                      </ul>
                    </div>

                  </div>
                </nav>
               </body>
            </html>

Answer №1

adjust padding in a tag

.navbar-custom { 
  background-color: #484848;
}

ul.ml-auto > li > a > span{
  font: 15px Roboto,sans-serif;
  font-weight: 500;
  position: relative;
  display: inline-block;
  color: #FFFFFF80;
  overflow: hidden;
  padding-bottom: 5px;
}
ul.ml-auto > li > a > span::before {
  position: absolute;
  content: attr(data-content);
  color: #00FFFF;
  clip-path: polygon(0 0, 0 0, 0% 100%, 0 100%);
  transition: clip-path 275ms ease;
  display: block;
  border-bottom: 2px solid #00FFFF;
  padding-bottom: 4px;
}
ul.ml-auto > li > a > span:hover::before {
  clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
}
.nav-link {
  padding: .5rem 1rem 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<nav class="navbar navbar-custom navbar-expand-lg navbar-dark fixed-top py-2" id="mainNav">
    <div class="container">
      <a style="font: 20px Arial,sans-serif" class="navbar-brand js-scroll-trigger" href="#">HOME</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul style="font: 15px Roboto,sans-serif" class="navbar-nav ml-auto">
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#about"><span data-content="LOREM">LOREM</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#expertise"><span data-content="LOREM">LOREM</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#portfolio"><span data-content="LOREM">LOREM</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#contact"><span data-content="LOREM">LOREM</span></a>
          </li>
        </ul>
      </div>
    </div>
</nav>

Answer №2

To achieve the desired effect, simply eliminate the overflow and padding-bottom properties in your code:

ul.ml-auto > li > a > span {
    font: 15px Roboto,sans-serif;
    font-weight: 500;
    position: relative;
    display: inline-block;
    color: #FFFFFF80;
}

If you want to see a live demonstration of this in action, check out the code snippet below:

.navbar-custom {
  background-color: #484848;
}

ul.ml-auto>li>a>span {
  font: 15px Roboto, sans-serif;
  font-weight: 500;
  position: relative;
  display: inline-block;
  color: #FFFFFF80;
}

ul.ml-auto>li>a>span::before {
  position: absolute;
  content: attr(data-content);
  color: #00FFFF;
  clip-path: polygon(0 0, 0 0, 0% 100%, 0 100%);
  transition: clip-path 275ms ease;
  display: block;
  border-bottom: 2px solid #00FFFF;
  padding-bottom: 4px;
}

ul.ml-auto>li>a>span:hover::before {
  clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
}
<!DOCTYPE html>
<html>

<head>
  <title>Bootstrap Resize</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
</head>

<body>
  <nav class="navbar navbar-custom navbar-expand-lg navbar-dark fixed-top py-2" id="mainNav">
    <div class="container">
      <a style="font: 20px Arial,sans-serif" class="navbar-brand js-scroll-trigger" href="#">HOME</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>

      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul style="font: 15px Roboto,sans-serif" class="navbar-nav ml-auto">
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#about"><span data-content="LOREM">LOREM</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#expertise"><span data-content="LOREM">LOREM</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#portfolio"><span data-content="LOREM">LOREM</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link js-scroll-trigger" href="#contact"><span data-content="LOREM">LOREM</span></a>
          </li>
        </ul>
      </div>

    </div>
  </nav>
</body>

</html>

Answer №3

The issue here lies in the bottom padding of the span tag. Only bottom padding has been applied, causing an uneven spacing.

Resolution: To solve this, apply equal padding on both the top and bottom sides. The updated code snippet below illustrates this adjustment:

 ul.ml-auto > li > a > span {
 font: 15px Roboto,sans-serif;
 font-weight: 500;
 position: relative;
 display: inline-block;
 color: #FFFFFF80;
 overflow: hidden;
 padding-bottom: 5px;
 padding-top: 5px;/*New added for equal space*/
}

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

Adjustable design and content containment

Currently, I am exploring ways to utilize CSS in order to control the layout of my website effectively. My goal is to have the main frame of the website contain and confine all content within it. However, since I have not written any code yet, allow me to ...

Deactivate the connection button for accessing the database

I'm currently developing a PHP script to establish a connection with a MySQL database. My goal is to deactivate a button and display a spinner image when I click on a submit button to initiate the database connection. Below is the code snippet I have ...

Struggling to remove excess space at the bottom of an HTML page using CSS styling

After successfully centering a page with CSS, I am now faced with an issue of excess space at the bottom that I can't seem to eliminate. If anyone could take a look at my webpage hosted for free here: , it would be greatly appreciated. Below is the C ...

Display JSON values on PHP event calendar unless they are sold out

I have been struggling for days to find the most effective approach to achieve my goal. I am extracting data from a JSON URL that is structured in an array format as shown below. The data spans from a month to a year, and I need to check if tickets are sol ...

Change the top board division

Is it possible to make a CSS-div resemble the board shown in this image ? What is this particular design style referred to as? I experimented with the skew property but couldn't quite achieve the desired effect. .board { width: 325px; backg ...

Hiding content in HTML with the Display:none property

After exploring various posts on this topic, I am still unable to find a solution that fits my specific scenario. Despite the challenges, I thought it would be worth asking for recommendations. Currently, I have a PowerShell script generating a report in ...

Unable to vertically scroll on a position fixed element

I recently created a unique VueJS component that enables vertical scrolling. Within this component, there are two elements each with a height of 100vh. At the bottom of the component is a fixed position div. Interestingly, when I try to scroll down using m ...

Transitioning JS/CSS effects when the window is inactive

My latest project involved creating a small slider using JavaScript to set classes every X seconds, with animation done through CSS Transition. However, I noticed that when the window is inactive (such as if you switch to another tab) and then return, the ...

What is the best way to iterate through only the selected checkboxes to calculate their

I am in need of calculating the selected checkboxes only within a table field (harga_obat*jumlah) Below is my foreach data: @foreach ($obat as $o) <tr> <td> <input id="check" type="checkbox" name="select[]" value="{{ $o->nam ...

Position an image at the center within a footer widget

I am attempting to position an image at the center of a footer widget. I have gone through various solutions recommended here, but I am unsure if I am implementing the code correctly. Here is my CSS: #footer img.center { display: block; margin-le ...

Adjust the size of the <textarea> to match the height of the table cell

Below is the code I am using to generate a table containing an image along with a <textarea>: <table border="1" style="border-color: #a6a6a6" cellpadding="4" cellspacing="0" width="702">\ <col width="455"> <col width="230"> ...

Numerous declarations of !important classes and their order of precedence

If you were to consider this theoretical scenario: <style type="text/css> .class1 { color:#F00 !important; } .class2 { color:#00F !important; } </style> <p class="class2 class1">Test</p> What deter ...

The getimagesize functionality seems to be malfunctioning

I have developed a function that resizes images to fit as a background image for a div. I provide the function with the div size and the image path. It works perfectly when the height is larger than the width, but it fails when the width is larger than the ...

Adding fewer components to your current Angular 5 project

I have a JS Fiddle showcasing a CSS chart. How can I integrate LESS into my current Angular 5 project to make use of this chart? Also, where should I place the JavaScript and references to the LESS file from this example within my Angular component? The li ...

How can I make a clickable <div> easily navigable with tab functionality?

I am currently tackling a project that involves implementing an onclick event within a <div>. While I've successfully set up the primary functionality, there is one issue at hand. Specifically, I need the onclick event to trigger when a user nav ...

Enjoy the convenience of accessing your QR code result with just a click on the link provided

Stumbled upon this code snippet on . It allows scanning QR codes and displaying the results as plain text. However, I wanted the result to be a clickable link, like www.google.com/(qrcodescanresult). <script src="html5-qrcode.min.js">< ...

Display image URL data in a pop-up box when the button is clicked without dimming the background

In my quest to access the legend of a WMS layer in Openlayers 3, I have successfully obtained the legends of the layer. However, I aim to display them in a popup box with a movable and close button. Below is the content of the .html page: <label>&l ...

Modifying the color scheme of Bootstrap

In order to simplify referencing, I am establishing new theme colors as shown below, allowing me to use classes like bg-red instead of bg-danger or text-purple, and so forth. $primary : #24305e; $blue : #375abb; $indigo : #796eff; ...

Tips for inserting an element in the middle of two elements using HTML

Here is the current structure of my DOM: <body> <h1> </h1> <button> </button> <p> </p> I am trying to dynamically add another p element in between the button and the existing paragraph when the button is cl ...

Flex box causing Bootstrap5 responsive table to overflow

Need help with fixing overflow issue in a fixed-width div used as a left sidebar. The main content renders correctly except for tables with many columns, causing overflow before the scroll bar appears. How can this be resolved? Various layout attempts hav ...