【问题标题】:CSS Text underline animation issueCSS文本下划线动画问题
【发布时间】:2015-12-12 08:12:30
【问题描述】:

我正在尝试学习如何在我的网页上添加下划线动画效果,但遇到了一个小问题。

这是我用于下划线效果的代码:

.cmn-t-underline {
position: relative;
}

.cmn-t-underline:after {
display: block;
position: absolute;
left: 0;
bottom: -10px;
width: 0;
height: 3px;
background-color: dimgrey;
content: "";
transition: width 0.3s;

}
.cmn-t-underline:hover {
color: #333;
}

.cmn-t-underline:hover:after {
width: 100%;
}

这是我的 css 上的代码:

    <div class="col-sm-6">
    <h1 class="text-center text-padding cmn-t-underline">Access Everywhere</h1>
        <p class="text-center">Lorem ipsum dolor sit amet</p>
    </div>

这就是动画完成后的结果:

我的问题是,如何使下划线仅直接位于 H1 标题下方?我希望下划线从“A”开始并以“e”结束,但我很难弄清楚在代码中添加什么来解决这个问题。如果有人能帮我解决这个小问题,我将不胜感激。

提前致谢

【问题讨论】:

    标签: html css animation underline


    【解决方案1】:

    您可以在 h1 上使用 display-table 来模拟没有 100% 宽度的块状行为...

    body {
        text-align: center;
    }
    
    h1 {
        display: table;
        margin: 0 auto;
    }
    
    .cmn-t-underline {
        position: relative;
    }
    .cmn-t-underline:after {
        display: block;
        position: absolute;
        left: 0;
        bottom: -10px;
        width: 0;
        height: 3px;
        background-color: dimgrey;
        content:"";
        transition: width 0.3s;
    }
    .cmn-t-underline:hover {
        color: #333;
    }
    .cmn-t-underline:hover:after {
        width: 100%;
    }
    <div class="col-sm-6">
         <h1 class="text-center text-padding cmn-t-underline">Access Everywhere</h1>
         <p class="text-center">Lorem ipsum dolor sit amet</p>
    </div>

    【讨论】:

      【解决方案2】:

      h1的显示改为inline-block

      .cmn-t-underline {
          position: relative;
          display: inline-block;
      }
      .cmn-t-underline:after {
          display: block;
          position: absolute;
          left: 0;
          bottom: -10px;
          width: 0;
          height: 3px;
          background-color: dimgrey;
          content:"";
          transition: width 0.3s;
      }
      .cmn-t-underline:hover {
          color: #333;
      }
      .cmn-t-underline:hover:after {
          width: 100%;
      }
      <div class="col-sm-6">
         <h1 class="text-center text-padding cmn-t-underline">Access Everywhere</h1>
         <p class="text-center">Lorem ipsum dolor sit amet</p>
      </div>

      【讨论】:

      • 嗨,不幸的是,这似乎不起作用。该行仍然比 H1 文本长。
      • 真的吗?你有截图吗?你用的是什么浏览器?
      • 我正在使用 Firefox。当我添加 inline-block 时,它似乎做了同样的事情,我不知道为什么。然而,用户 'Sexy Turnip 的建议似乎奏效了。
      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 2017-05-19
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      相关资源
      最近更新 更多