How to maintain letter spacing when breaking a text element?

Another question. I'm trying to position an inline-block inside a text element, but clearly if I do <span>abc<div class="inline-block"/>efg</span> the standard letter spacing disappears and the inline div is flush to the left side of the 'e', and the right side of the 'c'. Is there a way to keep the spacing consistent and avoid manually adding arbitrary pixel margins to the inline block? I can create a variable and manually set both the letter-spacing on the <span> and the margin on the <div> to the same value, but I was wondering if there are better/other ways of doing it?
35 Replies
ἔρως
ἔρως2mo ago
you're putting a non-permitted block-level element inside a <span> you can just use an inline-block span then use margins or paddings set to the letter spacing or just try a span without anything
vinter.
vinter.OP2mo ago
I thought that in this case since either has to be explicitly set to inline-block it doesn't matter whether it's a div or a span?
ἔρως
ἔρως2mo ago
it shouldn't, but it isn't a permitted element
vinter.
vinter.OP2mo ago
doesn't work as I need to change its height/width
ἔρως
ἔρως2mo ago
what exactly are you trying to do?
vinter.
vinter.OP2mo ago
yeah, was hoping to avoid magical numbers
ἔρως
ἔρως2mo ago
css variables exist
vinter.
vinter.OP2mo ago
do some typography shenanigans
vinter.
vinter.OP2mo ago
No description
ἔρως
ἔρως2mo ago
what do you have so far?
vinter.
vinter.OP2mo ago
<h1>VINT<span class="letter"><span class="left"/><span class="right"/></span>R</h1>
<h1>VINT<span class="letter"><span class="left"/><span class="right"/></span>R</h1>
h1 {
font-size: 7rem;
letter-spacing: 10px;
font-optical-sizing: auto;
font-weight: 700;
font-style: normal;
}
.letter {
width: 10rem;
margin-right: 13px;
margin-left: 7px;
display: inline-flex;

.left {
flex-shrink: 0;
width: 0.145em;
height: 0.7em;
display: inline-block;
background-color: var(--tw-prose-headings);
}
.right {
height: 0.7em;
flex: 1;
width: 100%;
display: inline-block;
background-color: var(--tw-prose-headings);
background: linear-gradient(to bottom,
var(--tw-prose-headings) 0%,
var(--tw-prose-headings) 20%,
white 20%, white 40%,
var(--tw-prose-headings) 40%, var(--tw-prose-headings) 60%,
white 60%, white 80%,
var(--tw-prose-headings) 80%, var(--tw-prose-headings) 100%
);
}
}
h1 {
font-size: 7rem;
letter-spacing: 10px;
font-optical-sizing: auto;
font-weight: 700;
font-style: normal;
}
.letter {
width: 10rem;
margin-right: 13px;
margin-left: 7px;
display: inline-flex;

.left {
flex-shrink: 0;
width: 0.145em;
height: 0.7em;
display: inline-block;
background-color: var(--tw-prose-headings);
}
.right {
height: 0.7em;
flex: 1;
width: 100%;
display: inline-block;
background-color: var(--tw-prose-headings);
background: linear-gradient(to bottom,
var(--tw-prose-headings) 0%,
var(--tw-prose-headings) 20%,
white 20%, white 40%,
var(--tw-prose-headings) 40%, var(--tw-prose-headings) 60%,
white 60%, white 80%,
var(--tw-prose-headings) 80%, var(--tw-prose-headings) 100%
);
}
}
ἔρως
ἔρως2mo ago
now, can you take it and shove into a codepen or jsfiddle?
vinter.
vinter.OP2mo ago
probably better with some :before and :after but im bad at those yeah sure, gimme 1s
ἔρως
ἔρως2mo ago
thank you
ἔρως
ἔρως2mo ago
and what's the problem? the space between the T E R is different?
vinter.
vinter.OP2mo ago
(changed it so that the whole thing is a flex as intended) that it's terribly ugly and I'd like to not have stuff like this
margin-right: 13px;
margin-left: 7px;
margin-right: 13px;
margin-left: 7px;
if possible I understand that what I'm doing is not the pinnacle of proper html regardless but if I could avoid this it would be great
ἔρως
ἔρως2mo ago
how about you have margin: 0 var(--letter-spacing)? then you won't have to worry about anything then you can set the value in the h1
vinter.
vinter.OP2mo ago
awesome, seems to work 🙏
ἔρως
ἔρως2mo ago
if i were you, i would actually use em values by the way, you should have an E that is visually hidden
vinter.
vinter.OP2mo ago
ye makes sense
ἔρως
ἔρως2mo ago
otherwise, it will be read as VINTR by google you can set the E to 0 font size and the color set to transparent oh, and set aria-label="vinter" to the h1 otherwise, some screen reader may read it as "V I N T E R" (i'm not saying it will, but i wouldn't be surprised if something misbehaved)
vinter.
vinter.OP2mo ago
well, kinda works the spacing is a bit funky compared to the normal one but ehh good enough
vinter.
vinter.OP2mo ago
No description
vinter.
vinter.OP2mo ago
I am, aren't I? oh for the margins you meant
ἔρως
ἔρως2mo ago
you're using sub-pixel rendering
vinter.
vinter.OP2mo ago
yeah idk 13px was nicer than 0.167em or w/e it was :p
ἔρως
ἔρως2mo ago
yeah, but px is bad for text
vinter.
vinter.OP2mo ago
but ye I guess the var is good enough
ἔρως
ἔρως2mo ago
but you have those issues because of this
No description
ἔρως
ἔρως2mo ago
sub-pixel rendering will give you funky funky stuff
vinter.
vinter.OP2mo ago
is there a way to prevent that?
ἔρως
ἔρως2mo ago
make sure all values are integer pixels but in essence, not exactly since you need to use em or rem for texts, due to accessibility issues, then it is very ... touchy
vinter.
vinter.OP2mo ago
yeah I figured well, I'll live with it thanks!
ἔρως
ἔρως2mo ago
you're welcome

Did you find this page helpful?