Prevent uniform width for child elements in flexbox when using flex-direction column

In my navigation bar, I implemented flex with flex-direction column to stack the child elements vertically. However, I am facing an issue where all the child elements have the same width. I want each child element to have its own specific width so that I can create a moving underline effect using span. Does anyone have any suggestions on how to avoid giving all child elements the same width and instead allow them to have their individual widths?

body > aside {
  width: 50%;
  display: flex;
  flex-direction: column;
}

body > aside > section {
  height: calc(100vh - 50px);
  position: sticky;
  top: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

body > aside > section > nav {
  height: 80vh;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

body > aside > section > nav > div {
  display: flex;
  flex-direction: column;
}

body > aside > section > nav > div > a {
  color: grey;
  font-weight: 900;
  text-decoration: none;
  font-size: 25px;
  transition: 0.3s;
  font-variant: small-caps;
}

body > aside > section > nav > div > a:hover {
  color: var(--4nd-main-color);
  text-decoration: none;
  font-size: 25px;
}

body>aside>section>nav>div>span {
  background-color: var(--4nd-main-color);
  width: 00%;
  margin-top: 1.5px;
  border-radius: 3.5px;
  transition: 0.5s;
  height: 8px;
}
<body>
  <aside>
    <section>
      <nav>
        <div>
          <a class="h" href="#1">Home</a>
          <span id="underline1"></span>
        </div>
        <div>
          <a id="e" href="#2">Employees</a>
          <span id="underline2"></span>
        </div>
        <div>
          <a id="cu" href="#3">Contact us</a>
          <span id="underline3"></span>
        </div>
       <div>
         <a id="au" href="#4">About us</a>
         <span id="underline4"></span>
       </div>
     </nav>
   </section>
  </aside>
</body>

Answer №1

To ensure the divs have a width that matches their content, align the flex items to flex-start.

 nav {
   align-items: flex-start;
 } 

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

My grid layout remains unchanged despite tweaking grid-template-columns

I am currently learning HTML and CSS and experimenting with building a website. My goal is to have 4 boxes aligned next to each other, similar to a navigation bar, but with box items. Here is the code for these boxes: HTML: <section class="boxes&q ...

CSS Navigation Menu - Submenu displaying above main link

<div id="header"> <div class="cont"> <div id="banner"> <div id="nav"> <ul> <li><a href="index.htm">home</a></li> <li><a href="work.html">Works»</a> <ul> ...

javascript - Alternate text colors several times within specified color ranges

Seeking assistance with changing the text color multiple times from #627CA9 to #FFFFFF and vice versa. Here's what I've tried: function changeColor(id) { var x = document.getElementById(id); if (document.getElementById(id).style.color = " ...

Display the Full Width of Your Website on iPhones and iPads

Seeking guidance from a CSS expert. I'm facing an issue where only the right side of my website displays on iPhone/iPad browsers, leaving the left 1/3rd unseen. If anyone can shed light on what's causing this and provide a solution, it would be ...

Error in Blinking Tooltip when Hovering Skill Bubble (React and d3)

I've encountered a frustrating issue with tooltips on my website - they just won't stop blinking when I hover over the skill bubbles. I tried fixing the tooltips at a certain location, but whenever there's a bubble in that spot and I hover o ...

What techniques can I use to ensure that my website's layout adjusts correctly when resized?

body { padding: 0; margin: 0; font-family: 'Roboto', sans-serif; font-size: 16px; box-sizing: border-box !important; } a { display: flex; justify-content: center; align-items: center; width: 100%; height: 100%; text-decorat ...

What might be causing Flex not to function properly on my personalized landing page?

I attempted to create a basic landing page featuring an image background with a logo, button, and a row of 4 images displayed side by side using Flex. However, for some reason, the Flex is not functioning properly for the row of images. Here is the code s ...

Swap out a portion of HTML content with the value from an input using JavaScript

I am currently working on updating a section of the header based on user input from a text field. If a user enters their zip code, the message will dynamically change to: "GREAT NEWS! WE HAVE A LOCATION IN 12345". <h4>GREAT NEWS! WE HAVE A LOCATIO ...

What are the guidelines for incorporating tag names in CSS selectors?

I created a button that when clicked, displays a table containing actors. I have named them as follows: <button class="actors">Show actors</button> <table class="actors">...</div> Next, I want to style and access them in my CSS (a ...

Effortless scrolling on specific div elements, rather than the entire body

Currently, I am utilizing the following code to achieve smooth scrolling for links: function filterPath(string) { return string .replace(/^\//,'') .replace(/(index|default).[a-zA-Z]{3,4}$/,'') .replace( ...

Issues with Swiper functionality in Jquery Mobile

I have implemented the Swiper plugin by idangero.us along with jQuery Mobile... In this case, I am utilizing the Scroll Container Swiper for a content slider... However, I am encountering several difficulties when trying to integrate the code... You can ...

What is the best way to ensure grid element takes up 100% of the available height within a flex column

I am working with this HTML structure: https://i.stack.imgur.com/fy5Yz.png My goal is to have the .SceneWithHistory-Container take up 100% of the available height in its parent element. Below is my CSS code: .Module-Container margin-top: 120px; displ ...

What is the reason for the ineffectiveness of percentage padding/margin on flex items in Firefox and Edge browsers?

I am trying to create a square div within a flexbox. Here is the code I have used: .outer { display: flex; width: 100%; background: blue; } .inner { width: 50%; background: yellow; padding-bottom: 50%; } <div class="outer"> <div c ...

"Creating a distinctive CSS style for underlining titles without including the actual

Although I've searched on SO, I still haven't found a satisfactory solution to my question. So here's the issue: I want to underline an h3 tag with 100% width EXCEPT for the actual text part. It should look like this: My super title ...

"When the screen resolution changes, the absolute positioning of images won't function as expected

I am trying to overlay one image on top of another background image, but I am facing an issue. When I change the screen resolution, the position of the image that should be placed over the background image also changes. Below is the code I am using: ...

What are the recommended best practices for using partial views within 'container' divs?

What is the most effective approach to managing HTML markup for partial views that are refreshed through AJAX? One of the main challenges I face is deciding where to position the 'container' div... Imagine you have a masterpage and a partial vie ...

Exploring the possibilities of customizing gutters in Bootstrap v4 Beta

Is it possible to customize grid gutters at different breakpoints? I am aware that Twitter invested millions of dollars in perfecting Bootstrap v4, and I don't expect to overhaul everything overnight. However, I am curious if there is a way to create ...

When verifying the source, the empty() function does not eliminate any HTML content

I am facing an issue with an AJAX call that retrieves data from a PHP file. I am attempting to remove existing HTML content and replace it with the data fetched from the PHP file. To clear the content, I am using the following: $('#due-tasks-content& ...

jQuery unable to populate a div with content using the .load method

My goal is to use a jQuery script to grab the content from the "content" class on another page and insert it into the "code" div. Here's the code I'm working with: <script type="text/javascript"> $(document).ready(function() { $(&a ...

Tips for creating a website with an integrated, easy-to-use editing tool

Recently, I designed a website for a friend who isn't well-versed in HTML/CSS/JavaScript. He specifically requested that the site be custom made instead of using a web creator. Now, he needs to have the ability to make changes to the site without nee ...