Groupings of CSS style declarations

I am currently learning CSS and I have a question that may seem basic or silly, but I can't seem to find the answer anywhere. Here is the CSS code I am working with:

#table-of-contents ol {
  list-style-type: none;
  counter-reset: item;
  margin: 0;
  padding: 0;
}

#table-of-contents ol > li {
  display: table;
  counter-increment: item;
  margin-bottom: 0.6em;
}

#table-of-contents ol > li:before {
  content: counters(item, ".") ". ";
  display: table-cell;
  padding-right: 0.6em;    
}
#table-of-contents li ol > li {
  margin: 0;
}

#table-of-contents li ol > li:before {
  content: counters(item, ".") "  ";
}

My question is, is there a way to simplify this code by writing the #table-of-contents selector only once, similar to the following:

#table-of-contents {
    ol {
      list-style-type: none;
      counter-reset: item;
      margin: 0;
      padding: 0;
    }

    ol > li {
      display: table;
      counter-increment: item;
      margin-bottom: 0.6em;
    }

    ol > li:before {
      content: counters(item, ".") ".  ";
      display: table-cell;
      padding-right: 0.6em;    
    }
    li ol > li {
      margin: 0;
    }

    li ol > li:before {
      content: counters(item, ".") "  ";
    }
}

Is it possible to achieve something like this in CSS?

Answer №1

This new feature, known as CSS Nesting, is currently under development and has not been officially implemented yet. However, it can already be utilized in CSS preprocessor languages like Sass, Less, and Stylus. It may be worth exploring these options for now.

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

What is the best way to show a dropdown menu on the right side of the

I developed a demo that functions properly on both desktop and mobile devices. However, I am facing an issue where the dropdown menu is not displayed when switching to the mobile version. Here is my code snippet: http://codepen.io/anon/pen/OyNLqa ...

Clicking two times changes the background

I am facing an issue with three boxes on my website. Each box is associated with a corresponding div. When I click on any box, the div displays and the background color turns red. However, when I click on the same box again, the div disappears but the back ...

How to easily make a table in the center of the page using

I have a collection of 20 icons that I would like to arrange in a centered horizontal line of images. However, I am looking for a responsive solution and cannot use an HTML table. It needs to be mobile-friendly and wrap properly for small screens. If I wer ...

Ways to condense a text by using dots in the middle portion of it

How can I dynamically shorten the text within a container that varies in width? The goal is to replace the strings between the first and last words with dots so that it fits within the container. For example, Sydney - ... - Quito. It should only replace wh ...

Can you guide me on implementing a transition animation through class toggling?

I am facing an issue where certain elements need to be highlighted or unhighlighted conditionally. To achieve this, I have assigned a class called highlight to the elements that require highlighting with a transition animation. The challenge arises when t ...

Looking for consistent vertical and horizontal scrolling behavior in a React project

I am in need of assistance as I lack the necessary expertise in ReactJs to transform this design. Currently, it is written in Jquery but I would like guidance on how to recreate it in ReactJs. Specifically, I want the left div with the topic for each row t ...

The placement of blocks, the positioning of text, and the underlining of links

Attempting to implement pagination on one of my websites has posed a challenge for me due to my average CSS skills. You can view the issue I'm facing in this example: http://jsfiddle.net/rmurzea/qE7Ku/3/ 1). I had to add the margin-bottom property t ...

the deactivation of my rollover class is being caused by my float class

With the assistance of a generous contributor on stackoverflow, I was able to overlay different colored CSS boxes onto various images. These boxes could be removed upon mouseover, revealing the images beneath. You can view the code I used on fiddle here. ...

Incorporate a span element within the PHP code located in the final div

Having trouble with adding a span in PHP The last div is not functioning correctly. **In need of help on how to insert a span within PHP code.** echo" <tr> <td>$id</td> <td>$name</td> <td>$la ...

Is there a way to ensure that the content within a transparent div is completely visible?

Can this be done? If not, does anyone have any creative workarounds? This demonstration will clearly illustrate the issue I am facing http://jsfiddle.net/9AWdz/ ...

What are the best ways to incorporate mistakes into my JavaScript mortgage calculator?

I am struggling to set up my calculator so that it triggers an error message if the APR goes over 25% or falls below 0. Also, the Loan Term should be greater than 0 and less than or equal to 40, otherwise display an error message. I have attempted differen ...

CSS Transition - Troubleshooting my malfunctioning code

I seem to be facing a simple issue with my code. The following snippet does not work in Chrome or Firefox, as the transition property is not working properly. Instead of smoothly transitioning from blue to red when hovered over, the element immediately swi ...

Changing in height by utilizing javascript's style.height animation

When attempting to adjust the height property using JavaScript instead of jQuery, a challenge arises. The issue lies in the inability to reset the height back to zero after setting it to the scrollHeight of the element. The following is the JavaScript cod ...

The CSS background design is experiencing issues in IE9

Check out this code snippet I have for customizing the background of a webpage: body { font-family: "HelveticaNeue", Helvetica, Arial, sans-serif; font-size: 12px; background: -webkit-radial-gradient(50% 20%, circle farthest-side, transparent ...

jQuery insert custom text into HTML and eliminate malfunctioning elements

Using jQuery, I have created a form where the entered text is appended to certain elements. This functionality works correctly. Additionally, I have included a remove link with each added element. When someone clicks on the remove button, it should remove ...

Pictures fail to load on Safari browser for desktop and iPhone, yet display correctly on various other smartphones and Windows browsers

Having trouble with images not displaying on Safari browsers for Mac and iPhones, but they load fine on Windows and Android. Even after updating to the latest version of Bootstrap, the issue persists. Here are the current Bootstrap content delivery networ ...

Reduce the text of the link

Trying to simplify a task, but I'm struggling with the solution. What I want to achieve is shortening a link to 30 characters and adding ... at the end if it's longer. Also, I'd like to make it possible to see the full link on hover similar ...

When creating a PDF from HTML using iText, the CSS styling is not applied

Even though I am able to generate a PDF from an HTML string, the issue is that it doesn't apply the CSS styles. How can I create a PDF with CSS styling? Please assist! I have already attempted using CSSResolver. Below is my code snippet: {String res ...

Is it feasible to place an image on top of a box?

Hello everyone, I have been trying to figure out how to achieve a specific layout in React. I am using MUI so there is no need for any hard-coded CSS. However, my attempts have not been successful so far. The layout I am aiming for consists of an image on ...

What is the best way to connect or link to a HTML table row <tr>

Is there a way to trigger an alert when the user double clicks on the whole row? ...