CSS 组样式定义

标签 css

我正在学习 CSS,所以这可能是一个非常基本/愚蠢的问题,但我无法在任何地方找到它,抱歉。我有以下 CSS 代码:

#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, ".") " ";
}

我想知道是否有任何方法可以只写一次#table-of-contents,比如

#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, ".") " ";
    }
}

可以做这样的事情吗?

最佳答案

它的名字是 CSS Nesting正在开发中,尚未正式发布。但它可以在 Sass、Less、Stylus 等 CSS 预处理语言中使用。您可以考虑使用它们。

关于CSS 组样式定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66896392/

相关文章:

javascript - Bootstrap 轮播 : how to know which slider it is when carousel is paused

javascript - JQuery 在 IE 和 Chrome 中的工作方式不同

angular - 使用 CSS 在 angular material 2 元素内按 Id 或 Class 定位特定元素样式

javascript - 使用同位素javascript插件,我怎样才能摆脱差距?

python - 如何使用自定义标记颜色将每个句子包装在标签中?

html - aspx页面对齐问题

javascript - 有没有办法不通过点击关闭antd下拉菜单?

JQuery 将文本更改为标题

javascript - 为什么我不能切换汉堡菜单的显示

css - 绕 y 轴旋转图像