【问题标题】:pseudo element across multiple lines to create a continuous underline animation跨多行的伪元素创建连续的下划线动画
【发布时间】:2020-09-13 02:42:04
【问题描述】:

我在链接下有一条动画线。它适用于单行链接,但我有一些用换行符分隔的链接<br> 有没有办法让动画下划线沿着链接的所有行出现? 谢谢

body {
  padding: 20px;
  font-family: Helvetica;
}

a {
  font-weight: bold;
  color: black;
  position: relative;
  text-decoration: none;
}

a::before {
  content: "";
  position: absolute;
  width: 0%;
  height: 2px;
  bottom: 0;
  background-color: #000;
  transition: all 0.2s;
}

a:hover::before {
  width: 100%;
}
<a href="">my link</a>
<br><br>
<a href="">this is<br>a much<br>longer link</a>

【问题讨论】:

    标签: css css-animations pseudo-element


    【解决方案1】:

    使用如下渐变:

    body {
      padding: 20px;
      font-family: Helvetica;
    }
    
    a {
      font-weight: bold;
      color: black;
      position: relative;
      text-decoration: none;
      background:linear-gradient(#000,#000) left bottom no-repeat;
      background-size:0% 2px;
      transition: all 0.5s;
    }
    
    a:hover {
      background-size:100% 2px;
    }
    /* this will give another kind of animation (all lines will animate at the same time)*/
    .alt {
      -webkit-box-decoration-break:clone;
              box-decoration-break:clone;
    }
    <a href="">my link</a>
    <br><br>
    <a href="">this is<br>a much<br>longer link</a>
    <br><br>
    <a class="alt" href="">this is<br>a much<br>longer link</a>

    相关:

    How to animate underline from left to right?

    How can I achieve a CSS text loading animation over multiple lines?

    【讨论】:

    • 太棒了。第二个版本,使用克隆选项正是我所追求的。非常感谢。
    • 如果我在&lt;a&gt; 中有一个&lt;img&gt;,我怎样才能得到文本下的背景线,而不是图像? jsfiddle.net/n5yqfute
    • @Lawmate 为图片添加负边距以隐藏背景
    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 2017-04-27
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多