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

Retrieve the selected option from the dropdown menu in the specified form

What should I do if I have numerous products and want users to be able to add new dropdown boxes dynamically? When the submit button is clicked, only the value of "category[]" within the form should be retrieved. https://i.stack.imgur.com/v1fnd.png Below ...

Switching from HTML to PHP programming

Trying to build a website from scratch and incorporate a form has been challenging. My goal is for the Submit button to send an email without launching any email program, but I'm facing redirection issues. Despite searching on Google for solutions, I ...

Connecting to an element within a separate node (XSLT)

I have an XML document that includes a list of companies. My goal is to use XSLT to create links that point to the <link> child of the next company node. To provide a clearer picture, here is a snippet of sample XML data I am working with: <portf ...

"What might be causing the error 'cannot access property 'top' of undefined' while trying to read

My website has a sticky navbar fixed at the top, and this is the structure of my sticky navbar: $(function() { $(window).scroll(function() { if ($(window).scrollTop() > $(".b").offset().top + $(".b").height() && $("input").val() == "") { ...

Prevent clicks from passing through the transparent header-div onto bootstrap buttons

I have a webpage built with AngularJS and Bootstrap. It's currently in beta and available online in (German and): teacher.scool.cool simply click on "test anmelden" navigate to the next page using the menu This webpage features a fixed transparent ...

Designing a navigation system that revolves around a central logo image

I have created a navigation bar that you can see in this image: https://i.stack.imgur.com/eT4TL.jpg Using foundation to construct a website, I have designed the nav bar in the following manner: HTML: <nav class="top-bar"> <ul> ...

Is there a way to extract the content within the HTML tags as well as the text enclosed within parentheses?

When working with my HTML content, I came across the following line: <span>(48 עמודים סה"כ )</span> I am seeking a way to extract only the text "48 עמודים סה"כ" and store the number 48 into an integer variable. Although in t ...

Errors are not being shown on mandatory fields in the form

After watching a tutorial video, I attempted to create a sign-up form. However, despite following all the steps, I encountered an issue where the Surname and Given name fields, which I set as required fields, were still processing blank when the form was s ...

Separating the rules for development and production modes in the webpack configuration file

I'm currently in the process of working on a front-end project using HTML. Within my project, I have integrated the Webpack module bundler and am utilizing the image-webpack-loader package for image optimization. However, I've encountered an issu ...

Find the line containing the selected text within a JavaScript code

I am working on a contentEditable div where users can enter multi-line text. I need to be able to inspect the line that the user is currently typing in when they press enter. Is there a way to retrieve the context of that specific line (or all lines)? Is ...

Displaying content on the <div> element

Looking for recommendations for a jQuery plugin or JavaScript solution that allows me to load a full "view" into a <div> when a user clicks on a link. The challenge I'm facing is that I have 8 pages, with the Homepage consisting of 3 divisions: ...

Is there a file outside of the website's root that needs to be linked?

Hey there, I could use some assistance with a little issue I am having: Running the latest release of Ubuntu server 64bit with 2 virtual websites Here is my folder structure: site1 (Accessible on the web) /var/www/site1 site2 (Intranet site) /var/www ...

Automatically scraping Facebook API data with PHP

I'm completely new to php and I am looking for a way to automatically scrape metadata. I came across a solution, but I am unsure how to implement it in php. POST /?id={object-instance-id or object-url}&scrape=true Can anyone provide a sample php ...

JavaScript and AJAX: Dynamically Create URLs

I don't have any experience with web development, so could you please explain in detail? If my questions are unclear, feel free to ask for more information. Imagine I have a webpage with the following URL: www.mycompany.com/category1 This page has ...

Curved Edges Ensure Non-Interactive Elements

element, there is a query about utilizing a color wheel from a repository on GitHub. The goal is to disable any actions when clicking outside of the canvas border radius in this color wheel. Various steps need to be taken to prevent unwanted outcomes: - S ...

Issue with alignment in the multiselect filter of a React data grid

In React Data Grid, there is a issue where selecting multiple filter options messes up the column headers. Is there a solution to display selected filter options above a line in a dropdown rather than adding them to the column header? The column header siz ...

What are some ways to make source code more visually appealing on an HTML/JSP page as it is being

I've recently created a webpage with some Java source code, neatly organized within blocks. However, I'm looking to enhance its appearance so it truly looks like Java code. Take a look at my page's code here for reference: Are there any onl ...

Reasons for dividing by 1 in JavaScript

While completing a basic programming assignment from my teacher, I encountered an interesting issue in Javascript. I found that when dividing a number by 1, it returns an unexpected value. Can anyone provide an explanation for this? I have created a jsfidd ...

My navbar in bootstrap is having trouble staying aligned with the button and search box. What can I do to ensure everything stays in line?

<nav id="NavbarMaster" class="navbar NavCompression"> <!-- style="min-height:100px;" --> <div id="navToggler" class="container-fluid TitleBG TitleHeight"> <div id="navopener" class="Pointer" style="position:fixed;top:0;left:0; ...

What is the best way to reveal CSS files downloaded via npm?

I'm currently working on a Sass/CSS library that I have hosted on npm. I am exploring ways to make it easier for clients to access it, such as using: @import 'library_name' Instead of the less appealing option: @import 'node-modules/ ...