Is it possible for the parent element in CSS to have the same width as

Struggling with creating an informal dropdown menu featuring images amid a block of text, the issue lies in setting the correct width for the parent container relative to the dropdown. Ideally, the width of the dropdown should be determined by the clickable item's width.

The green (#st) width appears fine without 'display: block;' on the #nd, but the #nd doesn't extend beyond the surrounding text.

After spending 2 days debugging using Firefox Developer Tools (formerly FireBug), I'm yet to find a viable solution. It has been about 5 years since I last worked extensively with CSS and used to handle such challenges effortlessly. ;-)

#container {
  position: relative;
  display: inline-block;
}

#st {
  background-color: green;
  display: block;
}

#nd {
  background-color: red;
  position: absolute;
  white-space: nowrap;
}
<ul>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfasd asdaff</li>
  <li>tgestergsgfsdfg
    <div id="container">
      <span id="st">testing</span>
      <ul id="nd">
        <li>sadfasdf asdfsadf</li>
        <li>rwert2345234 345345</li>
      </ul>
    </div>
  </li>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfas dfasdfas</li>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfas dfasdfas</li>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfas dfasdfas</li>
</ul>

Answer №1

Placing the position:absolute property on an element will result in it being taken out of the normal document flow. This enables the element to be positioned above or below other elements, without its parent element being aware of its dimensions or position.

At times, using this approach can serve as a substitute for assigning position:absolute to an element.

float: left;
margin-bottom: -999em;
position: relative;

#container {
  position: relative;
  display: inline-block;
}

#st {
  background-color: green;
  display: block;
}

#nd {
  background-color: red;
  position: relative;
  float: left;
  margin-bottom: -999em;
  white-space: nowrap;
}
<ul>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfasd asdaff</li>
  <li>tgestergsgfsdfg
    <div id="container">
      <span id="st">testing</span>
      <ul id="nd">
        <li>sadfasdf asdfsadf</li>
        <li>rwert2345234 345345</li>
      </ul>
    </div>
  </li>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfas dfasdfas</li>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfas dfasdfas</li>
  <li>sadfasdf asdf sadfasdf asdfas dfasdfas dfasdfas</li>
</ul>

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

Setting the second tab as the primary active tab

I am currently working on a script that is well-known, and everything is functioning perfectly. However, I want to change it so that when the page is first opened, it displays the second tab instead of the first one (the first tab being a mail compose tab ...

Developing an AngularJS directive in place of a bulky jQuery function

I have a javascript function, mainly utilizing jquery, that I currently run when the page loads. My goal is to convert this into an angularjs directive, but I am facing difficulties in figuring out how to achieve it. As someone new to angular, any assista ...

The Bootstrap Grid Leaves Blank Spaces Unfilled

View the screenshot here Take a look at the image above. I'm struggling to fill the empty space with div elements. I've experimented with display:inline-block and other display properties, but they haven't yielded the desired results. < ...

Is there a way to arrange an HTML list in this specific manner using CSS or JavaScript?

I need to arrange a list of items in columns with 5 rows each, as shown in the attached image. This list is generated dynamically using an SQL query with a loop on the li tag. I am looking for a solution to order the list in this way using javascript or ...

A step-by-step guide on implementing styled components in React.js with conditional statements

Embarking on my latest project with the goal of going "from Zero to Hero", I received a tip from a friend about styled components. Intrigued, I decided to ditch my traditional .css files and make the switch. Although I understand the basics - using <My ...

Displaying a trio of images in a rotating carousel with Bootstrap 4. Move forward one slide

I came across this carousel and I'm trying to figure out how to integrate it seamlessly with the latest Bootstrap 4 version. < script > $('#recipeCarousel').carousel({ interval: 10000 }) < /script> .carousel-inner ...

Excessive gap beneath the footer in Wordpress

I have come across many solutions for this issue, but none of them seem to work for me. On my main page, when I scroll to the bottom, the footer appears to have extra space beneath it, which actually seems to be the background of the page. This problem onl ...

The overflow phenomenon similar to what can be found in Photoshop

I'm curious if there's a way to create an overlay effect similar to what you see in Photoshop or Illustrator. I have a background picture and a colored div in front of it. I'd like to add an overlay effect to the front div that looks more i ...

How about "Switching Background Images Using a Click Trigger"?

I have a single web page with three different tabs. By clicking on each tab, the text displayed changes accordingly. I am looking to add three separate images for each tab so that when a user clicks on a specific tab, the image associated with it also chan ...

Is there a way for me to adjust this alignment?

"Newly Arrived" & "Here Comes the Latest". They won't be on the same alignment horizontally. How can I make them appear in a single line? Check out this demo http://jsfiddle.net/CtCuk/2/ HTML Markup <div class="top_page"> <div clas ...

Navigate through the list of options by scrolling and selecting each one until the desired element is

Hey, I've got this AngularJs directive that selects an item based on the "key-pressed" event, and it's working perfectly. The issue arises when there is a scroll bar since the elements get hidden, making it difficult for me to see them. You can ...

Locating the ID of the child div that was clicked within a parent div element

My HTML code snippet looks like: <div id="edit" ng-click="editFunction($event)"> <span id="1"> click1 </span> <span id="2"> click2 </span> <span id="3"> click3 </span> <span id="4"> ...

Display the element once the class enters the viewport

I am attempting to display a div only when certain elements with a specific class are visible in the viewport. I made progress with this example: http://jsfiddle.net/blowsie/M2RzU/ $(document).ready(function(){ $('.myclass').bind('inv ...

Seeking a method to easily reset the search filter in Isotope using a single click in jQuery?

I'm having trouble clearing my search filter without deleting the text in the search field. I want to use a button to clear the search field, but when I do, the items in the grid don't reappear. I attempted to add a Clear button as part of the c ...

I am experiencing an issue where the CSS file in the wwwroot directory does not update when I make changes to the code in ASP.NET MVC

Here is a link to my CSS file: Click here for image description This is how the file looks when built (no changes): Click here for image description Occasionally, running "clean-solution" and "build-solution" fixes the issue, but it's not working no ...

Is it possible to use an SVG mask with multiple video sources?

Wondering if it's feasible to use an SVG circle mask for various fixed video DOM elements. https://i.sstatic.net/JzdnU.png The goal is to have each SVG circle mask a separate video on different layers, then scale up to fit the browser height and wid ...

Utilize Bootstrap rows and columns to extend the column color to the bottom of the page

I'm having trouble getting the background color of a bootstrap col to extend to the bottom of the page without setting a fixed height. I want it to reach all the way to the bottom of the browser screen. HTML: <div class="container"> <div ...

Is it possible to modify the backdrop of the entire webpage?

I'm encountering an issue in my React Native project where I am unable to set a consistent background color across all elements on my pages. This is the code snippet causing the problem: import React, { Component } from "react"; import { View, Text, ...

Tips on displaying a modal pop-up at the center of the screen while also keeping its position intact when the screen is resized

I have a modal pop-up that I am displaying in the center of the screen using JavaScript. $('div.openIDPopup').css({ 'height': $(document).height() + 'px', 'width': $(window).width() + 'px', ...

Using CSS Flex to style text that has been transformed

I'm attempting to achieve a specific date display effect, but I'm running into some difficulties. Despite using flex and text transform, I can't seem to get rid of the extra width on the right side of the year. Here's my current outcom ...