Tips for ensuring consistent spacing between font icons within a navigation bar when using text with font icons as links

Below is the code I have implemented for a navigation bar:

body {
  margin: 0;
}

.navbar {
  display: inline-block;
  background-color: #495057;
  width: 100%;
}

.navbar ul {
  list-style-type: none;
  text-align: center;
  float: left;
}

.navbar ul li {
  display: inline-block;
  padding-right: 20px;
}

.navbar ul li a {
  text-decoration: none;
  color: #adb5bd;
}

.navbar ul li a:hover {
  color: white;
}
<link href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" rel="stylesheet"/>

<div class="navbar">
  <ul>
    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>test</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>123test123</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12345test12345</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12test12</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>1test1</div>
      </a>
    </li>
  </ul>
</div>

I have encountered an issue where the font icons are not equally spaced due to variations in the length of link names. Despite setting equal padding, the output does not look visually appealing. I strive to achieve uniform spacing between each font icon regardless of the link name length for a more polished appearance.

In my vision, all font icons should be spread out evenly irrespective of the space occupied by the link names. The desired outcome can be visualized through this image: https://i.sstatic.net/2bU1t.png

Regrettably, my attempts to attain this ideal layout have been unsuccessful.

Answer №1

Modified the style as shown below

.navbar ul {
  list-style-type: none;
  text-align: center;
  display: flex;
  justify-content: space-around;
  padding: 0;
}

.navbar {
  display: inline-block;
  background-color: #495057;
  width: 100%;
}

.navbar ul {
  list-style-type: none;
  text-align: center;
  display: flex;
  justify-content: space-around;
  padding: 0;
}

.navbar ul li {
  display: inline-block;
}

.navbar ul li a {
  text-decoration: none;
  color: #adb5bd;
}

.navbar ul li a:hover {
  color: white;
}
<link href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" rel="stylesheet"/>

<div class="navbar">
  <ul>
    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>test</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>123test123</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12345test12345</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12test12</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>1test1</div>
      </a>
    </li>
  </ul>
</div>

Answer №2

To create a navigation bar with equal spacing between items, you can utilize CSS flexbox and apply justify-content:space-around to your ul

body {
  margin: 0;
}

.navbar {
  background-color: #495057;
  width: 100%;
}

.navbar ul {
  display: flex;
  justify-content: space-around;
  text-align: center;
  list-style-type: none;
  padding: 20px
}

.navbar ul li a {
  text-decoration: none;
  color: #adb5bd;
}

.navbar ul li a:hover {
  color: white;
}
<link href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" rel="stylesheet" />

<div class="navbar">
  <ul>
    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>test</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>123test123</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12345test12345</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12test12</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>1test1</div>
      </a>
    </li>
  </ul>
</div>

Answer №3

Here is the solution that I found to be effective:

body {
  margin: 0;
}

.navbar {
  display: inline-block;
  background-color: #495057;
  width: 100%;
}

.navbar ul {
  list-style-type: none;
  text-align: center;
  display: flex;
  align-items: flex-start;
}

.navbar ul li {
  display: inline-block;
  padding-right: 10px;
  margin: 0px 20px;
}

.navbar ul li a {
  text-decoration: none;
  color: #adb5bd;
}

.navbar ul li a:hover {
  color: white;
}
<link href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" rel="stylesheet" />

<div class="navbar">
  <ul>
    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>test</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>123test123</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12345test12345</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>12test12</div>
      </a>
    </li>

    <li>
      <a href="#">
        <i class="fas fa-font"></i>
        <div>1test1</div>
      </a>
    </li>
  </ul>
</div>

Make adjustments to the code below:

.navbar ul {
  list-style-type: none;
  text-align: center;
  display: flex;
  align-items: flex-start;    
}

.navbar ul li {
  display: inline-block;
  padding-right: 10px;
  margin: 0px 20px;
  
}

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 can we adjust the flex values to ensure the label displays properly on a narrow container width?

The labels in the left column of the form are initially sized correctly, but shrink when there is not enough room. However, there's a discrepancy in label size between rows with 2 items and rows with 3 items. body { font-family: Arial; font-siz ...

Retain chosen option after form submission in PHP/HTML

I have seen many questions asked about this before, but despite trying various solutions here, none seem to work for me. I am populating a list from my mysqli database: <select multiple="multiple" name="formCountries[]" size="5" id="keuzeproduct"> ...

Streamlined Infinite Scrolling Data Visualization Using SVG Implemented in HTML and CSS

I have developed a dynamic graph that continuously updates with new data points being plotted. Right now, I am utilizing requestAnimationFrame() to render the element positions 30 times per second, but performance can be sluggish with numerous SVG element ...

In my HTML document, I have three identical Id's and I need to figure out how to target the third one using CSS. Can you help

I am working on an HTML document that contains multiple instances of ids and classes. Here is a snippet of the code: <!DOCTYPE html> <html> <body> <div id="Caption_G" class="editor-group"> editor-group<br /> ...

Transferring information from an HTML form to a C# webservice through AJAX

I've created a form in HTML using Bootstrap within PHPStorm, and now I want to send the information to a C# webservice using AJAX. However, I'm unsure about what to include in the AJAX URL section (shown below). Here is the HTML/Bootstrap form I ...

CSS mismatch detected between production and development modes

Currently, I am utilizing the Vuetify framework coupled with custom CSS for a project developed using a webpack template. During development mode, my custom CSS modifications are successfully applied to HTML elements; however, in Production mode, these cha ...

Maintain Open State of Toggle Drop Down Menu

Having an issue with the toggle down menu or list on my webpage. The problem is that the menu is constantly open (the #text_box) when the page loads. My intention was for it to be closed initially, and then open only when the user clicks on the 'Ope ...

Two nested divs positioned below text within a parent div

I am styling a content div with text and two child divs positioned next to each other within the content div. My goal is to have the child divs display below the text, rather than beside it. body { text-align: center; } #first, #second { border: so ...

Retrieving addresses by extracting p from the Python class within a div

Currently the code: Extracts URLs for various gyms and saves them in a CSV file as shown below: https://www.lifetime.life/life-time-locations/al-vestavia-hills.html https://www.lifetime.life/life-time-locations/az-biltmore.html Desired functionality: I&a ...

Utilizing the Current URL from the address bar as a variable to link within the same page

My goal is to: Extract the current URL address from the browser bar, which will have a format like this: http://example.com/test/index.html?&dv1=1023faf2ee37cbbfa441eca0e1a36c Retrieve the lengthy ID number located at the end of the URL 1023faf2ee37c ...

Tips for designing a navbar specific to a profile section

I am currently working on an angular application with a navigation bar composed of anchor tags. When the user logs in, I have an ngIf directive to display my profile icon with dropdown options. However, I am facing challenges in styling it correctly. I aim ...

Adaptable pictures within a set area

My goal is to design a responsive gallery that displays images of various dimensions. I envision having 5 square divs of equal size on a full screen, with each image centered both horizontally and vertically, scaled to fit with some padding that adjusts pr ...

What could be causing the discrepancy in functionality between Safari and Chrome for my overlay feature?

After testing the image hover overlay feature on various platforms such as desktop and Android devices, it has come to my attention that it does not function correctly on Safari. Specifically, the feature does not work on Apple devices like iPhone, iPad, a ...

Enhance your input text form with a stylish button using Bootstrap

This is the code snippet I am currently working on: <h2>Workout Selector</h1> <div class="form-group"> <label for="workout-name">Search by Keywords (Comma Separated):</label> <input ...

Creating a CSS grid layout with dual columns to dynamically adjust and accommodate two items in each row

I am attempting to create this layout using CSS grid: https://i.sstatic.net/Ktbnm.png The goal is to have an input and a textarea. The textarea should be of the size of 2 inputs. If there is already a textarea in the previous column, the next column shou ...

Why is it that the date selector is missing in Firefox?

When using the provided HTML code, a date selector will appear on Google Chrome: <html> <head> <title> </title> </head> <body> <form> <input type="date" /> </form> </body> However, ...

How can I address the issue of an inner content scroll bar?

I am facing an issue with the scroll bar on inner content. When I hover over an arrow on the inner content, the scroll bar appears as desired. However, it changes the content in a way that looks odd. Is there a solution to have a scroll bar on the inner co ...

The locator for the span class cannot be found or interacted with in Selenium

Here is the HTML code snippet for a span element: <td class="header-logout-btn"> <a href="logout.htm" class="btn switch-btn"> <i class="fa fa-times"></i><span class="hidden-xs">Home</span> </a> </td> I ...

Problem with transitions not working properly in Vue.js.The issue

Experiencing an issue with the Vue.js transition feature. Check out this link for reference. When the transition enters, it works smoothly, but when transitioning out, it's not functioning properly. File: genetic.vue <transition name="slide-f ...

Tips for including HTML in a JSON request in ReactJS

Upon sending the request as shown below, the response was successfully received. { "contractId": "siva8978", "html": "<p>PREFERENCE SHAREHOLDER AGREEMENTTHIS AGREEMENT is made on the&nbsp;$$Contract Start Date$$ BETWEEN&nbsp;CRADLE WEALTH ...