Utilizing Cascading Style Sheets for customizing a numbered list

I am looking to create HTML that will generate the following output:

OL {
    counter-reset: numeric
}
OL LI OL LI OL {
    counter-reset: latin
}
LI {
    display: block
}
LI:before {
    content: counters(numeric, ".") " "; 
    counter-increment: numeric
}
OL LI OL LI OL LI:before {
    content: "(" counter(latin, lower-latin) ") "; 
    counter-increment: latin
}

Using HTML like this:

xxx
<ol>
    <li>one
        <ol>
            <li>onedotone</li>
            <li>onedottwo
                <ol>
                    <li>A</li>
                </ol>
            </li>
        </ol>
    <li>two</li>
</ol>

Yields the result:

padding-left does not solve the alignment issue as desired.

1 one
1.1 onedotone

Instead of:

1.     one
1.1    onedotone

Given these challenges, I may need to resort to using a table unless anyone can provide expert guidance on a pure CSS solution.

Answer №1

Here is an example illustrating how to achieve the desired outcome using <ol> (ordered lists) and CSS with counters. The method may seem a bit unconventional due to the need for proper alignment when lines wrap around, but it proves to be more efficient than manual numbering or table usage.

To ensure consistent spacing between the numbers and text, set a specific width for the li:before pseudo-element and style it as display: inline-block. Adjust the width, margin-left, and

padding-left</code values accordingly for optimal styling.</p>

<p>CSS Counters enjoy decent <a href="http://caniuse.com/#search=counter" rel="nofollow"><strong>browser support</strong></a>.</p>

<p><div>
<div>
<pre class="snippet-code-css lang-css"><code>.level1, .level2, .level3 {
    list-style-type: none;
}
.level1 {
    counter-reset: level1; /* initializing first-level counter */
}
.level2 {
    counter-reset: level2; /* initializing second-level counter */
}
.level3 {
    counter-reset: level3; /* initializing third-level counter */
}
li {
    display: block;
}
li:not(:last-child):after, li > ol:before{
    content: " ";
    display: block;
    position: relative;
    height: 20px; /* placeholder for required line height */
    left: 0px; top: 100%;
}
.level1, .level2, .level3 {
    margin: 0;
    padding: 0;
}
.level1 > li, .level3 > li {
    padding-left: 40px;
}
li:before {
    margin-left: -40px;
    width: 40px;
    display: inline-block;
}
.level1 > li{
    font-weight: bold;
}
.level1 > li:before, .level1 > li * {
    font-weight: normal;
}
.level1 > li:before {
    content: counter(level1)"."; /* display current item number + dot */
    counter-increment: level1; 
}
.level2 > li:before {
    content: counter(level1)"." counter(level2); /* format level 1 counter + dot + level 2 counter */
    counter-increment: level2;
}
.level3 > li:before {
    content: "(" counter(level3, lower-latin)") "; /* format ( + level3 counter + ) */
    counter-increment: level3;
}
<ol class='level1'>
    <li>one
        <ol class='level2'>
            <li>one dot one - lengthy text that wraps around.</li>
            <li>one dot two
                <ol class='level3'>
                    <li>A</li>
                    <li>B - lengthy text wrapping onto next line.</li>
                </ol>
            </li>
        </ol>
    </li>
    <li>two - lengthy text wrapping to next line.</li>
</ol>

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

Including an image side by side with clickable text within a list

Check out my progress here: http://jsfiddle.net/dDK6z/ I've experimented with using display:inline(-block), as well as adjusting margins, padding, and more, but I can't seem to get the image to move down or the text to move up. Does anyone have ...

Exploring and deciphering the intricacies of the underlying technology

Apologies if this question seems misplaced, but as a complete beginner, I'm a bit lost. Let's consider this website as an example: I'm trying to uncover the libraries and technologies employed in the background to learn more and replicate ...

When you hover the cursor over it, the material-ui icon button shines with an elliptical background effect

There seems to be a strange issue with the IconButton in @material-ui/core/IconButton that is causing a weird elliptical background to appear when hovering over it. https://i.stack.imgur.com/Xof8H.png Even after copying the code directly from the materia ...

Mastering the art of utilizing Angular Routing alongside CSS grid for seamless website navigation

My <app-root> layout is pretty straightforward: app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.compone ...

Attempting to establish a means to switch between languages

Currently, I am in the process of implementing a language switch feature for a website project that I am currently working on. This feature will allow users to seamlessly switch between English, Latvian, and Norwegian languages. To achieve this functionali ...

Conceal the galleryView plugin seamlessly without the need for a

I have implemented jQuery on my webpage to load content without refreshing, displaying only the necessary information. Within the gallery section, there are links for "Other Photography" and "Nude Art". Clicking on these links initializes the gallery with ...

When the sidebar is set to position: fixed, the icons magically vanish

This CSS is specifically for the side navigation bar. .side-nav { // position: sticky; top: 0; left: 0; height: 100vh; z-index: 100; width: calc(3.5vw + 3.5vh); // position: fixed; &.content { height: 100%; display: flex; fl ...

Show a notification on the screen if the source of the iframe is blank

Is there a way to show a message "Please click on a match to get started" when no match link has been clicked? For example, when the iframe does not have an src attribute. My backend is built on Django. Below is the HTML code snippet: </form><b ...

Utilizing unique background images tailored to different screen resolutions

In order to enhance the user experience on my website, I am looking to implement a feature that dynamically changes the background images based on the user's screen resolution. My plan is to use a small snippet of JavaScript within the <head> s ...

How float elements can affect z-index through opacity

While troubleshooting the alignment of my floated images, I came across an unexpected issue related to opacity: http://jsfiddle.net/tshwbnLo/ It appears that when an element has opacity, it does not adhere to the usual behavior and instead overlaps the f ...

Can I access properties from the index.html file within the Vue.js mount element?

<!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="widt ...

Increasing the font size on a document

Once again, faced with my own silly problems. This time I stumbled upon the following page: http://jsfiddle.net/g3VBT/ I am trying to style the "enter your U.R.L here" text with the following properties: font-weight: 600; font-size: 1.5em; The issue ...

Is it possible to adjust the width of the comment box on the Facebook "Like" button?

Is there a way to set the width of the comment box that appears after clicking the Facebook "Like" button? I know how to adjust the width of the button itself and related content, but can't find any options for the comment box: I've tried overri ...

Tips for transforming a scroll element into the viewport using Angular 2+

This is a sample Here is a component with a list of items: class HomeComponent { text = 'foo'; testObject = {fieldFirst:'foo'}; itemList = [ '1', '2', '3', & ...

Just finished my first webpage - can't seem to get the footer to stay put!

After spending 3-4 months learning HTML and CSS, I am now working on my first web page. However, I'm facing an issue where the footer is stuck to the slideshow and I can't seem to figure out how to position it at the bottom of the page. Any tips ...

Stop flex child from shrinking in size

I have a form row with two inputs. There is a requirement to show ⚠️ (warning emoji) next to the second input in the row, based on the input number. However, because of how flexbox behaves, the input size gets reduced... How can I maintain the input s ...

Iterating through data to showcase vertical columns for a specific time span of 3 years, highlighting only the months with available data

If data has been available for every month over the past 3 years, I need it to be displayed in a specific format. You can refer to this example fiddle for reference: https://jsfiddle.net/bthorn/ncqn0jwy/ However, there are times when certain months may no ...

Tips for keeping the background image in place while resizing the page

How can I set the background image for the whole page without it changing position with resizing? The image is 3065px in height so that should not be the issue. .bg { background-image: url("#"); height: 3065px; width: 100%; background-positio ...

The appearance of my sidebar items' right border varies depending on the web browser being used

My sidebar item's right border appears differently in Mozilla and Chrome. How can I resolve this issue? Here are the images from both browsers: https://i.stack.imgur.com/YGkkz.png https://i.stack.imgur.com/JxNs3.png "use client"; import { ...

Why is the image not disappearing even after using JavaScript to do so?

I'm having an issue with my JavaScript code that is supposed to make an image disappear when the data-slide-to attribute is equal to 1. However, the image remains visible even though the code seems correct. Can anyone help me understand why it's ...