CSS style for formatting content attribute

I have a list in numerical order where I utilize the counter feature within the content.

Query:

Is there a way to change the color of the numbers only (e.g. 1.2, 1.3, ...) to red using CSS without making any changes to the HTML code?

Note: Only seeking a solution using CSS (no need to alter the HTML structure!)

HTML:

<ol>
        <li>Item
            <ol>
                <li>Item</li>
                <li>Item
                    <ol>
                        <li>Item</li>
                        <li>Item</li>
                        <li>Item</li>
                    </ol>
                </li>
                <li>Item</li>
            </ol>
        </li>
        <li>Item</li>
        <li>Item</li>
    </ol>

CSS:

ol {
    list-style-type: none;
    counter-reset: item;
}
ol li:before {
    content: counters(item, ".")". ";
    counter-increment: item
}

Desired outcome:

Answer №1

If you want to apply this style to all numbers, just include color:red; in your css for the numbers like so

ol li:before {
    content: counters(item, ".")". ";
    counter-increment: item;
    color:red;
}

http://jsfiddle.net/ZSkLE/3/

If you prefer to apply this style only to lower levels (as it seemed from your original question) you can try this approach.

ol li ol li:before {
    color:red;
}

To prevent the next level from being red, you can use the following code:

ol li ol li ol li:before {
    color:black;
}

http://jsfiddle.net/ZSkLE/1/

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

Why isn't the document.getElementById().value() function functioning properly within my PHP code?

When working on my PHP code, I included the following: <select class="ht__select" onchange="sorting('<?php echo $id ?>')" id="sorting"> <option value="">Default sorting</option> <option value="low_price">Sor ...

Shrink Table Column Size with Bootstrap and Stylus to Optimize Space Usage

Currently experimenting with Bootstrap to ensure my table is responsive on a search list I am developing. However, I am facing an issue where two of my columns are taking up more space than necessary. In the image below, you can see that Date and Link colu ...

Ways to verify if a div is empty following an ajax request

When trying to load a php page into a div, I encountered an issue where the content of the div appeared empty. Despite attempting various methods to check if the div contained any html content after loading, I was unable to find a solution. ...

What crucial element is absent from my array.map function?

I have successfully implemented a table with v-for in my code (snippet provided). However, I am now trying to use Array.map to map one array to another. My goal is to display colors instead of numbers in the first column labeled as networkTeam.source. I at ...

Is there a way to modify the appearance of a text box to suit individual preferences?

Is it possible to alter the shape of a text box, creating a rectangle with a portion removed from one side? I have included an example image for reference. https://i.sstatic.net/oNyum.png I came across clip-path: polygon in CSS, but it seems to only accep ...

Unusual layout issues arise when combining images and text

Check out this jsFiddle My goal is to display images and text side by side in a horizontal layout. The text sections are using the jQuery Cycle Lite Plugin to cycle through a list of words. However, when you view the provided jsFiddle link, you'll no ...

Maintain user input within the table following a page refresh

I have been working on developing a table that allows users to edit it. I successfully created a script that adds comments to the table, but unfortunately, these comments disappear when the page is refreshed. I understand that I may need some additional to ...

Implementing dynamic class changes with *ngIf within a child component

Apologies if this question has been addressed before, I found two similar answers here: Question related | Close Solution An Angular Material Sidenav component contains three components. The parent component (highlighted in red) includes tab-like but ...

The JavaScript script to retrieve the background color is malfunctioning

I am currently working on developing a highlighting feature for an HTML table that will dynamically change the row colors on mouseover. Below is the code snippet I have been using, but it seems to be experiencing some issues. Any assistance would be greatl ...

What is the reason for the leftward shift of the buttons?

Currently following a Udemy tutorial on Node, React, and Express. The instructor is using a Header component in his code that shifts the Login and Cart links to the far right. I have implemented the same code as instructed, but my links are not aligning t ...

Side Panel Extending Off Screen Upon Resizing

I'm encountering an issue with the sidebar on my HTML page. The problem arises when I click the 'Upload Data' button, causing the sidebar's header to extend beyond the screen if I resize the window. The only way to fix this is by refres ...

Using jQuery and JSON: selecting classes based on associated JSON strings

If you want to see the complete codepen, I will share it here: http://codepen.io/JTBennett/pen/NdLWJy Let me try to simplify the problem. I have a click event that selects from a JSON object based on one of its string values. Text from various strings is ...

Choosing a language - Sending dropdown selection from HTML5 to PHP

Is there a way to store the selected language from an HTML5 dropdown menu into a variable for later use in PHP? Currently, I have hardcoded the initial value of the variable in the code itself. I want to pass the selected language dynamically from HTML t ...

Understanding fluid design concept

Check out this example. I've noticed that when resizing the viewport, the font size within the .main class increases while there is no change in the .aside class. Can someone help shed light on this for me? Thank you in advance! ...

css: varying container heights for columns in Chrome and Safari using Bootstrap 4

In my code snippet below (this is just a prototype, in the actual application everything is different but follows a similar structure): <body> <div class="row"> <div class="col-3 d-none d-sm-block"> <div class="h-1 ...

When clicking, the drop-down feature is malfunctioning as all the menus are dropping down simultaneously

I have been trying to implement a dropdown on click feature for my website, but it is not functioning as intended. () When I click on button 1, button 2 also drops down unexpectedly. This behavior is not desired. Below is the code snippet from the page: ...

Merging Angular with jQuery: Organizing jQuery into a distinct file leads to the error message 'Property 'top' cannot be read because it is undefined.'

My project is based on AngularJS, and I've been trying to incorporate a jQuery code for a sticky sidebar feature. Strangely enough, when I tested my code on Plunkr, it works perfectly fine. However, when I try to implement it in my actual project, it ...

Using Custom .woff Format Fonts in Sendgrid: A Guide

Having trouble with implementing custom fonts in Sendgrid. While Google fonts work fine, the custom .woff format font doesn't seem to cooperate. I've attempted three solutions below. Solution number 1 shows up in the Preview tab but not in the em ...

When the content in one box exceeds the content in the other, one box is considered to be overfilled

For the image, click here: I am facing an issue with boxes stacking on top of each other. When one box has more content, it overlaps the other. CSS: #destaques_container{ margin: 0 auto; } #destaques_container_dentro{ float: left; margi ...

Changing the text color of an active button in Bootstrap

Hello everyone, I've been struggling to find a solution to my issue and I could really use some help. The problem I'm facing is changing the text color of the active button in Bootstrap. Specifically, after clicking on a button, I want it to tra ...