简体   繁体   English

如何将悬停下划线从中心开始而不是从左侧开始?

[英]How to hover underline start from center instead of left?

I have a ul list and there i have given hover effect using :after this is working fine on hover, but now i want to it will be start from center instead of left. 我有一个ul列表,并且我已经使用了悬停效果:after悬停:after工作正常,但现在我想它将从中心开始而不是从左侧开始。

My Code: 我的代码:

 ul.my-list{ margin: 20px; padding: 0px; width: 200px;} ul.my-list li{ list-style: none; position: relative; display: inline;} ul.my-list li a{ color:#333; text-decoration: none;} ul.my-list li:after{ content: ''; position: absolute; left: 0px; bottom: -2px; width:0px; height: 2px; background: #333; transition: all 0.45s;} ul.my-list li:hover:after{ width: 100%;} ul.my-list li a:hover{ text-decoration: none;} 
 <ul class="my-list"> <li><a href="#">Welcome to my website</a></li> </ul> 

Quick solution: 快速解决方案

Move the original position to left:50% and then, on hover, move it to left:0 . 将原始位置left:50%移动left:50% ,然后在悬停时将其left:0移动left:0

 ul.my-list { margin: 20px; padding: 0px; width: 200px; } ul.my-list li { list-style: none; position: relative; display: inline; } ul.my-list li a { color: #333; text-decoration: none; } ul.my-list li:after { content: ''; position: absolute; left: 50%; bottom: -2px; width: 0px; height: 2px; background: #333; transition: all 0.45s; } ul.my-list li:hover:after { width: 100%; left: 0; } ul.my-list li a:hover { text-decoration: none; } 
 <ul class="my-list"> <li><a href="#">Welcome to my website</a></li> </ul> 

You can simplify your code using background like below 您可以使用如下背景简化代码

 ul.my-list { margin: 20px; padding: 0px; width: 200px; } ul.my-list li { list-style: none; display: inline-block; padding-bottom:2px; /*the space for the gradient*/ background: linear-gradient(#333,#333) center bottom; /*OR bottom right OR bottom left*/ background-size: 0% 2px; /*width:0% height:2px*/ background-repeat:no-repeat; /* Don't repeat !!*/ transition: all 0.45s; } ul.my-list li a { color: #333; text-decoration: none; } ul.my-list li:hover { background-size: 100% 2px; /*width:100% height:2px*/ } 
 <ul class="my-list"> <li><a href="#">Welcome to my website</a></li> </ul> 

This is simple, you call the element after left 50% that means this it's the goon left to the middle when you hover the element after the width 100% and left 0, you already added some transition So, it looks middle to left and right. 这很简单,你在左边50%后调用该元素,这意味着当你在宽度100%之后悬停元素并且离开0时,它是中间左边的goon,你已经添加了一些过渡因此,它看起来是中间到左边和右边。 here the code; 这里的代码;

 ul.my-list{ margin: 20px; padding: 0px; width: 200px;} ul.my-list li{ list-style: none; position: relative; display: inline;} ul.my-list li a{ color:#333; text-decoration: none;} ul.my-list li:after{ content: ''; position: absolute; left: 50%; /* change this code 0 to 50%*/ bottom: -2px; width:0px; height: 2px; background: #333; transition: all 0.45s; } ul.my-list.rtl li:after { right: 0; left: inherit; } ul.my-list li:hover:after{ left:0; width: 100%;} /* add poperty left 0 */ ul.my-list li a:hover{ text-decoration: none;} 
 <ul class="my-list"> <li><a href="#">Welcome to my website</a></li> </ul> <h2>left start and end right</h2> <ul class="my-list rtl"> <li><a href="#">Welcome to my website</a></li> </ul> 
= = = Thank you = = = = = = 谢谢 = = =

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

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