简体   繁体   English

CSS Text下划线动画问题

[英]CSS Text underline animation issue

I'm trying to learn how to add an underline animation effect on my webpage, but am having a small issue. 我正在尝试学习如何在网页上添加下划线动画效果,但是存在一个小问题。

Here's the code I used for the underline effect: 这是我用于强调效果的代码:

.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%;
}

And here's the code on my css: 这是我的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>

And this is how it comes out once the animation is done: 这是动画制作完成后的结果:

在此处输入图片说明

My question is, how do I make it so that the underline only stays directly under the H1 header? 我的问题是,如何使下划线仅保持在H1标头的正下方? I want the underline to start at 'A' and end at 'e', but I am having a hard time figuring out what to put in the code to fix this. 我希望下划线以“ A”开头,以“ e”结尾,但是我很难弄清楚要在代码中添加哪些内容以解决此问题。 I would really appreciate if someone can help me with this small issue. 如果有人可以帮助我解决这个小问题,我将不胜感激。

Thanks in advance 提前致谢

You could use display-table on your h1 to simulate block like behaviour without the 100% width... 您可以在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> 

Change the display of the h1 to inline-block 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM