语法结构

yuhuo2022-03-16Html/CssCss基础

概念

1. 应用位置

<html>
    <head>
        <!-- 外部样式(必须放在head标签内) -->
        <link rel="stylesheet" type="text/css" href="./style.css">
        <style type="text/css">
            /* 外部样式 */
            @import "./style.css";
            /* 文档样式 */
            body {
                color: black;
            }
        </style>
    </head>
    <!-- 内联样式 -->
    <body style="color: black;">
    </body>
</html>

2. 层叠优先级

!important > 选择器优先级 > 位置优先级 > 继承

同等优先级下, 后定义 > 前定义

  • 位置优先级:内联样式 > 文档样式 > 外部样式 > 浏览器样式
  • 选择器优先级:内联选择器 > id 选择器 > 类选择器 > 标签选择器...(特指度↑ 优先级↑)

3. 继承

可继承的属性:

  • 字体相关:font-family、font-style、font-size、font-weight 等;
  • 文本相关:text-align、text-indent、text-shadow、letter-spacing、word-spacing、word-break、white-space、line-height、color 、hyphens 等;
  • 列表相关:list-style、list-style-image、list-style-type、list-style-position 等;
  • 其他属性:visibility、cursor 等;

相关属性值:

  • inherit:强制继承父元素对应属性的属性值;
  • initial:应用该属性的默认值;
  • unset:可继承使用继承 ,否则使用默认值(相当于不设置);
  • revert:还原为浏览器默认设置;

注意

继承只会向下传播给后代,只有当 html 没设背景时,body 的 background 属性会完全转移到 html 上,body 自身背景失效。

注意

initial 和 revert 的区别,比如 font-weight 默认值是 400,但是在 <b> 元素中浏览器默认设置 font-weight 的值为 bold。即对一个 <b> 元素而言:

font-weight: initial; = font-weight: 400;

font-weight: revert; = font-weight: bold;

4. 上右下左省略规则

对于属性值为 *top* *right* *bottom* *left* 4个值组成的,有通用省略规则:

从右往左省略,左值省略时同右值,下值省略时同上值,右下左值省略时同上值。

示例:

  • a b c = a b c b
  • a b = a b a b
  • a = a a a a

5. 厂商前缀

  • -moz-:FireFox
  • -ms-:IE
  • -webkit-:Chrome,Safari

规则

@规则 - CSS(层叠样式表) | MDN (mozilla.org)open in new window

@charset

@charset <字符编码>;

@charset "UTF-8";

注意

  • 只能在 css 文件中使用,并且必须放在最开头;
  • 有多个 @charset 规则被声明,只有第一个会被使用;
  • 引号和空格格式要求严格,参考上示例子;

@import

@import <样式文件>

/* 相对路径 */
@import "./style.css";
@import url(./style.css);
/* 根路径 */
@import "/style.css";
@import url(/style.css);
/* 绝对路径 */
@import "https://yuhuo.com/style.css";
@import url(https://yuhuo.com/style.css);

注意

@import 规则必须放在样式开头,在 @charset 规则之后。

@supports

@supports (not? (<属性:值>)) [and|or (not? (<属性:值>))]# { <样式表> }

@supports (not (display: 1px)) and (display: flex) {
    .box {
        color: red;
    }
}

@media

@media [not|only]? <媒体类型>? [and (<媒体特性>)]# { <样式表> }

媒体类型

  • all:所有设备 默认值
  • screen:屏幕
  • print:打印预览模式
  • speech:语音合成器

媒体特性

width: <视口宽度>

height: <视口高度>

min-width: <视口最小宽度>

min-height: <视口最小高度>

max-width: <视口最大宽度>

max-height: <视口最大高度>

device-width: <屏幕宽度>

device-height: <屏幕高度>

min-device-width: <屏幕最小宽度>

min-device-height: <屏幕最小高度>

max-device-width: <屏幕最大宽度>

max-device-height: <屏幕最大高度>

aspect-ratio: <视口宽高比>

  • 数值/数值

max-aspect-ratio: <视口最大宽高比>

min-aspect-ratio: <视口最小宽高比>

device-aspect-ratio: <屏幕宽高比>

max-device-aspect-ratio: <屏幕最大宽高比>

min-device-aspect-ratio: <屏幕最小宽高比>

color: <单种颜色分量位数>

  • 数值

说明

颜色份量由红,蓝,绿3种组成,比如当前单种颜色分量位数为 8,则 javascript 中 screen.colorDepth = 24。(推测的)

max-color: <单个颜色分量最大位数>

min-color: <单个颜色分量最小位数>

orientation: <方向>

  • landscape:横屏
  • portrait:竖屏
<link rel="stylesheet" media="screen and (min-width: 500px)" href="style.css" />
<style>
    @media screen and (min-width: 500px) {
        .div { 
            background: red; 
        }
    }
</style>

@keyframes

@keyframes <动画名> { [<关键帧> { <属性表> }]# }

动画名

  • 文本值

关键帧

  • from:起始帧 = 0%
  • to:终止帧 = 100%
  • 百分值
@keyframes slidein {
    from {
        transform: translateX(0%);
    }
    50% {
        transform: translateX(20%);
    }
    to {
        transform: translateX(100%);
    }
}
特定作用属性

@font-face

@font-face { <字体描述符># }

src: [<本地字体> | <字体文件> <字体格式>?,]# 必选项

本地字体

字体文件

字体格式

@font-face {
    font-family: "MyFont";
    /* IE9兼容模式 */
    src: url(./MyFont.eot);
    	 /* 本地字体 */
    src: local("微软雅黑"),
         /* IE6~IE8 */
         url("./MyFont.eot?#iefix") format('embedded-opentype'),
        /* woff升级版,还未被广泛支持 */
         url("./MyFont.woff2") format('woff2'),
        /* 现代浏览器,W3c标准,对ttf和otf进一步压缩 */
         url("./MyFont.woff") format('woff'),
         /* ttf升级版 */
         url("./MyFont.otf") format('opentype'),
         /* Safari,Android,iOS */
         url("./MyFont.ttf") format('truetype'),
         /* 旧版IOS */
         url("./MyFont.svg") format('svg');
}

TIP

​ 移动端推荐使用 ttf,web 端推荐使用 woff2 / woff。

font-family: <字族> 必选项

font-weight: <字重>

font-style: <字形>

font-variant: <字体变体>

font-stretch: <字宽>

font-display: <字体显示>;

  • auto:自动 默认值,一般效果同 block
  • block
  • swap
  • fallback
  • option

font-display 详解open in new window

font-display.png

unicode-range: <应用字符范围>,#

  • U+4E00-9FFF:Unicode 码位范围 4E00-9FFF
  • U+30??:Unicode 码位范围 3000-30FF
  • U+45:Unicode 单个码位 45

通用值

长度值

单位

  • px:像素
  • rem:相对根元素的字号(默认16px)
  • em:相对元素字号,当 font-size 属性使用时相对父元素字号
  • ex:相对字符 x 的高度(不同字体相同字号,x 的高度会不同)
  • vw:相对屏幕视区宽度
  • vh:相对屏幕视区高度
  • vmin:相对屏幕视区宽度和高度中较小者
  • vmax:相对屏幕视区宽度和高度中较大者

角度值

单位

  • deg:角度(360deg = 1turn)
  • grad:梯度(400grad = 1turn)
  • rad:弧度(2πrad = 1turn)
  • turn:圈数

时间值

单位

  • s:秒
  • ms:毫秒

颜色值

数字

  • #112233:6位十六进制
  • #123:3位十六进制
  • #11223344:8位十六进制
  • #1234:4位十六进制

关键字

  • currentColor:当前 color 属性的值
  • transparent:透明
  • black:黑色
  • white:白色
  • red:红色
  • green:绿色
  • blue:蓝色
  • gray:灰色
  • yellow:黄色
  • purple:紫色
  • orange:橙色
  • gold:金色
  • pink:粉色
  • ...

函数

图像值

函数

比例值

单位

  • fr:比例

频率值

单位

  • hz:赫兹
  • khz:千赫兹
Last Updated 12/4/2026, 10:28:57 AM