To adjust the position of the ::first-letter
vertically without changing the box's height, you can use the float
property on the pseudo-element and apply a negative margin-top
to it:
See Example Here
#third::first-letter {
margin-top: -.5em;
float: left;
}
*{margin:0;padding:0;}
html {font-size: 200%;}
#first {background-color:pink;}
#second {background-color:aqua;}
#third {background-color:lightgreen; margin-top: 1em;}
#third::first-letter {
margin-top: -.5em;
float: left;
}
<p id="first">My list:</p>
<p id="second">|-> not last element</p>
<p id="third">|-> last element</p>
However, as highlighted by @Alohci in the comments, Firefox may not match the first character with ::first-letter
if it's not a letter or digit.
In cases where the first character is not a letter or digit but another symbol like a pipe |
, consider using the ::before
pseudo-element to properly style it:
See Example Here
#third::before {
content: "|";
margin-top: -.5em;
float: left;
}
*{margin:0;padding:0}
html {font-size: 200%;}
#first{background-color:pink;}
#second{background-color:aqua;}
#third{background-color:lightgreen; margin-top: 1em;}
#second::before,
#third::before {
content: "|";
}
#third::before {
margin-top: -.5em;
float: left;
}
<p id="first">My list:</p>
<p id="second">-> not last element</p>
<p id="third">-> last element</p>