【问题标题】:CSS text hover underline animation inside of divdiv内的CSS文本悬停下划线动画
【发布时间】:2017-05-19 06:16:52
【问题描述】:

我正在尝试为我的网页上的某个单词添加下划线动画。它可以工作,但下划线在 div 下显示文本在里面。您可以在 sn-p 上看到 div 应该填满整个显示屏,但现在您必须向下滚动才能看到下划线。如何让它显示在文本下方?

body {
  margin: 0;
  padding: 0;
}
div {
  width: 50%;
  height: 100vh;
  text-align: center;
  line-height: 100vh;
  font-size: 150%;
  top: 0;
  -webkit-transition: font-size 0.5s;
  -moz-transition: font-size 0.5s;
  -o-transition: font-size 0.5s;
  transition: font-size 0.5s;
}
div:hover {
  font-size: 200%;
}
a {
  display: inline-block;
  position: relative;
  padding-bottom: 3px;
}
a:after {
  content: '';
  display: block;
  margin: auto;
  height: 3px;
  width: 0px;
  background: transparent;
  transition: width .5s ease, background-color .5s ease;
}
a:hover:after {
  width: 100%;
  background: blue;
}
<div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); float: left;">
  <a>Webpage</a>
</div>
<div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); float: right;">
  <a>Photos</a>
</div>

【问题讨论】:

    标签: html css underline


    【解决方案1】:

    锚点的行高在父节点中被修改。

    恢复为默认值

    body {
      margin: 0;
      padding: 0;
    }
    div {
      width: 50%;
      height: 100vh;
      text-align: center;
      line-height: 100vh;  /* this value is inherited by the a */
      font-size: 150%;
      top: 0;
      -webkit-transition: font-size 0.5s;
      -moz-transition: font-size 0.5s;
      -o-transition: font-size 0.5s;
      transition: font-size 0.5s;
    }
    div:hover {
      font-size: 200%;
    }
    a {
      display: inline-block;
      position: relative;
      padding-bottom: 3px;
      line-height: initial;  /* added */
    }
    a:after {
      content: '';
      display: block;
      margin: auto;
      height: 3px;
      width: 0px;
      background: transparent;
      transition: width .5s ease, background-color .5s ease;
    }
    a:hover:after {
      width: 100%;
      background: blue;
    }
    <div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); float: left;">
      <a>Webpage</a>
    </div>
    <div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); float: right;">
      <a>Photos</a>
    </div>

    【讨论】:

    • 想知道原因。
    • @xxCodexx div 的行高为 100vh。 a 在 div 内部,所以这个设置 cascades 到它,并且由于它是一个巨大的值,它会将伪造的下划线设置得非常低。只需将其再次设置为 initial 即可恢复原始值。
    【解决方案2】:

    我会在里面放一个 div,而不是使用括号。

    <div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); float: right;"><div id="hoverDiv">Photos</div></div>
    

    然后在 CSS 中,您只需在悬停时将 div 的底部边框显示为蓝色。

    对不起,我没有时间做所有的代码,但这是一个初稿......希望它能给你一个好主意!

    【讨论】:

    • 不想和你争论,但我很想知道为什么它不会?
    • 你可以用sn-p自己试试。下划线仍显示在主 div 下方。
    • 是的,好的,我的意思是你必须重做代码才能使用 div 并且只显示底部边框。我给你另一个想法,告诉你如何做同样的事情。我看到有人给了你一个更接近你当前解决方案的解决方案,所以这一切都很好:D
    【解决方案3】:

    你可以使用 flex 和 background-size:

    body {
      margin: 0;
      height: 100vh;
      display: flex;
    }
    div {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    a {
      padding-bottom: 3px;
      background: linear-gradient(to left, currentcolor, currentcolor) bottom center no-repeat;
      transition: 0.5s;
      background-size: 0 3px;
    }
    a:hover {
      background-size: 100% 3px;
    }
    <div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); "><a>Webpage</a>
    </div>
    <div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); "><a>Photos</a>
    </div>

    你也可以使用 display:table

    html {
      margin: 0;
      height: 100%;
      width: 100%;
    }
    html {
      display: table;
      table-layout: fixed;
    }
    body {
      display: table-row;
    }
    div {
      display: table-cell;
      text-align: center;
      vertical-align: middle;
    }
    a {
      padding-bottom: 3px;
      background: linear-gradient(to left, currentcolor, currentcolor) bottom center no-repeat;
      transition: 0.5s;
      background-size: 0 3px;
    }
    a:hover {
      background-size: 100% 3px;
    }
    <div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); "><a>Webpage</a>
    </div>
    <div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); "><a>Photos</a>
    </div>

    如果您将&lt;a&gt; 转换为inline-block 元素,则两个示例都可以使用伪:after

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 2021-12-12
      • 2020-09-10
      相关资源
      最近更新 更多