【常用CSS】搜罗天下之常用的CSS特效,总有一款用的着

1,049 阅读3分钟

本文已参与好文召集令活动,点击查看:后端、大前端双赛道投稿,2 万元奖池等你挑战!

实用 CSS 特效

一、svg 实现跳转 github 图标

在查阅文档时, 经常看到这个分享图标, 鼠标移上去还会动, 以为很是麻烦, 就是一个 svg 图标

搭建个人博客等网站时,也可以把这个按钮添加进来, 有点技术元素了没! 先上效果:

1

超简单使用: 代码

<!-- 右上角 github corner -->
<div class="svg-wrap">
  <a href="https://your-url/" target="_blank" class="github-corner">
    <svg width="80" height="80" viewBox="0 0 250 250" style="fill:#b45dea; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true">
      <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
      <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
      <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body" ></path>
    </svg>
  </a>
</div>

二、四角边框

需求: 是不是似曾相识..

qr-card

实现效果如下:

qr-card-example

Talk is cheap, show the codes

<div class="card-wrapper">
  <div class="content-box"></div>
</div>
.card-wrapper {
  display: inline-block;
  padding: 13px;
  background: linear-gradient(#00ffd4, #00ffd4) left top, linear-gradient(#00ffd4, #00ffd4) left top,
    linear-gradient(#00ffd4, #00ffd4) right top, linear-gradient(#00ffd4, #00ffd4) right top,
    linear-gradient(#00ffd4, #00ffd4) left bottom, linear-gradient(#00ffd4, #00ffd4) left bottom,
    linear-gradient(#00ffd4, #00ffd4) right bottom, linear-gradient(#00ffd4, #00ffd4) right bottom;
  background-repeat: no-repeat;
  background-size: 2px 20px, 20px 2px;
}
.content-box {
  width: 213px;
  height: 132px;
  border: #b45dea 1px solid;
  background: rgba(213, 213, 213, 0.3);
}

三、渐变边框的探索

1. 最原始方案: 背景组合 叠加产生渐变边框

父元素使用背景渐变 linear-gradient, 再通过叠加一个其他颜色的背景盖在上面, 即子(伪)元素使用背景 background 盖住, 从而达到渐变边框的需求. 效果如下:

1.背景组合产生渐变边框

非常简单,简单的示意图如下:

1.背景组合产生渐变边框

实现代码如下:

<div class="box bd-6">
  <div class="content bd-6">1. 背景组合产生渐变边框</div>
</div>
<div class="box">
  <div class="content">1. 背景组合产生渐变边框</div>
</div>
.bd-6 {
  border-radius: 6px;
}
.box {
  width: 270px;
  height: 66px;
  background: linear-gradient(145deg, #0061fe, #ff00f0);
}
.content {
  width: 265px;
  height: 60px;
  font-size: 18px;
  color: #fff;
  text-align: center;
  margin: 3px;
  background-color: rgb(24, 24, 24);
}

同理也可以使用 伪元素 ::before::after, 这样一个标签就可实现了,

但是如果需求背景是透明的就不行了

2. CSS3 border 属性 border-image

前面的实现方法都是使用了多个标签, 不雅也不推荐,

根据 CSS3 找到最新实现方案: border-image 我们可以给 border 元素添加 image, 类似于 background-image,可以是渐变也可以是图片, 也可背景透明, 但设置圆角会失效。

具体语法可以参考: border-image

<div class="border-image">border-image</div>
.border-image {
  width: 200px;
  height: 100px;
  /* 这里边框类型没有效果 但仍需设置 */
  border: 10px solid;
  /* 圆角失效 */
  border-radius: 10px;
  border-image-source: linear-gradient(145deg, #0061fe, #ff00f0);
  border-image-slice: 1;
  border-image-repeat: stretch;
}

border-radius 失效

border-image

3. 使用 background-clip

只需一个单独的标签即可,

同样: 如果需求背景是透明的就不行了

bg-clip

.bg-clip {
  width: 200px;
  height: 100px;
  border: solid 10px transparent;
  border-radius: 10px;
  background-image: linear-gradient(#fee, #fee), linear-gradient(to right, green, gold);
  background-origin: border-box;
  background-clip: content-box, border-box;
}

4. clip-path

<div class="border-image-clip-path"></div>
.border-image-clip-path {
  position: relative;
  width: 200px;
  height: 80px;
  border: 10px solid;
  border-image: linear-gradient(45deg, #0061fe, #ff00f0) 1;
  clip-path: inset(0 round 10px);
}

另外,还可以用 filter: hue-rotate() 再加个渐变动画:

.border-image-clip-path {
  filter: hue-rotate(360deg);
}

@keyframes huerotate {
  0% {
    filter: hue-rotate(0deg);
  }
  100% {
    filter: hue-rorate(360deg);
  }
}

边框背景渐变动画:

1

四、渐变字体

grident-text

<h2 class="gradient-text">
  gradient Text gradient Text gradient Text gradient Text gradient Text gradient Text
</h2>
.gradient-text {
  background-image: linear-gradient(to right, #0061fe, #ff00f0);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-transform: uppercase;
}

五、在售状态标志

比较原始的方案: 采用定位方法...后续更新...

<div class="box">
  <div class="badge">ON SALE</div>
  <img
    src="https://cdn.jsdelivr.net/gh/xn213/img-hosting@master/juejin-posts-imgs/compare_mbp16__fykfvftfaeuu_large.6fi0c3rwsq00.png"
    alt=""
  />
</div>
.box {
  width: 200px;
  height: 200px;
  position: relative;
  overflow: hidden;
}
.badge {
  position: absolute;
  top: 20px;
  left: -30px;
  background: red;
  color: #fff;
  width: 120px;
  text-align: center;
  transform: rotate(-45deg);
}

compare_mbp16__fykfvftfaeuu_large_onsale

其他

color-copy-share

🎉

🎉

🎉