What is the best way to increase the width of an individual list item beyond the width of the list itself?

I am dealing with a situation where I have a list that has a fixed width in pixels. Within this list, there are two items: one with content that fits within the width of the list, and another item whose content is longer than the width of the list itself.

My goal is to dynamically adjust the width of each list item so that it accommodates the larger size between the list width and the content width (ensuring that the second item can fully display its content).

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

ul {
  width: 400px;

  overflow-x: scroll;
  outline: 1px dashed red;
}

li {
  max-width: none;
  min-width: 100%;

  padding: 0.5rem 1rem;

  white-space: nowrap;
  background-color: skyblue;
  outline: 1px dashed blue;
}
<ul>
  <li>test</li>
  <li>test test test test test test test test test test test test test test test test test test test test test</li>
</ul>

Answer №1

Utilize the display: inline-block property in place of display: block to ensure that the width of the list adjusts to fit the content.

See example below:

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

ul {
  width: 400px;
  overflow-x: scroll;
  outline: 1px dashed red;
}

li {
  /* When using inline-block, the default width is adjusted to fit the content, with other differences compared to block */
  display: inline-block;
  min-width: 100%;
  padding: 0.5rem 1rem;
  white-space: nowrap;
  background-color: skyblue;
  outline: 1px dashed blue;
}
<ul>
  <li>test</li>
  <li>test test test test test test test test test test test test test test test test test test test test</li>
</ul>

For further information, visit: https://developer.mozilla.org/en-US/docs/Web/CSS/display

Answer №2

Set the container to have a display: grid property, and both items will automatically adjust to match the size of the largest one.

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

ul {
  width: 400px;
  display: grid;

  overflow-x: scroll;
  outline: 1px dashed red;
}

li {
  padding: 0.5rem 1rem;

  white-space: nowrap;
  background-color: skyblue;
  outline: 1px dashed blue;
}
<ul>
  <li>test</li>
  <li>test test test test test test test test test test test test test test test test test test test test</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

Enhancing Tapestry Grid component column sort icons

In the Tapestry Grid components, the default column sort icons are blue and white, but if your page has a different color scheme, you may want to personalize them. How can you replace the default Tapestry Grid column sort icons with your own custom icons? ...

Show a grid layout featuring varying heights using HTML

I am trying to create a grid view for displaying images in an HTML document. I want each li tag to have a different height, but currently they are all the same height. Below is my HTML code: <div class="blog_main_midd_section"><ul> ...

What is the best way to ensure one div expands to full width while simultaneously hiding another div on the page?

Hey there, I'm looking for a way to expand the width of a clicked div while making the other div disappear from the screen simultaneously. It should also be toggleable, so clicking the button again will shrink the div and bring back the other one. For ...

Enhancing the appearance of unvisited links with background styles

I am endeavoring to incorporate an icon into all unvisited links on a specific Wordpress category page. Below is the CSS code that I am utilizing. It has been placed at the conclusion of my final CSS file. .category-xyzzy .entry-title a:link { background ...

What is the best way to shift a link to the right within a "div" element?

I've been working on setting up a navigation menu for my website, and I'm having trouble getting the "quit" link to align to the right while keeping the other links aligned to the left. I've tried various methods but just can't seem to ...

Creating a sidebar that remains fixed in place even as the page is scrolled down can be achieved by using CSS and positioning properties

I am looking to create a website with a specific layout design as shown in this image: https://i.stack.imgur.com/ndKcz.png The main focus is on making the sidebar (F/T container) function as a social network link, sticking to the right side of the page ev ...

how to arrange wordpress menu items in a vertical layout

Looking to display the sub-submenus of my site beneath their parent menu node similar to the layout of the Oracle website. Utilized a theme called Oracle, and it's visually appealing. Now, the goal is to exhibit the sub sub menus directly under their ...

Integrating an HTML resume, complete with its CSS style sheet, into a different HTML page

Looking for tips on seamlessly integrating one HTML page within another? I'm in the process of customizing my personal website using a template, but I recently came across a free resume HTML code that I'd like to incorporate. However, when I atte ...

Creating a fixed navigation bar for an ecommerce website

Recently, I purchased a webshop and I am trying to implement a sticky header that remains at the top of the page all the time. Unfortunately, none of the solutions I've tried seem to be working, and I'm stuck on how to proceed. Is there anyone ou ...

Why am I unable to view the image in the material-ui-next "Cards" example?

I came across this example on the material-ui-next website, but strangely I am unable to view the lizard image when I try to run it on my computer. Why is the image not showing? There are no errors in my console, and the "simple card" example on the websi ...

The keyframe spinner fails to function properly on Firefox and results in blurry text

I am facing an issue with a keyframe spinner that rotates an image on the y-axis. It works perfectly in Chrome but fails to function in Firefox. I have tried changing the HTML tags to div or span class/id, but nothing seems to work. Interestingly, other ke ...

Safari is struggling with the backface visibility feature not functioning as expected

I have implemented a cssflip animation in my code where the element rotates on hover. I included transition and backface-visibilty properties. While it works well on Chrome, I faced issues with Safari. I even added the webkit prefix for Safari browser. `. ...

How can you ensure that text in a container automatically moves to a new line and causes the container to expand downward if necessary, when

Are you searching for a way to make text inside a container wrap to a new line and have the container expand downwards if needed? Update <div class="modal hide fade" id="modalRemoveReserve" style="display:none;"> <div class="modal-header"&g ...

The functionality of Dompdf's style does not seem to be functioning properly

I've been experimenting with dompdf and while my script successfully generates a PDF document, the style tag seems to have no effect. Here's my code. I'm familiar with how it operates and have used it with pure HTML before, but this is my fi ...

The :before element is not displaying when using jQuery's slideToggle() method

When using only CSS, the .nav-main:before{} element is displayed, but with jQuery, it does not toggle that element and always keeps it hidden. Disclaimer: This issue has been observed on mobile devices (tested in Android Chrome). jQuery $('.menu-to ...

Changes made to index.php are not reflecting on the WordPress website

Currently making changes to my WordPress theme and just included a new div with the class "sidebar." However, I'm encountering an issue where it's not updating on the homepage of the website. Strangely enough, the stylesheet does seem to be updat ...

Consistently ensuring even horizontal CSS padding throughout all components

I'm working on a website using React and TailwindCSS. Each part of the site is its own React Component, but I want to make sure that all sections have consistent horizontal padding and a maximum width where the content stays centered and doesn't ...

Tips for removing a particular slide from bootstrap carousel with JQuery

I'm a beginner when it comes to bootstrap. I have a Carousel with some slides that include a Decline button. When the button is clicked, I want to delete/remove that slide and move on to the next one. Can this be achieved using JQuery? I've been ...

Display the icon beneath the list item label

How can I display the ion-edit icon below the list names? The icons are currently appearing on the right side of each list name, but I want them to be aligned below with some spacing. When clicking on "Data Source Connections," a sublist will be shown whe ...

Implementing CSS styles on selected elements using jQuery

My current HTML layout looks like this.. <div class="container> <div class="panel"> <div> <div> <div> <img class="match" src="myimage.jpg"> &l ...