Files
notes_estom/CSS/css-grammar.md
2022-04-18 23:03:54 +08:00

73 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 语法格式
```css
选择器 { : ; : ;...}
```
语法特点:
- CSS声明总是以键值对(key\value)形式存在。
- CSS声明总是以分号(;)结束。
- 声明组以大括号({})括起来。
- 为了让CSS可读性更强每行只描述一个属性。
## CSS 注释
注释是用来解释你的代码并且可以随意编辑它浏览器会忽略它。CSS注释以 `/*` 开始, 以 `*/` 结束。
```html
<style type="text/css">
/* 这是一个注释 */
div {
/* 这是另一个注释 */
color : red;
}
</style>
```
## 值得注意的问题
### 值的不同写法和单位
例如在设置字体颜色时,以下几种方式效果相同。
- 第一种方式
```css
#show1 {color : red;}
```
- 第二种方式
```css
#show2 {color : #ff0000;}
```
像上面这种使用十六进制设置颜色时,如果两两相同,可以写成如下格式:
```css
#show2 {color : #f00;}
```
- 第三种方式
```css
#show3 {color : rgb(255,0,0);}
```
上面的格式还可以写成如下格式:
```css
#show3 {color : rgb(100%,0%,0%);}
```
> **值得注意的是:** 当使用 RGB 百分比时,即使当值为 0 时也要写百分比符号。但是在其他的情况下就不需要这么做了。
### 值为若干单词,记得写引号
```css
div {font-family : Courier, "Courier New", monospace;}
```
### 多重声明
如果要定义不止一个声明,则需要用分号将每个声明分开。
### 空格和大小写
大多数样式表包含不止一条规则,而大多数规则包含不止一个声明。多重声明和空格的使用使得样式表更容易被编辑。