使用类级联或显式将CSS应用于div中的所有元素?

时间:2014-07-01 17:08:57

标签: html css css3

好的,我刚刚遇到过这个问题:http://codepen.io/drygiel/pen/HBKyI

将css应用于div中所有带有类似类的元素:

.timeline {
    height: 100%;

    canvas {
        position: absolute;
        width: 100%;
    }
    figcaption {
        text-transform: uppercase;
    }
    h2 {
        color: #b2cde9;
    }
}

我以前做的是:

.timeline {
    height: 100%;
}

.timeline canvas {
    position: absolute;
    width: 100%;
}
.timeline figcaption {
    text-transform: uppercase;
}
.timeline h2 {
    color: #b2cde9;
}

这第一个选项是一个好主意/可能吗?它可以在旧浏览器中使用吗?

我只提到这个,因为我以前从未见过它,我已经用CSS编写了2年。

3 个答案:

答案 0 :(得分:2)

第一个例子是LESS,一个CSS预处理器。这不是有效的CSS本身,它需要“编译”成原始CSS。 Codepen正在为您做到这一点。通过编译LESS生成的实际CSS看起来几乎与第二个示例相同,但空格较少。

事实上,如果我们查看iframe的来源,我们可以确切地看到它是如何编译的,并且它几乎与您编写的内容相同:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body,
html {
  color: #2C2C2C;
  min-width: 600px;
  height: 100%;
  background: white;
  font: 400 1em 'Lato';
  -webkit-font-smoothing: antialiased;
}
#timeline {
  padding-top: 5%;
}
.timeline {
  height: 100%;
  position: relative;
}
.timeline canvas {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
}
.timeline figcaption {
  font: 900 190% 'Lato';
  text-transform: uppercase;
  -webkit-text-stroke: .25px;
}
.timeline h2 {
  font: 400 400% 'Raleway';
  padding-bottom: 100px;
  color: #b2cde9;
}
.timeline h6 {
  color: #0090F5;
  font: 400 80% Tahoma;
}
.timeline p,
.timeline ol {
  font: 400 95% 'Raleway';
  padding: 3px 0 20px 0;
  color: #575757;
  text-align: justify;
  width: 70%;
}
.timeline ol {
  list-style: disc;
  margin-top: -20px;
  padding-left: 40px;
}
.timeline figure {
  float: right;
  width: 100%;
}
.timeline article {
  position: relative;
  width: 38%;
  overflow: hidden;
  margin-bottom: 100px;
}
.timeline article:first-of-type {
  float: left;
  text-align: right;
}
.timeline article:first-of-type p,
.timeline article:first-of-type figure {
  float: right;
}
.timeline article:last-of-type {
  float: right;
}
.timeline article:last-of-type h2 {
  color: #c6e0aa;
}
.timeline article:last-of-type h6,
.timeline article:last-of-type a {
  color: #40aa00;
}
.timeline article:last-of-type a:hover {
  color: #95D40D;
}

LESSSASS是编写作者友好的CSS的两个常用选项,其中包含选择器嵌套(以及许多其他功能),这些选项可以编译为浏览器的vanilla CSS。

答案 1 :(得分:0)

LESS

LESS是Lean CSS,它是一个css预处理器,可以编译成CSS

你知道它在括号中提到。

答案 2 :(得分:0)

第一种方式可以使用Sass,它允许在CSS选择器中嵌套。像这个例子:

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  li { display: inline-block; }

  a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
  }
}

来源:http://sass-lang.com/guide