选择某个父元素的多个子元素

Selecting multiple child elements of a certain parent element

我只是想知道 CSS 是否可以支持使用更简单的语法定位多个子元素。

例如

我只想更改 div 下的 h1、h2、h3 的 css,id 为 parent

通常你必须去...

#parent h1, #parent h2, #parent h3 { color:red; }

有没有更简单的方法来做到这一点。像...

#parent {
  h1,h2,h3 { color:red; }

  .my_class { font-size:100%; } 

  #header { width:100%; }
}

在我的第二个假设示例中,css 仅在元素是#parent 的子元素时应用。

谢谢

CSS不支持你想要的。

两者 Sass and Less 都可以(偶然,与您写的完全一样)。

Sass:

Sass is the most mature, stable, and powerful professional grade CSS extension language in the world.

减去:

Less is a CSS pre-processor, meaning that it extends the CSS language, adding features that allow variables, mixins, functions and many other techniques that allow you to make CSS that is more maintainable, themable and extendable.

这两种产品都非常成熟,几乎共享彼此的所有功能,但它们扩展的内容之间确实存在一些细微差别,这使得它们无法 100% 相互兼容(在生成 CSS 之前) .

在 css 中你必须使用这样的东西:

#parent > h1, #parent > h2, #parent > h3
{
   color: red;
}

JSFIDLE:http://jsfiddle.net/fg6g7kt6/