当前位置:首页 > CSS

css制作加号按钮

2026-01-28 17:18:02CSS

使用伪元素创建加号按钮

通过CSS伪元素(::before::after)可以高效地绘制加号形状。核心思路是创建一个水平条和一个垂直条叠加形成加号。

.plus-button {
  width: 40px;
  height: 40px;
  position: relative;
  background-color: #3498db;
  border-radius: 4px;
  cursor: pointer;
}

.plus-button::before,
.plus-button::after {
  content: "";
  position: absolute;
  background-color: white;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.plus-button::before {
  width: 60%;
  height: 4px;
}

.plus-button::after {
  width: 4px;
  height: 60%;
}

使用边框实现加号效果

通过控制边框宽度和元素尺寸,可以直接用CSS边框绘制加号。这种方法适合需要简单图标的场景。

.plus-icon {
  width: 20px;
  height: 20px;
  position: relative;
}

.plus-icon::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: 0;
  height: 100%;
  border-left: 2px solid black;
}

.plus-icon::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: 0;
  border-top: 2px solid black;
}

结合SVG的响应式加号

使用内联SVG可以创建分辨率无关的加号按钮,适合需要高清晰度的场景。

<button class="svg-plus">
  <svg viewBox="0 0 24 24" width="24" height="24">
    <path d="M12 4V20M4 12H20" stroke="currentColor" stroke-width="2"/>
  </svg>
</button>
.svg-plus {
  width: 48px;
  height: 48px;
  background: #2ecc71;
  border: none;
  border-radius: 50%;
  color: white;
  padding: 12px;
}

动画增强的加号按钮

为加号按钮添加悬停动画可以提升用户体验,以下示例实现旋转和颜色变化效果。

.animated-plus {
  width: 50px;
  height: 50px;
  background: #e74c3c;
  position: relative;
  transition: all 0.3s ease;
}

.animated-plus:hover {
  background: #c0392b;
  transform: rotate(90deg);
}

.animated-plus::before,
.animated-plus::after {
  content: "";
  position: absolute;
  background: white;
  transition: all 0.3s ease;
}

.animated-plus::before {
  width: 60%;
  height: 8%;
  left: 20%;
  top: 46%;
}

.animated-plus::after {
  width: 8%;
  height: 60%;
  left: 46%;
  top: 20%;
}

使用Flexbox居中加号

当需要将加号完美居中在按钮内部时,Flexbox布局是最简洁的解决方案。

css制作加号按钮

.flex-plus {
  width: 64px;
  height: 64px;
  background: #9b59b6;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
}

.flex-plus span {
  position: relative;
  width: 30px;
  height: 30px;
}

.flex-plus span::before,
.flex-plus span::after {
  content: "";
  position: absolute;
  background: white;
}

.flex-plus span::before {
  width: 100%;
  height: 4px;
  top: 13px;
}

.flex-plus span::after {
  width: 4px;
  height: 100%;
  left: 13px;
}

标签: 加号按钮
分享给朋友:

相关文章

vue实现按钮

vue实现按钮

Vue 实现按钮的方法 使用原生 HTML 按钮 在 Vue 模板中可以直接使用 HTML 的 <button> 元素,通过 v-on 或 @ 绑定点击事件。 <template&…

vue实现按钮弹窗

vue实现按钮弹窗

实现按钮弹窗的基本方法 在Vue中实现按钮点击触发弹窗功能,可以通过多种方式完成。以下是几种常见实现方法: 使用原生HTML和Vue指令 通过v-if或v-show控制弹窗显示状态,结合点击事件切换…

vue实现单选按钮

vue实现单选按钮

实现单选按钮的基本方法 在Vue中实现单选按钮通常使用v-model指令绑定数据,结合input元素的type="radio"属性。以下是一个基础示例: <template> <…

vue实现按钮渐变

vue实现按钮渐变

Vue 中实现按钮渐变的几种方法 使用 CSS 线性渐变 通过 CSS 的 background 属性实现线性渐变效果,适用于大多数场景。 <template> <button…

css3按钮制作

css3按钮制作

CSS3 按钮制作方法 基础按钮样式 使用 border-radius 和 padding 创建圆角按钮: .btn { display: inline-block; padding: 1…

vue实现按钮循环

vue实现按钮循环

Vue 实现按钮循环的方法 使用 v-for 指令 在 Vue 中可以通过 v-for 指令轻松实现按钮的循环渲染。假设有一个按钮数组,可以这样实现: <template> <…