CSS 系列之:一些实用功能

发布于:2025-06-20 ⋅ 阅读:(20) ⋅ 点赞:(0)

文字处理

文字超长显示省略号

span {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    width: 200px; /* 根据需求调整宽度 */
    display: inline-block; /* 或者其他合适的display值 */
}

white-space 空格和换行处理

HTML 代码的空格通常会被浏览器忽略。文字的前部和后部的空格都会忽略,内部的连续空格只会算作一个。这就是浏览器处理空格的基本规则。

<p>  hello   world  </p>

输出:hellow word。

如果希望空格原样输出,可以使用 <pre> 标签。

<pre>  hello  world  </pre>

或使用转义字符

<p>&nbsp;&nbsp;hello&nbsp;&nbsp;world&nbsp;&nbsp;</p>
描述
normal 默认。合并连续的空白字符为单个空格,文本在必要时自动换行。
nowrap 合并空白字符,禁止文本换行,除非遇到<br>标签。(最常用
pre 保留空白字符,禁止自动换行,类似 HTML 中的<pre>标签。
pre-wrap 保留空白字符,文本在必要时或遇到换行符时换行。
pre-line 合并空白字符,但保留换行符,文本在必要时换行。
break-spaces CSS3 新增,类似pre-wrap,但每个空白字符后都可能换行,且空白字符会占据空间。

text-overflow 文本溢出

规定当文本溢出包含元素时发生的事情。

描述
clip 默认值,修剪文本。
ellipsis [ɪˈlɪpsɪs] 显示省略符号来代表被修剪的文本。
string 使用给定的字符串来代表被修剪的文本。

letter-spacing 调整文字间距

letter-spacing: 10px;

text-shadow 应用于阴影文本

text-shadow: h-shadow v-shadow blur color;
text-shadow: 5px 5px 5px #FF0000;
描述
h-shadow 必需。水平阴影的位置。允许负值。
v-shadow 必需。垂直阴影的位置。允许负值。
blur 可选。模糊的距离。
color 可选。阴影的颜色。

可以解决使用 font-weight: bold; 后字体跳动的问题

text-decoration 定义文字下划线

描述
none 默认。定义标准的文本。
underline 定义文本下的一条线。
overline 定义文本上的一条线。
line-through 定义穿过文本下的一条线。
blink 定义闪烁的文本。
inherit 规定应该从父元素继承 text-decoration 属性的值。

transform

作用

对元素进行平移(translate)、旋转(rotate)、缩放(scale)、倾斜(skew),它可以改变元素的形状、位置和大小,而不会影响文档的正常流布局。

CSS transform 是高性能的属性,因为它主要由浏览器的合成器线程处理,而不会触发重排(reflow)和重绘(repaint),只会触发合成(composite),因此非常适合创建流畅的动画效果。

基本用法

  1. 平移变换

    • translate(x, y):沿 X 轴和 Y 轴移动元素

    • translateX(x):仅沿 X 轴移动

    • translateY(y):仅沿 Y 轴移动

    • translateZ(z):仅沿 Z 轴移动(需要 3D 环境)

    • translate3d(x, y, z):3D 平移

  2. 旋转变换

    • rotate(angle):2D 旋转(围绕 Z 轴)

    • rotateX(angle):围绕 X 轴旋转

    • rotateY(angle):围绕 Y 轴旋转

    • rotateZ(angle):围绕 Z 轴旋转(等同于 rotate)

    • rotate3d(x, y, z, angle):3D 旋转

  3. 缩放变换

    • scale(x, y):按比例缩放元素

    • scaleX(x):仅在 X 轴上缩放

    • scaleY(y):仅在 Y 轴上缩放

    • scaleZ(z):仅在 Z 轴上缩放

    • scale3d(x, y, z):3D 缩放

  4. 倾斜变换

    • skew(x-angle, y-angle):沿 X 轴和 Y 轴倾斜元素

    • skewX(angle):仅沿 X 轴倾斜

    • skewY(angle):仅沿 Y 轴倾斜

  5. 矩阵变换

    • matrix(a, b, c, d, e, f):2D 变换矩阵

    • matrix3d(a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4):3D 变换矩阵

原点变换

通过 transform-origin 属性可以设置变换的中心点,默认值是元素的中心点(50% 50%)。例如:

transform-origin: top left; /* 左上角作为变换原点 */
transform-origin: 0 0; /* 同样是左上角 */
transform-origin: 20px 30px; /* 自定义坐标 */

3D 变换环境

要启用 3D 变换效果,需要在父元素上设置:

perspective: 1000px; /* 透视距离,值越小,3D效果越明显 */
perspective-origin: 50% 50%; /* 透视点位置 */
transform-style: preserve-3d; /* 子元素保持3D变换效果 */

综合示例

<template>
  <div>
    <div class="demo">原始状态</div>
    <div class="demo translate">translate(50px, 20px)</div>
    <div class="demo rotate">rotate(45deg)</div>
    <div class="demo scale">scale(1.5, 0.7)</div>
    <div class="demo skew">skew(20deg, 10deg)</div>
    <div class="demo matrix">matrix(1, 0.5, -0.5, 1, 50, 20)</div>

    <div class="perspective-container">
      <div class="demo rotate-x">rotateX(45deg)</div>
    </div>

    <div class="perspective-container">
      <div class="demo rotate-y">rotateY(45deg)</div>
    </div>

    <div class="perspective-container">
      <div class="demo rotate-3d">rotate3d(1, 1, 1, 45deg)</div>
    </div>

    <div class="demo complex">复合变换</div>
  </div>
</template>

<style>
.demo {
  width: 150px;
  height: 150px;
  background-color: #4CAF50;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 10px;
  transition: transform 0.5s;
}

.demo:hover {
  transform: scale(1.1);
}

.translate {
  transform: translate(50px, 20px);
}

.rotate {
  transform: rotate(45deg);
}

.scale {
  transform: scale(1.5, 0.7);
}

.skew {
  transform: skew(20deg, 10deg);
}

.matrix {
  transform: matrix(1, 0.5, -0.5, 1, 50, 20);
}

.perspective-container {
  perspective: 500px;
}

.rotate-x {
  transform: rotateX(45deg);
}

.rotate-y {
  transform: rotateY(45deg);
}

.rotate-3d {
  transform: rotate3d(1, 1, 1, 45deg);
}

.complex {
  transform: translate(50px, 20px) rotate(30deg) scale(1.2);
}
</style>

transition

作用

让CSS属性值在一定时间内平滑变化的效果,而不是瞬间改变,这使得网页元素的状态变化更加流畅和自然。

基本语法

transition是一个简写属性,可以同时设置四个过渡相关的属性:

transition: property duration timing-function delay;
  • property:指定要应用过渡效果的 CSS 属性名称。

    • all:默认值,所有可过渡的属性。

    • 单个属性名:并非所有 CSS 属性都能应用过渡效果,只有具有中间值的属性才能过渡。常见的可过渡属性包括:

      • 尺寸相关:width、height、margin、padding 等。

      • 位置相关:top、right、bottom、left 等。

      • 颜色相关:color、background-color、border-color 等。

      • 透明度:opacity。

      • 变换:transform。

      • 其他:box-shadow、text-shadow、filter 等。

    • 多个属性:用逗号分隔,如 width, height, opacity。

  • duration:过渡效果的持续时间,可以使用秒 (s) 或毫秒 (ms) 为单位。

    • 必须指定,否则默认值为 0s,不会有过渡效果
  • timing-function:定义过渡的速度曲线,控制过渡的节奏。

    • ease:默认值,慢 - 快 - 慢。

    • linear:匀速过渡。

    • ease-in:慢速开始,逐渐加速。

    • ease-out:快速开始,逐渐减速。

    • ease-in-out:慢速开始和结束,中间加速。

    • cubic-bezier(n,n,n,n):自定义贝塞尔曲线,通过四个数值控制点来定义。

    • steps(n, start/end):分步过渡,n 表示步数,start 或 end 指定每一步的开始或结束状态。

  • delay:过渡效果开始前的延迟时间,单位同样是秒或毫秒。

    • 可以是正数(延迟开始)或负数(提前开始,但从中间状态开始显示)。

触发方式

过渡效果需要一个触发条件才能启动,常见的触发方式包括:

  • :hover:鼠标悬停时。

  • :focus:元素获得焦点时。

  • :active:元素被激活时(如按钮被点击)。

  • :checked:表单元素被选中时。

  • JavaScript 动态修改元素的 CSS 属性时。

综合示例

<template>
  <div>
    <h2>基本按钮悬停效果</h2>
    <button class="btn">悬停效果</button>

    <h2>尺寸变化过渡</h2>
    <div class="box">悬停查看效果</div>

    <h2>透明度变化过渡</h2>
    <div class="fade">悬停查看效果</div>

    <h2>颜色变化过渡</h2>
    <p class="color-change">悬停查看文字颜色变化</p>

    <h2>自定义贝塞尔曲线过渡</h2>
    <div class="bezier">悬停查看效果</div>

    <h2>分步过渡效果</h2>
    <div class="steps">悬停查看效果</div>
  </div>
</template>

<style>
.btn {
  display: inline-block;
  padding: 10px 20px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;

  /* 应用过渡效果:宽度、背景色、transform和阴影,持续0.3秒,使用ease-in-out速度曲线 */
  transition: width 0.3s ease-in-out,
    background-color 0.3s ease-in-out,
    transform 0.3s ease-in-out,
    box-shadow 0.3s ease-in-out;
}

/* 鼠标悬停时的样式变化 */
.btn:hover {
  background-color: #2980b9;
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* 尺寸变化示例 */
.box {
  width: 100px;
  height: 100px;
  background-color: #e74c3c;
  margin-top: 20px;

  /* 过渡:宽度和高度,持续1秒,延迟0.5秒 */
  transition: width 1s, height 1s;
  transition-delay: 0.5s;
}

.box:hover {
  width: 200px;
  height: 200px;
}

/* 透明度变化示例 */
.fade {
  opacity: 0.5;
  margin-top: 20px;
  padding: 10px;
  background-color: #2ecc71;
  color: white;

  /* 过渡:透明度,持续0.5秒 */
  transition: opacity 0.5s;
}

.fade:hover {
  opacity: 1;
}

/* 颜色变化示例 */
.color-change {
  color: #9b59b6;
  margin-top: 20px;

  /* 过渡:颜色,持续0.8秒,使用ease-in速度曲线 */
  transition: color 0.8s ease-in;
}

.color-change:hover {
  color: #e67e22;
}

/* 自定义贝塞尔曲线示例 */
.bezier {
  margin-top: 20px;
  width: 100px;
  height: 50px;
  background-color: #1abc9c;

  /* 过渡:宽度,持续1.5秒,使用自定义贝塞尔曲线 */
  transition: width 1.5s cubic-bezier(0.1, 0.7, 1.0, 0.1);
}

.bezier:hover {
  width: 300px;
}

/* 分步过渡示例 */
.steps {
  margin-top: 20px;
  width: 100px;
  height: 50px;
  background-color: #34495e;

  /* 过渡:宽度,持续1秒,分5步过渡 */
  transition: width 1s steps(5, end);
}

.steps:hover {
  width: 300px;
}
</style>

也可以通过 JavaScript 动态修改元素的 CSS 属性,从而触发过渡效果:

// 获取元素
const element = document.querySelector('.box');

// 添加点击事件
element.addEventListener('click', () => {
    // 修改元素样式,触发过渡
    element.style.width = '200px';
    element.style.height = '200px';
});

注意事项

transition 应该写在元素的默认状态上,而不是在触发状态(如:hover)中。这样设置后,当元素的属性值在默认状态和触发状态之间切换时,都会有过渡动画效果。

如果 transition 写在触发状态,那么从默认状态到触发状态没有动画,只有从触发状态回到默认状态时才有动画。

某些属性的过渡(如 width, height, margin, padding 等)可能导致浏览器重排(reflow),影响性能。建议优先使用 transform 和 opacity 等性能更好的属性。

磨砂玻璃效果

 <div class="parent">
   <div class="children"></div>
 </div>
 
 <style>
.parent {
  height: 100%;
  padding: 50px;
  background-color: skyblue;
}

.children {
  height: 200px;
  width: 200px;
  background: rgba(255, 255, 255, 0.2); /* 半透明背景 */
  backdrop-filter: blur(10px); /* 模糊效果 */
  -webkit-backdrop-filter: blur(10px); /* Safari 支持 */
  border-radius: 10px; /* 可选圆角 */
  border: 1px solid rgba(255, 255, 255, 0.3); /* 可选边框 */
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 可选阴影 */
}
</style>

在这里插入图片描述

opacity 透明度

[əʊˈpæsəti]

  • 作用:控制元素及其所有子元素的整体透明度。

  • 取值:0(完全透明)到1(完全不透明)之间的小数。

  • 特点:

    • 影响元素的整个渲染结果,包括内容、背景和边框。

    • 子元素无法通过设置更高的不透明度来覆盖父元素的透明度。

.element {
  opacity: 0.5;
}

filter 滤镜

  • 作用:对元素应用图形效果(如模糊、亮度、饱和度等)。

  • 取值:内置滤镜函数(如blur()、brightness()、grayscale()等)。

  • 特点:

    • 会影响元素自身及其所有子元素。

    • 可以叠加多个滤镜效果。

.element {
  filter: blur(5px);       /* 高斯模糊 */
  filter: brightness(0.5); /* 亮度降低 50% */
  filter: grayscale(100%); /* 完全灰度 */
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5)); /* 投影 */
  
  /* 叠加多个滤镜 */
  filter: blur(3px) brightness(0.8) saturate(1.5);
}

示例:

<div class="parent">
  <div class="children"></div>
</div>

<style>
  .parent {
    filter: blur(2px) opacity(50%);
    background: blue;
    padding: 20px;
  }
  .children {
    background: red;
    width: 50px;
    height: 50px;
  }
</style>

在这里插入图片描述

如果希望滤镜仅作用于元素自身而不影响子元素,可以将滤镜应用在伪元素上,子元素放在伪元素之外

.parent {
  position: relative;
  background: transparent;
  padding: 20px;
}
.parent::before {
  content: "";
  position: absolute;
  inset: 0;
  background: blue;
  filter: blur(2px) opacity(50%);
  z-index: -1;
}
.children {
  background: red;
  width: 50px;
  height: 50px;
}

在这里插入图片描述

backdrop-filter 背景滤镜

  • 作用:对元素背后的区域(即背景)应用滤镜效果,而不影响元素自身。

  • 取值:与 filter 类似的滤镜函数(如blur()brightness()等)。

  • 特点:

    • 需配合半透明背景(如background-color: rgba(255,255,255,0.5))才能看到效果。

    • 常用于创建毛玻璃效果。

<template>
  <div class="parent">
    <div class="children">
      我是内容
    </div>
  </div>
</template>

<style>
.parent {
  background: skyblue;
  padding: 20px;
  width: 200px;
}

.children {
  background-color: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border-radius: 10px;
  padding: 20px;
  text-align: center;
}
</style>

在这里插入图片描述
filter 和 backdrop-filter 最大的区别是:

  • filter 改变元素本身的效果

  • backdrop-filter 改变元素背后的区域的效果

水平垂直居中

Flexbox

使用 display: flex 配合 justify-content: center 和 align-items: center

# 基础样式
.parent {
    width: 200px;
    height: 200px;
    margin: 10px;
    background-color: #e3f2fd;
    border: 1px solid #90caf9;
    display: inline-block;
    vertical-align: top;
}
.child {
    width: 50px;
    height: 50px;
    background-color: #2196f3;
    color: white;
    text-align: center;
    line-height: 50px;
    font-weight: bold;
}
.parent {
    display: flex;
    justify-content: center;
    align-items: center;
}

在这里插入图片描述

Flexbox - 带 margin auto

使用 display: flex 配合 margin: auto

.parent {
    display: flex;
}

.child {
    margin: auto;
}

Grid - 最简单的方法

使用 display: grid 配合 place-items: center

.parent {
    display: grid;
    place-items: center;
}

Grid - 带 margin auto

使用 display: grid 配合 margin: auto

.parent {
    display: grid;
}

.child {
    margin: auto;
}

绝对定位 + transform

使用 position: absolute 配合 top: 50%, left: 50% 和 transform: translate(-50%, -50%)

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

绝对定位 + 负边距

使用 position: absolute 配合 top: 50%, left: 50% 和负边距

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -25px; /* 高度的一半 */
    margin-left: -25px; /* 宽度的一半 */
}

绝对定位 + 全部为0 + margin auto

使用 position: absolute 配合 top: 0, right: 0, bottom: 0, left: 0 和 margin: auto

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
}

绝对定位 + calc

使用 position: absolute 配合 calc 函数

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: calc(50% - 25px); /* 50% - 高度的一半 */
    left: calc(50% - 25px); /* 50% - 宽度的一半 */
}

绝对定位 + 行高和文本对齐

使用 position: relative 配合 text-align: center 和 line-height

.parent {
    position: relative;
    text-align: center;
    line-height: 200px; /* 父元素高度 */
}

.child {
    display: inline-block;
    vertical-align: middle;
    line-height: 50px; /* 重置回子元素高度 */
}

表格布局

使用 display: table-cell 配合 text-align: center 和 vertical-align: middle

.parent {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

.child {
    display: inline-block;
}

margin 叠加效应

废话不多说,先看代码:

<template>
  <div>
    <div class="top">头部</div>
    <div class="parent">
      <div class="children">
        内容......
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
.top {
  position: fixed;
  height: 30px;
  background-color: blue;
  top: 0;
  left: 0;
  width: 100%;
}

.parent {
  margin-top: 30px;
  background-color: yellow;
  height: calc(100vh - 30px);
}

.children {
  margin-top: 50px;
  background-color: #666;
}
</style>

效果图:
在这里插入图片描述
头部和内容中间有一段白色区域,为什么?

原因是由于 .parent 和 .children 元素的 margin 叠加效应(margin collapsing)造成的。

  • .parent 设置了 margin-top: 30px,这是为了让内容区域避开 fixed 定位的头部(30px 高)。

  • .children 又设置了 margin-top: 50px,这会与父元素 .parent 的 margin 发生叠加。

  • 叠加后的效果是:实际渲染时,父元素的 margin 会被子元素的更大 margin(50px)取代,导致整体下移 50px(而不是预期的 30px)。

    • 这里如果子元素的 margin 小于 父元素的 margin,则父元素的 margin 会覆盖子元素的 margin,即子元素的 margin 会失效。
  • 因此,头部(30px)和 .parent 的实际开始位置(50px)之间会出现 20px 的白色间隙。

解决方法非常简单,只需要为 .parent 元素创建 BFC 即可。

BFC 即块级格式化上下文(Block Formatting Context),是一个独立的渲染区域,规定了内部的块级元素如何布局,并且与外部元素相互隔离。简单来说,BFC 就像是一个容器,容器内的元素布局不受外部影响,同时也不会影响外部元素。

触发 BFC 的方式:

方式 示例
float(非 none) float: left / float: right
position(绝对定位) position: absolute / position: fixed
display(特定值) display: inline-block / display: table-cell / display: flex
overflow(非 visible) overflow: hidden / overflow: auto / overflow: scroll
contain(新标准) contain: layout / contain: content

网站公告

今日签到

点亮在社区的每一天
去签到