Is a CSS-only autoexpanding label possible within a list?

I am interested in having all the labels automatically expand to the size of the widest one. Below is the HTML structure:

 <div class="body">
  <ul class="list">
    <li>
      <span>
          <label>condition</label>
          <span>used</span>
      </span>
    </li>
    <li>
      <span>
          <label>with invoice</label>
          <span>yes</span>
      </span>
    </li>
    <li>
      <span>
          <label>with warranty</label>
          <span>no</span>
      </span>
    </li>
        <li>
      <span>
          <label>with extra large text</label>
          <span>not looking good!</span>
      </span>
    </li>
  </ul>
 </div>

In addition, here is the related CSS code:

.body {
  background-color: #f1c40f;
  padding: 1em;
  width: 300px;
}

.list {
  padding-bottom: 1em;
  padding-top: 1em;
  > li {
    margin-bottom: 1em;
    &:last-child {margin-bottom: 0;}
  }  
  > li > span {
    background-color: #e0e0e1;
    border-radius: 5px;
    color: #6c777f;
    display: inline-block;
    width: 100%;
  }
  label {
    border-bottom-left-radius: 5px;
    border-top-left-radius: 5px;
    background-color: #a1aab0;
    color: white;
    display: inline-block;
    margin-bottom: 0;
    margin-right: .5em;
    min-width: 40%;
    padding: .5em;
    text-transform: capitalize;
  }
}

The question remains - can all the labels adjust their size based on the length of the fourth label?

Fiddle: https://jsfiddle.net/emvidi/qe6bpn14/1/ Updated Fiddle for fluid design: https://jsfiddle.net/emvidi/qe6bpn14/4/

Answer №1

Absolutely possible! However, a slight modification in the structure is required.

To achieve this layout, you will need the following HTML structure:

<ul class="list">
  <li>
    <label>condition</label>
    <span>used</span>
  </li>
  <li>
    <label>with invoice</label>
    <span>yes</span>
  </li>
  ...
  ...
  <li>
    <label>with invoice</label>
    <span>yes</span>
  </li>
</ul>

The list items will be rendered as table rows, and both the <label> and <span> elements will act as table cells.

.list {
  border-spacing: 0 10px;  
  display: table;
}
.list > li {
  display: table-row;
}
.list > li > label,
.list > li > span {
  display: table-cell;
}

.body {
  background-color: #f1c40f;
  padding: 1em;
  width: 300px;
}

.list {
  display: table;
  border-spacing: 0 10px;
  padding: 0.5em 0;
}

.list > li {
  background-color: #e0e0e1;
  border-radius: 5px;
  color: #6c777f;
  display: table-row;
  width: 100%;
}
.list > li > label {
  border-bottom-left-radius: 5px;
  border-top-left-radius: 5px;
  background-color: #a1aab0;
  color: white;
  display: table-cell;
  min-width: 40%;
  padding: .5em;
  text-transform: capitalize;
}

.list > li > span {
  border-radius: 0 5px 5px 0;
  background-color: #e0e0e1;
  display: table-cell;
  padding: .5em;
}
<div class="body">
  <ul class="list">
    <li>
      <label>condition</label>
      <span>used</span>
    </li>
    <li>
      <label>with invoice</label>
      <span>yes</span>
    </li>
    <li>
      <label>with warranty</label>
      <span>no</span>
    </li>
    <li>
      <label>with extra large text</label>
      <span>not looking good!</span>
    </li>
  </ul>
</div>

Answer №2

One option to achieve the desired size is by adding .label{width:150px}. However, keep in mind that this size setting will remain constant and won't change dynamically.

.body {
  background-color: #f1c40f;
  padding: 1em;
  width: 400px;
}

.list {
  padding-bottom: 1em;
  padding-top: 1em;
}
.list > li {
  margin-bottom: 1em;
}
.list > li:last-child {
  margin-bottom: 0;
}
.list > li > span {
  background-color: #e0e0e1;
  border-radius: 5px;
  color: #6c777f;
  display: inline-block;
  width: 100%;
}
.list label {
  border-bottom-left-radius: 5px;
  border-top-left-radius: 5px;
  background-color: #a1aab0;
  color: white;
  display: inline-block;
  margin-bottom: 0;
  margin-right: .5em;
  min-width: 40%;
  padding: .5em;
  text-transform: capitalize;
  width: 150px
}
<div class="body">
      <ul class="list">
        <li>
          <span>
              <label>condition</label>
              <span>used</span>
          </span>
        </li>
        <li>
          <span>
              <label>with invoice</label>
              <span>yes</span>
          </span>
        </li>
        <li>
          <span>
              <label>with warranty</label>
              <span>no</span>
          </span>
        </li>
            <li>
          <span>
              <label>with extra large text</label>
              <span>not looking good!</span>
          </span>
        </li>
      </ul>
    </div>

Answer №3

One approach is to utilize jQuery to identify the longest label and adjust the width of other labels accordingly:

$(function(){
    var longestLabel = $('label').outerWidth();
    $('label').each(function(){
        if ($(this).outerWidth() > longestLabel) {
            longestLabel = $(this).outerWidth();
        }
    });
    $('label').css('width', longestLabel);
});

Answer №4

To avoid having to check the widest label as previously mentioned, you have the option of using nth-last-child(n) to dynamically adjust the width based on the specific label:

$(function() {
   var width = $('li:nth-last-child(1) label').width(); // Targeting only the last label
   $('label').width(width);
});

Answer №5

If you're familiar with jquery, the following code will work:

$(document).ready(function(){
    var width = 0;
    $(".labelWidth").each(function () {
        if ($(this).width() > width) {
            width = $(this).width();
        }
    });
    $(".labelWidth").css("width", width+"px");
});

Prior to executing the code, ensure that the display of the labels is set to:

display: inline-block;

To keep the labels on new lines, add the following CSS:

float:left; 
clear: both;

With jquery: https://jsfiddle.net/Conner160/aztbgo37

Without jquery: https://jsfiddle.net/Conner160/sacfszy4/

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

Ideas for displaying or hiding columns, organizing rows, and keeping headers fixed in a vast data table using jQuery

My data table is massive, filled with an abundance of information. Firstly, I am looking to implement show/hide columns functionality. However, as the number of columns exceeds 10-12, the performance significantly decreases. To address this issue, I have ...

What is the best way for me to insert a paragraph between two lists?

Is it possible to insert a paragraph in between lists even though it's not allowed? Here is an example of the code: <ol> <li>One</li> <p>Paragraph 1</p> <li>Two</li> <p>Paragraph 2</p ...

What is the role of z-index in relation to floating elements?

Why is it impossible to assign a z-index to a floated image and what are the reasons behind this limitation? ...

I need help picking out specific elements from this messy HTML code using XPath and lxml - any ideas on how to do

I am looking to extract specific strings from this HTML document using only lxml and clever XPath. The content of the strings may vary, but the structure of the surrounding HTML will remain constant. The strings I need are: 19/11/2010 AAAAAA/01 Normal U ...

Guidelines on transforming XML files into HTML tables using C++ programming language

Is there a C++ library available that can help me convert an XML file into an HTML table using my C++ application? For example: <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <d ...

Seamless flow from one image to the next

I'm working on a project with a slideshow, but I've noticed that when it transitions between images, it can be quite abrupt. I'd like to find a way for it to change smoothly from one image to the next. <div> <img class="mySli ...

Is there a way to stop these symbols (♈♉♊♋♌♍♎♏♐♑♒♓) from being substituted with emojis?

I am currently working on a project involving the simulation of the Prague Astronomical Clock (). My goal is to display the glyphs representing signs of the zodiac, which are in the Unicode range U+263C-2653 along with other astronomical symbols. My prefe ...

Sorting of boxes is not possible when utilizing the sortable() function

Is there a way to swap the positions of the left and right boxes using the sortable() function from jQuery UI? It seems to work fine for numbers 1 and 2, but not for switching the boxes. $(".sort-me").sortable({ connectWith: ".connectedSortable" } ...

The second tab on Bootstrap5's tab content feature seems to generate an endless amount of white

I am working on a project where I need to display statistics for both the current month and year. To organize this data, I created a card with tabs for each - one tab for the month and another for the year. By default, the month tab is loaded first. Howev ...

Canvas displaying inaccurately (unsteady)

While working on a simple HTML5/javascript game, I encountered an unusual issue with the canvas drawings - they seem to be unstable. I have a map that needs to be drawn in a specific way: The background consists of 2 layers: the ocean and the islands. Th ...

What is the best way to add padding to each line within a block of text containing multiple lines

My <span> tag has a background color and left and right padding applied to it. However, the padding is only visible at the beginning and end of the <span>, not on each line when the text wraps onto multiple lines. Is there a way to add padding ...

Activate Bootstrap button functionality with JavaScript

Hey team, I've got a Bootstrap button on my HTML page: ..... <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> ... <div class="accept-btn"> <button type="button" class="bt ...

JSON Novice - persistently storing data in JSON during browser refreshes

My AJAX poll was set up following a guide found here: http://www.w3schools.com/php/php_ajax_poll.asp Instead of storing the poll entries from an HTML radio button in an array within a text file as demonstrated in the tutorial, I wanted to save them in a J ...

Infinite scrolling made effortless with jQuery and Ajax

I am attempting to create a basic infinite scroll feature that monitors when the user scrolls to the bottom in an HTML file. Once the bottom is reached, it should then load additional content from another HTML file which contains more text. The second HTM ...

Exploring Hover Effects in Reactjs and Material UI

I've been working with reactjs and material ui, but I'm having trouble changing the background color of selected items in various components, like in the SelectField. <SelectField floatingLabelText="Choose a sport" value={this.state.val ...

A DIV element may not have any visible height, even when it contains content

My <div> contains multiple <img> elements, each wrapped in its own inner <div>. Despite setting the outer div's height to auto, it fails to adjust dynamically based on the content. To display the inner divs inline, I've applied ...

Using percentages for sizing and margins in CSS

Looking to create a visually appealing page that always fills the entire screen? Check out this code snippet that does just that: #posts{ width:100%; height:auto; background:#666; } .entry{ float:left; margin-left: 4%; margin-top:10 ...

jQuery prioritizes enforcing style over using a CSS file

I am facing an issue with a nested div functioning similar to a drop-down menu. The problem arises when I click on the #two div as I want it to disappear. To handle this, I utilized jQuery and tried using both .hide() and .slideUp(). However, upon implem ...

Is there a way to repurpose a JavaScript object that gets sent back to the client upon page loading?

Upon initial page load, an AJAX request fetches a list of JavaScript objects which display only one value each. The page includes buttons to expand each item and reveal the remaining values. Originally, I used another AJAX call to retrieve individual objec ...

When you click the button, the page refreshes with no content

Every time I click on a button, an empty page reloads after invoking the button's onClick event. The code snippet on the page is as follows: <html> <head></head> <body>2015</body> </html> I am ...