CSS笔记
CSS3 全面概述
CSS3 是 CSS 的最新版本,为前端开发人员提供了丰富的工具,能够轻松实现现代网页设计需求。以下将详细介绍 CSS3 的选择器、盒子模型、属性、Flexbox 布局及动画等,并提供相应的代码示例。
选择器
CSS3 新增和扩展了许多选择器,使选择元素更加灵活和精确。以下是 CSS3 的选择器类型和示例代码:
简单选择器
- 元素选择器: 选择 HTML 元素
1
2
3p {
color: blue;
} - 类选择器: 选择拥有特定类名的元素
1
2
3.example {
color: red;
} - ID 选择器: 选择拥有特定 ID 的元素
1
2
3#header {
background-color: grey;
} - 属性选择器: 选择带有特定属性的元素
1
2
3a[target="_blank"] {
color: green;
}
组合选择器
子选择器: 选择直接子元素
1
2
3div > p {
color: purple;
}后代选择器: 选择任何后代元素
1
2
3div p {
color: yellow;
}相邻兄弟选择器: 选择紧接在另一个元素之后的元素
1
2
3h1 + p {
margin-top: 0;
}通用兄弟选择器: 选择在另一个元素之后的所有兄弟元素
1
2
3h1 ~ p {
color: orange;
}
伪类选择器
:hover: 选择鼠标悬停的元素1
2
3a:hover {
text-decoration: underline;
}:nth-child(n): 选择父元素的第 n 个子元素1
2
3tr:nth-child(odd) {
background-color: #f2f2f2;
}:not(selector): 选择不匹配指定选择器的元素1
2
3input:not([type="submit"]) {
background-color: lightyellow;
}
伪元素选择器
::before: 在元素内容之前插入内容1
2
3
4p::before {
content: "Note: ";
font-weight: bold;
}::after: 在元素内容之后插入内容1
2
3
4p::after {
content: " (End of paragraph)";
color: grey;
}
盒模型
CSS 盒模型描述了元素在页面中的布局,包括内容、内边距(padding)、边框(border)和外边距(margin)。
盒模型的组成
- 内容区域(content box): 元素的实际内容部分
- 内边距(padding): 内容与边框之间的空白区域
- 边框(border): 围绕内容和内边距的线
- 外边距(margin): 元素与外部元素之间的空白区域
相关属性及其值
width和height: 定义内容区域的宽度和高度1
2
3
4div {
width: 200px;
height: 100px;
}padding: 定义内边距,可以单独设置每个方向的内边距1
2
3
4
5
6
7div {
padding: 10px; /* 所有方向 */
padding-top: 20px; /* 仅顶部 */
padding-right: 15px; /* 仅右侧 */
padding-bottom: 10px; /* 仅底部 */
padding-left: 5px; /* 仅左侧 */
}margin: 定义外边距,可以单独设置每个方向的外边距1
2
3
4
5
6
7div {
margin: 10px; /* 所有方向 */
margin-top: 20px; /* 仅顶部 */
margin-right: 15px; /* 仅右侧 */
margin-bottom: 10px; /* 仅底部 */
margin-left: 5px; /* 仅左侧 */
}border: 设置元素边框的样式、宽度和颜色1
2
3
4div {
border: 1px solid black; /* 所有边框 */
border-top: 2px dashed blue; /* 仅顶部边框 */
}box-sizing: 决定盒子模型的计算方式1
2
3
4div {
box-sizing: content-box; /* 默认值 */
box-sizing: border-box; /* 包含内边距和边框 */
}
盒模型的示例
1 | div { |
属性详解
content-box: 默认值。元素的宽度和高度仅包括内容区域,不包括内边距和边框。border-box: 元素的宽度和高度包括内容区域、内边距和边框。
属性(CSS Properties)
CSS3 提供了大量属性来控制元素的外观和行为。以下是常见属性及其可能的值和含义。
颜色与背景
color: 设置文本颜色1
2
3
4
5p {
color: #333; /* 十六进制颜色 */
color: rgb(255, 0, 0); /* RGB颜色 */
color: rgba(255, 0, 0, 0.5); /* 带透明度的颜色 */
}background-color: 设置元素的背景颜色1
2
3div {
background-color: #f0f0f0;
}background-image: 设置背景图像1
2
3body {
background-image: url('background.webp');
}background-repeat: 设置背景图像的重复方式repeat: 水平和垂直方向重复(默认)no-repeat: 不重复repeat-x: 仅在水平方向重复repeat-y: 仅在垂直方向重复1
2
3body {
background-repeat: no-repeat;
}
background-size: 设置背景图像的大小auto: 保持原始尺寸(默认)cover: 背景图像填满整个容器contain: 背景图像保持宽高比缩放到最大而不超出容器1
2
3body {
background-size: cover;
}
background-position: 设置背景图像的位置left top: 左上角center: 居中right bottom: 右下角1
2
3body {
background-position: center;
}
文字与字体
font-family: 设置字体族1
2
3p {
font-family: Arial, sans-serif;
}font-size: 设置字体大小1
2
3
4
5p {
font-size: 16px; /* 固定大小 */
font-size: 1em; /* 相对大小 */
font-size: 100%; /* 相对于父元素的大小 */
}font-weight: 设置字体的粗细normal: 正常bold: 粗体100到900: 数值表示的粗细程度1
2
3
4p {
font-weight: bold;
font-weight: 700;
}
font-style: 设置字体样式normal: 正常italic: 斜体oblique: 斜体(不同于italic,倾斜效果不同)1
2
3p {
font-style: italic;
}
line-height: 设置行高1
2
3p {
line-height: 1.5;
}text-align: 设置文本对齐方式left: 左对齐right: 右对齐center: 居中justify: 两端对齐1
2
3p {
text-align: justify;
}
text-decoration: 设置文本的装饰线none: 无underline: 下划线overline: 上划线line-through: 删除线1
2
3a {
text-decoration: underline;
}
text-transform: 设置文本转换none: 无转换capitalize: 每个单词的首字母大写uppercase: 转为大写lowercase: 转为小写1
2
3p {
text-transform: uppercase;
}
盒子模型相关属性
display: 设置元素的显示方式block: 块级元素inline: 内联元素inline-block: 内联块级元素none: 不显示1
2
3div {
display: inline-block;
}
visibility: 控制元素的可见性visible: 可见(默认)hidden: 隐藏,但仍占据空间collapse: 对表格元素隐藏且不占据空间1
2
3div {
visibility: hidden;
}
overflow: 控制内容溢出时的行为visible: 溢出内容可见(默认)hidden: 溢出内容隐藏scroll: 溢出内容可滚动auto: 如果溢出内容则出现滚动条1
2
3div {
overflow: auto;
}
position: 设置元素的定位方式static: 默认定位,不设置偏移(默认)relative: 相对定位,元素相对于其正常位置进行偏移absolute: 绝对定位,元素相对于最近的已定位祖先元素进行偏移fixed: 固定定位,元素相对于浏览器窗口进行偏移sticky: 粘性定位,元素根据用户的滚动在相对和固定定位之间切换1
2
3
4
5div {
position: absolute;
top: 50px;
left: 100px;
}
z-index: 设置元素的堆叠顺序1
2
3
4div {
position: absolute;
z-index: 10;
}
过渡与动画
transition: 设置元素的过渡效果1
2
3div {
transition: all 0.5s ease;
}animation: 设置动画1
2
3
4
5
6
7
8
9
10
11
12
13@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
div {
animation-name: example;
animation-duration: 4s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
}transform: 设置元素的 2D 或 3D 变换translate(x, y): 平移rotate(angle): 旋转scale(x, y): 缩放skew(x-angle, y-angle): 倾斜1
2
3div {
transform: rotate(45deg);
}
Flexbox 布局
C SS Flexbox(弹性盒模型)提供了一种更灵活的布局方式,适用于复杂的页面布局。以下是 Flexbox 的主要属性及其值:
容器属性
display: flex: 将容器设为 Flex 容器1
2
3.container {
display: flex;
}flex-direction: 设置主轴方向row: 水平从左到右(默认)row-reverse: 水平从右到左column: 垂直从上到下column-reverse: 垂直从下到上1
2
3.container {
flex-direction: column;
}
flex-wrap: 控制是否换行nowrap: 不换行(默认)wrap: 换行,溢出的项目移到下一行wrap-reverse: 换行,下一行项目排列在上方1
2
3.container {
flex-wrap: wrap;
}
justify-content: 设置主轴上的对齐方式flex-start: 从主轴的起点对齐(默认)flex-end: 从主轴的终点对齐center: 居中对齐space-between: 两端对齐,中间均匀分布space-around: 项目均匀分布,项目之间的间距相等space-evenly: 项目之间和项目与容器边框之间的间距相等1
2
3.container {
justify-content: space-between;
}
align-items: 设置交叉轴上的对齐方式stretch: 拉伸项目以填满容器(默认)flex-start: 从交叉轴的起点对齐flex-end: 从交叉轴的终点对齐center: 居中对齐baseline: 基线对齐1
2
3.container {
align-items: center;
}
align-content: 设置多根轴线的对齐方式(当项目换行时)stretch: 拉伸项目以填满容器(默认)flex-start: 从交叉轴的起点对齐flex-end: 从交叉轴的终点对齐center: 居中对齐space-between: 两端对齐,中间均匀分布space-around: 项目均匀分布,项目之间的间距相等1
2
3.container {
align-content: space-around;
}
子项属性
order: 设置子项的排列顺序1
2
3.item {
order: 2;
}flex-grow: 定义项目的放大比例1
2
3.item {
flex-grow: 1; /* 项目将分配剩余空间 */
}flex-shrink: 定义项目的缩小比例1
2
3.item {
flex-shrink: 0; /* 项目不会缩小 */
}flex-basis: 定义项目的初始大小1
2
3.item {
flex-basis: 100px; /* 固定大小 */
}align-self: 允许单个项目在交叉轴上有不同的对齐方式1
2
3.item {
align-self: flex-end;
}
动画与过渡
过渡效果
transition: 设置过渡效果的速率和持续时间transition-property: 设置要过渡的属性(如width、height、background-color等)transition-duration: 设置过渡效果的持续时间transition-timing-function: 设置过渡效果的速率曲线ease: 慢开始,快中间,慢结束(默认)linear: 匀速ease-in: 慢开始ease-out: 慢结束ease-in-out: 慢开始和慢结束
transition-delay: 设置过渡效果的延迟时间1
2
3
4
5
6div {
transition: width 2s ease-in-out;
}
div:hover {
width: 300px;
}
动画
@keyframes: 定义动画的关键帧1
2
3
4@keyframes example {
0% { background-color: red; }
100% { background-color: yellow; }
}animation: 应用动画animation-name: 指定要使用的关键帧名称animation-duration: 设置动画的持续时间animation-timing-function: 设置动画的速率曲线animation-delay: 设置动画的延迟时间animation-iteration-count: 设置动画的播放次数(infinite表示无限次)animation-direction: 设置动画的播放方向(normal、reverse、alternate)animation-fill-mode: 设置动画结束时元素的状态(forwards、backwards)animation-play-state: 控制动画的播放状态(paused、running)1
2
3
4
5
6
7div {
animation-name: example;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-direction: alternate;
}
transform: 定义元素的变换(平移、旋转、缩放、倾斜)translate(x, y): 平移rotate(angle): 旋转scale(x, y): 缩放skew(x-angle, y-angle): 倾斜1
2
3div {
transform: rotate(45deg);
}
perspective: 设置 3D 透视视角1
2
3div {
perspective: 500px;
}transform-style: 设置子元素是否在 3D 空间中呈现1
2
3div {
transform-style: preserve-3d;
}backface-visibility: 控制元素的背面是否可见1
2
3div {
backface-visibility: hidden;
}
通过理解和掌握这些 CSS3 的功能和属性,开发者可以创建出丰富多彩、功能强大的网页布局和视觉效果。CSS3 的灵活性和强大性能使得它成为现代网页设计的核心技术之一。









