首页 > 其他分享 >纯css实现边框环绕的效果

纯css实现边框环绕的效果

时间:2023-09-14 12:45:45浏览次数:35  
标签:color ease 0.2 边框 width 环绕 height border css

最近做项目遇到一个需求,需要给卡片添加一个边框环绕的效果,当鼠标移入卡片时,出现边框。
这里我主要是使用:before 和 :after来辅助实现这个功能。
在很多时候,我们可以巧用:before 或者 :after 来实现一些看起来较为复杂的效果。
我们先看看效果:

代码如下,希望大家能受到些许的启发,能在自己的项目中也运用起来。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="box">
      <div class="border-line"></div>
    </div>
    <style>
    .box {
      width: 260px;
      height: 100px; 
      margin: auto;
      position: relative;
      box-sizing: border-box;
    }

    .border-line {
      width: 100%;
      height: 100%;
      border: 1px solid #ccc;
      transition: all 0.6s ease-in;
      box-sizing: border-box;
    }

    .border-line:before {
      bottom: 0;
      right: 0;
      -webkit-transition: border-color 0s ease-in 0.4s, width 0.2s ease-in 0.2s, height 0.2s ease-in;
      transition: border-color 0s ease-in 0.4s, width 0.2s ease-in 0.2s, height 0.2s ease-in;
    }

    .border-line:after {
      top: 0;
      left: 0;
      -webkit-transition: border-color 0s ease-in 0.8s, width 0.2s ease-in 0.6s, height 0.2s ease-in 0.4s;
      transition: border-color 0s ease-in 0.8s, width 0.2s ease-in 0.6s, height 0.2s ease-in 0.4s;
    }

    .border-line:before,
    .border-line:after {
      content: '';
      display: block;
      position: absolute;
      box-sizing: border-box;
      border: 3px solid transparent;
      width: 0;
      height: 0;
    }

    .border-line:hover {
      border: 1px solid green;
    }

    .border-line:hover:before {
      border-bottom-color: green;
      border-left-color: green;
      -webkit-transition: border-color 0s ease-out 0.4s, width 0.2s ease-out 0.4s, height 0.2s ease-out 0.6s;
      transition: border-color 0s ease-out 0.4s, width 0.2s ease-out 0.4s, height 0.2s ease-out 0.6s;
    }

    .border-line:hover:after {
      border-top-color: green;
      border-right-color: green;
      -webkit-transition: width 0.2s ease-out, height 0.2s ease-out 0.2s;
      transition: width 0.2s ease-out, height 0.2s ease-out 0.2s;
    }

    .border-line:hover:before,
    .border-line:hover:after {
      border-width: 3px;
      width: 100%;
      height: 100%;
    }
  </style>
</body>
</html>

标签:color,ease,0.2,边框,width,环绕,height,border,css
From: https://www.cnblogs.com/dubayaoyao/p/17702215.html

相关文章

  • How to fix Tailwind CSS colors not work in Next.js All In One
    HowtofixTailwindCSScolorsnotworkinNext.jsAllInOneTailwindCSS&Next.js13errorimporttype{Config}from'tailwindcss'constconfig:Config={content:['./src/pages/**/*.{js,ts,jsx,tsx,mdx}','......
  • PostCSS received undefined instead of CSS string
    问题npmrunserve启动项目后,报错SyntaxError:Error:PostCSSreceivedundefinedinsteadofCSSstring解决node-sass版本兼容问题导致,按照应用使用的node-sass版本切换(可使用nvm)到对应的node版本,再重新npmi......
  • CSS中的变量
    前言在CSS中,有很多需要反复使用的属性值,如果每个使用的地方都直接写死这个值,而没有使用变量去定义这个值的话,后期修改起来会很麻烦。有很多人忽略了在CSS中也可以定义变量这个事情,相信你会爱上它!CSS使用变量有很多好处:可以减少样式代码的重复性,增加样式代码的扩展性和灵......
  • react配置css相关
    1.installnormalize:yarnaddnormalize.css-D(1).引入src/index.js或src/index.tsx:import"normalize.css";2.installsass:[email protected].配置less:(1).安装less相关依赖:[email protected]@5.0.0-D(2).修改config/webpack.config.js文件:......
  • 全栈工程师必须要掌握的前端CSS技能
    作为一名全栈工程师,在日常的工作中,可能更侧重于后端开发,如:C#,Java,SQL,Python等,对前端的知识则不太精通。在一些比较完善的公司或者项目中,一般会搭配前端工程师,UI工程师等,来弥补后端开发的一些前端经验技能上的不足。但并非所有的项目都会有专职前端工程师,在一些小型项目或者初创公......
  • 去除字符串中的css标记
       有时我们拿到的资料中会有很多网页css标记符号,非常影响资料的可读性。例如:<p>\u3000\u3000第一首</p><p>\u3000\u3000晓日曈曈万象融、河清海晏庆年丰、生逢盛世\真欢乐、好把心田答化工。</p><p>\u3000\u3000晓日:黎明时的太阳</p><p>\u3000\u3000曈曈:形\容太阳刚出还不......
  • CSS3 中 transition 和 animation 的属性分别有哪些
    Transition属性:transition 属性用于定义元素在状态改变时从一个样式转换到另一个样式的过渡效果。它包含以下几个属性:transition-property:指定过渡效果应用的CSS属性名称,多个属性可以用逗号分隔。transition-duration:指定过渡效果的持续时间,单位可以是秒(s)或毫秒(ms)。transit......
  • Qt-QWidget对样式表设置边框无效的解决方法
    相关资料:https://www.ngui.cc/el/1647992.html?action=onClick   1、现象在对QWidget使用样式表时无效QWidget#MyWgt{border:1pxsolidgray;}2、原因原因是QWidget只支持background、background-clip和background-origin属性。官方说明3、解决方法3.1使用QFrame代替QW......
  • css中文字太多显示方法
    第一种方法:“溢出省略号”,即当文字超出一定宽度时,将其省略,并显示“...”。p{overflow:hidden;/*将超出部分隐藏*/text-overflow:ellipsis;/*显示省略号*/white-space:nowrap;/*禁止换行*/}第二种方法:使用“分行显示”,即当文字超出一定宽度时,将其自动换行......
  • css3属性之 text-overflow:ellipsis
    语法:text-overflow:clip |  ellipsis默认值为clip不显示省略标记clip:当前对象内文本溢出时不显示省略标记,而是将溢出部分裁剪。ellipsis:当对象内文本一处时显示省略标记(...)。一、常见的单行文本溢出显示省略写法:text-overflow:ellipsis;overflow:hidden;white-......