I've been trying to figure out how to vertically center an ::after
pseudo-element that contains text (a content
attribute).
Previous questions: there are some previous questions which address this, but none of the solutions seem to work for this simple case.
Here I basically have a simple DIV
that should function as a "next" button. It contains a simple right arrow (Unicode 0x203A
).
The markup is basically just:
<div class = "next-button"></div>
And the CSS is:
.next-button {
position: absolute;
height: 70px;
line-height: 70px;
text-align: center;
background: black;
cursor: pointer;
}
.next-button::after {
content: "\203A";
color: white;
font-size: 3em;
line-height: 0;
}
JSFiddle link: https://jsfiddle.net/me6a3nq2/
So, I've been trying various things to center this. Firstly, vertical-align:middle
doesn't work here. Another CSS trick is to set the line-height
of the parent element equal to the height
of the parent element. However, this doesn't seem to work either.
I can add position:relative
to next-button::after
, and then adjust top
accordingly, but this won't exactly center the arrow unless I know the exact height of the arrow, which may change or vary across browsers.
I also tried using a common CSS method, which is to use translateY
, as specified here. I usually have very good success with this method:
.next-button::after {
content: "\203A";
color: white;
font-size: 3em;
line-height: 0;
position: relative;
top: 50%;
transform: translateY(-50%);
}
But even this doesn't work with the pseudo element. It appears to move the arrow, but it places it off-centered towards the bottom, at least on Chrome:
I also notice, when using the Chrome Inspector tool, that the arrow itself, which is basically just a character of text, seems to have "padding" automatically above and below it, and that the "above" padding is seemingly larger than the below padding, causing the arrow to be uncentered vertically:
...but, I can't really tell why this padding exists, nor do I seem to have any control of it. I thought it might be line-height
, so I set line-height
to 0
, but that had no effect.
So, I'm out of ideas. I'm thinking I may need to give up on using a pseudo element and just make the arrow inside a nested DIV.
Question: Is there any way to vertically align a pseudo element? (And also, what is the cause of the "padding" above and below of the text in the pseudo element content
?)