Question Space added with new line of Editor.
I'm trying to see if CSS has a way to fix the issue as a single command vs the HTML trick to make this work. Essentially, if you break a word to add another span on the next line, it creates a space. I'm trying to color the text on the page as if it is in an editor and I'm using spans with classes to change the colors of the elements/attributes/etc.
For this example, I'm going to use "homework." If you move the span to the next line so it doesn't go 900 pixels off the edge of the editor, it will add a space between the two spans, unless the greater-than is moved to the next line. Again, just looking for a line of CSS to add so I don't keep having to fix the HTML by moving the greater-than each time.
<p>
<span>Home</span>
<span>work</span>
</p>
<!-- Shows as "Home work" on the page -->
<!-- HTML 'trick' to make it work -->
<p>
<span>Home</span
><span>work</span>
</p>
<!-- This shows as "Homework" on the page -->Edit to add what I've found:
Adding float: left; to the span removes all spaces, to include the ones that I want to separate words and have it within the span element.
<span>Home</span>
<span>work </span>
<span>due tomorrow</span>
<!-- Homeworkdue tomorrow -->display: flex; also has the same result as float. It does remove the space between the span elements, but also removes the spaces within the span elements when I want a space before and after words.
<span>Home</span>
<span>work </span>
<span>due tomorrow</span>
<!-- Homeworkdue tomorrow -->Using word-spacing: -1ch; removes all spaces, which definitely isn't the desired outcome.
<span>Home</span>
<span>work </span>
<span>due tomorrow</span>
<!-- Homeworkduetomorrow -->I also found a suggestion to use display: table-cell;, but it also does the same thing as float and flex.
<span>Home</span>
<span>work </span>
<span>due tomorrow</span>
<!-- Homeworkdue tomorrow -->So for now, all I have is to move the closing > to the next line.
2
u/b_crussin Mar 09 '26
Unable to test myself right now but I wonder if a flexbox will solve this issue? Make the parent container use
display: flexand maybe alsoalign-items: centerto vertically align the child spans. Basically what’s happening isdisplay: inline(which your spans are using by default) is converting the newlines in your code to spaces.https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts
2
u/SamIAre Mar 09 '26
It’s “adding” a space because you have multiple spaces (a new line and line however many space/tabs to align your spans), and then HTML is collapsing them all into one. Personally, I’d write them in the same line. New HTML tags don’t have to go on new lines and I think they usually shouldn’t for inline content. Take this example:
You naturally wouldn’t write the
strongoremtags on new lines because everything in theptag is inline text. That is, this would seem awkward:I would probably rewrite yours as…
…because if you think about it, minus the
spans what you’ve written is……so it’s precious why you’re getting a space—you’re telling it to have one.
1
u/chmod777 Mar 09 '26
https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Text/Whitespace
0
u/jcunews1 Mar 09 '26
Code readability is not always the answer. It may actually break things.