普通视图

发现新文章,点击刷新页面。
今天 — 2025年11月19日首页

Gird布局详解

作者 嬉皮客
2025年11月18日 21:16

一、什么是 Grid 布局?

CSS Grid Layout(网格布局)是 CSS 提供的二维布局系统,可以同时在行(row)和列(column)两个方向上对元素进行排列。
它和 Flexbox 不同,Flexbox 是一维布局(只能按行或列排),而 Grid 是二维布局,可以轻松实现复杂的跨行、跨列布局。

特点:

  • 二维布局:同时控制行和列,对应一维布局复杂度也随之增加
  • 跨行跨列:元素可以跨多个单元格,一维度布局需要层层嵌套来做这些效果
  • 语义清晰grid-template-areas 像地图一样描述布局,布局就像ASCLL表一样清晰直观
  • 响应式友好:只改 Grid 定义,不动 HTML,就能重排布局,经常配合@media使用
  • 现代浏览器支持好:桌面端和移动端几乎全覆盖(IE11 部分支持旧语法),现代浏览器基本都是支持的,排除一下老旧项目(政府、银行等)还在使用

二、基本概念

容器和项目

什么是容器?

设置了 display: grid 或 display: inline-grid 的元素,就是 Grid 容器,容器的直接子元素就是 Grid 项目。容器负责定义整个网格的结构(行、列、间距、对齐方式等)。

什么是项目?

Grid 容器的直接子元素就是 Grid 项目,项目可以通过属性控制它在网格中的位置、跨行跨列、对齐方式等。

行和列

容器里面的水平区域称为"行"(row),垂直区域称为"列"(column)。

下图水平深色区域就是“行”,垂直深色区域就是“列”

image.png

单元格

行和列的交叉区域,称为"单元格"(cell),n行和m列会产生n x m个单元格。比如,3行3列会产生9个单元格。

网格线

划分网格的线,称为"网格线"(grid line)。水平网格线划分出行,垂直网格线划分出列,n行有n + 1根水平网格线,m列有m + 1根垂直网格线,比如三行就有四根水平网格线。

如下图就有5根水平网格线和5根垂直网格线

image.png

三、容器属性

display:grid | inline-grid

作用:指定一个容器采用网格布局

语法:

.container{
    display: grid | inline-grid;
}

注意:设为网格布局以后,容器子元素(项目)的floatdisplay: inline-blockdisplay: table-cellvertical-aligncolumn-*等设置都将失效。

grid-template-columns与grid-template-rows

作用:grid-template-columns属性定义每一列的列宽,grid-template-rows属性定义每一行的行高

实例:指定了一个三行三列的网格,列宽和行高都是100px

image.png

.container {
  display: grid;
  grid-template-columns: 100px 100px 100px;
  grid-template-rows: 100px 100px 100px;
}

.container > div {
  width: 60px;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
<div class="container">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
</div>
repeat()

作用:使用repeat()函数,简化重复的值,接受两个参数,第一个参数是重复的次数,第二个参数是所要重复的值,也可以重复某个模式

实例:

  • 简单重复值

image.png

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
}
.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
  • 重复某个模式

image.png

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
}
.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
auto-fill与auto-fit

作用:单元格的大小是固定的,但是容器的大小不确定。如果希望每一行(或每一列)容纳尽可能多的单元格,这时可以使用auto-fill关键字表示自动填充,只有当容器足够宽,可以在一行容纳所有单元格,并且单元格宽度不固定的时候,才会有差异:auto-fill会用空格子填满剩余宽度,auto-fit则会尽量扩大单元格的宽度

实例:

例子中宽设置了400px,高设置200px,属性auto-fill,高400px/100px=4个格子,宽200px/100px=2个格子,所以超出的第九个被空放了

image.png

.container {
  width: 400px;
  height: 200px;
  display: grid;
  grid-template-columns: repeat(auto-fill, 100px);
  grid-template-rows: repeat(auto-fill, 100px);
}
fr

作用:为了方便表示比例关系,网格布局提供了fr关键字(fraction 的缩写,意为"片段")。如果两列的宽度分别为1fr2fr,就表示后者是前者的两倍

实例:

image.png

.container {
  width: 400px;
  display: grid;
  grid-template-columns: 300px 1fr 2fr;
  grid-template-rows: repeat(3, 100px);
}
minmax

作用:minmax()函数产生一个长度范围,表示长度就在这个范围之中。它接受两个参数,分别为最小值和最大值

实例:

每行第三列位置宽度100px~2fr

image.png

.container {
  display: grid;
  grid-template-columns: 1fr 1fr minmax(100px, 2fr);
  grid-template-rows: repeat(3, 100px);
}
auto

作用:由浏览器自己决定长度

实例:

image.png

.container {
  width: 400px;
  display: grid;
  grid-template-columns: 100px auto 100px;
  grid-template-rows: repeat(3, 100px);
}
自定义网格线名称

作用:指定每一根网格线的名字,方便以后的引用

实例:

指定网格布局为3*3,因此有4根垂直网格线和4根水平网格线,每个网格线可以指定多个名字,比如[r4 xipiker666]

.container {
  display: grid;
  grid-template-columns: [c1] 100px [c2] 100px [c3] auto [c4];
  grid-template-rows: [r1] 100px [r2] 100px [r3] auto [r4 xipiker666];
}

grid-columns-gap(columns-gap)与grid-rows-gap(rows-gap)与grid-gap(gap)

作用:grid-columns-gap设置列间距、grid-rows-gap设置行间距,可以分别简写为columns-gaprows-gapgrid-gap设置行列间距grid-gap: <行间距> <列间距>grid-gap可以简写为gap,如果行和列的值相等可以gap: xxpx

实例:

image.png

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  gap: 20px;
}

grid-template-areas

作用:网格布局允许指定"区域"(area),一个区域由单个或多个单元格组成

实例:

  • 划分出9个单元格
.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  grid-template-areas: 
    'a b c'
    'd e f'
    'g h i';                 
  gap: 20px;  
}
  • 多个单元格合并成一个区域
.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  grid-template-areas: 
    'a a a'
    'b b b'
    'c c c';                 
  gap: 20px;  
}
  • 某些区域不需要利用,则使用.表示
.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  grid-template-areas: 
    'a . a'
    'b . b'
    'c . c';                 
  gap: 20px;  
}

grid-auto-flow

作用:容器的子元素的排序方式,属性值row先行后列,属性值columns先列后行,属性值row dense子项目指定位置后再先行后列排序,属性值columns dense子项目指定位置后再先列后行排序

实例:

  • row先行后列

image.png

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  grid-auto-flow: row;
}
  • columns先列后行

image.png

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  grid-auto-flow: column;
}
  • row dense子项目指定位置后先行后列

image.png

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  grid-auto-flow: row dense;
}

.item_first {
  grid-column-start: 1;
  grid-column-end: 3;
}

.item_two {
  grid-column-start: 1;
  grid-column-end: 3;
}

.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
  • columns dense子项目指定位置后先列后行

image.png

.container {
  width: 400px;
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  grid-auto-flow: column dense;
}

.item_first {
  grid-column-start: 1;
  grid-column-end: 3;
}

.item_two {
  grid-column-start: 1;
  grid-column-end: 3;
}

.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}

justify-items与align-items与place-items

作用:justify-items属性设置单元格内容的水平位置,align-items属性设置单元格内的垂直位置,place-items属性是justify-itemsalign-items的组合属性

实例:

  • justify-items: start | center | end | stretch

image.png

.container {
  width: 300px;
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  justify-items: center;
}

.container > div {
  width: 60px;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
  • align-items: start | center | end | stretch

image.png

.container {
  width: 300px;
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  align-items: center;
}

.container > div {
  width: 60px;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
  • place-items: <align-items> <justify-items>

image.png

.container {
  width: 300px;
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  place-items: center end;
}

.container > div {
  width: 60px;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}

justify-content与align-content与place-content

作用:justify-content属性设置内容区域在容器里面的水平位置,align-content属性设置内容区域在容器里面的垂直位置,place-content属性是justify-contentalign-content的组合属性

实例:

  • justify-content: start | end | center | stretch | space-around | space-between | space-evenly

image.png

.container {
  width: 600px;
  background: #f9f9f9;
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  justify-content: center;
}

.container > div {
  width: 60px;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
  • align-content: start | end | center | stretch | space-around | space-between | space-evenly

image.png

.container {
  width: 600px;
  height: 400px;
  background: #f9f9f9;
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  align-content: center;
}

.container > div {
  width: 60px;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
  • place-content: <align-content> <justify-content>

image.png

.container {
  width: 600px;
  height: 400px;
  background: #f9f9f9;
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  place-content: center center;
}

grid-auto-columns与grid-auto-rows

作用:可以理解为,现有网格的外部设置多余的网格,用来放超出的子项目

实例:

  • grid-auto-columns

image.png

.container {
  background: #f9f9f9;
  display: grid;
  grid-template-columns: 100px 100px;
  grid-template-rows: 50px;
  grid-auto-rows: 80px
  grid-auto-flow: row; /*这里要设置按照行方向填充,可以不设置,因为默认按照行方向*/
}
  • grid-auto-rows

image.png

.container{
  background: #f9f9f9;
  display: grid;
  grid-template-rows: 50px 50px;
  grid-template-columns: 100px;
  grid-auto-columns: 150px; 
  grid-auto-flow: column; /*注意这里要设置按照列方向填充,必须设置,因为默认按照行防线*/
}

grid-template与grid

作用:grid-template属性是grid-template-columnsgrid-template-rowsgrid-template-areas这三个属性的合并简写形式,grid属性是grid-template-rowsgrid-template-columnsgrid-template-areas、 grid-auto-rowsgrid-auto-columnsgrid-auto-flow这六个属性的合并简写形式

四、项目属性

grid-column-start、grid-column-end与grid-row-start、grid-row-end

作用:可以理解为设置项目占多少个网格线,分别定位在哪根网格线

实例:

  • grid-column-start: <number>左边框所在的垂直网格线、grid-column-end: <number>右边框所在的垂直网格线

image.png

.container {
  background: #f9f9f9;
  display: grid;
  grid-template-rows: repeat(3, 100px);
  grid-template-columns: repeat(3, 100px);
}

.item_first {
  grid-column-start: 1;
  grid-column-end: 3;
}

.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
<div class="container">
    <div className="item_first">1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
</div>
  • grid-row-start: <number>上边框所在的垂直网格线、grid-row-end: <number>下边框所在的垂直网格线

image.png

.container {
  background: #f9f9f9;
  display: grid;
  grid-template-rows: repeat(3, 100px);
  grid-template-columns: repeat(3, 100px);
}

.item_first {
  grid-row-start: 1;
  grid-row-end: 3;
}

.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
<div class="container">
    <div className="item_first">1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
</div>

grid-column与grid-row

作用:grid-column属性是grid-column-startgrid-column-end的简写形式、grid-row属性是grid-row-startgrid-row-end的简写形式

实例:

  • grid-column: <grid-column-start> / <grid-column-end>
  • grid-row: <grid-row-start> / <grid-row-end>

grid-area

作用:指定项目放在哪个区域,grid-area属性还可用作grid-row-startgrid-column-startgrid-row-endgrid-column-end的合并简写形式,直接指定项目的位置

实例:

  • 指定放在哪个区域

image.png

.container {
  background: #f9f9f9;
  display: grid;
  grid-template-rows: repeat(3, 100px);
  grid-template-columns: repeat(3, 100px);
  grid-template-areas:
    'a b c'
    'd e f'
    'g h i';
}

.item_first {
  grid-area: e;
}

.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
  • grid-area: <row-start> / <column-start> / <row-end> / <column-end>

image.png

@pos: ~'1 / 1 / 3 / 3';

.container {
  background: #f9f9f9;
  display: grid;
  grid-template-rows: repeat(3, 100px);
  grid-template-columns: repeat(3, 100px);
}

.item_first {
  grid-area: @pos;
}

.container > div {
  width: 100%;
  height: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}

justify-self与align-self与place-self

作用:justify-self属性设置单元格内容的水平位置、align-self属性设置单元格内容的垂直位置,place-self属性是justify-selfalign-self的属性组合

实例:

image.png

.container {
  background: #f9f9f9;
  display: grid;
  grid-template-rows: repeat(3, 100px);
  grid-template-columns: repeat(3, 100px);
}

.item_first {
  justify-self: center;
  align-self: center;
}

.container > div {
  width: 60px;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}

五、常见布局案例

三列等宽布局

效果

image.png

代码

.container {
  background: #f9f9f9;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.container > div {
  width: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
}
<div className="container">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>

圣杯布局

效果

image.png

代码

.container {
  display: grid;
  grid-template-areas:
    'header header'
    'sidebar main'
    'footer footer';
  grid-template-columns: 200px 1fr;
  grid-template-rows: 60px 1fr 40px;
  gap: 10px;
}
.header {
  grid-area: header;
  background: lightblue;
}
.sidebar {
  grid-area: sidebar;
  background: lightgoldenrodyellow;
}
.main {
  grid-area: main;
  background: lightgreen;
}
.footer {
  grid-area: footer;
  background: lightpink;
}
<div class="container">
    <div className="header">header</div>
    <div className="sidebar">sidebar</div>
    <div className="main">main</div>
    <div className="footer">footer</div>
</div>

媒体查询响应式布局(Gird+Flex最佳拍档)

效果

屏幕宽度小于等于660px

image.png

屏幕宽度小于等于1024

image.png

屏幕宽度大于1024

image.png代码

<div class="dashboard">
    <div class="stats">Stats</div>
    <div class="overview">Overview</div>
    <div class="lifecycle">Lifecycle</div>
    <div class="calendar">Calendar</div>
    <div class="monitor">Monitor</div>
    <div class="ladder">Ladder</div>
    <div class="stage">Stage</div>
</div>
.dashboard {
  display: grid;
  gap: 20px;
  padding: 20px;
  /* 桌面端布局 */
  grid-template-columns: 2fr 2fr 1fr;
  grid-template-areas:
    'stats stats stats'
    'overview lifecycle calendar'
    'monitor ladder calendar'
    'stage stage stage';
}

.dashboard > div {
  background: #eee;
  padding: 10px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  font-weight: bold;
}

.stats {
  grid-area: stats;
}
.overview {
  grid-area: overview;
}
.lifecycle {
  grid-area: lifecycle;
}
.calendar {
  grid-area: calendar;
}
.monitor {
  grid-area: monitor;
}
.ladder {
  grid-area: ladder;
}
.stage {
  grid-area: stage;
}

@media (max-width: 1024px) {
  .dashboard {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      'stats stats'
      'overview lifecycle'
      'calendar calendar'
      'monitor ladder'
      'stage stage';
  }
}

@media (max-width: 600px) {
  .dashboard {
    grid-template-columns: 1fr;
    grid-template-areas:
      'stats'
      'overview'
      'lifecycle'
      'calendar'
      'monitor'
      'ladder'
      'stage';
  }
}

Grid瀑布流布局

效果

image.png

代码

.container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-auto-rows: 10px;
  grid-auto-flow: dense;
  gap: 10px;
}
.item {
  background: lightblue;
}
.item1 {
  grid-row-end: span 15;
}
.item2 {
  grid-row-end: span 25;
}
.item3 {
  grid-row-end: span 20;
}
<div class="container">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
    <div class="item item1">4</div>
</div>

六、总结

上述内容主要参考,大家可以参考原文,如果能帮到您,欢迎点赞、收藏、+关注

CSS Grid 网格布局教程 - 阮一峰的网络日志

昨天 — 2025年11月18日首页

element-plus主题配置及动态切换主题

作者 天外来物
2025年11月18日 14:02

创建vue项目的不同方式

pnpm create vite 使用于创建一些组件库,第三方库的时候

pnpm create vue 适用于vue项目,内部有一些基础的样式,vue的主题,

element-plus主题配置

按需引入element

1. scss变量自定义主题

分支:feature-element-theme-anxu-scss

src/styles/element/index.scss

/* just override what you need */
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
  $colors: (
    'primary': (
      'base': green,
    ),
  )
);

vite.config.ts

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
      ],
    }),
    ElementPlus({
      useSource: true,
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
})

按需引入组件时,使用组件,会自动引入对应组件的样式,不用再main.ts中引入elememnt的全部样式。

主题配置只需要修改需要改变的颜色变量即可

可以看到,对应组件的颜色变量已经变化

image.png

2. css变量自定义主题

分支: feature-element-theme-anxu-css

src/styles/element/index.scss

:root {
  --el-color-primary: green;
}

vite.config.ts

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
      ],
    }),
    ElementPlus({
      useSource: true,
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
})

可以看到,css变量被注入到组件对应的scss文件顶部,从而让主题生效

image.png

全量引入element

scss全量引入

分支 feature-element-theme-all-scss

/* just override what you need */
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
  $colors: (
    'primary': (
      'base': green,
    ),
  )
);

// If you just import on demand, you can ignore the following content.
// 如果你是全量导入,需要加下边这句,如果是按需引入,请注释掉下边这句
@use 'element-plus/theme-chalk/src/index.scss' as *;

main.ts

import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import './styles/element/index.scss'
import App from './App.vue'
import router from './router'

const app = createApp(App)

app.use(createPinia())
app.use(router)

app.use(ElementPlus)
app.mount('#app')

这时候可以看到样式被全部引入了

image.png

css全量引入

分支:feature-element-theme-all-css src/styles/element/index.css

:root {
  --el-color-primary: green;
}

main.ts

import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './styles/element/index.css'
import App from './App.vue'
import router from './router'

const app = createApp(App)

app.use(createPinia())
app.use(router)

app.use(ElementPlus)
app.mount('#app')

可以看到,element的css变量已经被覆盖了

image.png

动态主题

运行时动态切换主题,不能依赖scss变量

原因

SCSS 变量是编译时变量,在构建阶段就被替换掉了,在浏览器运行时无法再改变,所以无法用于“动态切换主题”。

动态主题应该使用css变量来实现

官方提供了一个切换主题的项目github.com/element-plu…

里面引入了一套暗黑模式的样式,然后通过 useToggle进行切换

分支 feature-element-theme-anxu-scss-dynamic 还是采用按需引入elememnt的方式

src/styles/index.scss 这里全量引入暗黑模式的样式

// import dark theme
@use 'element-plus/theme-chalk/src/dark/css-vars.scss' as *;

// :root {
//   --ep-color-primary: red;
// }

body {
  font-family:
    Inter, system-ui, Avenir, 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB',
    'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  margin: 0;
}

a {
  color: var(--ep-color-primary);
}

main.ts在main.ts中引入样式

// import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import '@/styles/index.scss'

const app = createApp(App)

app.use(createPinia())
app.use(router)

app.mount('#app')

src/styles/element/index.scss 这里是自定义默认主题变量

$--colors: (
  'primary': (
    'base': rgb(0, 128, 19),
  ),
  'success': (
    'base': #21ba45,
  ),
  'warning': (
    'base': #f2711c,
  ),
  'danger': (
    'base': #db2828,
  ),
  'error': (
    'base': #db2828,
  ),
  'info': (
    'base': #42b8dd,
  ),
);

// You should use them in scss, because we calculate it by sass.
// comment next lines to use default color
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
  // do not use same name, it will override.
  $colors: $--colors // $button-padding-horizontal: ('default': 50px)
);

// if you want to import all
// @use "element-plus/theme-chalk/src/index.scss" as *;

// You can comment it to hide debug info.
// @debug $--colors;

// custom dark variables
@use './dark.scss';

src/styles/element/dark.scss 这是对暗黑主题颜色重新定义

// only scss variables

$--colors: (
  'primary': (
    'base': #589ef8,
  ),
);

@forward 'element-plus/theme-chalk/src/dark/var.scss' with (
  $colors: $--colors
);

vite.config.ts 这里引入自定义主题变量,自定义默认主题和暗黑主题颜色

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
      ],
    }),
    ElementPlus({
      useSource: true,
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
})

动态切换主题

  • 通过useDark获取当前是否是暗黑模式
  • 通过useToggle切换主题
<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
import { useDark, useToggle } from '@vueuse/core'
const isDark = useDark()
const toggleDark = useToggle(isDark)
const changeTheme = () => {
  toggleDark()
}
</script>

<template>
  <header>
    <el-button type="primary" @click="changeTheme"> 切换主题 </el-button>
    <el-input></el-input>
  </header>

  <RouterView />
</template>

<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>

代码仓库地址 github.com/Stacey1018/…

昨天以前首页

CSS 像素≠物理像素:0.5px 效果的核心密码是什么?

2025年11月14日 17:02

先明确两者的关系:CSS 像素是 “逻辑像素”(页面布局用),物理像素是屏幕实际发光的像素点,两者通过 设备像素比(DPR)  关联,公式为:1 个 CSS 像素 = DPR × DPR 个物理像素(仅高清屏缩放为 1 时)。

理解这个核心关系后,再看 0.5px 效果的实现逻辑就更清晰了,以下重新整理(重点补充像素关系,再对应方法):

一、先搞懂:CSS 像素、物理像素、DPR 的核心关系

  1. 定义

    • CSS 像素:写代码时用的单位(如 width: 100px),是浏览器渲染布局的 “逻辑单位”,和屏幕硬件无关。

    • 物理像素:屏幕面板上实际的发光点(如手机屏分辨率 1080×2340,就是横向 1080 个、纵向 2340 个物理像素),是屏幕的硬件属性。

    • DPR(设备像素比):DPR = 物理像素宽度 / CSS 像素宽度(默认页面缩放为 1 时),由设备硬件决定。

      • 例 1:老款普通屏(DPR=1):1 个 CSS 像素 = 1×1 个物理像素(写 1px 就对应屏幕 1 个发光点)。
      • 例 2:高清屏(DPR=2,如 iPhone 8):1 个 CSS 像素 = 2×2 个物理像素(写 1px 实际占用屏幕 4 个发光点,视觉上更粗)。
      • 例 3:超高清屏(DPR=3,如 iPhone 14 Pro):1 个 CSS 像素 = 3×3 个物理像素(写 1px 占用 9 个发光点,更粗)。
  2. 关键结论

    • 我们想要的 “0.5px 效果”,本质是 让线条只占用 1 个物理像素(视觉上最细)。
    • 但高清屏(DPR≥2)默认下,1 个 CSS 像素会占用多个物理像素,所以不能直接写 1px,需要通过方法 “压缩” CSS 像素对应的物理像素数量,最终落到 1 个物理像素上。

二、按 DPR 要求分类的 0.5px 实现方法(结合像素关系)

(一)仅 DPR≥2 生效:直接让 CSS 像素对应 1 个物理像素

核心逻辑:利用 DPR≥2 的像素映射关系,让 CSS 像素经过计算后,刚好对应 1 个物理像素。

1. 直接声明 0.5px
  • 像素关系:DPR=2 时,0.5px CSS 像素 = 0.5×2 = 1 个物理像素(刚好满足需求);DPR=3 时,0.5px CSS 像素 = 0.5×3 = 1.5 个物理像素(接近细线条,视觉可接受)。
  • 前提:DPR≥2 + 浏览器支持亚像素渲染(iOS 9+、Android 8.0+)。
  • 代码border: 0.5px solid #000;
  • 局限:DPR=1 时,0.5px CSS 像素 = 0.5×1 = 0.5 个物理像素(屏幕无法渲染,会四舍五入为 0px 或 1px)。
2. transform: scale(0.5) 缩放
  • 像素关系:先写 1px CSS 像素(DPR=2 时对应 2 个物理像素),再缩放 50%,最终 2×50% = 1 个物理像素。

  • 前提:DPR≥2(只有 DPR≥2 时,1px CSS 像素才会对应 ≥2 个物理像素,缩放后才能落到 1 个)。

  • 代码

    .line::after {
      content: '';
      width: 200%;
      height: 1px; /* 1px CSS = 2 物理像素(DPR=2) */
      background: #000;
      transform: scale(0.5); /* 2 物理像素 × 0.5 = 1 物理像素 */
    }
    
  • 局限:DPR=1 时,1px CSS 像素 = 1 物理像素,缩放后变成 0.5 物理像素(屏幕无法渲染,线条消失或模糊)。

3. viewport 缩放(全局方案)
  • 像素关系:通过 initial-scale=1/DPR 改变页面缩放比例,让 1px CSS 像素直接对应 1 个物理像素。

    • 例:DPR=2 时,缩放 50%(1/2),此时 1px CSS 像素 = 1 物理像素(原本 2 物理像素,缩放后压缩为 1);DPR=3 时,缩放 33.3%(1/3),1px CSS 像素 = 1 物理像素。
  • 前提:DPR≥2(高清屏),需配合布局单位(如 rem)调整。

  • 代码

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <script>
      const dpr = window.devicePixelRatio || 1;
      document.querySelector('meta[name="viewport"]').setAttribute('content', 
        `width=device-width, initial-scale=${1/dpr}, user-scalable=no`
      );
    </script>
    
  • 优势:直接写 border: 1px 就是 1 物理像素,适配所有 DPR≥2 的设备。

  • 局限:全局缩放会影响布局,需重新计算 rem 基准值(如 html { font-size: 16px * dpr })。

(二)DPR≥2 最优,DPR=1 可模拟:视觉层面实现 “细于 1px”

核心逻辑:不依赖像素映射的精准计算,而是通过视觉欺骗或矢量渲染,让线条看起来比 1px 细(DPR=1 时无法实现 1 物理像素,只能模拟)。

1. SVG 绘制
  • 像素关系:SVG 是矢量图,不依赖 CSS 像素和物理像素的映射,直接按 “坐标 + 线条宽度” 渲染。

    • DPR≥2 时:stroke-width="1" + y1="0.5" 直接渲染为 1 个物理像素(矢量渲染支持亚像素精准控制)。
    • DPR=1 时:同样的代码会渲染为 “视觉上 0.5px 细的线条”(实际还是 1 物理像素,但矢量缩放让边缘更细腻,比直接写 1px 看起来细)。
  • 前提:无严格 DPR 要求,所有支持 SVG 的浏览器(几乎所有移动端)。

  • 代码

    <svg width="100%" height="1" xmlns="http://www.w3.org/2000/svg">
      <line x1="0" y1="0.5" x2="100%" y2="0.5" stroke="#000" stroke-width="1" />
    </svg>
    
2. 背景渐变(background-image
  • 像素关系:利用 1px 高的 CSS 容器,通过颜色分割模拟 “半像素”。

    • DPR=2 时:1px CSS 容器 = 2 物理像素高,渐变 “透明 50% + 有色 50%” 刚好对应 1 个物理像素的有色线条。
    • DPR=1 时:1px CSS 容器 = 1 物理像素高,渐变后视觉上是 “半透明细线”(比纯 1px 细,但本质是 1 物理像素的颜色叠加)。
  • 前提:支持 CSS3 渐变的浏览器(iOS 7+、Android 4.4+)。

  • 代码

    .line {
      height: 1px;
      background: linear-gradient(to bottom, transparent 50%, #000 50%);
    }
    
3. box-shadow 模拟
  • 像素关系:DPR=2 时,box-shadow: 0 0.5px 0 #000 中,0.5px CSS 偏移量 = 1 物理像素,形成 1 物理像素的细阴影(视觉上是细线条)。
  • 前提:DPR≥2(DPR=1 时,0.5px 偏移 = 0.5 物理像素,屏幕无法渲染,阴影不显示或模糊)。
  • 代码box-shadow: 0 0.5px 0 #000;

三、最终总结(结合像素关系)

实现方式 像素映射逻辑(核心) 依赖 DPR 视觉效果
直接 0.5px DPR≥2 时,0.5px CSS = 1 物理像素 DPR≥2 精准细线条
transform: scale DPR≥2 时,1px CSS(2 物理像素)缩放 50% = 1 物理像素 DPR≥2 兼容性好,精准细线条
viewport 缩放 DPR≥2 时,缩放 1/DPR 让 1px CSS = 1 物理像素 DPR≥2 全局适配,精准细线条
SVG 绘制 矢量渲染,直接控制 1 物理像素(DPR≥2)或模拟细线条(DPR=1) 无(DPR≥2 最优) 跨设备,细腻无模糊
背景渐变 DPR≥2 时 1px CSS(2 物理像素)颜色分割 = 1 物理像素;DPR=1 时视觉欺骗 无(DPR≥2 最优) 模拟细线条,无兼容性问题
box-shadow DPR≥2 时,0.5px CSS 偏移 = 1 物理像素阴影 DPR≥2 非边框线条适用

核心一句话:所有 “真实 0.5px 效果”(1 物理像素)都依赖 DPR≥2 的高清屏(利用 CSS 像素与物理像素的映射关系);DPR=1 时只能模拟,无法实现物理级半像素。

以下是包含 CSS 像素 / 物理像素 / DPR 关系说明 的 0.5px 兼容代码合集,每个方法都标注核心逻辑和适用场景,可直接复制使用:

一、说明(所有方法通用)

  • 核心目标:让线条最终占用 1 个物理像素(视觉最细)。
  • 像素关系:1 CSS 像素 = DPR × DPR 物理像素(默认缩放 1 时),高清屏(DPR≥2)需通过代码 “压缩” 映射关系。
  • 适配原则:优先选兼容性广、无布局影响的方法(如 SVG、transform 缩放)。

二、6 种实用兼容代码

1. 推荐首选:transform: scale (0.5) 缩放(DPR≥2 生效,兼容性最好)

  • 核心逻辑:1px CSS 像素(DPR=2 时对应 2 物理像素)→ 缩放 50% → 最终 1 物理像素。
  • 适用场景:边框、独立线条,不影响布局。
/* 通用细线条类(上下左右可按需调整) */
.thin-line {
  position: relative;
  /* 父容器需触发 BFC,避免线条溢出 */
  overflow: hidden;
}

.thin-line::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 1px; /* 1px CSS = 2 物理像素(DPR=2) */
  background: #000; /* 线条颜色 */
  transform: scaleY(0.5); /* 垂直缩放 50% → 2 物理像素 → 1 物理像素 */
  transform-origin: 0 0; /* 缩放原点避免偏移 */
}

/* 横向线条(默认)、纵向线条(按需添加) */
.thin-line-vertical::after {
  width: 1px;
  height: 100%;
  transform: scaleX(0.5);
}
  • 使用方式:<div class="thin-line">内容</div>

2. 跨 DPR 优选:SVG 绘制(所有设备适配,精准无模糊)

  • 核心逻辑:SVG 矢量渲染不依赖像素映射,直接指定 1 物理像素线条(DPR≥2 精准,DPR=1 模拟细线条)。
  • 适用场景:UI 严格还原、跨设备兼容(推荐用于分割线、边框)。
<!-- 横向细线条(直接嵌入,可复用) -->
<svg class="svg-thin-line" width="100%" height="1" xmlns="http://www.w3.org/2000/svg">
  <!-- y1="0.5" + stroke-width="1" → 直接对应 1 物理像素(DPR≥2) -->
  <line x1="0" y1="0.5" x2="100%" y2="0.5" stroke="#000" stroke-width="1" />
</svg>

<!-- 纵向细线条(宽度 100%,高度自适应) -->
<svg class="svg-thin-line-vertical" width="1" height="100%" xmlns="http://www.w3.org/2000/svg">
  <line x1="0.5" y1="0" x2="0.5" y2="100%" stroke="#000" stroke-width="1" />
</svg>

<!-- 样式优化(可选) -->
<style>
  .svg-thin-line {
    display: block;
    margin: 8px 0; /* 上下间距 */
  }
</style>
  • 使用方式:直接嵌入 HTML,修改 stroke 颜色、width/height 适配场景。

3. 现代设备:直接 0.5px 声明(简洁高效,DPR≥2 + 现代浏览器)

  • 核心逻辑:DPR=2 时,0.5px CSS 像素 = 1 物理像素,浏览器直接渲染。
  • 适用场景:iOS 9+、Android 8.0+ 设备,无需兼容旧机型。
/* 直接声明,简洁高效 */
.simple-thin-line {
  border-bottom: 0.5px solid #000; /* 横向线条 */
  /* 纵向线条:border-left: 0.5px solid #000; */
}

/* 兼容写法(部分浏览器需前缀) */
.compact-thin-line {
  border-bottom: 0.5px solid #000;
  -webkit-border-bottom: 0.5px solid #000;
}
  • 使用方式:<div class="simple-thin-line">内容</div>

4. 全局适配:viewport 缩放(DPR≥2,全局细线条统一)

  • 核心逻辑:缩放页面为 1/DPR,让 1px CSS 像素 = 1 物理像素(需配合 rem 布局)。
  • 适用场景:整个页面需要大量细线条,愿意调整布局单位。
<!-- 第一步:设置 viewport(初始缩放 1.0) -->
<meta name="viewport" id="viewport" content="width=device-width, user-scalable=no">

<!-- 第二步:动态调整缩放比例 -->
<script>
  (function() {
    const dpr = window.devicePixelRatio || 1;
    const viewport = document.getElementById('viewport');
    // 缩放 1/DPR,让 1px CSS = 1 物理像素(DPR=2 → 缩放 50%)
    viewport.setAttribute('content', `width=device-width, initial-scale=${1/dpr}, user-scalable=no`);
    
    // 可选:调整 rem 基准值(避免布局错乱)
    const html = document.documentElement;
    html.style.fontSize = `${16 * dpr}px`; // 1rem = 16*dpr px(适配缩放后布局)
  })();
</script>

<!-- 第三步:直接写 1px 即可(此时 1px = 1 物理像素) -->
<style>
  .global-thin-line {
    border-bottom: 1px solid #000; /* 实际是 1 物理像素细线条 */
    margin: 0.5rem 0; /* rem 单位适配缩放后布局 */
  }
</style>
  • 使用方式:全局引入脚本,之后所有 1px 边框都会变成细线条。

5. 视觉模拟:背景渐变(无兼容性问题,DPR≥2 最优)

  • 核心逻辑:1px CSS 容器(DPR=2 时 2 物理像素)→ 颜色分割为 50% 透明 + 50% 有色 → 视觉上 1 物理像素。
  • 适用场景:背景线条、无法用边框 / 伪元素的场景。
/* 横向线条 */
.gradient-thin-line {
  height: 1px;
  width: 100%;
  /* 上半透明,下半有色 → 视觉上细线条 */
  background: linear-gradient(to bottom, transparent 50%, #000 50%);
  background-size: 100% 1px;
}

/* 纵向线条 */
.gradient-thin-line-vertical {
  width: 1px;
  height: 100%;
  background: linear-gradient(to right, transparent 50%, #000 50%);
  background-size: 1px 100%;
}
  • 使用方式:<div class="gradient-thin-line"></div>(独立线条容器)。

6. 非边框场景:box-shadow 模拟(DPR≥2,适合阴影类线条)

  • 核心逻辑:DPR=2 时,0.5px CSS 偏移 = 1 物理像素,阴影即细线条。
  • 适用场景:无需占用布局空间的线条(如文字下方细下划线)。
.shadow-thin-line {
  height: 0;
  /* y 轴偏移 0.5px → 1 物理像素,无模糊、无扩散 */
  box-shadow: 0 0.5px 0 #000;
  -webkit-box-shadow: 0 0.5px 0 #000; /* 兼容 Safari */
}

/* 文字下划线示例 */
.text-thin-underline {
  display: inline-block;
  box-shadow: 0 0.5px 0 #000;
  padding-bottom: 2px;
}
  • 使用方式:<span class="text-thin-underline">带细下划线的文字</span>

三、使用建议

  1. 优先选 transform 缩放 或 SVG 绘制:兼容性广、无布局影响,覆盖 99% 场景。
  2. 现代设备(iOS 9+/Android 8.0+)直接用 0.5px 声明:代码最简洁。
  3. 全局大量细线条用 viewport 缩放:需配合 rem 布局,一次性解决所有线条问题。

锚点跳转-附带CSS样式 & 阻止页面刷新技术方案

作者 刀疤
2025年11月14日 16:24

问题:触发浏览器默认锚点行为,首次点击,刷新页面,虽然回到顶部,但未保存数据被清空。

<!-- 原始 -->

<span id="topAnchor"></span>

<!-- 回到顶部按钮 -->

<a href="#topAnchor" class="back-top-btn">

<a-icon type="arrow-up" />

</a>

解决方案:阻止默认行为 + 编程控制


<a @click.prevent="scrollToTop">回到顶部</a>


scrollToTop() {

    const anchor = document.getElementById('topAnchor')

        if (anchor) {

        anchor.scrollIntoView({ behavior: 'smooth', block: 'start' })

        } else {

        window.scrollTo({ top: 0, behavior: 'smooth' })

    }

}

关键技术点

  • @click.prevent - 阻止默认链接行为
  • scrollIntoView() - 编程式控制滚动
  • behavior: 'smooth' - 添加平滑动画
  • URL保持不变 - 避免路由重载

适用场景

  • 单页应用(SPA)
  • 需要平滑滚动效果
  • 希望保持URL稳定的场景
配合CSS:

<a @click.prevent="scrollToTop">回到顶部</a>


scrollToTop() {

    const anchor = document.getElementById('topAnchor')

        if (anchor) {

        anchor.scrollIntoView({ behavior: 'smooth', block: 'start' })

        } else {

        window.scrollTo({ top: 0, behavior: 'smooth' })

    }

}


// css:回到顶部按钮样式
.back-top-btn {
    position: fixed;
    right: 80px;
    bottom: 100px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: #1890ff;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    transition: all 0.3s;
    cursor: pointer;
    z-index: 1000;
    &:hover {
    background-color: #40a9ff;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transform: translateY(-2px);
}

 
 &:active {
    transform: translateY(0);
 }
}

效果图

SCSS新手教学(知识点概览)

作者 thomaswade
2025年11月14日 15:50

scss:sass3以后的新语法,功能完全等价,语法上{ scss使用大括号,sass使用缩进 }不能混用,会报错 并且scss中可以直接写css

Sass有三个版本Dart SasslibsassRuby Sass

npm install -g sass 全局安装sass

scss选择器嵌套语法 避免重复输入父选择器,提高开发效率,减少样式覆盖可能造成的异常问题 属性也可以嵌套,例.container { font: { family: fantasy; size: 30em; weight: bold; } } 等价于.container { font-family: fantasy; font-size: 30em; font-weight: bold; }避免重复输入相同的开头

父选择器& 就是向上找一层,选择上外层的父选择器 用法 &: 即可

注释/* */编译时会被编译到css文件中 注释// 编译时不会编译到css文件中(scss专属注释)

变量 $ 赋值写法与css相同 {

1. 变量以美元符号`$`开头,后面跟变量名;
    
2. 变量名是不以数字开头的可包含字母、数字、下划线、横线(连接符);
    
3. 通过连接符`-`与下划线`_`定义的同名变量为同一变量;
    
4. 变量一定要先定义,后使用;
    
5. 写法同`css`,即变量名和值之间用冒号`:`分隔;

}

变量作用域:嵌套规则内定义的变量只能在嵌套规则内使用,但声明在最外层的变量可以在任何地方使用,属于全局变量 !global可以将局部变量提升为全局变量

scss数据类型{

number;string;color;boolean;null;数组(用空格或逗号分开);maps(类似于js的object 键值对)

}

!default“温柔默认值”,如果没有其他赋值,就使用!default所标注的赋值,有其他赋值就当!default不存在

== != 正常规则

>    <     >=    <=     正常规则   

and or not

加减乘除余(纯数字与百分号或单位运算时会自动转化成相应的百分比与单位值)

加还可连接字符串{

1. `+`可用于连接字符串;
    
2. 如果有引号字符串(位于 + 左侧)连接无引号字符串,运算结果是有引号的;
    
3. 无引号字符串(位于 + 左侧)连接有引号字符串,运算结果则没有引号。

}

插值语句#{} 规则与js模版字符串类似

@import 导入文件 以下情况下,@import 仅作为普通的css语句,不会导入scss文件:

1. 文件拓展名是`.css`2. 文件名以 `http://`开头;
    
3. 文件名是`url()`4. `@import`包含媒体查询。

@media媒体查询增强 @media 指令允许在css规则中嵌套,编译时,@media 将被编译到文件的最外层,包含嵌套的父选择器。 @media允许互相嵌套使用,编译时,scss自动添加 and

mixin 混入 定义可重复使用的样式 例 @mixin block { width: 96%; margin-left: 2%; border-radius: 8px; border: 1px #f6f6f6 solid; } .container { .block { @include block; } } 还可以传入参数 参数还可制定默认值,调用时携带参数则使用携带的参数,不携带则使用默认值

总结

1. `mixin`是可以重复使用的一组`css`声明,有助于减少重复代码,只需声明一次,就可在文件中引用;
    
2. 混合指令可以包含所有的 `css`规则,绝大部分`scss`规则,可以传递参数,输出多样化的样式;
    
3. 使用参数时建议加上默认值;
    
4. `@import`导入局部模块化样式(类似功能、同一组件);
    
5. `@minix`定义的是可重复使用的样式

@function 用于封装复杂的操作 用法与mixin类似 @function一般用来计算,而mixin用来封装样式

@extend 继承 @function和@mixin都是封装好一个函数,然后后续调用 而@extend则是写好一个样式后,在要调用这个样式的时候@extend+这个样式 可以使用多个@extend 继承也可以嵌套,实现多层继承

@use 代替 @import

@at-root 用来跳出嵌套进行操作 @without和with 默认@at-root只会跳出选择器嵌套,而不能跳出@media@support,如果要跳出这两种,则需使用@at-root (without: media)@at-root (without: support)@at-root的关键词有四个:

1. `all`表示所有;
    
2. `rule`表示常规`css`选择器;
    
3. `media` 表示`media`4. `support`表示`support``@support`主要是用于检测浏览器是否支持`css`的某个属性)。

scss内置扩展

color lighten()与 darken()可用于调亮或调暗颜色,opacify()使颜色透明度减少,transparent()使颜色透明度增加,mix()用来混合两种颜色

String 向字符串添加引号的quote()、获取字符串长度的string-length()和将内容插入字符串给定位置的string-insert()

math percentage()将无单元的数值转换为百分比,round()将数字四舍五入为最接近的整数,min()max()获取几个数字中的最小值或最大值,random()返回一个随机数

list length()返回列表长度,nth()返回列表中的特定项,join()将两个列表连接在一起,append()在列表末尾添加一个值

Map map-get()根据键值获取map中的对应值,map-merge()来将两个map合并成一个新的mapmap-values()映射中的所有值

selector selector-append()可以把一个选择符附加到另一个选择符,selector-unify()将两组选择器合成一个复合选择器

CSS实现边框光点围绕特效

作者 小杨累了
2025年11月14日 14:51

在前端开发中,会遇到一些特殊的视觉效果需求,比如只在指定区域内显示内容。本文将介绍如何通过CSS实现一个"边框光点围绕"的特效,通过运用CSS的层叠上下文和z-index属性,实现只在特定区域显示内容的效果。

实现思路

  1. 创建一个容器作为显示区域
  2. 使用伪元素创建装饰性的边框效果
  3. 使用另一个伪元素作为遮罩,遮挡不需要显示的部分
  4. 通过精确控制z-index层级关系,实现只显示边框区域内容的效果

核心代码

HTML结构

<div class="bottomContentItem"> <div class="main-title">取信于客户 服务于客户</div> <div class="sub-title">讲时效、保质量、重合同、守信誉</div> </div> 

CSS实现

.bottomContentItem { display: flex; background-color: #404040; width: 50%; height: 240px; margin: 30px 0; flex-direction: column; justify-content: center; align-items: center; gap: 40px; color: #FFFFFF; position: relative; overflow: hidden; .main-title { font-size: 3rem; font-weight: bolder; letter-spacing: 0.2rem; opacity: 0; transform-origin: center; transform: scaleX(0); transition: transform 0.8s cubic-bezier(0.23, 1, 0.32, 1), opacity 0.8s ease; z-index: 1; } .sub-title { font-size: 1.5rem; opacity: 0; transform: translateY(20px); transition: all 1s cubic-bezier(0.23, 1, 0.32, 1); transition-delay: 0.3s; z-index: 1; } &::before { content: ''; position: absolute; width: 120%; height: 3.125rem; background: linear-gradient(45deg, rgba(255, 213, 135, .9215686275), rgba(251, 0, 0, .2392156863)); top: 50%; left: 50%; transform: translate(-50%, -50%); animation: effect-btn-borderflow-rotation 6s linear infinite; z-index: 0; } &::after { content: ''; position: absolute; width: calc(100% - 2px); height: calc(100% - 2px); top: 1px; left: 1px; z-index: 0; background-color: #404040; } } 

实际效果

css_border.gif

流程解析

1. 层叠上下文的运用

在这个实现中,我们通过z-index创建了三个层级:

  • 文字内容(z-index: 1):位于最上层,确保用户可以看到
  • 装饰边框(z-index: 0):位于中间层,提供视觉效果
  • 遮罩背景(z-index: 0):与装饰边框同层级,但通过定位覆盖不需要显示的区域

2. 伪元素的使用

我们使用了两个伪元素:

  • ::before:创建旋转的装饰边框效果
  • ::after:创建遮罩,隐藏边框以外的装饰内容

3. 精确的定位控制

&::before { width: 120%; /* 比容器宽20%,确保旋转时边缘不露出 */ height: 3.125rem; /* 固定高度,形成边框效果 */ top: 50%; left: 50%; transform: translate(-50%, -50%); /* 精确居中 */ } &::after { width: calc(100% - 2px); /* 略小于容器,形成边框 */ height: calc(100% - 2px); top: 1px; left: 1px; } 

4. 动画效果

@keyframes effect-btn-borderflow-rotation { 0% { transform: translate(-50%, -50%) rotate(0deg); } 100% { transform: translate(-50%, -50%) rotate(360deg); } } 

实现原理详解

这种效果的实现原理基于以下几点:

  1. 遮罩技术:通过::after伪元素创建一个与容器背景色相同的遮罩层,覆盖整个容器
  2. 局部显示:由于::before伪元素只在垂直居中的窄条区域内显示,其他部分被::after遮罩遮挡
  3. 层级控制:通过z-index控制显示层级,确保文字内容始终可见

实际应用建议

  1. 兼容性考虑:确保目标浏览器支持所需的CSS特性
  2. 性能优化:对于复杂动画,考虑使用transformwill-change属性优化性能
  3. 响应式适配:根据不同屏幕尺寸调整边框高度和定位参数
  4. 可维护性:将颜色值和尺寸定义为CSS变量,便于统一管理

总结

通过运用CSS的层叠上下文、伪元素和定位技术,可以实现复杂的视觉效果。在实际项目中,可以根据需求调整颜色、尺寸和动画效果,创造出更多个性化的界面效果。

这种方法的优势在于:

  • 纯CSS实现,无需额外的JavaScript代码
  • 性能良好,利用浏览器的硬件加速
  • 易于维护,结构清晰
  • 可扩展性强,便于调整样式和效果

从 Vue 构建错误到深度解析:::v-deep 引发的 CSS 压缩危机

作者 一川_
2025年11月14日 14:13

前言

在日常的前端开发中,我们经常会遇到各种构建错误,有些错误信息明确,容易定位;而有些则像迷宫一样,需要一步步排查。最近在开发一个 Vue 2 项目时,我就遇到了一个令人头疼的 CSS 压缩错误,经过多轮排查和尝试,最终找到了问题的根源和解决方案。本文将详细记录这个问题的排查过程,并深入分析相关的技术原理。

问题初现

那是一个普通的开发日,我正在为一个生产工单管理系统添加新功能。在完成代码编写后,我像往常一样执行构建命令:

npm run build:prod

然而,控制台却报出了令人困惑的错误:

ERROR Error: CSS minification error: Cannot read property 'trim' of undefined. File: static/css/chunk-25fbebba.59b3af06.css
Error: CSS minification error: Cannot read property 'trim' of undefined. File: static/css/chunk-25fbebba.59b3af06.css
    at C:\Users\wzh\Desktop\BatchProductionWorkOrderReport\node_modules@intervolga\optimize-cssnano-plugin\index.js:106:21

第一阶段:常规排查

尝试方案一:清除缓存和重新安装

面对构建错误,我的第一反应是清除缓存和重新安装依赖,这是前端开发中的"万能药":

# 清除 npm 缓存
npm cache clean --force

# 删除 node_modules 和 package-lock.json
rm -rf node_modules package-lock.json

# 重新安装依赖
npm install

# 重新构建
npm run build:prod

然而,这次"万能药"并没有奏效,同样的错误再次出现。

尝试方案二:更新相关依赖

注意到控制台有一个警告:"A new version of sass-loader is available",我尝试更新相关依赖:

# 更新 sass-loader
npm update sass-loader

# 更新 Vue CLI 和相关构建工具
npm update @vue/cli-service

# 更新 CSS 相关插件
npm update @intervolga/optimize-cssnano-plugin cssnano postcss

更新后重新构建,问题依旧。

第二阶段:深入分析

错误信息分析

仔细分析错误信息,有几个关键点:

  1. 错误位置@intervolga/optimize-cssnano-plugin/index.js:106:21
  2. 错误类型Cannot read property 'trim' of undefined
  3. 涉及文件chunk-25fbebba.59b3af06.css

这表明问题出现在 CSS 压缩阶段,某个 CSS 内容在压缩时变成了 undefined

代码审查

我开始审查项目中最近修改的代码,重点关注样式部分。发现问题出现在一个使用了 ::v-deep 的 Vue 组件中:

<style scoped>
.workorder-table {
  height: 100%;
}

::v-deep .el-table__body-wrapper {
  height: 100% !important;
  overflow-y: auto;
}

::v-deep .el-table th {
  background: #e3e9f3 !important;
  color: #1f1f1f !important;
  font-weight: 600;
  font-size: 13px;
  border-bottom: 2px solid #c3c9d4 !important;
  padding: 12px;
}

::v-deep .el-table td {
  padding: 12px;
}
</style>

第三阶段:技术原理探究

什么是 ::v-deep?

::v-deep 是 Vue.js 中用于样式穿透的伪类选择器。在 Vue 的 scoped CSS 中,样式默认只作用于当前组件,但有时候我们需要修改子组件的样式,这时就需要使用样式穿透。

Vue 2 和 Vue 3 中的差异

在排查过程中,我发现不同 Vue 版本对深度选择器的支持有所不同:

Vue 2 支持的形式:

  • >>>
  • /deep/
  • ::v-deep

Vue 3 支持的形式:

  • :deep()
  • ::v-deep(已弃用)

构建过程中的 CSS 处理流程

理解构建过程中 CSS 的处理流程对于解决问题至关重要:

  1. Vue Loader 处理:Vue Loader 解析 .vue 文件中的 <style> 块
  2. CSS 预处理:如果使用了 Sass/Less,会进行相应的预处理
  3. PostCSS 处理:应用各种 PostCSS 插件,包括 scoped CSS 处理
  4. CSS 提取:将 CSS 从 JavaScript 中提取出来
  5. CSS 压缩:使用 cssnano 等工具进行压缩

问题根源分析

经过深入分析,我发现问题的根源在于:

  1. 版本兼容性问题:项目中使用的 @intervolga/optimize-cssnano-plugin 版本与当前的 Vue CLI 版本存在兼容性问题
  2. 深度选择器解析:在某些情况下,::v-deep 在构建过程中可能被解析为空的 CSS 规则
  3. CSS 压缩异常:当遇到这些异常的 CSS 规则时,压缩插件无法正确处理,导致 undefined 错误

第四阶段:解决方案尝试

方案一:使用 /deep/ 替代 ::v-deep

这是最直接的解决方案,将所有的 ::v-deep 替换为 /deep/

<style scoped>
.workorder-table {
  height: 100%;
}

/deep/ .el-table__body-wrapper {
  height: 100% !important;
  overflow-y: auto;
}

/deep/ .el-table th {
  background: #e3e9f3 !important;
  color: #1f1f1f !important;
  font-weight: 600;
  font-size: 13px;
  border-bottom: 2px solid #c3c9d4 !important;
  padding: 12px;
}

/deep/ .el-table td {
  padding: 12px;
}
</style>

结果:构建成功!这是最快速的解决方案。

方案二:使用 CSS Modules

为了更彻底地解决问题,我尝试了 CSS Modules 方案:

<template>
  <div class="workorder-page">
    <el-table :class="$style.workorderTable">
      <!-- 表格内容 -->
    </el-table>
  </div>
</template>

<style module>
.workorderTable {
  width: 100%;
  min-width: 1400px;
}

.workorderTable :global(.el-table__body-wrapper) {
  height: 100% !important;
  overflow-y: auto;
}

.workorderTable :global(.el-table th) {
  background: #e3e9f3 !important;
  color: #1f1f1f !important;
  font-weight: 600;
  font-size: 13px;
  border-bottom: 2px solid #c3c9d4 !important;
  padding: 12px;
}

.workorderTable :global(.el-table td) {
  padding: 12px;
}
</style>

结果:构建成功,且代码更加规范。

方案三:配置 vue.config.js

如果必须使用 ::v-deep,可以通过配置 vue.config.js 来解决问题:

module.exports = {
  css: {
    loaderOptions: {
      css: {
        // 启用 CSS Modules 模式避免深度选择器问题
        modules: false
      },
      postcss: {
        plugins: [
          require('autoprefixer')
        ]
      }
    }
  },
  chainWebpack: config => {
    // 优化 CSS 压缩配置
    config.plugin('optimize-css').tap(args => {
      if (args[0] && args[0].cssnanoOptions) {
        args[0].cssnanoOptions.preset = ['default', {
          discardComments: {
            removeAll: true
          },
          normalizeWhitespace: false
        }]
      }
      return args
    })
  }
}

最终解决方案

综合考虑项目现状和长期维护性,我选择了方案二(CSS Modules) 作为最终解决方案,原因如下:

  1. 符合现代前端开发规范
  2. 更好的样式隔离
  3. 避免深度选择器的兼容性问题
  4. 便于代码维护和重构

技术深度解析

Vue Scoped CSS 原理

Vue 的 scoped CSS 是通过 PostCSS 插件实现的,工作原理如下:

  1. 为每个选择器添加属性选择器.example → .example[data-v-xxxxxx]
  2. 为模板元素添加属性<div class="example"> → <div class="example" data-v-xxxxxx>
  3. 样式仅限于带有相同 data-v 属性的元素

深度选择器的实现机制

深度选择器的工作原理是移除属性选择器:

/* 原始代码 */
::v-deep .child-component { color: red; }

/* 转换后 */
[data-v-xxxxxx] .child-component { color: red; }

CSS Modules 的优势

  1. 真正的局部作用域:通过类名哈希实现
  2. 无冲突的类名:每个模块的类名都是唯一的
  3. 显式依赖:明确知道样式在哪里被使用
  4. 代码压缩优化:类名可以被压缩得更短

经验总结

通过这次问题的排查和解决,我总结了以下几点经验:

1. 构建错误排查方法论

  • 从简单到复杂:先尝试清除缓存、重新安装等简单操作
  • 分析错误堆栈:仔细阅读错误信息,定位问题发生的具体位置
  • 版本兼容性检查:检查相关依赖的版本兼容性
  • 代码审查:重点关注最近修改的代码

2. Vue 样式开发最佳实践

  • Vue 2 项目:推荐使用 /deep/ 或 CSS Modules
  • Vue 3 项目:推荐使用 :deep() 选择器
  • 大型项目:优先考虑 CSS Modules 或 CSS-in-JS 方案

3. 预防措施

// 在 package.json 中固定关键依赖版本
{
  "dependencies": {
    "vue": "^2.6.14"
  },
  "devDependencies": {
    "@vue/cli-service": "^4.5.19",
    "sass-loader": "^10.2.1"
  }
}

结语

这次 CSS 压缩错误的排查过程,让我对 Vue 的样式系统有了更深入的理解。从前端的表面现象到底层的构建原理,从简单的样式编写到复杂的工程化问题,每一个技术细节都值得深入探究。

作为前端开发者,我们不仅要会使用框架提供的便利功能,更要理解其背后的原理和实现机制。只有这样,当遇到问题时,我们才能快速定位并找到最优解决方案。

希望这篇文章能帮助到遇到类似问题的开发者,也欢迎大家分享自己的问题和解决方案,共同进步!

一文搞懂 CSS 定位:relative、absolute、fixed、sticky

作者 烟袅
2025年11月14日 13:27

在前端开发中,CSS 的 position 属性是布局的核心之一。理解不同定位方式的原理和使用场景,能让你轻松应对各种页面布局需求。

今天我们就来梳理一下常见的几种定位方式:relativeabsolutefixedsticky,以及它们与文档流的关系。


🌐 什么是文档流?

文档流是 HTML 元素默认的布局方式:

  • 块级元素垂直排列
  • 行内元素水平排列
  • 遵循从上到下、从左到右的自然顺序

当一个元素脱离了文档流,它不再占据原来的位置,后面的元素会“填补”它的空间。


🔹 1. position: relative —— 相对定位

position: relative;
  • 相对于自身原本位置进行偏移
  • 不会脱离文档流,原位置依然保留
  • 后续元素仍按标准流布局

✅ 适用于需要微调位置但不破坏布局的场景,比如配合 top/bottom/left/right 调整元素位置。


🔹 2. position: absolute —— 绝对定位

position: absolute;
  • 脱离文档流,不再占据空间
  • 相对于最近的 拥有定位属性的父元素 定位
  • 如果父元素没有定位,则以 body 或最近非 static 的祖先为参考

⚠️ 使用时注意:绝对定位的元素会“漂浮”在其他元素之上,需谨慎控制层级(z-index)。


🔹 3. position: fixed —— 固定定位

position: fixed;
  • 以浏览器窗口为参照物
  • 脱离文档流
  • 滚动页面时,元素位置固定不变

✅ 常用于顶部导航栏、侧边栏等需要“固定显示”的组件。


🔹 4. position: sticky —— 粘性定位

position: sticky;
  • 结合 relative 和 fixed 的特性
  • 默认行为像 relative
  • 当滚动到指定阈值(如 top: 0)时,变为 fixed,固定在视口某处

💡 例如:粘性标题、悬浮菜单,用户体验更友好。

.sticky-header {
  position: sticky;
  top: 0;
  background: #fff;
}

🔹 5. position: static —— 静态定位(默认)

position: static; /* 默认值 */
  • 元素按照正常文档流布局
  • topbottomleftright 无效
  • 一般不需要显式声明,除非要重置定位

🧩 总结对比表

定位方式 是否脱离文档流 参考对象 适用场景
relative ❌ 不脱离 自身原始位置 微调位置
absolute ✅ 脱离 最近定位父元素或 body 弹窗、遮罩层
fixed ✅ 脱离 浏览器窗口 固定导航、悬浮按钮
sticky 部分脱离 视口 + 文档流 粘性头部、侧边栏
static ❌ 不脱离 默认状态,无需设置

✅ 小贴士

  • 使用 absolute 时,记得给父容器设置 position: relative,避免定位混乱。
  • sticky 需要设置 top/bottom 等属性才生效。
  • display: none 会隐藏元素且不占空间,与定位无关,但常被混淆。

HTML&CSS&JS:赛博木鱼

作者 前端Hardy
2025年11月14日 09:55

用 HTML + CSS + JS 打造一个禅意十足的互动小应用——赛博木鱼, 本文将拆解其核心实现逻辑,带你从 0 到 1 理解 “敲木鱼” 背后的技术细节。


大家复制代码时,可能会因格式转换出现错乱,导致样式失效。建议先少量复制代码进行测试,若未能解决问题,私信回复源码两字,我会发送完整的压缩包给你。

演示效果

演示效果

演示效果

HTML&CSS


<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>赛博木鱼</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #121212;
            color: #FFFFFF;
            font-family: 'Kaiti SC', 'PingFang SC', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            overflow: hidden;
        }

        .title-container {
            text-align: center;
            margin-bottom: 20px;
        }

        .main-title {
            color: #FFFFFF;
            font-size: 6vw;
            font-family: 'Kaiti SC', sans-serif;
            background: linear-gradient(180deg,
                rgba(255, 176, 103, 0.05) 0%,
                rgba(115, 55, 0, 0.02) 100%);
            background-clip: text;
            margin: 0;
            line-height: 1;
        }

        .score-container {
            text-align: center;
            margin: 20px 0;
        }

        .score {
            font-size: 10vh;
            font-family: 'PingFang SC', sans-serif;
            font-weight: 900;
            margin: 0;
        }

        .description {
            font-size: 5vh;
            font-family: 'PingFang SC', sans-serif;
            margin-top: 10px;
        }

        .wooden-fish-container {
            position: relative;
            width: 300px;
            height: 250px;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 30px 0;
        }

        .wooden-fish {
            width: 284px;
            height: 236px;
            transition: transform 0.1s ease;
        }

        .mallet {
            position: absolute;
            top: 20px;
            right: 20px;
            width: 119px;
            height: 69px;
            transition: transform 0.1s ease;
            z-index: 10;
        }

        .floating-text {
            position: absolute;
            top: -50px;
            left: 50%;
            transform: translateX(-50%);
            font-family: 'PingFang SC', sans-serif;
            font-size: 1.5em;
            color: #FFFFFF;
            opacity: 0;
            pointer-events: none;
            animation: floatUp 0.4s ease-out forwards;
        }

        @keyframes floatUp {
            0% {
                opacity: 1;
                transform: translateX(-50%) translateY(0);
            }
            100% {
                opacity: 0;
                transform: translateX(-50%) translateY(-60px);
            }
        }

        .bottom-text {
            font-family: 'Kaiti SC', sans-serif;
            font-size: 1.2em;
            text-align: center;
            margin-top: 30px;
        }
    </style>
</head>
<body>
    <div class="title-container">
        <div class="main-title">赛博木鱼</div>
    </div>

    <div class="score-container">
        <div class="score" id="score">0</div>
        <div class="description">功德</div>
    </div>

    <div class="wooden-fish-container" id="fishContainer">
        <img src="https://img.alicdn.com/imgextra/i3/O1CN012CO0YU1VSfNs506ZS_!!6000000002652-2-tps-284-236.png"
             alt="木鱼" class="wooden-fish" id="woodenFish">
        <img src="https://img.alicdn.com/imgextra/i1/O1CN01tKb5Et1aSjWRjCHK3_!!6000000003329-2-tps-119-69.png"
             alt="锤子" class="mallet" id="mallet">
    </div>

    <div class="bottom-text">轻敲木鱼,细悟赛博真经。</div>

    <audio id="clickSound" preload="auto">
        <source src="https://qianwen.alicdn.com/resource/qiaomuyu.mp3" type="audio/mpeg">
    </audio>

    <script>
        let score = 0;
        const scoreElement = document.getElementById('score');
        const woodenFish = document.getElementById('woodenFish');
        const mallet = document.getElementById('mallet');
        const fishContainer = document.getElementById('fishContainer');
        const clickSound = document.getElementById('clickSound');

        fishContainer.addEventListener('click', function(e) {
            // 播放音效
            clickSound.currentTime = 0;
            clickSound.play();

            // 锤子旋转动画
            mallet.style.transform = 'rotate(-25deg)';
            setTimeout(() => {
                mallet.style.transform = 'rotate(0deg)';
            }, 100);

            // 木鱼放大动画
            woodenFish.style.transform = 'scale(1.1)';
            setTimeout(() => {
                woodenFish.style.transform = 'scale(1)';
            }, 100);

            // 增加分数
            score++;
            scoreElement.textContent = score;

            // 创建漂浮文字
            const floatingText = document.createElement('div');
            floatingText.className = 'floating-text';
            floatingText.textContent = '功德+1';
            fishContainer.appendChild(floatingText);

            // 移除漂浮文字
            setTimeout(() => {
                floatingText.remove();
            }, 400);
        });
    </script>
</body>
</html>




HTML

  • title-container:标题容器:承载 “赛博木鱼” 主标题,通过居中布局突出视觉核心
  • score-container:分数展示区:包含 “功德数” 和 “功德” 描述,是用户交互的核心反馈载体
  • wooden-fish-container:交互核心容器
  • wooden-fish:木鱼图片:通过 CDN 引入资源,无需本地存储,是点击交互的视觉核心
  • mallet:锤子图片:绝对定位在木鱼右上方,点击时通过旋转动画模拟 “敲击” 动作
  • clickSound:音效载体:预加载木鱼敲击音效(preload="auto"),点击时触发播放,增强沉浸感
  • floating-text:动态生成元素:点击时创建 “功德 + 1” 漂浮文字,完成交互反馈闭环

CSS

  • .mallet:点击时锤子绕右上角旋转 25 度,模拟 “敲下去” 的动作,100 毫秒快速回弹
  • .wooden-fish:点击时木鱼轻微放大 1.1 倍,模拟 “被敲击后的震动”,与锤子动作同步:
  • .floating-text:自动向上淡出,营造“功德升天”的视觉效果。

JavaScript

  1. 初始化变量:获取 DOM 元素与状态
let score = 0; // 功德数初始值
const scoreElement = document.getElementById('score'); // 分数显示元素
const woodenFish = document.getElementById('woodenFish'); // 木鱼元素
const mallet = document.getElementById('mallet'); // 锤子元素
const fishContainer = document.getElementById('fishContainer'); // 交互容器
const clickSound = document.getElementById('clickSound'); // 音效元素

通过 getElementById 获取核心交互元素,提前定义功德数状态。

  1. 核心交互:点击事件处理
fishContainer.addEventListener('click', function(e) {
    // 1. 播放敲击音效(每次点击从头播放,支持连续敲击)
    clickSound.currentTime = 0;
    clickSound.play();

    // 2. 锤子旋转动画:敲下→回弹
    mallet.style.transform = 'rotate(-25deg)';
    setTimeout(() => {
        mallet.style.transform = 'rotate(0deg)';
    }, 100);

    // 3. 木鱼缩放动画:放大→还原
    woodenFish.style.transform = 'scale(1.1)';
    setTimeout(() => {
        woodenFish.style.transform = 'scale(1)';
    }, 100);

    // 4. 功德数累加与更新
    score++;
    scoreElement.textContent = score;

    // 5. 生成“功德+1”漂浮文字
    const floatingText = document.createElement('div');
    floatingText.className = 'floating-text';
    floatingText.textContent = '功德+1';
    fishContainer.appendChild(floatingText);

    // 6. 动画结束后移除文字(避免DOM堆积)
    setTimeout(() => {
        floatingText.remove();
    }, 400);
});
  1. 关键技术点拆解

音效重置:clickSound.currentTime = 0 确保连续点击时音效不叠加,每次都是完整的 “敲击声”;

动画同步:锤子旋转和木鱼缩放的过渡时长(0.1s)与定时器延迟(100ms)一致,动作协调;

DOM 优化:漂浮文字动画结束后通过 remove()移除,避免页面 DOM 元素过多导致性能问题;

无依赖设计:不使用任何框架,仅原生 API,兼容性覆盖所有现代浏览器。


各位互联网搭子,要是这篇文章成功引起了你的注意,别犹豫,关注、点赞、评论、分享走一波,让我们把这份默契延续下去,一起在知识的海洋里乘风破浪!

前端好搭档:table 和 position sticky

2025年11月14日 09:14

前言

前端开发中,总是会接到各式各样的交互需求。最近产品想在移动端中实现一个报表功能,在页面中以表格的形式展示各个统计数据。

这本来是个普普通通的需求,使用 html 的 table 标签就可以实现,但是要求在页面滑动过程中表头要固定在顶部,同时在表格滑出页面时,表头取消固定在顶部,大致如下:

image-20251011154234246

表头固定现在有 CSS position: sticky 可以直接实现,但是滑出屏幕后不再固定,这个交互第一直觉要用 JS 去实现了,因为 position: sticky 应该会让元素一直保持在顶部,就像下面这个“固定在顶部的内容”元素一样。

table_base1

代码示例:

当时的想法是先写个示例看看,用 position: sticky 加 JS 实现。

方案实现

页面的基础内容如下,一个页面标题加内容,内容区域使用 table 标签实现表格功能。

<h1>HTML表格基础示例</h1>
<div class="content">
  <p>这是第一个表格,包含10条数据。</p>
  <table>
    <colgroup>
      <col style="width: 200px;">
      <col style="width: 150px;">
      <col style="width: 300px;">
      <col style="width: 200px;">
    </colgroup>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>职位</th>
        <th>部门</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>张三</td>
        <td>28</td>
        <td>软件工程师</td>
        <td>研发部</td>
      </tr>
      <!-- ...更多数据行 -->
    </tbody>
  </table>
  <!-- ...更多其他表格 -->
</div>

CSS 部分就是设置内容区域(类名为 content 的元素)可以滑动和表头固定,关键代码如下:

body {
    display: flex;
    flex-direction: column;
}
.content {
    flex: 1;
    overflow-y: auto;
}
table {
    border-collapse: collapse;
}
th {
    position: sticky;
    top: 0;
    z-index: 10;
}

代码中我们在表头设置了 position: sticky 属性,让其在滑动出屏幕时可以固定在顶部,让我们看下效果。

table_base2

示例代码:

可以看到我们直接实现了最终的效果,在表格离开视图后,表头也不再固定在顶部。平时在使用 position: sticky 时倒是没发现这点,查了 MDN 文档解释如下:

一个 sticky 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的 overflowhiddenscrollautooverlay 时),即便这个祖先不是最近的真实可滚动祖先。

但是示例代码中只在内容区域(类名为 content 的元素)设置了 overflow 属性,按理说表头会一直固定在顶部才对,只能暂时理解 table 有特殊的渲染机制。

总结

实践结果来说,在 table 表头设置 position: sticky 会有一些比较特殊的行为,这个行为又比较符合平时认知的习惯(表格的表头顶部固定,表格滑出屏幕后不再固定表头)。

上面的 position: sticky 仅仅是初步的方案,如果你的表格宽度超过一屏幕,此时的左右滑动会是基于内容区域(类名为 content 的元素),你可能想通过在 table 外层加一个 div 来实现仅仅 table 区域左右滑动,但是外层 div 设置了 overflow 属性,表头就不会再基于内容区域(类名为 content 的元素)固定了,所以可能需要通过 JS 实现左右滑动。

在此基础上要是有首列固定的需求,也需要通过 JS 去实现。

左右滑动是基于内容区域效果如下:

table_base3

最后,文中若有不对的地方,欢迎讨论指正。

🚀 10 分钟吃透 CSS position 定位!从底层原理到避坑实战,搞定所有布局难题

2025年11月12日 01:17

在前端开发中,布局是核心技能之一,而position属性更是布局的 “灵魂”—— 它决定了元素在页面中的位置关系,是实现复杂布局、悬浮组件、固定导航等效果的关键。很多开发者入门时会混淆absolutefixed,踩坑sticky不生效的问题,本质上是没吃透其底层原理。本文结合 5 个实战案例,从文档流本质出发,全面解析position的 5 种属性用法,帮你彻底掌握这个 CSS 基础核心知识点。

一、先搞懂:文档流是什么?

要理解position,必须先明确 “文档流” 的概念 —— 这是 HTML 元素默认的布局规则,就像现实中排队一样,元素按照代码顺序依次排列。

块级元素(如divp)默认垂直排列,每个元素独占一行,自上而下依次分布;行内元素(如spana)则水平排列,从左到右紧密排布,直到一行放不下才换行。这种 “自上而下、从左到右” 的自然布局方式,就是文档流。

position属性的核心作用,就是打破或遵循这个默认规则,改变元素的定位方式。其中,是否脱离文档流是区分不同定位属性的关键:脱离文档流的元素会 “跳出” 排队队伍,不再占用原来的位置,其他元素会忽略它重新排列;不脱离的元素则仍在队伍中,只是在原位置上进行微调。

二、逐个击破:5 种 position 属性的底层逻辑

1. static:默认定位(无定位)

staticposition的默认值,所有元素在未设置定位时,都遵循static规则 —— 按照文档流正常排列,不受topleftrightbottom属性影响。

核心特点

  • 完全遵循文档流,不脱离
  • 无法通过top/left等属性调整位置
  • 可用于取消已设置的定位(如示例 5 中,5 秒后将absolute改为static

实战示例(示例 5 初始状态):

css

.parent {
  position: absolute; /* 初始定位 */
  left: 100px;
  top: 100px;
}
/* 5秒后取消定位,恢复static默认状态 */
setTimeout(() => {
  oParent.style.position = 'static';
}, 5000);

position改为static后,lefttop失效,元素回到文档流的原始位置。


2. relative:相对定位(不脱离文档流)

relative是 “相对” 于自身在文档流中的原始位置进行定位,这是它最核心的特征。

核心特点

  • 不脱离文档流,原始位置仍被占用(后面元素不会补位)
  • 通过top/left/right/bottom调整位置,参考点是自身默认位置
  • 常作为absolute的 “定位容器”(子绝父相)

实战示例(示例 1):

css

.parent {
  width: 500px;
  height: 500px;
  background: pink;
  position: relative; /* 相对定位 */
  left: 100px;
  top: 100px;
}
.child {
  width: 300px;
  height: 200px;
  background: skyblue;
}

这里.parent会相对于自己的默认位置(左上角)向右移动 100px、向下移动 100px,而它原来的位置仍被占用,.box元素会按照文档流在它后面正常排列,不会向前补位。


3. absolute:绝对定位(完全脱离文档流)

absolute是最常用的定位属性之一,元素会完全脱离文档流,相当于 “跳出” 排队队伍,不再占用任何空间。

核心特点

  • 完全脱离文档流,原始位置被释放(后面元素会补位)
  • 定位参考点是 “最近的已定位祖先元素”(position不为static
  • 若没有已定位祖先,则参考body(浏览器视口)
  • 必须配合top/left等属性使用,否则定位无效

实战示例(示例 2):

css

.parent {
  width: 550px;
  height: 500px;
  background: pink;
  position: relative; /* 定位容器 */
}
.child {
  width: 300px;
  height: 200px;
  background: skyblue;
  position: absolute; /* 绝对定位 */
  right: 100px; /* 相对于.parent右侧偏移100px */
}
.box {
  width: 100px;
  height: 100px;
  background: green;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); /* 水平垂直居中 */
}

示例中.parent设置relative作为定位容器,.child.boxabsolute定位都以.parent为参考。其中.box通过left:50%+top:50%+transform:translate(-50%,-50%)实现了相对于父容器的完美居中,这是absolute的经典用法。


4. fixed:固定定位(完全脱离文档流)

fixed的核心是 “固定于浏览器视口”,无论页面如何滚动,元素位置始终不变。

核心特点

  • 完全脱离文档流,不占用原始位置
  • 定位参考点是浏览器视口(可视区域)
  • 不受祖先元素定位影响(即使父元素有relative,仍参考视口)
  • 常用来实现固定导航、悬浮按钮、弹窗等

实战示例(示例 3):

css

.child {
  width: 300px;
  height: 200px;
  background: blue;
  position: fixed; /* 固定定位 */
  right: 100px;
  bottom: 100px; /* 相对于视口右下角偏移100px */
}
body {
  height: 2000px; /* 让页面可滚动 */
}

即使页面滚动,.child始终固定在视口右下角 100px 的位置。需要注意的是,fixed元素会跟随浏览器窗口移动,不会被父容器的overflow属性影响(除非父容器有transform属性,这是常见坑点)。


5. sticky:粘性定位(动态切换定位方式)

sticky是 “相对定位” 和 “固定定位” 的结合体,堪称布局神器,常用于导航栏滚动吸顶效果。

核心特点

  • 未达到滚动阈值时,表现为relative(遵循文档流)
  • 达到滚动阈值时,自动切换为fixed(固定于视口)
  • 不脱离文档流,原始位置仍被占用(切换为fixed后也不会让后面元素补位)
  • 必须配合top/left等属性设置阈值,否则无效

实战示例(示例 4):

css

.box {
  width: 100px;
  height: 100px;
  background: green;
  position: sticky; /* 粘性定位 */
  top: 100px; /* 滚动阈值:距离视口顶部100px时固定 */
}
body {
  height: 2000px; /* 可滚动页面 */
}

当页面滚动时,.box在未到达视口顶部 100px 前,会跟随文档流正常滚动;一旦距离顶部小于 100px,就会固定在顶部 100px 的位置,直到滚动到父元素底部,又会恢复relative状态。

三、关键区别:5 种定位属性核心对比

定位属性 是否脱离文档流 定位参考点 核心用途
static 否(默认) 取消定位、默认布局
relative 自身默认位置 微调位置、作为 absolute 容器
absolute 最近已定位祖先 /body 精准定位、弹窗、居中
fixed 浏览器视口 固定导航、悬浮组件
sticky 否(动态切换) 文档流 / 视口 滚动吸顶、粘性导航

四、实战避坑:这些问题一定要注意

  1. absolute无法定位? 检查父元素是否有position: relative/absolute/fixed/sticky,若没有则参考body,可能因父元素未设置高度导致定位异常。
  2. sticky不生效? 确保满足三个条件:设置了top/left等阈值、父元素没有overflow: hidden、元素在文档流中(没有脱离文档流的祖先)。
  3. fixed被父元素 “困住”? 若父元素有transform属性(如transform: translate(0)),fixed会以该父元素为参考,而非视口,需避免这种嵌套。
  4. 脱离文档流的影响? absolutefixed会脱离文档流,可能导致页面布局塌陷,需提前预留空间或用其他元素补位。

五、总结:定位属性的选择逻辑

  1. 只需微调元素位置,不影响其他布局 → relative
  2. 需精准定位,且不占原始空间 → absolute(配合relative容器)
  3. 需固定在视口,不受滚动影响 → fixed
  4. 需滚动吸顶 / 吸底效果 → sticky
  5. 恢复默认布局或取消定位 → static

position属性看似简单,实则是 CSS 布局的底层逻辑体现。掌握它们的核心区别和使用场景,能让你在实现复杂布局时游刃有余。建议结合本文的示例代码,亲自在浏览器中调试,观察元素位置变化,加深对文档流和定位规则的理解。

最后

如果你在使用position时遇到过特殊坑点,或者有更巧妙的用法,欢迎在评论区分享~ 也可以点赞收藏,下次遇到定位问题直接翻这篇指南!

❌
❌