Looking for assistance with centering the navigation bar and adding padding around the text

Seeking help with HTML/CSS to create a centered navigation bar in the header using flex, with an animated underline effect on hover from bottom left to right. Struggling to achieve center alignment and spacing around each item.

Snippet of HTML Code:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="style.css" />
  <title>XXXXX</title>
</head>

<body>
<header>
    <div class="nav">
        <ul>
            <li><a href="#about">About</a></li>
            <li><a href="#experience">Experience</a></li>
            <li><a href="#works">Works</a></li>
            <li><a href="#hobbies">Hobbies</a></li>
        </ul>
    </div>
 </header>  
</body>

</html>


CSS Snippet:

*{
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
body {
    background-color: #333;
    color: #fff;
    font-family: 'Kalam', cursive;
}
header {
    text-align: center;
    display: flex;
    justify-content: center;
}
.nav a {
    text-decoration: none;
    color: #fff;
    position: relative;
  }

.nav a::after {
    content: "";
    display: block;
    height: 3px;
    width: 0;
    background-color: #fff;
    transition: width 0.3s ease-in-out;
    position: absolute;
    bottom: 0;
    left: 0;
  }

 .nav a:hover::after {
    width: 100%;
  }

.nav li {
    display: inline-block;  
    margin: 0 10px; 
    list-style-type: none;
}

Current layout: | About Experience Works Hobbies |

Example Image Here

Desired output: About Experience Works Hobbies

Appreciate any assistance!

Answer №1

Applying space-around requires setting display: flex;. The a elements within the navbar are contained within the ul, which is nested inside the nav class. To apply display: flex;, it must be set on the direct parent of the elements you want to target, in this case the ul.

*{
    ox-sizing: border-box;
    margin: 0;
    padding: 0;
}
body {
    background-color: #333;
    color: #fff;
    font-family: 'Kalam', cursive;
}
header {
    text-align: center;
    align-items: center;
}
.nav ul {
    display: flex;
    justify-content: space-around;
    align-items:center;
  }

.nav a {
    text-decoration: none;
    color: #fff;
    position: relative;
  }

.nav a::after {
    content: "";
    display: block;
    height: 3px;
    width: 0;
    background-color: #fff;
    transition: width 0.3s ease-in-out;
    position: absolute;
    bottom: 0;
    left: 0;
  }

 .nav a:hover::after {
    width: 100%;
  }

.nav li {
    display: inline-flex;  
    justify-content: space-between;
    list-style-type: none;
}
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>XXXXX</title>
  </head>

  <body>
  <header>
      <div class="nav">
          <ul>
              <li><a href="#about">About</a></li>
              <li><a href="#experience">Experience</a></li>
              <li><a href="#works">Works</a></li>
              <li><a href="#hobbies">Hobbies</a></li>
          </ul>
      </div>
   </header>  
  </body>

</html>

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 I activate the mobile zoom feature on kinetic js?

I am currently working on a website using kinetic js + html5 canvas. Everything looks great on the web, but I am facing some issues with the display on mobile devices. Is there a solution to enable the mobile pan and zoom function in kinetic js? Thank you ...

Tips for accurately aligning a relative p tag within a td

Description: I am trying to prevent text overflow in a table cell and position the text underneath a header without wrapping it under an image. I have tried setting a static width for the image and adjusting the left property, but it is not working as expe ...

change content of attached document when clicked

On all of my pages, I have included my header.php file. However, I am facing an issue with destroying sessions. Even though I have a logout.php page where I attempt to destroy the session, it is not happening as expected. The session remains registered wit ...

Is there a way to display identical information in separate divs?

What is the best way to access the text within a div on this website? I am trying to extract data from multiple divs, specifically the article2 divs in the col... divs. I need all of the text data from these divs. However, my current code isn't workin ...

Showing information in a CodeIgniter view with a formatted table

Hi there, I'm currently facing an issue with sorting the correct <td> to display subject results for students. We have a total of 11 subjects offered in the school and senior students are supposed to take 8 subjects, as 4 of them are electives a ...

Ensuring the modal is stored in one central location (Bootstrap)

I'm currently working on a website project using bootstrap, and I need to incorporate the same modal across all pages. Right now, I find myself duplicating the entire code in each HTML file to make it accessible on every page. However, this method is ...

unique design for custom scrollbar on dropdown menu

I'm looking to customize the scrollbar for a select box. I've experimented with the code below, but I want to avoid using the default scrollbar on the select box. Instead, I want to implement a custom scrollbar without using the size attribute on ...

What could be causing my hover effect to function exclusively on Chromium-based browsers and not on Firefox?

Could use some help with this code snippet .podmenu { display: flex; flex-direction: column; list-style: none; width: 10rem; margin-left: 3rem; margin-bottom: 60px; } .verze { list-style: none; flex-direction: column; ...

Tips on causing a div to overflow instead of wrapping onto a new line

Are you looking to design a slider with 3 image containers all in the same row, where the third one overflows instead of wrapping to a new line? Here is an example: Desired Design: https://i.stack.imgur.com/dKyEg.png Current State: https://i.stack.imgu ...

Tips on sending an image as a string from an HTML form in PHP to a server or API

I attempted to develop a straightforward web application that utilizes the Microsoft computer vision API to analyze images. Despite successfully testing the endpoints on rapidapi.com, I encountered difficulties getting the correct output on the app. Below ...

Learn how to extract data located between two specific tags using Java HtmlUnit

<span> <a></a> Hello <div>A plethora of irrelevant text</div> </span> My goal is to retrieve only the "Hello" from the webpage. While I can use XPath to target the span, calling .getTextContent() also includes the text ...

Is there a way to escape from an iFrame but only for specific domains?

if (top.location != self.location) { top.location = self.location.href; } If my website is being displayed in an iFrame, this code will break out of it. But I want this to happen only for specific domains. How can I perform that check? ...

What is the best way to superimpose an image onto a canvas?

I am working on a cool project where I have created a canvas that displays matrix binary code raining down. However, I would like to enhance it by adding an image overlay on top of the canvas. Here is my current setup: <div class="rain"> ...

The headless mode of Google Chrome is displaying HTML content in a mobile-friendly format

I'm currently using Google Chrome headless with Laravel to convert HTML to PDF. However, I am facing an issue where it is rendering in a mobile view instead of displaying as a large screen-sized device. Here is my code: $content = '.......' ...

Navigating a page by manipulating the location.hash property in the Safari browser

There is an issue with my forum page that shows a tree view of messages under the selected message. Upon clicking on a message in the tree, the new message body loads into a div near the top of the page using AJAX. Subsequently, the following code runs: w ...

Position the div to the right of a left navigation bar instead of beneath it using Bootstrap 5

After using the code snippet from , I'm struggling to align content to the right of the navigation bar. It seems like a simple issue related to changes in Bootstrap 5 that I can't seem to figure out. <div class="d-flex flex-column vh-100 ...

Tips for retrieving the option text value following an onchange event in AngularJS

Whenever I change the selection in my dropdown menu for 'Cities', the alert is displaying the value of the previous selection instead of the current one. For example, if I select a state and then switch to Cities, the alert shows the text related ...

Arranging links in a horizontal line (JSFiddle)

CodePen: https://codepen.io/example/abc123 I am attempting to align the elements "Favourite", "0", and "Reply" next to each other so they appear as: Favourite 0 Reply ...

The correct terminology for divs, spans, paragraphs, images, anchor tags, table rows, table data, unordered lists, list

Just wondering, I've noticed that every time I come across a page element '<###>[content]<###>' and want to learn how to manipulate it, I usually search for something like "how to do x with div" or "how to get y from div". I know ...

The loading bar function seems to be malfunctioning when paired with the navbar element <nav> in an HTML document

Can you assist me with a coding issue I am facing? I have implemented a loadingbar on my webpage similar to YouTube, and it works perfectly on the page itself. However, when I try to integrate it with a navigation bar, it stops working. Below is the code ...