CSS3 入门教程 CSS3 备忘录

2024-02-25 开发教程 CSS3 入门教程 匿名 4

这是 CSS 优点的快速参考速查表,列出了选择器语法、属性、单位和其他有用的信息位。

开始

介绍

CSS 具有丰富的功能,不仅仅是简单的页面布局。

外部 CSS

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

内联 CSS

<h2 style="text-align: center;">Centered text</h2>

<p style="color: blue; font-size: 18px;">Blue, 18-point text</p>

内部 CSS

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

添加类

<div class="classname"></div>
<div class="class1 ... classn"></div>

在一个元素上支持多个类。

!important

.post-title {
color: blue !important;
}

覆盖所有以前的样式规则。

选择器

h1 { }
#job-title { }
div.hero { }
div > p { }

请参阅:选择器

文字颜色

color: #2a2aff;
color: green;
color: rgb(34, 12, 64, 0.6);
color: hsla(30 100% 50% / 0.6);

请参阅:颜色

背景

#背景

background-color: blue;
background-image: url("nyan-cat.gif");
background-image: url("../image.png");

请参阅:背景

字体

.page-title {
font-weight: bold;
font-size: 30px;
font-family: "Courier New";
}

请参阅:字体

位置

.box {
position: relative;
top: 20px;
left: 20px;
}

另见:位置

动画

animation: 300ms linear 0s infinite;

animation: bounce 300ms linear infinite;

参见:动画

注释

/* This is a single line comment */

/* This is a
multi-line comment */

弹性布局

div {
display: flex;
justify-content: center;
}
div {
display: flex;
justify-content: flex-start;
}

请参阅:弹性盒| 灵活技巧

网格布局

#container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}

#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}

请参阅:网格布局

变量和计数器

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

所有 p 标记
在 div 深一层

div + p

P 标签紧跟在 div 之后

div ~ p

以 div 开头的 P 标签

另请参阅: 相邻 /兄弟

/

选择器

属性选择器

a[target]

用一个 target 属性

a[target="_blank"]

在新标签页中打开

a[href^="/index"]

以 /index 开始

[class|="chair"]

以 char 开始

[class*="chair"]

包含 char

[title~="chair"]

包含 char 这个词

a[href$=".doc"]

以 .doc 结束

[type="button"]

指定类型

另请参阅:属性选择器

用户操作伪类

a:link

正常状态下的链接

a:active

处于点击状态的链接

a:hover

用鼠标链接

a:visited

访问过的链接

伪类

p::after

在 p 后添加内容

p::before

在 p 前添加内容

p::first-letter

p 中的第一个字母

p::first-line

p 中的第一行

::selection

由用户选择

::placeholder

占位符属性

:root

文档根元素

:target

突出显示活动锚点

div:empty

没有孩子的元素

p:lang(en)

P 带有 en 语言属性

:not(span)

不是跨度的元素

输入伪类

input:checked

检查输入

input:disabled

禁用输入

input:enabled

启用输入

input:focus

输入有焦点

input:in-range

范围内的值

input:out-of-range

输入值超出范围

input:valid

输入有效值

input:invalid

输入值无效

input:optional

没有必需的属性

input:required

具有必需属性的输入

input:read-only

带有只读属性

input:read-write

没有只读属性

input:indeterminate

具有不确定

状态

结构化伪类

p:first-child

第一个孩子

p:last-child

最后一个孩子

p:first-of-type

首先是某种类型

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

其父母的唯一孩子

CSS 字体

特性

font-family:

<字体>

font-size:

<尺寸>

letter-spacing:

<尺寸>

line-height:

<编号>

font-weight:

<数字> / 粗体 / 正常

font-style:

斜体/正常

text-decoration:

下划线/无

text-align:

左/右
居中/对齐

text-transform:

大写/大写/小写

另见:字体

速记

风格 权重尺寸(必填) 行高 类型(必填)

font:

italic

400

14px

/

1.5

sans-serif

例子

font-family: Arial, sans-serif;
font-size: 12pt;
letter-spacing: 0.02em;

例子

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

CSS 背景

特性

财产 描述

background:

(速记)

background-color:

请参阅:颜色

background-image:

网址(...)

background-position:

左/中/右上
/中/下

background-size:

盖XY

background-clip:

边框框
填充框
内容框

background-repeat:

不重复
重复-x
重复-y

background-attachment:

滚动/固定/本地

速记

颜色 图片 位置X 位置Y 尺寸 重复 附加

background:

#ff0

url(a.jpg)

left

top

/

100pxauto

no-repeat

fixed;

background:

#abc

url(b.png)

center

center

/

cover

repeat-x

local;

例子

background: url(img_man.jpg) no-repeat center;

background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;

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;
width: 500px;
}

另见:max-width/min-width/ max-hight/min-hight

外边距 margin / 内边距 padding

.block-one {
margin: 20px;
padding: 10px;
}

另请参阅:margin/padding

盒子尺寸

.container {
box-sizing: border-box;}

另请参阅:box-sizing

可见性

.invisible-elements {
visibility: hidden;
}

另见:visibility

自适应外边距

div {
margin: auto;
}

另见:margin

溢出

.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 弹性盒

简单的例子

.container {
display: flex;
}

容器 Container

.container > div {
flex: 1 1 auto;
}
.container{
display: flex;
display: inline-flex;
flex-direction: row; /* ltr - default */
flex-direction: row-reverse; /* rtl */
flex-direction: column; /* top-bottom */
flex-direction: column-reverse; /* bottom-top */
flex-wrap: nowrap; /* one-line */
flex-wrap: wrap; /* multi-line */
align-items: flex-start; /* vertical-align to top */
align-items: flex-end; /* vertical-align to bottom */
align-items: center; /* vertical-align to center */
align-items: stretch; /* same height on all (default) */
justify-content: flex-start; /* [xxx ] */
justify-content: center; /* [ xxx ] */
justify-content: flex-end; /* [ xxx] */
justify-content: space-between; /* [x x x] */
justify-content: space-around; /* [ x x x ] */
justify-content: space-evenly; /* [ x x x ] */
}

子类选择器 Child

.container > div {
/* This: */
flex: 1 0 auto;

/* Is equivalent to this: */
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
order: 1;
align-self: flex-start; /* left */
margin-left: auto; /* right */
}

CSS Flexbox 技巧

垂直居中

.container {
display: flex;
}

.container > div {
width: 100px;
height: 100px;
margin: auto;
}

垂直居中 (2)

.container {
display: flex;

/* vertical */
align-items: center;

/* horizontal */
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;
}

一个固定高度的顶部栏和一个动态高度的内容区域。

像表格一样

.container {
display: flex;
}


/* the 'px' values here are just suggested percentages */
.container > .checkbox { flex: 1 0 20px; }
.container > .subject { flex: 1 0 400px; }
.container > .date { flex: 1 0 120px; }

这会创建具有不同宽度的列,但会根据情况相应地调整大小。

垂直

.container {
align-items: center;
}

垂直居中所有项目。

左和右

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

CSS 网格布局

网格模板列

#grid-container {
display: grid;
width: 100px;
grid-template-columns: 20px 20% 60%;
}

fr 相对单位

.grid {
display: grid;
width: 100px;
grid-template-columns: 1fr 60px 1fr;
}

网格间隙

/*The distance between rows is 20px*/
/*The distance between columns is 10px*/
#grid-container {
display: grid;
grid-gap: 20px 10px;
}

CSS 块级网格

#grid-container {
display: block;
}

CSS 网格行

CSS 语法:

  • 网格行:网格行开始/网格行结束;

例子

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

CSS 内联关卡网格

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

minmax() 函数

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

网格行开始和网格行结束

CSS 语法:

  • 网格行开始:自动|行行;
  • 网格行端:自动|行行|跨度n;

例子

grid-row-start: 2;
grid-row-end: span 2;

CSS 网格行的长度值

grid-row-gap: length;

任何合法的长度值,如 px 或 %。0 是默认值

CSS 网格区域

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

对齐项目

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

网格项位于行的右侧(末尾)。

CSS 动态内容

变量

定义 CSS 变量

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

变量用法

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

另请参阅:CSS 变量

计数器

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

另见:计数器集

使用计数器

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 3 技巧

滚动条平滑

html {
scroll-behavior: smooth;
}

单击我,页面将平滑滚动到入门

另见