CSS 使用全教程
介紹
CSS(層疊樣式表,Cascading Style Sheets)是一種樣式表語言,用于描述 HTML 或 XML 文檔的布局和外觀,它允許開發者將文檔的內容結構與樣式表現分離,通過定義一系列的樣式規則來控制網頁元素的字體、顏色、布局、響應式設計等多方面的視覺效果,從而實現網站的美觀和一致性,并且CSS 功能豐富,不僅僅是布局頁面,還能夠提高網頁的維護性和可訪問性。
一個簡單的小例子:
在經過CSS 樣式潤色后:
外部樣式表 <link>
<link
href="./path/to/stylesheet/style.css"
rel="stylesheet"
type="text/css"
>
內部樣式表 <style>
<style>
body {
background-color: linen;
}
</style>
內聯樣式 style
<h2 style="text-align: center;">
居中文本
</h2>
<p style="color: blue; font-size: 18px;">
藍色,18像素文本
</p>
添加 class 類
<div class="classname"></div>
<div class="class1 ... classn"></div>
支持一個元素上的多個類
!important
.post-title {
color: blue !important;
}
覆蓋所有以前的樣式規則
選擇器
h1 { }
#job-title { }
div.hero { }
div > p { }
查看: CSS 選擇器
文本顏色
color: #2a2aff;
color: green;
color: rgb(34, 12, 64, 0.6);
color: hsla(30 100% 50% / 0.6);
查看: Colors
背景
background-color: blue;
background-image: url("nyan-cat.gif");
background-image: url("../image.png");
查看: Backgrounds
字體
.page-title {
font-weight: bold;
font-size: 30px;
font-family: "Courier New";
}
查看: Fonts
定位
.box {
position: relative;
top: 20px;
left: 20px;
}
另見: Position
動畫
animation: 300ms linear 0s infinite;
animation: bounce 300ms linear infinite;
查看: Animation
注釋
/* 這是一行注釋 */
/* 這是
多行注釋 */
Flex 布局
div {
display: flex;
justify-content: center;
}
div {
display: flex;
justify-content: flex-start;
}
查看: Flexbox | Flex Tricks
Grid 布局
#container {
display: grid;
s grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
查看: Grid Layout
變量和計數器
counter-set: subsection;
counter-increment: subsection;
counter-reset: subsection 0;
:root {
--bg-color: brown;
}
element {
background-color: var(--bg-color);
}
查看: 動態內容
CSS 選擇器
示例
組選擇器
h1, h2 {
color: red;
}
鏈選擇器
h3.section-heading {
color: blue;
}
屬性選擇器
div[attribute="SomeValue"] {
background-color: red;
}
第一個子選擇器
p:first-child {
font-weight: bold;
}
無子選擇器
.box:empty {
background: lime;
height: 80px;
width: 80px;
}
基礎
選擇器 | 說明 |
---|---|
* | 所有元素 |
div | 所有 div 標簽 |
.classname | 具有類的所有元素 |
#idname | 帶 ID 的元素 |
div,p | 所有 div 和段落 |
#idname * | #idname 中的所有元素 |
另見: 元素 / 類 / ID / 通配 選擇器
組合器
選擇器 | 說明 |
---|---|
div.classname | 具有特定類名的 div |
div#idname | 具有特定 ID 的 div |
div p | div 中的所有段落 |
div > p | 父元素是 div 的 P 標簽 |
div + p | div 之后的第一個同級 P 標簽 |
div ~ p | div 之后所有的同級 P 標簽 |
另見: 相鄰兄弟 / 通用兄弟 / 子 選擇器
屬性選擇器
選擇器 | 說明 |
---|---|
a[target] | 帶有 target 屬性 # |
a[target="_blank"] | 在新標簽中打開 # |
a[href^="/index"] | 以 /index 開頭 # |
[class|="chair"] | 以chair開頭 # |
[class*="chair"] | 包含chair # |
[title~="chair"] | 包含單詞 chair # |
a[href$=".doc"] | 以 .doc 結尾 # |
[type="button"] | 指定類型 # |
另見: 屬性選擇器
用戶操作偽類
選擇器 | 說明 |
---|---|
a:link | 鏈接正常 # |
a:active | 鏈接處于點擊狀態 # |
a:hover | 鼠標懸停鏈接 # |
a:visited | 訪問鏈接 # |
/* 未訪問鏈接 */
a:link { color: blue; }
/* 已訪問鏈接 */
a:visited { color: purple; }
/* 用戶鼠標懸停 */
a:hover { background: yellow; }
/* 激活鏈接 */
a:active { color: red; }
偽類
選擇器 | 說明 |
---|---|
p::after | 在 p 之后添加內容 # |
p::before | 在 p 之前添加內容 # |
p::first-letter | p中的第一個字母 # |
p::first-line | p 中的第一行 # |
::selection | 由用戶選擇 # |
::placeholder | 占位符 屬性 # |
:root | 文檔根元素 # |
:target | 突出顯示活動錨點 # |
div:empty | 沒有子元素的元素 # |
p:lang(en) | 帶有 en 語言屬性的 P # |
:not(span) | 不是跨度的元素 # |
:host | shadowDOM 中選擇自定義元素 # |
::backdrop | 處于全屏模式的元素樣式 # |
::marker | li 項目符號或者數字 # |
::file-selector-button | type="file" input 按鈕 # |
輸入偽類
選擇器 | 說明 |
---|---|
input:checked | 檢查 input # |
input:disabled | 禁用 input # |
input:enabled | 啟用的 input # |
input:default | 有默認值的元素 # |
input:blank | 空的輸入框 # |
input:focus | input 有焦點 # |
input:in-range | 范圍內的值 # |
input:out-of-range | input 值超出范圍 # |
input:valid | input 有效值 # |
input:invalid | input 無效值 # |
input:optional | 沒有必需的屬性 # |
input:required | 帶有必需屬性的 input # |
input:read-only | 具有只讀屬性 # |
input:read-write | 沒有只讀屬性 # |
input:indeterminate | 帶有 indeterminate 狀態 # |
結構偽類
選擇器 | 說明 |
---|---|
p:first-child | 第一個孩子 # |
p:last-child | 最后一個孩子 # |
p:first-of-type | 第一個 p 類型的元素 # |
p:last-of-type | 某種類型的最后一個 # |
p:nth-child(2) | 其父母的第二個孩子 # |
p:nth-child(3n42) | Nth-child(an + b) 公式 # |
p:nth-last-child(2) | 后面的二孩 # |
p:nth-of-type(2) | 其父級的第二個 p # |
p:nth-last-of-type(2) | ...從后面 # |
p:only-of-type | 其父級的唯一性 # |
p:only-child | 其父母的唯一孩子 # |
:is(header, div) p | 可以選擇的元素 # |
:where(header, div) p | 與 :is 相同 # |
a:has(> img) | 包含 img 元素的 a 元素 # |
::first-letter | 第一行的第一個字母 # |
::first-line | 第一行應用樣式 # |
CSS 字體
屬性
屬性 | 說明 |
---|---|
font-family: | 字體族名或通用字體族名 # |
font-size: | 字體的大小 # |
letter-spacing: | 文本字符的間距 # |
line-height: | 多行文本間距 # |
font-weight: | 粗細程度 # |
font-style: | 字體樣式 # |
text-decoration: | 文本的修飾線外觀 # |
text-align: | 相對它的塊父元素對齊 # |
text-transform: | 指定文本大小寫 # |
另見: Font
速記
font: italic 400 14px / 1.5 sans-serif
┈┈┬┈┈┈ ┈┬┈ ┈┬┈┈ ┈┬┈ ┈┬┈┈┈┈┈┈┈┈
樣式 粗細 大小(必需的) 行高 字體(必需的)
示例
font-family: Arial, sans-serif;
font-size: 12pt;
letter-spacing: 0.02em;
大小寫
div {
/* 首字母大寫 Hello */
text-transform: capitalize;
/* 字母大寫 HELLO */
text-transform: uppercase;
/* 字母小寫 hello */
text-transform: lowercase;
}
@font-face
@font-face {
font-family: 'Glegoo';
src: url('../Glegoo.woff');
}
CSS 顏色
命名顏色
color: red;
color: orange;
color: tan;
color: rebeccapurple;
更多標準顏色
十六進制顏色
color: #090;
color: #009900;
color: #090a;
color: #009900aa;
rgb() 顏色
color: rgb(34, 12, 64, 0.6);
color: rgba(34, 12, 64, 0.6);
color: rgb(34 12 64 / 0.6);
color: rgba(34 12 64 / 0.3);
color: rgb(34.0 12 64 / 60%);
color: rgba(34.6 12 64 / 30%);
HSL 顏色
color: hsl(30, 100%, 50%, 0.6);
color: hsla(30, 100%, 50%, 0.6);
color: hsl(30 100% 50% / 0.6);
color: hsla(30 100% 50% / 0.6);
color: hsl(30.0 100% 50% / 60%);
color: hsla(30.2 100% 50% / 60%);
其它
color: inherit;
color: initial;
color: unset;
color: transparent;
color: currentcolor; /* 關鍵字 */
全局值
/* 全局值 */
color: inherit;
color: initial;
color: unset;
CSS 背景
屬性
屬性 | 說明 |
---|---|
background: | (速記) |
background-color: | 查看: Colors |
background-image: | 一個或者多個背景圖像 |
background-position: | 背景圖片設置初始位置 |
background-size: | 背景圖片大小 |
background-clip: | 背景(圖片或顏色)是否延伸到邊框、內邊距盒子、內容盒子下面 |
background-repeat: | 圖像重復方式 |
background-attachment: | scroll /fixed /local |
速記
background: #ff0 url(a.jpg) left top / 100px auto no-repeat fixed;
#abc url(b.png) center center / cover repeat-x local;
┈┬┈┈ ┈┬┈┈┈┈┈┈┈ ┈┬┈┈ ┈┬┈ ┈┈┬┈┈┈┈┈┈┈ ┈┈┬┈┈┈┈┈┈ ┈┈┬┈┈┈
顏色 圖片 位置x 位置x 圖片大小 圖像重復方式 位置是在視口內固定
示例
background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
background: url(img_man.jpg) no-repeat center;
background: rgb(2,0,36);
background: linear-gradient(90deg, rgba(2,0,36,1) 0%,
rgba(13,232,230,1) 35%,
rgba(0,212,255,1) 100%);
CSS 盒子模型
最大值/最小值
.column {
max-width: 200px; /* 最大寬度 200 像素 */
width: 500px; /* 寬度 500 像素 */
}
另見: max-width / min-width / max-height / min-height
邊距/補白
.block-one {
margin: 20px; /* 邊距 20 像素 */
padding: 10px; /* 補白 10 像素 */
}
另見: 邊距(margin) / 補白(padding)
Box-sizing
.container {
/* 設置的邊框和補白的值是包含在 width 內的 */
box-sizing: border-box;
}
另見: box-sizing
能見度
.invisible-elements {
visibility: hidden; /* 隱藏元素 */
}
另見: Visibility
Auto 關鍵字
div {
/* 覽器自己選擇一個合適的外邊距 */
margin: auto;
}
另見: 邊距(margin)
溢出(Overflow)
.small-block {
/* 瀏覽器總是顯示滾動條 */
overflow: scroll;
}
另見: 溢出(overflow)
CSS 動畫
速記
animation: bounce 300ms linear 100ms infinite alternate-reverse both reverse
┈┬┈┈ ┈┬┈┈┈ ┈┬┈┈┈┈ ┈┈┬┈┈ ┈┈┈┬┈┈┈┈ ┈┈┬┈┈┈┈┈┈┈┈┈┈┈┈┈┈ ┈┈┬┈┈┈ ┈┈┬┈┈┈
動畫名 動畫時間 緩動函數 延遲 運行的次數 動畫是否反向播放 如何將樣式應用于其目標 是否運行或者暫停
屬性
屬性 | 說明 |
---|---|
animation: | (速記) |
animation-name: | 動畫名 # |
animation-duration: | 動畫周期的時長 # |
animation-timing-function: | 緩動函數 # |
animation-delay: | 延遲 # |
animation-iteration-count: | 運行的次數 # |
animation-direction: | 動畫是否反向播放 # |
animation-fill-mode: | 如何將樣式應用于其目標 # |
animation-play-state: | 是否運行或者暫停 # |
另見: 動畫(Animation)
示例
/* @keyframes duration | timing-function | delay |
iteration-count | direction | fill-mode | play-state | name */
animation: 3s ease-in 1s 2 reverse both paused slidein;
/* @keyframes duration | timing-function | delay | name */
animation: 3s linear 1s slidein;
/* @keyframes duration | name */
animation: 3s slidein;
animation: 4s linear 0s infinite alternate move_eye;
animation: bounce 300ms linear 0s infinite normal;
animation: bounce 300ms linear infinite;
animation: bounce 300ms linear infinite alternate-reverse;
animation: bounce 300ms linear 2s infinite alternate-reverse forwards normal;
Javascript 事件
.one('webkitAnimationEnd oanimationend msAnimationEnd animationend')
CSS Flexbox
簡單實例
.container {
display: flex;
}
.container > div {
flex: 1 1 auto;
}
容器
.container {
display: flex;
display: inline-flex;
flex-direction: row; /* ltr - 行(左向右) ? */
flex-direction: row-reverse; /* rtl - 行(右向左) ? */
flex-direction: column; /* top-bottom ▼ */
flex-direction: column-reverse; /* bottom-top ▲ */
flex-wrap: nowrap; /* 擺放到一行 */
flex-wrap: wrap; /* 被打斷到多個行中 */
align-items: flex-start; /* 垂直對齊 - 頂部 */
align-items: flex-end; /* 垂直對齊 - 底部 */
align-items: center; /* 垂直對齊 - 中間 */
align-items: stretch; /* 所有人都一樣的高度 (默認) */
justify-content: flex-start; /* [??? ] */
justify-content: center; /* [ ■■■ ] */
justify-content: flex-end; /* [ ???] */
justify-content: space-between; /* [? ■ ?] */
justify-content: space-around; /* [ ■ ■ ■ ] */
justify-content: space-evenly; /* [ ■ ■ ■ ] */
}
子元素
.container > div {
/* 這個: */
flex: 1 0 auto;
/* 相當于這個: */
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
order: 1;
align-self: flex-start; /* left */
margin-left: auto; /* right */
}
justify-content
justify-content: flex-start | flex-end | center | space-between
flex-start
:左對齊(默認值)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆╭┈┈╮╭┈╮╭┈┈┈╮ ┆
┆╰┈┈╯╰┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
flex-end
:右對齊
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
center
: 居中
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
space-between
:兩端對齊,項目之間的間隔都相等
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆╭┈┈╮ ╭┈╮ ╭┈┈┈╮┆
┆╰┈┈╯ ╰┈╯ ╰┈┈┈╯┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
space-around
:每個項目兩側的間隔相等,項目之間的間隔比項目與邊框的間隔大一倍
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ╭┈╮ ╭┈┈┈╮ ┆
┆ ╰┈┈╯ ╰┈╯ ╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
上面示例,假設主軸為從左到右
flex-wrap
flex-wrap: nowrap | wrap | wrap-reverse;
nowrap
:不換行(默認)
╭1╮╭2╮╭3╮╭4╮╭5╮╭6╮╭7╮╭8╮╭9╮╭10╮
╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈╯╰┈┈╯
wrap
:換行,第一行在 上方
╭1┈╮ ╭2┈╮ ╭3┈╮ ╭4┈╮ ╭5┈╮ ╭6┈╮ ╭7┈╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
╭8┈╮ ╭9┈╮ ╭10╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯
wrap-reverse
:換行,第一行在 下方
╭8┈╮ ╭9┈╮ ╭10╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯
╭1┈╮ ╭2┈╮ ╭3┈╮ ╭4┈╮ ╭5┈╮ ╭6┈╮ ╭7┈╮
╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
項目都排在一條線(又稱"軸線")上
flex-direction
flex-direction: row | row-reverse | column | column-reverse;
╭┈┈╮ ▲ ╭┈┈╮ ┆
╰┈┈╯ ┆ ╰┈┈╯ ┆
╭┈┈╮ ┆ ╭┈┈╮ ┆
╰┈┈╯ ┆ ╰┈┈╯ ┆ ┈┈┈┈┈┈┈┈┈┈┈? ?┈┈┈┈┈┈┈┈┈┈┈
╭┈┈╮ ┆ ╭┈┈╮ ┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮
╰┈┈╯ ┆ ╰┈┈╯ ▼ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈
column-reverse column row row-reverse
屬性決定主軸的方向(即項目的排列方向)
align-items
align-items: flex-start | flex-end | center | baseline | stretch;
? flex-start(起點對齊) ? flex-end(終點對齊)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ┆ ┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ┆
┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆
┆ ┆ ┆ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? center(中點對齊) ? stretch(占滿整個容器的高度)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ┆
┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? baseline(第一行文字的基線對齊)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈╮ ┆
┆ ┆ ┆ ╭┈┈┈┈╮ ╭┈┈┈┈╮ ┆ ┆ ╭┈┈┈┈╮╭┈┈┈┈╮┆
┆ ┆ text ┆ ┆text┆ ┆text┆ ┆ text ┆ ┆text┆┆text┆┆
┆ ┆ ┆ ╰┈┈┈┈╯ ┆ ┆ ┆ ┆ ╰┈┈┈┈╯┆ ┆┆
┆ ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯ ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯┆
┆ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
align-content
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
? flex-start(起點對齊) ? flex-end(終點對齊)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆ ┆ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆ ┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆
┆ ╭┈┈┈╮╭╮ ┆ ┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆
┆ ╰┈┈┈╯╰╯ ┆ ┆ ╭┈┈┈╮╭╮ ┆
┆ ┆ ┆ ╰┈┈┈╯╰╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? center(中點對齊) ? stretch(滿整個交叉軸)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ┆ ┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆
┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆ ┆ ┆ ┆┆ ┆┆ ┆┆┆┆ ┆ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆ ┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆
┆ ╭┈┈┈╮╭╮ ┆ ┆ ╭┈┈┈╮╭╮╭┈╮ ┆
┆ ╰┈┈┈╯╰╯ ┆ ┆ ┆ ┆┆┆┆ ┆ ┆
┆ ┆ ┆ ╰┈┈┈╯╰╯╰┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? space-between(兩端對齊) ? space-around(均勻分布項目)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮ ┆ ┆ ┆
┆ ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯ ┆ ┆ ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮ ┆
┆ ┆ ┆ ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯ ┆
┆ ┆ ┆ ┆
┆ ┆ ┆ ╭┈┈╮ ┆
┆ ╭┈┈╮ ┆ ┆ ╰┈┈╯ ┆
┆ ╰┈┈╯ ┆ ┆ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
order
.item {
order: <integer>;
}
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈╮
┆ ╭1┈╮ ╭1┈┈╮ ╭1┈╮ ╭2┈╮ ╭3┈┈┈┈┈┈╮ ┆ ┆ ╭2┈┈┈┈╮ ┆
┆ ╰┈┈╯ ╰┈┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈┈┈┈┈┈╯ ┆ ┆ ╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ┆ ╭2┈┈┈┈╮ ┆
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ┆ ╰┈┈┈┈┈╯ ┆
┆ ╭-┈┈╮ ╭┈┈┈╮ ╭┈┈┈┈┈┈┈┈╮ ╭┈┈┈╮ ┆ ┆ ╭99┈┈┈╮ ┆
┆ ┆-1 ┆ ┆ 1 ┆ ┆ 2 ┆ ┆ 5 ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈┈╯ ╰┈┈┈╯ ╰┈┈┈┈┈┈┈┈╯ ╰┈┈┈╯ ┆ ┆ ╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈╯
屬性 order 定義項目的排列順序。數值越小,排列越靠前,默認為 0
flex-grow
.item {
flex-grow: <number>; /* default 0 */
}
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈1┈┈╮╭┈┈2┈┈╮╭┈┈1┈┈╮ ┆
┆ ╰┈┈┈┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈1┈╮╭┈┈┈┈2┈┈┈┈╮╭┈1┈╮ ┆
┆ ╰┈┈┈╯╰┈┈┈┈┈┈┈┈┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
屬性 flex-grow 定義項目的放大比例,默認為0
,即如果存在剩余空間,也不放大
CSS Flexbox 技巧
垂直中心
.container {
display: flex;
}
.container > div {
width: 100px;
height: 100px;
margin: auto;
}
垂直中心 (2)
.container {
display: flex;
/* 垂直的 */
align-items: center;
/* 水平的 */
justify-content: center;
}
重新排序
.container > .top {
order: 1;
}
.container > .bottom {
order: 2;
}
移動布局
.container {
display: flex;
flex-direction: column;
}
.container > .top {
flex: 0 0 100px;
}
.container > .content {
flex: 1 0 auto;
}
一個固定高度的頂部欄和一個動態高度的內容區域
Table-like 像表格
.container {
display: flex;
}
/* 這里的“px”值只是建議的百分比 */
.container > .checkbox { flex: 1 0 20px; }
.container > .subject { flex: 1 0 400px; }
.container > .date { flex: 1 0 120px; }
這會創建具有不同寬度的列,但會根據情況相應地調整大小
Vertical 垂直的
.container {
align-items: center;
}
垂直居中所有項目
左和右
.menu > .left { align-self: flex-start; }
.menu > .right { align-self: flex-end; }
CSS Grid 網格布局
網格模板列
#grid-container {
display: grid;
width: 100px;
grid-template-columns: 20px 20% 60%;
}
fr 相對單位
.grid {
display: grid;
width: 100px;
grid-template-columns: 1fr 60px 1fr;
}
Grid Gap 網格間隙
/* 行間距為 20px */
/* 列之間的距離是 10px */
#grid-container {
display: grid;
grid-gap: 20px 10px;
}
CSS 網格行
CSS 語法:
- grid-row: grid-row-start / grid-row-end;
實例
.item {
grid-row: 1 / span 2;
}
CSS 塊級網格
#grid-container {
display: block;
}
CSS 內聯級別網格
#grid-container {
display: inline-grid;
}
CSS 網格行間隙
grid-row-gap: length;
任何合法的長度值,例如 px
或 %
。0
是默認值
CSS 網格區域
.item1 {
grid-area: 2 / 1 / span 2 / span 3;
}
minmax() 函數
.grid {
display: grid;
grid-template-columns: 100px minmax(100px, 500px) 100px;
}
定義了一個長寬范圍的閉區間
grid-row-start & grid-row-end
CSS 語法:
- grid-row-start: auto|row-line;
- grid-row-end: auto|row-line|span n;
實例
grid-row-start: 2;
grid-row-end: span 2;
對齊項目
#container {
display: grid;
justify-items: center;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 10px;
}
CSS 網格模板區域
.item {
grid-area: nav;
}
.grid-container {
display: grid;
grid-template-areas:
'nav nav . .'
'nav nav . .';
}
Justify Self
#grid-container {
display: grid;
justify-items: start;
}
.grid-items {
justify-self: end;
}
網格項目位于行的右側(末尾)
對齊項目
#container {
display: grid;
align-items: start;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 10px;
}
CSS 動態內容
變量
定義 CSS 變量
:root {
--first-color: #16f;
--second-color: #ff7;
}
變量用法
#firstParagraph {
background-color: var(--first-color);
color: var(--second-color);
}
另見: CSS Variable
計數器
/* Set "my-counter" to 0 */
counter-set: my-counter;
/* Increment "my-counter" by 1 */
counter-increment: my-counter;
/* Decrement "my-counter" by 1 */
counter-increment: my-counter -1;
/* Reset "my-counter" to 0 */
counter-reset: my-counter;
另見: Counter set
使用計數器
body { counter-reset: section; }
h3::before {
counter-increment: section;
content: "Section." counter(section);
}
ol {
counter-reset: section;
list-style-type: none;
}
li::before {
counter-increment: section;
content: counters(section, ".") " ";
}
CSS 函數
calc()
div {
width: calc(100% - 30px);
height: calc(100% - 30px);
}
calc()
CSS 函數允許您在指定 CSS 屬性值時執行計算
clamp()
font-size: clamp(1rem, 10vw, 2rem);
設置隨窗口大小改變的字體大小
attr()
p:before {
content: attr(data-foo) " ";
}
獲取選擇到的元素的某一 HTML 屬性值
counter()
返回一個代表計數器的當前值的字符串
<ol>
<li></li>
<li></li>
<li></li>
</ol>
ol {
counter-reset: listCounter;
}
li {
counter-increment: listCounter;
}
li::after {
content: "[" counter(listCounter) "] == ["
counter(listCounter, upper-roman) "]";
}
顯示
1. [1]==[I]
2. [2]==[II]
3. [3]==[III]
counters()
ol {
counter-reset: count;
}
li {
counter-increment: count;
}
li::marker {
content: counters(count, '.', upper-alpha) ') ';
}
li::before {
content: counters(count, ".", decimal-leading-zero) " == " counters(count, ".", lower-alpha);
}
嵌套計數器,返回表示指定計數器當前值的連接字符串
env()
<meta name="viewport" content="... viewport-fit=cover">
body {
padding:
env(safe-area-inset-top, 20px)
env(safe-area-inset-right, 20px)
env(safe-area-inset-bottom, 20px)
env(safe-area-inset-left, 20px);
}
用戶代理定義的環境變量值插入你的 CSS 中
fit-content()
fit-content(200px)
fit-content(5cm)
fit-content(30vw)
fit-content(100ch)
將給定大小夾緊為可用大小
max()
從一個逗號分隔的表達式列表中選擇最大(正方向)的值作為屬性的值
width: max(10vw, 4em, 80px);
例子中,寬度最小會是 80px,除非視圖寬度大于 800px 或者是一個 em 比 20px 寬
min()
width: min(1vw, 4em, 80px);
從逗號分隔符表達式中選擇一個最小值作為 CSS 的屬性值
minmax()
minmax(200px, 1fr)
minmax(400px, 50%)
minmax(30%, 300px)
minmax(100px, max-content)
minmax(min-content, 400px)
minmax(max-content, auto)
minmax(auto, 300px)
minmax(min-content, auto)
repeat() 軌道列表的重復片段
repeat(auto-fill, 250px)
repeat(auto-fit, 250px)
repeat(4, 1fr)
repeat(4, [col-start] 250px [col-end])
repeat(4, [col-start] 60% [col-end])
定義了一個長寬范圍的閉區間
url()
background: url("topbanner.png") #00D no-repeat fixed;
list-style: square url(http://www.example.com/redball.png)
var()
:root {
--main-bg-color: pink;
}
body {
background-color: var(--main-bg-color);
}
代替元素中任何屬性中的值的任何部分
CSS 技巧
強制不換行
p {
white-space:nowrap;
}
強制換行
p {
word-break:break-all; /* 英文 */
white-space:pre-wrap; /* 中文 */
}
滾動條平滑
html {
scroll-behavior: smooth;
}
點擊我頁面會平滑滾動到入門
修改瀏覽器自動填充 input 樣式
input[type="text"]:autofill {
box-shadow: 0 0 0 1000px #000 inset;
-webkit-text-fill-color: white;
}
另見: :autofill
修改 input type="color" 樣式
input[type="color"] {
-webkit-appearance: none;
border: none;
width: 32px;
height: 32px;
}
input[type="color"]::-webkit-color-swatch-wrapper {
padding: 0;
}
input[type="color"]::-webkit-color-swatch {
border: none;
}
忽略用作間距的換行符 <br />
br + br {
display: none;
}
使用 :empty 隱藏空 HTML 元素
:empty {
display: none;
}
CSS 重置
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
有助于在不同的瀏覽器之間強制樣式一致性,并為樣式元素提供干凈的盒子
設置光標樣式
body {
caret-color: red;
}
設置整個頁面灰色
html {
-webkit-filter: grayscale(.95);
}
上面示例設置了當前卡片灰色
<textarea>
自動增加其高度
textarea {
form-sizing: normal
}
定義容器的長寬比
div {
aspect-ratio: 1/1
}
屬性 aspect-ratio 可以非常容易的定義一個容器的長寬比
使用 unset 而不是重置所有屬性
使用 all
速記來指定元素的所有屬性。將值設置為 unset
會將元素的屬性更改為其初始值:
button {
all: unset;
}
注意:IE11
不支持 all
和 unset
速記
超出顯示省略號
p {
overflow: hidden;/*超出部分隱藏*/
/* 超出部分顯示省略號 */
text-overflow:ellipsis;
/* 規定段落中的文本不進行換行 */
white-space: nowrap;
width: 250px;/*需要配合寬度來使用*/
}
給正文添加行高
您不需要為每個 <p>
、<h*>
等添加行高。相反,將其添加到正文:
body {
line-height: 1.5;
}
這樣文本元素可以很容易地從 body
繼承
使用圖像作為光標
div {
cursor: url('path-to-image.png'), url('path-to-fallback-image.png'), auto;
/* 表情符號作為光標 */
cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>🚀</text></svg>"), auto;
}
文本溢出顯示省略號
.overflow-ellipsis {
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
一行文本截斷顯示省略號 (...)
將文本截斷到特定的行數
.overflow-truncate {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
多行文本截斷到特定的行數,末尾顯示省略號 (...)
粘性定位元素
.sticky {
position: sticky;
top: 0;
}
屬性 sticky 能在滾動到頂部的位置固定住元素
使用帶有空鏈接的屬性選擇器
a[href^="http"]:empty::before {
content: attr(href);
}
如果 <a>
標簽里面沒有內容,將 href
的值作為內容展示
使用 :root 表示靈活類型
響應式布局中的字體大小應該能夠根據每個視口進行調整,您可以使用 :root
根據視口高度和寬度計算字體大小
:root {
font-size: calc(1vw + 1vh + .5vmin);
}
您可以根據 :root
計算的值使用根 em
單位:
body {
font: 1rem/1.6 sans-serif;
}
吸附滾動
.container {
height: 250px;
overflow-x: scroll;
display: flex;
scroll-snap-type: x mandatory;
column-gap: 10px;
}
.child {
flex: 0 0 66%;
width: 250px;
background-color: #663399;
scroll-snap-align: center;
}
可用于 輪播圖
效果
類似 contenteditable 的樣式
div {
-webkit-user-modify:
read-write-plaintext-only;
}
通過樣式來控制一個元素 div
是否可以編輯
等寬表格單元格
嘗試使用 table-layout: fixed
以保持單元格寬度相等:
table {
table-layout: fixed;
}
利用屬性選擇器來選擇空鏈接
當 <a>
元素沒有文本內容,但有 href
屬性的時候,顯示它的 href
屬性:
a[href^="http"]:empty::before {
content: attr(href);
}
給 “默認” 鏈接定義樣式
給 “默認” 鏈接定義樣式:
a[href]:not([class]) {
color: #008000;
text-decoration: underline;
}
通常沒有 class
屬性,以上樣式可以甄別它們,而且不會影響其它樣式
用 rem 調整全局大小;用 em 調整局部大小
在根元素設置基本字體大小后 (html { font-size: 100%; }
), 使用 em 設置文本元素的字體大小:
h2 {
font-size: 2em;
}
p {
font-size: 1em;
}
然后設置模塊的字體大小為 rem:
article {
font-size: 1.25rem;
}
aside .module {
font-size: .9rem;
}
現在,每個模塊變得獨立,更容易、靈活的樣式便于維護
隱藏沒有靜音、自動播放的影片
這是一個自定義用戶樣式表的不錯的技巧。避免在加載頁面時自動播放。如果沒有靜音,則不顯示視頻:
video[autoplay]:not([muted]) {
display: none;
}
再次,我們利用了 :not()
的優點
為更好的移動體驗,為表單元素設置字體大小
當觸發 <select>
的下拉列表時,為了避免表單元素在移動瀏覽器(iOS Safari 和其它)上的縮放,加上font-size:
input[type="text"],
input[type="number"],
select,
textarea {
font-size: 16px;
}
使用指針事件來控制鼠標事件
指針事件允許您指定鼠標如何與其觸摸的元素進行交互。要禁用按鈕上的默認指針事件,例如:
button:disabled {
opacity: .5;
pointer-events: none;
}
就這么簡單
子元素選中父元素
div:has(img) {
background: black;
}
設置包含子元素 img
的 div
元素樣式,還可以嵌套:
div:has(h2):has(ul) {
background: black;
}
在用作間距的換行符上設置 display: none
用戶使用額外的換行符
br + br {
display: none;
}
給 body
添加行高
body {
line-height: 1.5;
}
您不需要為每個 <p>
、<h*>
等分別添加行高。相反,將其添加到正文
檢查本地是否安裝了字體
@font-face {
font-family: "Dank Mono";
src:
/* Full name */
local("Dank Mono"),
/* Postscript name */
local("Dank-Mono"),
/* 否則,請下載它! */
url("//...a.server/DankMono.woff");
}
code {
font-family: "Dank Mono",
system-ui-monospace;
}
您可以在遠程獲取字體之前檢查是否在本地安裝了字體,這也是一個很好的性能提示
獲取 HTML 元素的屬性
<a href="https://example.com">超鏈接</a>
attr HTML 元素的屬性名。
a:after {
content: " (" attr(href) ")";
}
為表單元素設置 :focus
a:focus, button:focus, input:focus,
select:focus, textarea:focus {
box-shadow: none;
outline: #000 dotted 2px;
outline-offset: .05em;
}
有視力的鍵盤用戶依靠焦點來確定鍵盤事件在頁面中的位置。使表單元素的焦點比瀏覽器的默認實現更加突出和一致
垂直居中任何東西
html, body {
height: 100%;
margin: 0;
}
body {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-flex;
display: flex;
}
...還有 CSS 網格:
body {
display: grid;
height: 100vh;
margin: 0;
place-items: center center;
}
圖片對齊不變形
img {
width: 200px;
height: 200px;
/** 確保圖片按原始寬高比例進行縮放 */
object-fit: cover;
object-position: left top;
transition: 1s;
}
img:hover {
/** 指定圖片顯示的位置,結合鼠標移動+過渡動畫 */
object-position: right bottom;
}
多行截斷,展示省略號
.clamp {
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
html
文本超過 3 行將被截斷,顯示省略號...
<p class="clamp">
展示多行文本,超過 3 行將被截斷,顯示省略號...
</p>
逗號分隔列表
ul > li:not(:last-child)::after {
content: ",";
}
使列表項看起來像一個真實的逗號分隔列表,使用 :not()
偽類,最后一項不會添加逗號