2

If you write this:

a
b

it will normally render as "a b". I want it to render as "ab". Any thoughts?

JChlip
  • 121
  • 1
  • 2
  • 3

3 Answers3

1

Nope sorry, you can't

If there is one character deemed to be white space (either a tab, space or line break) then a space will be added. You can use javascript to remove the spaces but it's not suggested. What ever is printing it out should be taking care of that for you.

What platform are you using, we may be able to help better if we knew the circumstances.

Community
  • 1
  • 1
lededje
  • 1,859
  • 1
  • 16
  • 25
  • I am writing an app with Rails, and want to be able to set the color of each character in a string individually. The way I wanted to do it was to have a loop that goes through each character and uses tags to set the color. However, when I do that, because of the whitespace issue, "Example" becomes "E x a m p l e". I'm sure there's a slicker way to do this, but I'm a beginner and couldn't think of any. – JChlip Oct 17 '12 at 22:48
  • Once you have the string replace all spaces in it with an empty string. Or post the Rails and I'll see what I can do. – lededje Feb 08 '13 at 19:45
0

You could add a comment:

a<!--
-->b​​​​​​​​​​​​​​​​​​​​​​​​​
Brian Ustas
  • 62,713
  • 3
  • 28
  • 21
  • If you are able to wrap your white space in comment tags, you should defiantly be able to remove the white space all together. This is very heavy to send to the client and should not be used. – lededje Oct 17 '12 at 22:09
  • It has its uses. Especially when dealing with inline-blocks. – Brian Ustas Oct 17 '12 at 22:15
  • ...What? Using comments to help your layout is never the answer. I am interested about this inline-block thing though, can you post a fiddle? – lededje Oct 17 '12 at 22:18
  • Hmm readability shouldn't get in the way of correct markup. http://jsfiddle.net/fxvhn/1/ inline-block is meant for blocks to be floated within inline text, like images and so on, so the space (like adding spaces in text) is visible for this reason. This functionality is meant to be built in, and if you are trying to stack elements like in your example, then you should really be floating them. – lededje Oct 17 '12 at 22:29
  • An inline-block was desired--this is the solution. It's also valid markup. – Brian Ustas Oct 17 '12 at 22:41
  • Tables work too but they are still not very nice. Function over form, go with the semantic stuff, rather than the hacky stuff – lededje Oct 17 '12 at 22:48
0

This is not about white space collapsing, since there is just a line break between the characters. Collapsing is about multiple consecutive whitespace being treated as one space.

There is no way in HTML to say that normal HTML whitespace rules be not applied in the sense of ignoring a line break.

In CSS, you could set e.g. word-spacing: -0.25em, though 0.25em is just a rough approximation of the width of a space.

To make “a” and “b” rendered without a space between them, write them as “ab”.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390