国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片

CSS 備忘清單

這是一份關(guān)于 CSS 優(yōu)點的快速參考備忘單,列出了選擇器語法、屬性、單位和其他有用的信息

入門

介紹

CSS 功能豐富,不僅僅是布局頁面

<link
  href="./path/to/stylesheet/style.css"
  rel="stylesheet"
  type="text/css"
>

內(nèi)部樣式表 <style>

<style>
  body {
    background-color: linen;
  }
</style>

內(nèi)聯(lián)樣式 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;
}

覆蓋所有以前的樣式規(guī)則

選擇器

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

變量和計數(shù)器

counter-set: subsection;
counter-increment: subsection;
counter-reset: subsection 0;
:root {
  --bg-color: brown;
}
element {
  background-color: var(--bg-color);
}

查看: 動態(tài)內(nèi)容

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;
}

基礎(chǔ)

選擇器說明
*所有元素
div所有 div 標簽
.classname具有類的所有元素
#idname帶 ID 的元素
div,p所有 div 和段落
#idname *#idname 中的所有元素

另見: 元素 / / ID / 通配 選擇器

組合器

選擇器說明
div.classname具有特定類名的 div
div#idname具有特定 ID 的 div
div pdiv 中的所有段落
div > p父元素是 div 的 P 標簽
div + pdiv 之后的第一個同級 P 標簽
div ~ pdiv 之后所有的同級 P 標簽

另見: 相鄰兄弟 / 通用兄弟 / 選擇器

屬性選擇器

選擇器說明
a[target]帶有 target 屬性 #
a[target="_blank"]在新標簽中打開 #
a[href^="/index"]/index 開頭 #
[class|="chair"]chair開頭 #
[class*="chair"]包含chair #
[title~="chair"]包含單詞 chair #
a[href$=".doc"].doc 結(jié)尾 #
[type="button"]指定類型 #

另見: 屬性選擇器

用戶操作偽類

選擇器說明
a:link鏈接正常 #
a:active鏈接處于點擊狀態(tài) #
a:hover鼠標懸停鏈接 #
a:visited訪問鏈接 #

/* 未訪問鏈接 */
a:link { color: blue; }        
/* 已訪問鏈接 */
a:visited { color: purple; }   
/* 用戶鼠標懸停 */
a:hover { background: yellow; }
/* 激活鏈接 */
a:active { color: red; }       

偽類

選擇器說明
p::after在 p 之后添加內(nèi)容 #
p::before在 p 之前添加內(nèi)容 #
p::first-letterp中的第一個字母 #
p::first-linep 中的第一行 #
::selection由用戶選擇 #
::placeholder占位符 屬性 #
:root文檔根元素 #
:target突出顯示活動錨點 #
div:empty沒有子元素的元素 #
p:lang(en)帶有 en 語言屬性的 P #
:not(span)不是跨度的元素 #
:hostshadowDOM 中選擇自定義元素 #
::backdrop處于全屏模式的元素樣式 #
::markerli 項目符號或者數(shù)字 #
::file-selector-buttontype="file" input 按鈕 #

輸入偽類

選擇器說明
input:checked檢查 input #
input:disabled禁用 input #
input:enabled啟用的 input #
input:default有默認值的元素 #
input:blank空的輸入框 #
input:focusinput 有焦點 #
input:in-range范圍內(nèi)的值 #
input:out-of-rangeinput 值超出范圍 #
input:validinput 有效值 #
input:invalidinput 無效值 #
input:optional沒有必需的屬性 #
input:required帶有必需屬性的 input #
input:read-only具有只讀屬性 #
input:read-write沒有只讀屬性 #
input:indeterminate帶有 indeterminate 狀態(tài) #

結(jié)構(gòu)偽類

選擇器說明
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第一行應(yīng)用樣式 #

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; /* 關(guān)鍵字 */

全局值

/* 全局值 */
color: inherit;
color: initial;
color: unset;

CSS 背景

屬性

屬性說明
background:(速記)
background-color:查看: Colors
background-image:一個或者多個背景圖像
background-position:背景圖片設(shè)置初始位置
background-size:背景圖片大小
background-clip:背景(圖片或顏色)是否延伸到邊框、內(nèi)邊距盒子、內(nèi)容盒子下面
background-repeat:圖像重復(fù)方式
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       圖片大小     圖像重復(fù)方式  位置是在視口內(nèi)固定

示例

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 {
  /* 設(shè)置的邊框和補白的值是包含在 width 內(nèi)的 */
  box-sizing: border-box;
}

另見: box-sizing

能見度

.invisible-elements {
  visibility: hidden; /* 隱藏元素 */
}

另見: Visibility

Auto 關(guān)鍵字

div {
  /* 覽器自己選擇一個合適的外邊距 */
  margin: auto;
}

另見: 邊距(margin)

溢出(Overflow)

.small-block {
  /* 瀏覽器總是顯示滾動條 */
  overflow: scroll;
}

另見: 溢出(overflow)

CSS 動畫

速記

animation:  bounce   300ms      linear     100ms    infinite   alternate-reverse  both                   reverse  
            ┈┬┈┈     ┈┬┈┈┈      ┈┬┈┈┈┈     ┈┈┬┈┈    ┈┈┈┬┈┈┈┈   ┈┈┬┈┈┈┈┈┈┈┈┈┈┈┈┈┈  ┈┈┬┈┈┈                 ┈┈┬┈┈┈
            動畫名    動畫時間     緩動函數(shù)    延遲     運行的次數(shù)   動畫是否反向播放      如何將樣式應(yīng)用于其目標    是否運行或者暫停

屬性

屬性說明
animation:(速記)
animation-name:動畫名 #
animation-duration:動畫周期的時長 #
animation-timing-function:緩動函數(shù) #
animation-delay:延遲 #
animation-iteration-count:運行的次數(shù) #
animation-direction:動畫是否反向播放 #
animation-fill-mode:如何將樣式應(yīng)用于其目標 #
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:每個項目兩側(cè)的間隔相等,項目之間的間隔比項目與邊框的間隔大一倍

╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆   ╭┈┈╮        ╭┈╮       ╭┈┈┈╮   ┆
┆   ╰┈┈╯        ╰┈╯       ╰┈┈┈╯   ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯

上面示例,假設(shè)主軸為從左到右

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 定義項目的排列順序。數(shù)值越小,排列越靠前,默認為 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;
}

一個固定高度的頂部欄和一個動態(tài)高度的內(nèi)容區(qū)域

Table-like 像表格

.container {
  display: flex;
}
/* 這里的“px”值只是建議的百分比 */
.container > .checkbox { flex: 1 0 20px; }
.container > .subject  { flex: 1 0 400px; }
.container > .date     { flex: 1 0 120px; }

這會創(chuàng)建具有不同寬度的列,但會根據(jù)情況相應(yīng)地調(diào)整大小

Vertical 垂直的

.container {
  align-items: center;
}

垂直居中所有項目

左和右

.menu > .left  { align-self: flex-start; }
.menu > .right { align-self: flex-end; }

CSS Grid 網(wǎng)格布局

網(wǎng)格模板列

#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 網(wǎng)格間隙

/* 行間距為 20px */
/* 列之間的距離是 10px */
#grid-container {
  display: grid;
  grid-gap: 20px 10px;
}

CSS 網(wǎng)格行

CSS 語法:

  • grid-row: grid-row-start / grid-row-end;

實例

.item {
  grid-row: 1 / span 2;
}

CSS 塊級網(wǎng)格

#grid-container {
    display: block;
}

CSS 內(nèi)聯(lián)級別網(wǎng)格

#grid-container {
  display: inline-grid;
}

CSS 網(wǎng)格行間隙

grid-row-gap: length;

任何合法的長度值,例如 px%。0 是默認值

CSS 網(wǎng)格區(qū)域

.item1 {
  grid-area: 2 / 1 / span 2 / span 3;
}

minmax() 函數(shù)

.grid {
  display: grid;
  grid-template-columns: 100px minmax(100px, 500px) 100px; 
}

定義了一個長寬范圍的閉區(qū)間

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 網(wǎng)格模板區(qū)域

.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;
}

網(wǎng)格項目位于行的右側(cè)(末尾)

對齊項目

#container {
  display: grid;
  align-items: start;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-gap: 10px;
}

CSS 動態(tài)內(nèi)容

變量

定義 CSS 變量

:root {
  --first-color: #16f;
  --second-color: #ff7;
}

變量用法

#firstParagraph {
  background-color: var(--first-color);
  color: var(--second-color);
}

另見: CSS Variable

計數(shù)器

/* 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

使用計數(shù)器

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 函數(shù)

calc()

width: calc(100% - 80px);

聲明 CSS 屬性值時執(zhí)行一些計算

clamp()

font-size: clamp(1rem, 10vw, 2rem);

設(shè)置隨窗口大小改變的字體大小

attr()

p:before {
  content: attr(data-foo) " ";
}

獲取選擇到的元素的某一 HTML 屬性值

counter()

返回一個代表計數(shù)器的當前值的字符串

<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);
}

嵌套計數(shù)器,返回表示指定計數(shù)器當前值的連接字符串

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);
}

用戶代理定義的環(huán)境變量值插入你的 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() 軌道列表的重復(fù)片段

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])

定義了一個長寬范圍的閉區(qū)間

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;
}

點擊我頁面會平滑滾動到入門

忽略用作間距的換行符 <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;
}

有助于在不同的瀏覽器之間強制樣式一致性,并為樣式元素提供干凈的盒子

設(shè)置光標樣式

body {
  caret-color: red;
}

設(shè)置整個頁面灰色

html {
  -webkit-filter: grayscale(.95);
}

上面示例設(shè)置了當前卡片灰色

使用 unset 而不是重置所有屬性

使用 all 速記來指定元素的所有屬性。將值設(shè)置為 unset 會將元素的屬性更改為其初始值:

button {
  all: unset;
}

注意:IE11 不支持 allunset 速記

超出顯示省略號

p {
  overflow: hidden;/*超出部分隱藏*/
  /* 超出部分顯示省略號 */
  text-overflow:ellipsis;
  /* 規(guī)定段落中的文本不進行換行 */
  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;
}

一行文本截斷顯示省略號 (...)

將文本截斷到特定的行數(shù)

.overflow-truncate {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

多行文本截斷到特定的行數(shù),末尾顯示省略號 (...)

計算函數(shù)

div {
  width: calc(100% - 30px);
  height: calc(100% - 30px);
}

calc() CSS 函數(shù)允許您在指定 CSS 屬性值時執(zhí)行計算

粘性定位元素

.sticky {
  position: sticky;
  top: 0;
}

屬性 sticky 能在滾動到頂部的位置固定住元素

使用帶有空鏈接的屬性選擇器

a[href^="http"]:empty::before {
  content: attr(href);
}

如果 <a> 標簽里面沒有內(nèi)容,將 href 的值作為內(nèi)容展示

使用 :root 表示靈活類型

響應(yīng)式布局中的字體大小應(yīng)該能夠根據(jù)每個視口進行調(diào)整,您可以使用 :root 根據(jù)視口高度和寬度計算字體大小

:root {
  font-size: calc(1vw + 1vh + .5vmin);
}

您可以根據(jù) :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;
}

可用于 輪播圖 效果,效果預(yù)覽

類似 contenteditable 的樣式

div {
  -webkit-user-modify: 
    read-write-plaintext-only;
}

通過樣式來控制一個元素 div 是否可以編輯

定義容器的長寬比

div {
  aspect-ratio: 1/1 
}

屬性 aspect-ratio 可以非常容易的定義一個容器的長寬比

等寬表格單元格

嘗試使用 table-layout: fixed 以保持單元格寬度相等:

table {
  table-layout: fixed;
}

利用屬性選擇器來選擇空鏈接

<a> 元素沒有文本內(nèi)容,但有 href 屬性的時候,顯示它的 href 屬性:

a[href^="http"]:empty::before {
  content: attr(href);
}

給 “默認” 鏈接定義樣式

給 “默認” 鏈接定義樣式:

a[href]:not([class]) {
  color: #008000;
  text-decoration: underline;
}

通常沒有 class 屬性,以上樣式可以甄別它們,而且不會影響其它樣式

用 rem 調(diào)整全局大??;用 em 調(diào)整局部大小

在根元素設(shè)置基本字體大小后 (html { font-size: 100%; }), 使用 em 設(shè)置文本元素的字體大小:

h2 { 
  font-size: 2em;
}
p {
  font-size: 1em;
}

然后設(shè)置模塊的字體大小為 rem:

article {
  font-size: 1.25rem;
}
aside .module {
  font-size: .9rem;
}

現(xiàn)在,每個模塊變得獨立,更容易、靈活的樣式便于維護

隱藏沒有靜音、自動播放的影片

這是一個自定義用戶樣式表的不錯的技巧。避免在加載頁面時自動播放。如果沒有靜音,則不顯示視頻:

video[autoplay]:not([muted]) {
  display: none;
}

再次,我們利用了 :not() 的優(yōu)點

為更好的移動體驗,為表單元素設(shè)置字體大小

當觸發(fā) <select> 的下拉列表時,為了避免表單元素在移動瀏覽器(iOS Safari 和其它)上的縮放,加上font-size:

input[type="text"],
input[type="number"],
select,
textarea {
  font-size: 16px;
}

使用指針事件來控制鼠標事件

指針事件允許您指定鼠標如何與其觸摸的元素進行交互。要禁用按鈕上的默認指針事件,例如:

button:disabled {
  opacity: .5;
  pointer-events: none;
}

就這么簡單

在用作間距的換行符上設(shè)置 display: none

用戶使用額外的換行符

br + br {
  display: none;
}

子元素選中父元素

div:has(img) {
  background: black;
}

設(shè)置包含子元素 imgdiv 元素樣式,還可以嵌套:

div:has(h2):has(ul) {
  background: black;
}