Set position: absolute; width: 100%; on an element to match its container size in css

So, here is the current layout:

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

We're specifically looking at the issue with the 'SERVICEUSER' button and its submenu.

Is there a way to ensure that the minimum width of the submenu (with absolute positioning) matches that of its parent?

I've replicated the scenario in this JSFiddle link.

Essentially, we need the .collection element to have a minimum width equal to that of the li.

The HTML structure for this setup is as follows:

<ul>
  <li>
    <a href="">SERVICEUSER</a>
    <div class="collection">
      <div class="item">Item-1</div>
      <div class="item">Item-2</div>
      <div class="item">Item-3</div>
    </div>
  </li>
</ul>

The CSS code for this situation is as follows:

ul { list-style-type: none; }

li { display: inline; }

a { background: green; }

.collection {
  position: absolute; // necessary for this setup
  background: white;
}

Answer №1

When you use the "absolute" positioning on an element, it will be contained within the flow of its closest relative parent. By setting the li to relative, you can define the width for the collection.

li { display: inline; position: relative; }

.collection {
  position: absolute;
  background: white;
  min-width: 100%;
  white-space: nowrap;
}

Update: I included white-space: nowrap to ensure that the collection (subitems) can extend beyond the main item's width.

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

What is the best way to utilize mysql for storing user state in order to restrict them from taking a particular action more than once per day?

< button type="button" id="daily-reward-button">Claim</button> 1) When a user clicks this button, it should be disabled which will result in the user being banned in the MYSQL database and unable to click the button again. 2) The button shoul ...

yet another problem with overflow-x being hidden while overflow-y is visible

I am currently working on creating a tab overflow user interface that is similar to what Firefox uses. This involves utilizing a combination of fixed width containers and a fluid width container in the middle to hold the tabs. The challenge arises when th ...

Creating a perpetual loop animation for two divs moving from right to left endlessly

Here is the code I have: HTML <div class="screen screen1"></div> <div class="screen screen2"></div> CSS .screen{ width: 100%; height: 50%; position: absolute; background-color:#001; background-image: radial- ...

Retrieve the text content within HTML tags that come after a specific paragraph using the DOM

I am looking to extract content from HTML documents. Specifically, I need the contents of all 'p' tags that come after 'h2' tags. Here is an example of the HTML to be parsed: <h1>Lorem ipsum</h1> <p> Lorem ipsum ...

Ensure that the search button remains at the bottom of the view without overlapping its container

this codepen link will take you to the relevant content https://codepen.io/rad-mej/pen/qBKedwB I have a fixed search menu on the left, along with data displayed on the right. The search menu contains a search button that I want to be anchored to the bott ...

How to style a sublevel menu to appear next to its parent using CSS

I have a menu that is currently functioning well, but I would like to add a new sublevel onto it. However, if I directly add the new options, they end up hiding the existing ones on the menu. Here's an image for reference: I want the new options to ...

Can anyone suggest a method to customize the appearance of select options in Vue.js?

https://i.sstatic.net/qNov8.png Is there a way to customize the color and font of select options? I'm referencing this link for an example: . I attempted CSS modifications but they don't seem to be taking effect. ...

Reduce the transparency of the overlay picture

I have been utilizing the 'Big Picture' template to design a webpage. The overlay.png file adds a lighter background effect by overlaying intro.jpg with a partially transparent blue graphic. Despite my efforts to adjust the opacity and gradient/R ...

Understanding the functionality of sessions in CodeIgniter when using AJAX

I am experiencing an issue with the Session in my code. It only works when the page is refreshed. I have a controller called ravi and the view named myview. Here is the code snippet: <?php class Ravi extends CI_Controller { public funct ...

Conceal a list item based on its specific value with the help of

I have a code snippet below where I need to hide the 'li' element by its value that is in the middle using JQuery. Here is the HTML: <ul id="tagCloud"> <li>Item1</li> <li>Item2</li> <li> ...

Steps for creating a rubber compound with reduced elements

Sample Image Looking for a way to reduce the size of elements on a webpage without adapting them? I want the elements to shrink and align in a single column, and I will handle the adaptation myself. HTML: <!-- Trading --> <div class="main-co ...

I encountered an issue while iterating through Object HTMLDivElement and applying ChileNode style z-index, resulting in an undefined output in both Firefox and Chrome browsers

function adjustPosition(currentDiv) { try { for (var i = 0; i < document.getElementById("parentContainer").childNodes.length; i++) { document.getElementById("parentContainer").childNodes[i].style.zIndex = 0; ...

How to keep the menu on one line in CSS

Is there a way to avoid the menu breaking onto a second line around 1180 width in the browser? Here is more information on this issue. ...

What is the best way to implement a loop using JQuery?

<script> $(function() { $('.slideshow').each(function(index, element) { $(element).crossSlide({ sleep: 2, fade: 1 }, [ { src: 'picture' + (index + 1) + '.jpg' } ]); }); ...

The active link color for navigation in Bootstrap 4

I am trying to update the active menu links to display in green on my website. Despite various attempts, I have not been successful in changing the color. Can anyone provide guidance on how to proceed? My website is built with Bootstrap 4 and mdbootstrap. ...

How to center the navigation list vertically with double lines and maintain the href block?

Looking for a solution to align vertically in a basic navigation list bar, particularly when some items have two lines of text. I want the text to be vertically aligned to the middle, while still maintaining the ability to hover and click within the list ...

Customizing text inputs in three.js

Why are the HTML textboxes disabled for input when the "render" button is working properly? I have tried changing the render.domElement to different elements on the page such as container, info, parent, and scene, but I cannot figure out the reason for t ...

Learn the process of generating sequential heading numbers for topic headings in each HTML document using DTA or XHTML

I attempted to include hierarchical numbers for topic headings in dita2xhtml_eclipsehelp.xsl. (DITA OT HTML output) However, I am facing difficulty in producing numbers similar to the following in each html file: 1 heading text 1.1 heading text 1.1.1 Head ...

Previewing a PDF file with multiple headers displayed above the text content

Creating multiple headers for each page of a PDF document using dompdf can be tricky. The headers you generated are fixed at the top with a width of 100%. However, you are facing an issue where on the second and subsequent pages, the header overlaps with t ...

Ways to prevent the remaining time from indicating the duration

Welcome to my website. If you have any tips on creating dropdowns in markdown, please share! Here is the code snippet: // Add your JavaScript code here This script is designed to play music from a file. To use it, simply click the play button and upload a ...