Styling with CSS based on attribute values falling within a specific range

I developed a calendar system where each day is represented as a list item. Within each item, there is a span element with a data-count attribute that indicates the availability of a specific resource, which is updated dynamically. My goal is to change the background color of the span based on the value of this attribute. Instead of handling each value individually, I am looking for a way to manage it in ranges. For example, if the data-count falls within [0, 999], the background should be green; for [1000, 9999], it should be orange; and for values greater than or equal to 10000, it should be red.

Is there a CSS-only solution to accomplish this without the need for additional JavaScript or HTML modifications? If so, how can I implement it? Also, is there a specific CSS selector that can target all spans with a data-count within the range of [0, 999]?

Below is the current styling being used:

li { 
    list-style-type: none;
    display: inline-block;
    width: 18%;
    text-align: left;
    margin-bottom: 5px;
    background: #bbb;
    padding: 5px;
}

li span {
    display: inline-block;
    padding: 5px;
    width: 90%;
}

li span[data-count="50"] {
    background: lime;
}
li span[data-count="5001"] {
    background: orange;
}
li span[data-count="50000"] {
    background: red;
}

The HTML structure is as follows:

<ul>
    <li><span data-count="50" >1/1/70 - free</span></li><br>
    <li><span data-count="51" >2/1/70 - free</span></li><br>
    <li><span data-count="5001">3/1/70 - hurry up</span></li><br>
    <li><span data-count="50000">4/1/70 - last chance</span></li>
</ul>

Answer №1

One suggestion is to utilize CSS variables to replace the data value (similar to this solution)

li { 
    list-style-type: none;
    display: inline-block;
    width: 18%;
    text-align: left;
    margin-bottom: 5px;
    background: #bbb;
    padding: 5px;
}

li span {
    display: inline-block;
    padding: 5px;
    width: 90%;
    background:
      /* from 10000 to infinity */
      linear-gradient(red,red)      0 /100% calc((var(--count) - 9999)*1px),
      /* from 1000 to 9999*/
      linear-gradient(orange,orange)0 /100% calc((var(--count) - 999)*1px),
      /* from 0 to 999*/
      green; 
}
<ul>
    <li><span style="--count:50" >1/1/70 - free</span></li>
    <li><span style="--count:51" >2/1/70 - free</span></li>
    <li><span style="--count:5001">3/1/70 - hurry up</span></li>
    <li><span style="--count:50000">4/1/70 - last chance</span></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

How to align a tooltip in the center horizontally below its parent element without specifying a fixed width

Is it possible to center a tooltip horizontally under its parent without setting a static width on the tooltip? I am facing this issue because the length of the text in the tooltip varies. Below is the code snippet for reference. Even though adjusting the ...

Designs created with shapes using HTML, CSS, and JavaScript

I've been given a challenge to reproduce the image shown below using only HTML, CSS, and JS - without utilizing any image files. Initially, I thought about creating the shapes with HTML elements and CSS, but I realize this might not be the best approa ...

Update the jQuery Mobile components on the page by refreshing them after dynamically adding them using AJAX and PHP

I have developed a PHP script that dynamically renders three form elements for each entry in a database. The script generates HTML output similar to the following example: <!-- Entry 1 --> <div data-role="fieldcontain"> < ...

How to achieve smooth transitions in CSS3 with dynamic height changes?

Currently, I am in the process of designing a unique layout. The main challenge lies in toggling between two classes for a div element, one of which has a height of 0px. To achieve this, I have successfully utilized CSS3 animations that effectively scale t ...

What are some methods to manipulate the appearance of text with jquery?

I am having trouble with jQuery and it seems like it's not working properly. Can anyone help me locate the error? My aim is to create a "Read less" button that will show more content when clicked. However, when I click on "Read More", nothing happens. ...

The CSS I've written isn't functioning properly within a div that was generated using

I have been trying to populate a div with data using JSON, but I am facing an issue where my div, which is set with JavaScript for each JSON element, does not seem to be working correctly. The classes "lulu" and "lora" are not being recognized. Apologies f ...

Update Button Colour upon clicking the Button

I have multiple buttons lined up in a row on my webpage. I want the button's color to change when I click on them. Below is a snippet of my code: $( "button.change" ).click(function() { $(this).toggleClass( "selected" ); }); .Button { font-fa ...

Change the button icon to something new when you hover over it

Below is the liquid code I am working with: <a href="{{ section.settings.button_link }}" class="btn--rounded"> {% include 'icon-btn--rounded' %} <div class="link-label">{{ section.settings.button_ ...

Navigating with a Stuck Navigation Bar Using BootstrapretaF

My website has a sticky navbar with a dropdown menu that allows users to jump to different sections of the page. However, I've noticed that when I navigate to a new section, the navbar covers part of the content at the top. After inspecting the navbar ...

Why is the col-1 in Bootstrap stacking up instead of flowing horizontally as intended?

Check out my HTML/CSS code below: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <d ...

Toggler for the Bootstrap navbar

Recently, I've been experimenting with Bootstrap for the first time and trying to understand its functionalities. While I've managed to make some progress with navbars, I've encountered a roadblock when it comes to the navbar-toggler. <n ...

Issue encountered while using Material-UI makeStyles: unable to access property 'down' due to being undefined

I'm currently in the process of developing my own website using Material-UI and I'm facing an issue with making it responsive. My goal is to hide an image on smaller screens by utilizing [theme.breakpoints.down('md')]. However, I keep e ...

CSS rule for elements in a disabled state

Currently, I am utilizing AngularJS to manage the enabling and disabling of image buttons. Despite my attempts to use a CSS selector to display them as transparent once disabled, it is not working as expected. While testing with a provided example that app ...

How can I remove the bouncing effect of the top bar in Chrome when scrolling in Three

There is a frustrating problem I am encountering on my website with the joystick control. Whenever I try to move my joystick down, Chrome triggers either the refresh dropdown or the top bar bounces and goes in and out of fullscreen mode. ...

Ensuring the height of the body and content div in Bootstrap 4 combined with Angular set to

Is there a way to center the <div class="card"> in the middle of the page or body? It works perfectly on a desktop browser, but on mobile view, it's not aligning properly. How can I center it using... html, body { height: 100%; background: ...

Repairing the base navigation interface and correcting the hyperlink to redirect to a different webpage

Can anyone assist with aligning my bottom navigation bar and fixing a link issue on both navigation bars? I've been struggling to center it. Do you think using padding could solve the problem? I've tried guessing the pixel count, but to no avail ...

Utilizing the Card-columns feature in Bootstrap

One issue I am currently facing is the way my generated cards appear in the card-columns div: while($row = $categorias->fetch_assoc()) { $imagen = $row["Imagen"]; $categoria = $row["Categoria"]; echo "<div class='card'>"; echo " ...

What is the best way to ensure that the text input spans the entire width of the div in Bootstrap 4?

I am currently exploring the implementation of Bootstrap 4. I have a basic div that includes a text box, and my goal is for the text box to occupy the full width of the div. Initially, I assumed that in Bootstrap, input elements automatically inherit the f ...

The CSS styles for a:link, a:active, and a:visited do not seem to work with

My page contains the following CSS rule: a:link,a:active,a:visited{text-decoration:none;} I have encountered an issue where this rule is not working as expected. To apply the rule within a div with the id="test", I had to modify the rule like this: #tes ...

Aligning table data elements within a division

Currently working on revamping a tech night website for my school. I made some changes to the navbar, switching it from the side to the top. However, I'm struggling to get the table data and menu to center properly within the div. If you'd like ...