CSS 备忘清单 === 这是一份关于 CSS 优点的快速参考备忘单,列出了选择器语法、属性、单位和其他有用的信息 入门 ------------ ### 介绍 CSS 功能丰富,不仅仅是布局页面 #### 外部样式表 `` ```html ``` #### 内部样式表 ` ``` #### 内联样式 `style` ```html
蓝色,18像素文本
``` ### 添加 class 类 ```html ``` 支持一个元素上的多个类 ### !important ```css .post-title { color: blue !important; } ``` 覆盖所有以前的样式规则 ### 选择器 ```css h1 { } #job-title { } div.hero { } div > p { } ``` 查看: [CSS 选择器](#css-选择器) ### 文本颜色 ```css color: #2a2aff; color: green; color: rgb(34, 12, 64, 0.6); color: hsla(30 100% 50% / 0.6); ``` 查看: [Colors](#css-颜色) ### 背景 ```css background-color: blue; background-image: url("nyan-cat.gif"); background-image: url("../image.png"); ``` 查看: [Backgrounds](#css-背景) ### 字体 ```css .page-title { font-weight: bold; font-size: 30px; font-family: "Courier New"; } ``` 查看: [Fonts](#css-字体) ### 定位 ```css .box { position: relative; top: 20px; left: 20px; } ``` 另见: [Position](https://learn-the-web.algonquindesign.ca/topics/css-layout-cheat-sheet/) ### 动画 ```css animation: 300ms linear 0s infinite; animation: bounce 300ms linear infinite; ``` 查看: [Animation](#css-动画) ### 注释 ```css /* 这是一行注释 */ /* 这是 多行注释 */ ``` ### Flex 布局 ```css div { display: flex; justify-content: center; } div { display: flex; justify-content: flex-start; } ``` 查看: [Flexbox](#css-flexbox) | [Flex Tricks](#css-flexbox-技巧) ### Grid 布局 ```css #container { display: grid; s grid: repeat(2, 60px) / auto-flow 80px; } #container > div { background-color: #8ca0ff; width: 50px; height: 50px; } ``` 查看: [Grid Layout](#grid-布局) ### 变量和计数器 ```css counter-set: subsection; counter-increment: subsection; counter-reset: subsection 0; :root { --bg-color: brown; } element { background-color: var(--bg-color); } ``` 查看: [动态内容](#css-动态内容) CSS 选择器 ----------- ### 示例 #### 组选择器 ```css h1, h2 { color: red; } ``` #### 链选择器 ```css h3.section-heading { color: blue; } ``` #### 属性选择器 ```css div[attribute="SomeValue"] { background-color: red; } ``` #### 第一个子选择器 ```css p:first-child { font-weight: bold; } ``` #### 无子选择器 ```css .box:empty { background: lime; height: 80px; width: 80px; } ``` ### 基础 选择器 | 说明 :- | :- `*` | 所有元素 `div` | 所有 div 标签 `.classname` | 具有类的所有元素 `#idname` | 带 ID 的元素 `div,p` | 所有 div 和段落 `#idname *` | #idname 中的所有元素 另见: [元素](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Type_selectors) / [类](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Class_selectors) / [ID](https://developer.mozilla.org/zh-CN/docs/Web/CSS/ID_selectors) / [通配](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Universal_selectors) 选择器 ### 组合器 选择器 | 说明 :- | :- `div.classname` | 具有特定类名的 div `div#idname` | 具有特定 ID 的 div `div p` | div 中的所有段落 `div > p` | 父元素是 div 的 `P` 标签 `div + p` | div 之后的第一个同级 `P` 标签 `div ~ p` | div 之后所有的同级 `P` 标签 另见: [相邻兄弟](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Adjacent_sibling_combinator) / [通用兄弟](https://developer.mozilla.org/zh-CN/docs/Web/CSS/General_sibling_combinator) / [子](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Child_combinator) 选择器 ### 属性选择器 选择器 | 说明 :- | :- `a[target]` | 带有