cnTIL

https://lovelacelee.github.io/cnTIL

This project is maintained by lovelacelee

CSS值

在MDN上,值的表示将被尖括号包围,如<color><length>。当您看到值<color>对特定属性有效时,这意味着您可以使用任何有效的颜色作为该属性的值,如 <color>参考页面所列。

数字,长度和百分比

颜色

<color>颜色关键字

十六进制RGB值

RGB和RGBA值

HSL和HSLA值

在颜色上设置alpha通道与使用我们前面看到的opacity属性有一个关键区别。当你使用不透明度时,你让元素和它里面的所有东西都不透明,而使用RGBA颜色只让你指定的颜色不透明。

图片

<image> 数据类型用于图像为有效值的任何地方。它可以是一个通过 url()函数指向的实际图像文件,也可以是一个渐变。

url(test.jpg)                          url()方法, 只要test.jpg是图像文件
linear-gradient(to bottom, blue, red)  一个 <gradient>标签
element(#colonne3)                     页面的一部分, 使用了element()方法, colonne3 是存在于页面中的一个元素id即可

位置

position表示一组2D坐标,它可以使用关键字(如 top, left, bottom, right, 以及center )将元素与2D框的特定边界对齐,以及表示框的顶部和左侧边缘偏移量的长度。

/* 1-value syntax */
keyword                  /* The corresponding edge, the other direction is corresponding to center, the middle of the edge */
<length> or <percentage> /* The position on the x-axis, 50% for the y-axis */

/* 2-value syntax */
keyword keyword          /* A keyword for each direction, the order is irrelevant */
keyword value            /* The value is the offset for the edge defined by the keyword */

字符串和标识符

函数

css函数是一段可重用的代码,可以多次运行,以完成重复的任务,函数通常与JavaScript、Python或c++等语言相关联,但它们也以属性值的形式存在于CSS中。我们已经在颜色部分看到了函数的作用——rgb()、hsl()等。用于从文件返回图像的值——url()——也是一个函数。

下面我们使用calc()使框宽为20% + 100px。20%是根据父容器.wrapper的宽度来计算的,因此如果宽度改变,它也会改变。

<style>
.wrapper {
  width: 400px;
}

.box {
  width: calc(20% + 100px);
}
</style>
<div class="wrapper">
  <div class="box">My width is calculated.</div> 
</div>