這是一份關(guān)于 CSS 優(yōu)點的快速參考備忘單,列出了選擇器語法、屬性、單位和其他有用的信息
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>
<div class="classname"></div>
<div class="class1 ... classn"></div>
支持一個元素上的多個類
.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
/* 這是一行注釋 */
/* 這是
多行注釋 */
div {
display: flex;
justify-content: center;
}
div {
display: flex;
justify-content: flex-start;
}
查看: Flexbox | Flex Tricks
#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);
}
查看: 動態(tài)內(nèi)容
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;
}
| 選擇器 | 說明 |
|---|---|
p::after | 在 p 之后添加內(nèi)容 # |
p::before | 在 p 之前添加內(nèi)容 # |
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 項目符號或者數(shù)字 # |
::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 | 范圍內(nèi)的值 # |
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 狀態(tài) # |
| 選擇器 | 說明 |
|---|---|
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)用樣式 # |
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-family: 'Glegoo';
src: url('../Glegoo.woff');
}
color: red;
color: orange;
color: tan;
color: rebeccapurple;
更多標準顏色名稱
color: #090;
color: #009900;
color: #090a;
color: #009900aa;
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%);
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;
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%);
.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)
.container {
/* 設(shè)置的邊框和補白的值是包含在 width 內(nèi)的 */
box-sizing: border-box;
}
另見: box-sizing
.invisible-elements {
visibility: hidden; /* 隱藏元素 */
}
另見: Visibility
div {
/* 覽器自己選擇一個合適的外邊距 */
margin: auto;
}
另見: 邊距(margin)
.small-block {
/* 瀏覽器總是顯示滾動條 */
overflow: scroll;
}
另見: 溢出(overflow)
animation: bounce 300ms linear 100ms infinite alternate-reverse both reverse
┈┬┈┈ ┈┬┈┈┈ ┈┬┈┈┈┈ ┈┈┬┈┈ ┈┈┈┬┈┈┈┈ ┈┈┬┈┈┈┈┈┈┈┈┈┈┈┈┈┈ ┈┈┬┈┈┈ ┈┈┬┈┈┈
動畫名 動畫時間 緩動函數(shù) 延遲 運行的次數(shù) 動畫是否反向播放 如何將樣式應(yīng)用于其目標 是否運行或者暫停
/* @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;
.one('webkitAnimationEnd oanimationend msAnimationEnd animationend')
.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: flex-start | flex-end | center | space-between
flex-start:左對齊(默認值)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆╭┈┈╮╭┈╮╭┈┈┈╮ ┆
┆╰┈┈╯╰┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
flex-end:右對齊
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
center: 居中
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
space-between:兩端對齊,項目之間的間隔都相等
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆╭┈┈╮ ╭┈╮ ╭┈┈┈╮┆
┆╰┈┈╯ ╰┈╯ ╰┈┈┈╯┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
space-around:每個項目兩側(cè)的間隔相等,項目之間的間隔比項目與邊框的間隔大一倍
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ╭┈╮ ╭┈┈┈╮ ┆
┆ ╰┈┈╯ ╰┈╯ ╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
上面示例,假設(shè)主軸為從左到右
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: row | row-reverse | column | column-reverse;
╭┈┈╮ ▲ ╭┈┈╮ ┆
╰┈┈╯ ┆ ╰┈┈╯ ┆
╭┈┈╮ ┆ ╭┈┈╮ ┆
╰┈┈╯ ┆ ╰┈┈╯ ┆ ┈┈┈┈┈┈┈┈┈┈┈? ?┈┈┈┈┈┈┈┈┈┈┈
╭┈┈╮ ┆ ╭┈┈╮ ┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮
╰┈┈╯ ┆ ╰┈┈╯ ▼ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯
┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈ ┈┬┈┈┈┈┈┈┈┈┈┈┈
column-reverse column row row-reverse
屬性決定主軸的方向(即項目的排列方向)
align-items: flex-start | flex-end | center | baseline | stretch;
? flex-start(起點對齊) ? flex-end(終點對齊)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ┆ ┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ┆
┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆
┆ ┆ ┆ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? center(中點對齊) ? stretch(占滿整個容器的高度)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ╭┈┈╮ ┆
┆ ╭┈┈╮ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╭┈┈╮ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ┆ ┆ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈╯ ┆ ┆ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? baseline(第一行文字的基線對齊)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈╮ ┆
┆ ┆ ┆ ╭┈┈┈┈╮ ╭┈┈┈┈╮ ┆ ┆ ╭┈┈┈┈╮╭┈┈┈┈╮┆
┆ ┆ text ┆ ┆text┆ ┆text┆ ┆ text ┆ ┆text┆┆text┆┆
┆ ┆ ┆ ╰┈┈┈┈╯ ┆ ┆ ┆ ┆ ╰┈┈┈┈╯┆ ┆┆
┆ ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯ ╰┈┈┈┈┈┈╯ ╰┈┈┈┈╯┆
┆ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
? flex-start(起點對齊) ? flex-end(終點對齊)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆ ┆ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆ ┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆
┆ ╭┈┈┈╮╭╮ ┆ ┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆
┆ ╰┈┈┈╯╰╯ ┆ ┆ ╭┈┈┈╮╭╮ ┆
┆ ┆ ┆ ╰┈┈┈╯╰╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? center(中點對齊) ? stretch(滿整個交叉軸)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ┆ ┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆
┆ ╭┈┈╮╭┈╮╭┈┈┈╮╭╮╭┈┈┈┈╮ ┆ ┆ ┆ ┆┆ ┆┆ ┆┆┆┆ ┆ ┆
┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆ ┆ ╰┈┈╯╰┈╯╰┈┈┈╯╰╯╰┈┈┈┈╯ ┆
┆ ╭┈┈┈╮╭╮ ┆ ┆ ╭┈┈┈╮╭╮╭┈╮ ┆
┆ ╰┈┈┈╯╰╯ ┆ ┆ ┆ ┆┆┆┆ ┆ ┆
┆ ┆ ┆ ╰┈┈┈╯╰╯╰┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
? space-between(兩端對齊) ? space-around(均勻分布項目)
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮ ┆ ┆ ┆
┆ ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯ ┆ ┆ ╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮╭┈┈╮ ┆
┆ ┆ ┆ ╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯╰┈┈╯ ┆
┆ ┆ ┆ ┆
┆ ┆ ┆ ╭┈┈╮ ┆
┆ ╭┈┈╮ ┆ ┆ ╰┈┈╯ ┆
┆ ╰┈┈╯ ┆ ┆ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
.item {
order: <integer>;
}
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ╭┈┈┈┈┈┈┈┈┈╮
┆ ╭1┈╮ ╭1┈┈╮ ╭1┈╮ ╭2┈╮ ╭3┈┈┈┈┈┈╮ ┆ ┆ ╭2┈┈┈┈╮ ┆
┆ ╰┈┈╯ ╰┈┈┈╯ ╰┈┈╯ ╰┈┈╯ ╰┈┈┈┈┈┈┈╯ ┆ ┆ ╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ┆ ╭2┈┈┈┈╮ ┆
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮ ┆ ╰┈┈┈┈┈╯ ┆
┆ ╭-┈┈╮ ╭┈┈┈╮ ╭┈┈┈┈┈┈┈┈╮ ╭┈┈┈╮ ┆ ┆ ╭99┈┈┈╮ ┆
┆ ┆-1 ┆ ┆ 1 ┆ ┆ 2 ┆ ┆ 5 ┆ ┆ ┆ ┆ ┆ ┆
┆ ╰┈┈┈╯ ╰┈┈┈╯ ╰┈┈┈┈┈┈┈┈╯ ╰┈┈┈╯ ┆ ┆ ╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯ ╰┈┈┈┈┈┈┈┈┈╯
屬性 order 定義項目的排列順序。數(shù)值越小,排列越靠前,默認為 0
.item {
flex-grow: <number>; /* default 0 */
}
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈┈1┈┈╮╭┈┈2┈┈╮╭┈┈1┈┈╮ ┆
┆ ╰┈┈┈┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
╭┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╮
┆ ╭┈1┈╮╭┈┈┈┈2┈┈┈┈╮╭┈1┈╮ ┆
┆ ╰┈┈┈╯╰┈┈┈┈┈┈┈┈┈╯╰┈┈┈╯ ┆
╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
屬性 flex-grow 定義項目的放大比例,默認為0,即如果存在剩余空間,也不放大
.container {
display: flex;
}
.container > div {
width: 100px;
height: 100px;
margin: auto;
}
.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ū)域
.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)整大小
.container {
align-items: center;
}
垂直居中所有項目
.menu > .left { align-self: flex-start; }
.menu > .right { align-self: flex-end; }
#grid-container {
display: grid;
width: 100px;
grid-template-columns: 20px 20% 60%;
}
.grid {
display: grid;
width: 100px;
grid-template-columns: 1fr 60px 1fr;
}
/* 行間距為 20px */
/* 列之間的距離是 10px */
#grid-container {
display: grid;
grid-gap: 20px 10px;
}
CSS 語法:
.item {
grid-row: 1 / span 2;
}
#grid-container {
display: block;
}
#grid-container {
display: inline-grid;
}
grid-row-gap: length;
任何合法的長度值,例如 px 或 %。0 是默認值
.item1 {
grid-area: 2 / 1 / span 2 / span 3;
}
.grid {
display: grid;
grid-template-columns: 100px minmax(100px, 500px) 100px;
}
定義了一個長寬范圍的閉區(qū)間
CSS 語法:
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;
}
.item {
grid-area: nav;
}
.grid-container {
display: grid;
grid-template-areas:
'nav nav . .'
'nav nav . .';
}
#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 變量
: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, ".") " ";
}
width: calc(100% - 80px);
聲明 CSS 屬性值時執(zhí)行一些計算
font-size: clamp(1rem, 10vw, 2rem);
設(shè)置隨窗口大小改變的字體大小
p:before {
content: attr(data-foo) " ";
}
獲取選擇到的元素的某一 HTML 屬性值
返回一個代表計數(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]
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ù)器當前值的連接字符串
<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(200px)
fit-content(5cm)
fit-content(30vw)
fit-content(100ch)
將給定大小夾緊為可用大小
從一個逗號分隔的表達式列表中選擇最大(正方向)的值作為屬性的值
width: max(10vw, 4em, 80px);
例子中,寬度最小會是 80px,除非視圖寬度大于 800px 或者是一個 em 比 20px 寬
width: min(1vw, 4em, 80px);
從逗號分隔符表達式中選擇一個最小值作為 CSS 的屬性值
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(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ū)間
background: url("topbanner.png") #00D no-repeat fixed;
list-style: square url(http://www.example.com/redball.png)
:root {
--main-bg-color: pink;
}
body {
background-color: var(--main-bg-color);
}
代替元素中任何屬性中的值的任何部分
p {
white-space:nowrap;
}
p {
word-break:break-all; /* 英文 */
white-space:pre-wrap; /* 中文 */
}
html {
scroll-behavior: smooth;
}
點擊我頁面會平滑滾動到入門
br + br {
display: none;
}
:empty {
display: none;
}
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
有助于在不同的瀏覽器之間強制樣式一致性,并為樣式元素提供干凈的盒子
body {
caret-color: red;
}
html {
-webkit-filter: grayscale(.95);
}
上面示例設(shè)置了當前卡片灰色
使用 all 速記來指定元素的所有屬性。將值設(shè)置為 unset 會將元素的屬性更改為其初始值:
button {
all: unset;
}
注意:IE11 不支持 all 和 unset 速記
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;
}
一行文本截斷顯示省略號 (...)
.overflow-truncate {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
多行文本截斷到特定的行數(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)容展示
響應(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ù)覽
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 屬性,以上樣式可以甄別它們,而且不會影響其它樣式
在根元素設(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)點
當觸發(fā) <select> 的下拉列表時,為了避免表單元素在移動瀏覽器(iOS Safari 和其它)上的縮放,加上font-size:
input[type="text"],
input[type="number"],
select,
textarea {
font-size: 16px;
}
指針事件允許您指定鼠標如何與其觸摸的元素進行交互。要禁用按鈕上的默認指針事件,例如:
button:disabled {
opacity: .5;
pointer-events: none;
}
就這么簡單
display: none用戶使用額外的換行符
br + br {
display: none;
}
div:has(img) {
background: black;
}
設(shè)置包含子元素 img 的 div 元素樣式,還可以嵌套:
div:has(h2):has(ul) {
background: black;
}
感谢您访问我们的网站,您可能还对以下资源感兴趣:
国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片