10

I have some html that looks like this:

<span>
  398
  <span>comments posted in</span>
</span>

A space is rendered after 398 because there is a line-break in the html. For reasons I won't go into, this line-break cannot be removed. Is there a way to stop a space being rendered there?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Nick Brunt
  • 9,533
  • 10
  • 54
  • 83

2 Answers2

6

No. It is not possible using CSS. Although you can use float, but it doesn't work out here.

But I have a crappy idea, give this CSS (Ideal Case):

span span {margin-left: -1em;}
span span {margin-left: -1ex;}

But the practical case was like this:

span span {margin-left: -0.4em;}
span span {margin-left: -0.7ex;}

1em or 1ex is the width of a space character in CSS. Hope it works! Everyone knows about em. So something about ex:

The ‘ex’ unit is defined by the font’s ‘x-height’. The x-height is so called because it is often equal to the height of the lowercase "x". However, an ‘ex’ is defined even for fonts that don’t contain an "x".

Demo: http://jsfiddle.net/UmdfA/

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
3

I think we are all agreed that the correct solution where possible is this:

<span>
  398<span>comments posted in</span>
</span>

But you could use some CSS shenanigans to sort this out...

span span {
    margin-left: -1ex;
}

Or

span span {
    position: relative;
    left: -1ex;
}
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • Yes, clearly putting it on the same line is ideal, I don't like having to hack it but margins seem like the only solution in my situation. – Nick Brunt Nov 19 '12 at 17:19
  • -1: [`ex` is for height](http://stackoverflow.com/questions/918612/what-is-the-value-of-the-css-ex-unit), using it for width is a bad idea. The number and the text overlap for me, in that Fiddle. (Firefox 16.) – ANeves Nov 19 '12 at 17:24
  • Ugh, the spec goes on about [`em squares`](http://www.w3.org/TR/CSS2/fonts.html#propdef-font-size), which indicates that em can be for width as well as for height... which I guess means ex can be for both too. Also, *"[according to Mozilla](http://kb.mozillazine.org/Em_vs._ex) Gecko can scale items using exes a bit more accurately than those using ems"* ([via](http://stackoverflow.com/questions/918612/what-is-the-value-of-the-css-ex-unit#comment9316381_918623)) I want to undo the -1 :( – ANeves Nov 19 '12 at 17:42
  • Ha! Don't worry about it :). The unit of ex is based on the height of an x but can be used to define a width. 1ex means "make this as wide as an x is high". – Fenton Nov 19 '12 at 18:36