How can I make the underline hover from the center instead of the left?


Rohit Verma

I have a ullist that works fine using hover :after, but I now want it to start from the center instead of the left.

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>

Insurance

Quick solution:

Move the original position to left:50%, then move it to on hover 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>

Related


Underline left to right on hover

R M Using the following code, I get a left-to-right hover underline effect. .underline { display: inline; position: relative; overflow: hidden; } .underline:after { content: ""; position: absolute; z-index: -1; left: 0; right: 100%; bottom: -

How can I make my tabs open from the center instead of the left?

Daddy Goose I am trying to have a pane in my site that will open from the center and gradually open after loading. I've set it to do this, but it starts from the left where the label starts, not from the center where I want it to start. How to start from the c

Underline left to right on hover

R M Using the following code, I get a left-to-right hover underline effect. .underline { display: inline; position: relative; overflow: hidden; } .underline:after { content: ""; position: absolute; z-index: -1; left: 0; right: 100%; bottom: -

Underline left to right on hover

R M Using the following code, I get a left-to-right hover underline effect. .underline { display: inline; position: relative; overflow: hidden; } .underline:after { content: ""; position: absolute; z-index: -1; left: 0; right: 100%; bottom: -

How can I make my tabs open from the center instead of the left?

Daddy Goose I am trying to have a pane in my site that will open from the center and gradually open after loading. I've set it to do this, but it starts from the left where the label starts, not from the center where I want it to start. How to start from the c

Underline left to right on hover

R M Using the following code, I get a left-to-right hover underline effect. .underline { display: inline; position: relative; overflow: hidden; } .underline:after { content: ""; position: absolute; z-index: -1; left: 0; right: 100%; bottom: -

Underline left to right on hover

R M Using the following code, I get a left-to-right hover underline effect. .underline { display: inline; position: relative; overflow: hidden; } .underline:after { content: ""; position: absolute; z-index: -1; left: 0; right: 100%; bottom: -

Underline left to right on hover

R M Using the following code, I get a left-to-right hover underline effect. .underline { display: inline; position: relative; overflow: hidden; } .underline:after { content: ""; position: absolute; z-index: -1; left: 0; right: 100%; bottom: -

How can I animate the underline hover effect?

scream queen Can someone help me figure out how to accomplish this cool animated underline hover effect on this site ? I've tried to replicate it myself without success. this is my jsbin jazz You need to write CSS and animation functions for this when you hove