Consistent max-width applied to various items using full HTML and CSS styling

Can a CSS class be created where the width is dynamically set to match the width of the longest item?

I know how to do this in jQuery by iterating through, but I need a solution using only CSS/CSS3.

Answer №1

How to dynamically set the width using CSS

In this CSS snippet, the width of all rows is determined by the width of the longest row in the list.

ul{
    margin: 0;
    padding: 5px;
    list-style: none;
    width:50%;
    background-color: #eee;
}

ul::after {
    clear: both;
    content: "";
    display: block;
}

li {
    background: grey none repeat scroll 0 0;
    display: block;
    float: left;
    margin: 5px;
    width: calc(50% - 10px);
}
<ul>
    <li>row 1</li>
    <li>row 2</li>
    <li>This is the widest row 3</li>
    <li>row 4</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

Include buttons in the HTML template once JSON data has been received

I am working on a feature to dynamically add buttons to the DOM using JSON data fetched from an API when users visit the site. Although I have successfully implemented the function to retrieve the data, I am facing challenges in adding these buttons dynami ...

The hyperlink appears to be malfunctioning with an additional border surrounding it within a pop-up window

I am facing an issue with a link <a> not functioning properly with a popup dialog on . Additionally, there seems to be a border that appears around the <a> when the popup initially displays. If you click on "Leer más" for any product, you can ...

Center-align the text in the navigation bar

Is there a way to center the text within my navigation and ensure it remains centered across all resolutions (left at 1920x1080, centered at 1420)? I understand that much of this code is inefficient and not working correctly, but for now I just want to fi ...

Static express fails to load CSS files from the main folder

When my app is loaded from a url like: server.com/pdf/12345, it tries to find static files at GET /pdf/12345/assets/css/stylesheet.css and returns a 404 error. I've been struggling to get it to search for the files in /public/assets instead. I've ...

Tips for shutting down an HTML webpage with the help of JavaScript

Recently, I've been working on a project to implement a feature that automatically closes the website when an incorrect password is entered. However, I am fairly new to JavaScript and still in the early stages of learning. Below is the code snippet I ...

Enhancing functionality with extra JavaScript tags in next.js

Recently, I delved into programming with react & next.js after previously working with node.js. It didn't take long for me to create my first application, but upon inspecting the page, I noticed an abundance of extra JavaScript code (see image below). ...

Accessing JavaScript cookie within .htaccess file to establish redirection parameters according to cookie content

Is there a way to modify the rules within my .htaccess file so that it can properly read a user-side cookie and redirect based on its content? The scenario is this: I have a cookie called userstate which contains an abbreviation of US states, such as AL ( ...

What are the steps to prevent the information in a text box from disappearing after submitting and calculating?

When this code is executed, it retrieves information about the starting and ending addresses and calculates the distance between them. However, a problem arises when the user submits the form - the input fields for entering the addresses are cleared. < ...

Challenge with Responsive Design

I'm struggling with implementing a responsive design for the first time. When I check the site on my iPhone, everything appears to be zoomed in for some reason. My goal is to have the logo aligned to the left within the '.container' when v ...

Is it possible to have the logo centered with the menu bar?

I found a cool theme: Check it out here! Does anyone know how to center the logo and menubar on this template? <section class="page_toplogo ls s-pt-25 s-pb-20 s-py-md-30"> <div class="container"> ...

Using HTML to design interactive buttons that can send API requests and display their current status

Seeking assistance with creating buttons to control a remote light's ON and OFF states while reflecting their status as well. The specific http API calls for the light are necessary to toggle its state. Turn On = http://192.168.102.22:3480/data_requ ...

Can a grid item be specifically placed on the final row or column at all times?

Is it possible to position a grid item in the last row and / or column of a grid without knowing the exact number of rows or columns? Here is the grid setup: .columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(min-content, 16.5re ...

Use a foreach loop to iterate over an array of dates and display them in individual tables

I am currently working on structuring my foreach loop to iterate through all the dates in my time stamp arrays. The goal is to display the results in 5 separate tables, each pertaining to one of the dates. Below is the function I've implemented to re ...

Using the float property in IE7 can cause a line break and may result in other divs being pushed down

Hey there, I've encountered a few issues with floats in IE multiple times. Here's an example to illustrate what I mean. Please note that this example functions properly in Firefox and Chrome. index.html <html> <link href="style.css" rel= ...

Eliminate the dark loading screen flash effect from HTML5 videos

I've been working on a website for an online game that I'm currently playing. I've added an HTML5 video in the header section, but there's a brief flash of black screen while it loads, specifically in Chrome (no issues in Internet Explo ...

Tips on aligning a form element with another element in a Bootstrap column

Is there a more efficient way to position a globe glyph directly to the right of a textbox in multiple places throughout my code, almost touching it? One possible solution is as follows: <div class="col-md-3"> <input class="form-control" ...

Creating a dynamic Bootstrap 4 hover effect for collapsible elements with numerous items

I have come across a few techniques for implementing the collapsible accordion feature in bootstrap. However, I am facing a challenge when it comes to customizing it so that only one panel-collapse is activated upon hovering over the div.panel-heading > ...

Tips to avoid the page from scrolling to the top when the menu is opened

Whenever a user taps the menu button on the page, a full-page menu opens. However, there is an issue - the main content page automatically scrolls to the top. Can you provide suggestions on how to prevent this from happening? I have already looked into a s ...

What is the best way to vertically center a row?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Unique Title</title> <link rel="stylesheet" href="custom-reboot.min.css"> <link rel="stylesheet" href="custom-style.min.css"&g ...

Create a div element that is fixed position, with a width that adjusts dynamically, and is centered on

Even though this is a common issue, I have yet to find a solution that actually works for me. The centred div in my chrome extension isn't displaying correctly, even though it works perfectly on jsfiddle. After dynamically appending a div with specif ...