HTML/CSS
2026/1/15大约 2 分钟
HTML/CSS
HTML5 新特性
语义化标签
<header>头部</header>
<nav>导航</nav>
<main>主内容</main>
<article>文章</article>
<section>区块</section>
<aside>侧边栏</aside>
<footer>底部</footer>新增 API
- Canvas / SVG
- Web Storage(localStorage/sessionStorage)
- Web Worker
- WebSocket
- Geolocation
- Drag and Drop
CSS 选择器
选择器优先级
!important > 内联样式 > ID > 类/伪类/属性 > 元素/伪元素 > 通配符
权重计算:
- 内联样式:1000
- ID:100
- 类/伪类/属性:10
- 元素/伪元素:1常用选择器
/* 属性选择器 */
[type="text"] { }
[class^="icon-"] { } /* 以 icon- 开头 */
[class$="-active"] { } /* 以 -active 结尾 */
/* 伪类 */
:first-child { }
:last-child { }
:nth-child(2n) { }
:not(.active) { }
/* 伪元素 */
::before { }
::after { }
::placeholder { }盒模型
/* 标准盒模型:width = content */
box-sizing: content-box;
/* IE盒模型:width = content + padding + border */
box-sizing: border-box;BFC(块级格式化上下文)
触发条件
- float 不为 none
- position 为 absolute 或 fixed
- display 为 inline-block、flex、grid
- overflow 不为 visible
应用场景
/* 1. 清除浮动 */
.clearfix::after {
content: "";
display: block;
clear: both;
}
/* 2. 防止 margin 重叠 */
.container {
overflow: hidden;
}
/* 3. 自适应两栏布局 */
.left { float: left; width: 200px; }
.right { overflow: hidden; } /* 触发 BFC */Flexbox 布局
.container {
display: flex;
/* 主轴方向 */
flex-direction: row | column;
/* 主轴对齐 */
justify-content: flex-start | center | space-between | space-around;
/* 交叉轴对齐 */
align-items: flex-start | center | stretch;
/* 换行 */
flex-wrap: wrap;
}
.item {
flex: 1; /* flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */
order: 1;
align-self: center;
}常见布局
/* 垂直水平居中 */
.center {
display: flex;
justify-content: center;
align-items: center;
}
/* 两端对齐 */
.space-between {
display: flex;
justify-content: space-between;
}
/* 底部固定 */
.sticky-footer {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.sticky-footer main { flex: 1; }Grid 布局
.container {
display: grid;
/* 定义列 */
grid-template-columns: 200px 1fr 200px;
grid-template-columns: repeat(3, 1fr);
/* 定义行 */
grid-template-rows: 100px auto 100px;
/* 间距 */
gap: 20px;
}
.item {
/* 跨列 */
grid-column: 1 / 3;
/* 跨行 */
grid-row: 1 / 2;
}响应式设计
媒体查询
/* 移动优先 */
.container { width: 100%; }
@media (min-width: 768px) {
.container { width: 750px; }
}
@media (min-width: 992px) {
.container { width: 970px; }
}
@media (min-width: 1200px) {
.container { width: 1170px; }
}响应式单位
/* rem:相对于根元素 font-size */
html { font-size: 16px; }
.box { width: 10rem; } /* 160px */
/* vw/vh:视口宽度/高度的百分比 */
.full-screen { width: 100vw; height: 100vh; }
/* clamp:响应式字体 */
.title { font-size: clamp(1rem, 2vw, 2rem); }CSS3 动画
Transition
.box {
transition: all 0.3s ease;
/* transition: property duration timing-function delay; */
}
.box:hover {
transform: scale(1.1);
}Animation
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.box {
animation: fadeIn 0.5s ease forwards;
/* animation: name duration timing-function delay iteration-count direction fill-mode; */
}Transform
.box {
transform: translate(10px, 20px);
transform: rotate(45deg);
transform: scale(1.5);
transform: skew(10deg);
}常见问题
1px 问题
/* 使用 transform 缩放 */
.border-1px {
position: relative;
}
.border-1px::after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 1px;
background: #000;
transform: scaleY(0.5);
}文本溢出
/* 单行 */
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 多行 */
.ellipsis-2 {
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}