Customize your lists with resizable columns and absolute positioning using ul & li elements

Here is a code snippet that we are working with: Our goal is to have the parent UL element adjust its width based on the longest text in the LI elements. The current max-width property limits the ul's width, but changing it to 100% would make it expand to the full screen size. We need it to dynamically fit the content while still maintaining two columns.

ul{
position: absolute;
  margin-top: 18px;
  font-size: 15px;
  column-width: auto;
  column-count: 2;
  max-width: 160px;
  z-index: 20;
  color: rgb(109, 113, 107);
  box-shadow: rgb(162, 151, 151) 3px 3px 10px;
  cursor: pointer !important;
  list-style: none;
  background: rgb(255, 255, 255);
  padding: 10px 12px;
}

ul li{
    display: inline-block;
    width: 100%;
    padding: 2px;
}
<div id="testDiv">
  
  <ul>
   <li>Testing</li>
   <li>Testing</li>
   <li>Testing</li>
   <li>This is very long text</li>
   <li>Testing</li>
   <li>Testing</li>
   <li>Testing</li>
  
  </ul>


</div>

Answer №1

I decided to swap out column-width for display:grid.

ul{
  position: absolute;
  margin-top: 18px;
  font-size: 15px;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-gap: 0 2em;
  max-width : 100%;
  z-index: 20;
  color: rgb(109, 113, 107);
  box-shadow: rgb(162, 151, 151) 3px 3px 10px;
  cursor: pointer !important;
  list-style: none;
  background: rgb(255, 255, 255);
  padding: 10px 12px;
}

ul li{
    display: inline-block;
    width: 100%;
    padding: 2px;
}
<div id="testDiv">
  
  <ul>
   <li>Testing</li>
   <li>Testing</li>
   <li>Testing</li>
   <li>This is very long text</li>
   <li>Testing</li>
   <li>Testing</li>
   <li>Testing</li>
  
  </ul>


</div>

Answer №2

If you make the following changes:

  • max-width removed from your ul element
  • display: inline-block removed from your li element
  • width: 100% removed from your li element

You should see the expected behavior.

ul{
  position: absolute;
  margin-top: 18px;
  font-size: 15px;
  column-width: auto;
  column-count: 2;
  z-index: 20;
  color: rgb(109, 113, 107);
  box-shadow: rgb(162, 151, 151) 3px 3px 10px;
  cursor: pointer !important;
  list-style: none;
  background: rgb(255, 255, 255);
  padding: 10px 12px;
  /* max-width: 160px;  removes max-width */
}

ul li{
    padding: 2px;
    /* display: inline-block; removes max-width */
    /* width: 100%; removes max-width */
}
<div id="testDiv">
  <ul>
   <li>Testing</li>
   <li>Testing</li>
   <li>Testing</li>
   <li>This is very long text</li>
   <li>Testing</li>
   <li>Testing</li>
   <li>Testing</li>
  </ul>
</div>

Answer №3

Not sure if this is the precise outcome you're seeking, but consider using width: fit-content (rather than setting a max-width) on your ul element and taking out display: inline from your li items.

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

Empty Angular-chart.js Container

How can I resolve the issue of getting a blank div and no output while trying to display a chart where the options, labels, and other data are initialized in the TypeScript controller and then used on the HTML page? I added the angular-chart.js library us ...

Matching objects in an array based on a specific property using the key of another object

To display information in the UI based on matching values between the data.examples array object's name.value property and the keys in the wcObject.notCoveredList, we need to create a logic. If there is a match, all corresponding values from wcObject ...

Tips for narrowing down table searches to only rows containing certain text within a column

Currently, I have a function that allows me to search through my table: //search field for table $("#search_field").keyup(function() { var value = this.value; $("#menu_table").find("tr").each(function(index) { if (index === 0) return; var id = $( ...

CSS tooltip fails to display

I'm having trouble with my code as the tooltip is not showing up! Interestingly, when I tested the CSS on a static table, it worked perfectly. Essentially, the table in the code is dynamically created using information from an array for headers and d ...

PHP and JavaScript are two powerful programming languages that are

While I understand that PHP and JavaScript operate in different locations, I am curious to know if there is a way to incorporate some PHP code into my JavaScript file. I need to create unique URLs for linking to profiles and news posts, such as /#/news/IDH ...

What is the best way to display fewer pages in Pagination on a mobile view?

Issue We need to display fewer pages in the mobile view so that it can align with the heading (My Orders) on the same line. https://i.sstatic.net/JROvk.png Resource material-ui/pagination Current Progress I have successfully removed the Next and Prev ...

Building Dynamic Forms with React

I am currently attempting to create a dynamic form using react hooks and .map(); So far, I have managed to generate the form statically, similar to this example : https://codesandbox.io/s/material-demo-ndh9q This is the static form I have created : < ...

What is the method to configure the current host for the callbackURL in Passport strategy?

Currently, I am implementing the Passport-Linkedin strategy within Express to enable users to log in using their LinkedIn profiles. The code snippet I am working with is as follows: passport.use(new LinkedInStrategy({ consumerKey: config.linkedin.LIN ...

What is causing the "mb-3" class from Tailwindcss to malfunction with next/link?

The "mb-3" class from Tailwindcss in this Next.js component seems to have no effect in the Y direction. However, when using the "m-3" class, it also doesn't affect the Y direction, but instead has an impact on the X direction. impo ...

Angular: How to restrict the compilation of rgba() to #rrggbbaa in your codebase

There are some browsers that do not support #rrggbbaa but do support rgba(). However, in my Angular project, background-color: rgba(58, 58, 58, 0.9) in common.css was compiled to background-color:#3a3a3ae6 in style.[hash].css. How can I prevent this co ...

Resolving the problem of degrading image quality in HTML Canvas

Recently, I came across an issue while trying to display graphics on an HTML page using canvas. The problem I encountered was that the canvas element was somehow reducing the quality of my images during rendering, especially after some time. Let me visual ...

Animating the Three.js Globe camera when a button is clicked

Struggling to create smooth camera movement between two points, trying to implement the function below. Currently working with the Globe from Chrome Experiments. function changeCountry(lat, lng) { var phi = (90 - lat) * Math.PI / 180; var theta = ...

"Displaying Rails Flash Notice Dynamically via Ajax upon successful completion of a

I'm trying to implement a flash notice feature that gets sent back to my view via ajax once my controller successfully completes an uploaded file. However, I'm having difficulty handling the response from the server. The code snippet below is wha ...

Is there a way to raise an error in React Native and make it visible?

I am working on a functional component where I need to call a method from another .js file. The method in the external file intentionally throws an error for testing purposes, but I want to propagate this error up to the functional component's method. ...

Guide to displaying an input box depending on the selection made in a Mat-Select component

I am working on a mat-select component that offers two options: Individual Customers and Organizational Customers. When selecting Individual Customers, the dropdown should display three options: First Name, Last Name, and Customer Id. However, when choosin ...

A cube created in Three.js featuring a unique texture on each of its six faces

I'm attempting to generate a cube using three.js with unique textures on each face, resembling a dice. This is being developed in a sandbox environment where a rotating cube with dice images (1-6) on each side will be created. Once completed, I plan t ...

Using JavaScript variables within the value section of a JSON is a common requirement for developers

var averageTemperature = req.params.rating; var destinationName = req.params.name; var query = { "results.name": destinationName }; var updateQuery = { $set: { "results.$.rating": averageTemperature } }; mach.update(query, updateQuery, function(err, res ...

Modifying the order of Vuetify CSS in webpack build process

While developing a web app using Vue (3.1.3) and Vuetify (1.3.8), everything appeared to be working fine initially. However, when I proceeded with the production build, I noticed that Vue was somehow changing the order of CSS. The issue specifically revol ...

The outcome of comparing with Bcrypt is consistently a negative result

bcrypt.compare() is consistently returning false when used in conjunction with this code within the user model. It appears that this issue is specific to bcrypt-nodejs. User.pre('save', function (callback) { this.password = bcrypt.hashSync(thi ...

Creating a reverse proxy using next.js

My goal is to set up a reverse proxy for the GeForce NOW website using Next.js, but I'm struggling to make it work. module.exports = { async rewrites() { return [ { source: '/:slug', destination: 'https://pla ...