What is the best way to ensure a span takes up 100% of its parent li's width

Looking to create a span that spans 100% of its parent li element

<ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a>
        <span>
              <a href="#">One</a>
              <a href="#">Three</a>
              <a href="#">Four</a>
        </span>
    </li>
    <li><a href="#">Services</a>
        <span>
              <a href="#">One</a>
              <a href="#">Two</a>
              <a href="#">Three</a>
        </span>
    </li>
    <li><a href="#">Responsibility</a>
        <span>
              <a href="#">One</a>
              <a href="#">Two</a>
        </span> </li>
    <li><a href="#">Contact</a></li>
  </ul>

My current navigation bar setup is as follows:

However, I want it to look like this:

Is there a way to accomplish this? I attempted to use width:100%; for the span, but it extended outside the li element and affected all the spans to the width of the ul.

Answer №1

To address the issue mentioned by others, a simple solution is to adjust the styling of the span and anchor tag by setting display: block.

li:hover span, li:hover span a {
  display: block;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background: blue;
}

ul {
  list-style: none;
  background-color: black;
  height: 40px;
  display: flex;
}

ul li {
  position: relative;
  line-height: 40px;
  width: 20%;
  text-align: center;
}

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

span, span a {
  display: none;
}

span {
  position: absolute;
  width: 100%;
}

span a {
  border-bottom: 1px solid black;
  text-align: center;
}

li:hover, span {
  background: red;
}
<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a>
    <span>
              <a href="#">One</a>
              <a href="#">Three</a>
              <a href="#">Four</a>
        </span>
  </li>
  <li><a href="#">Services</a>
    <span>
              <a href="#">One</a>
              <a href="#">Two</a>
              <a href="#">Three</a>
        </span>
  </li>
  <li><a href="#">Responsibility</a>
    <span>
              <a href="#">One</a>
              <a href="#">Two</a>
        </span> </li>
  <li><a href="#">Contact</a></li>
</ul>

The reason for this is that the span element is by default an inline element and does not occupy the full width of its parent. By changing it to display: block, you are instructing it to behave like a block element, thereby expanding to fill the width of its parent.

Answer №2

Explore the following style options:

li span {display:block; width: 100%; overflow: hidden;}
li span a{display: inline-block; float:left;}

Answer №4

To improve the structure, consider switching the span to display: block or replace it with a more suitable block element like div. It's not recommended to nest inline and block elements within an inline element. Try implementing it this way instead:

 <li><a href="#">About</a>
    <div>
          <a href="#">One</a>
          <a href="#">Three</a>
          <a href="#">Four</a>
    </div>
 </li>

Here is the CSS:

 li div a { 
       width: 100%;
       display: block;
       box-sizing: border-box;
       padding: 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

Hover over me to see the CSS animation translate upwards until it reaches a specific point

I am new to CSS animation and I am trying to create a circle that moves from one end to the other on hover. However, I am facing an issue where the circle goes off-screen when the browser size is changed. How can I make it stop at a certain point within th ...

What is the best way to make sure flexbox items fill in any available space?

I am currently working on a website project, and I am looking to display text content in two columns using flexbox with each column taking up 50% of the width. However, I am encountering an issue as illustrated in the image below: https://i.sstatic.net/Gh ...

How can I set a background image to automatically adjust to the width of the window, be full height, allow for vertical scrolling, and

How can I set a full-screen background image that adjusts to the body width without horizontal scrolling, maintains height proportionate to width, and allows for vertical scrolling? Here is my current code: html, body { margin: 0px; padding: 0px; } bo ...

What is the technique for utilizing JavaScript to toggle the opening and closing of a Bootstrap 5 navbar dropdown?

My website is built using Bootstrap 5, and I am working on creating a navbar dropdown functionality. On desktop, I want the dropdown to open on hover and lead to a new page when clicked. However, on mobile, I only want the dropdown to open and close on cli ...

What's the issue with this HTML button not functioning properly?

I'm having an issue with a button in my code that is supposed to change the text from 'hello' to 'Goodbye', but for some reason, it's not working. I have ensured that my code is properly indented in my coding program. Below i ...

What are some ways to prevent "window.onbeforeunload" from appearing when a form is submitted?

Is there a way to prevent the alert box from appearing when submitting a form using the Submit button below? I want to avoid being prompted to "Leave this page" or "stay on this" after submitting the form. <script> window.onbeforeunload = ...

Is it possible to create a button function in HTML that can alter the CSS image tag?

Is there a way I could create a function that can retrieve a value, update an image source, and then incrementally select another value? It would be incredibly helpful if you could provide a reference where I could learn how to do this or explain it in det ...

Effortless Page Navigation - Initial click may lead to incorrect scrolling position, but the subsequent link functions perfectly

While implementing the Smooth Page Scroll code from CSS Tricks, we encountered an issue where clicking on a navigation link for the first time only scrolls down to a point that is approximately 700px above the intended section. Subsequent clicks work perfe ...

Creating a unique tab spacing effect using HTML and CSS

I'm currently working on my personal website and I'm looking to create a tab-like effect (similar to word processors) for showcasing projects along with their descriptions. Here's an example layout: ReallyLong NameProject Project1 Descr ...

Ways to prevent negative values from appearing in the text field

Check out this code snippet on Fiddle. I need help with a text field that should only display positive values. If the input is negative, I want it to be stored in ng-model but not shown in the textbox. function LoginController($scope) { $scope.number = ...

I'm having some trouble with my jQuery.get() function in the javascript Saturday(), can anyone help me figure out what I'm doing wrong?

Can anyone help me troubleshoot my jQuery.get() method in the saturday() JavaScript function? Here is the code snippet I have been working on. This is what I have in my index.html file: <html> <head> <title>jVectorMap demo</title> ...

retrieving a date from a different source and displaying it in a date

Is there a way to ensure that the date format in an input of type date always follows the dd/MM/yyyy Brazilian pattern, regardless of the computer or browser specifications? <div class="input-group input-group text-center in ...

Dynamic pop-up content based on AJAX response

I don't have much experience in web development, so please excuse any beginner mistakes;) Currently, I'm working on creating a dynamic popup window in HTML, which is hidden by CSS. When a button is clicked, an AJAX post request is triggered to r ...

Issue with the Ajax auto-complete feature in Internet Explorer

I am facing an issue with my "ajax suggestion list" getting hidden behind the "select menu" located right below the text box that triggers the ajax function. This problem only occurs in IE 6.0, while it works fine in other browsers. I have already disabled ...

The keyboard automatically disappeared upon clicking the select2 input

Whenever I select the select2 input, the keyboard automatically closes $('select').on('select2:open', function(e) { $('.select2-search input').prop('focus',false); }); Feel free to watch this video for more i ...

The command response.setHeader("Refresh", "0") failed to execute

My webpage needs to be refreshed when the user session has expired or the connection is not active. Despite trying various codes, I have been unsuccessful in achieving this. The last code snippet that I implemented is as follows: if(session.getAttribute(" ...

Arranging a list of HTML elements based on their inner content text

I am faced with the task of sorting computer-generated text using JavaScript. To achieve this, I have duplicated the HTML markup in order to create a sorting function in JavaScript. The challenge lies in sorting an array of HTML objects based on their in ...

Trouble with CSS transitions not functioning while altering React state

Each time a user clicks on an accordion, the content should smoothly show or hide with a transition effect. However, the transition only seems to work when opening a closed accordion, not when closing an already open one! To get a clearer idea of this iss ...

Exploring the power of Vue.js by utilizing nested components within single file components

I have been attempting to implement nested single file components, but it's not working as expected. Below is a snippet of my main.js file : import Vue from 'vue' import BootstrapVue from "bootstrap-vue" import App from './App.vue&apos ...

Requesting PayPal for an HTTPS transaction

When attempting to call the Express Checkout Paypal API using $http.get(AngularJS), I encountered error 81002(Method Specified is not Supported). However, when I tried calling the API using the search bar in Google Chrome, I was able to retrieve the token ...