2

My problem and the reason of it:

I have this html:

<div class="container">
    List of anchors:
    <a href="#" class="child">First child</a>
    <a href="#" class="child">Second child</a>
</div>

and these styles:

.child {
  text-decoration: none;
  color: red;
  overflow: hidden; 
  display: inline-block;
}

But, for some reason, the anchors it strangely moves up, you can see it here.
And, I found out that this problem happens because of the overflow property, i tried to remove it and this is what I get, which also what I expected. But of course, I really need the overflow property, so I can't move on without it.

The reason I need to use the overflow property:
I want to use it because I want to make the effect, when I hover each anchor, there will be a line under it moves from left to right.
So I will use the ::after pesudo element, translate it 100% (transform: translateX(-100%)) to the left and when hover it, it would move back to 0% (transform: translateX(0%)). So the overflow property would hide the ::after pesudo element when it translate 100% to the left.

What I tried:
I tried to use the ul element instead, an wrap each anchors into each li element:

<ul class="list">
    List of anchors:
    <li class="child-list"><a href="#" class="child-anchor">First child</a></li>
    <li class="child-list"><a href="#" class="child-anchor">Second child</a></li>
</ul>
.child-anchor {
    text-decoration: none;
    color: red;
    display: inline-block;
    overflow: hidden;
}

.child-list {
    display: inline-block;
    list-style-type: none;
}

But, that it still moves up.

What I want to know:
I want to know why this weird problem happens, and also the way to fix it, or some other ways to make the effect that I mentioned in the The reason I need to use the overflow property section. So hopefully, you guys can help me with this and thank you so much for spending time reading this question!

Gia Phu Tang
  • 101
  • 6

1 Answers1

1

Have you tried :

vertical-align: middle;
DafuQi
  • 138
  • 4
  • Oh thank you so much, I fixed now, can you tell me why this happen, and why this vertical-align: middle fix this problem? Thank you so so much again! – Gia Phu Tang Jun 30 '21 at 12:05
  • To be honest, i dont have any idea ^^ most of the time vertical align seems to be useless BUT for this thing its working, have a good day :) – DafuQi Jun 30 '21 at 12:59