0

I have a H2 element with a span and some text:

<h2><span.../> Some text</h2>

The text is centered and I need to place the span on its left. I am using:

span {
  margin-left: -60px;
}

Which is working for Firefox and Chrome, but does not work on Internet Explorer 9. How can I make it work on IE9 and IE8?

Johnny Everson
  • 8,343
  • 7
  • 39
  • 75

2 Answers2

1

SPAN is an inline element. These cannot take horizontal margins or padding. You need to make the SPAN display: inline-block.

Mark
  • 5,680
  • 2
  • 25
  • 26
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

If the text is centered you can use a float: left; on the span. This will always ensure that the span is on the far left of the h2.

If you just want the span to also be centered but at the beginning of the line, you need not do anything because the span is an inline element.

See my example:

http://jsfiddle.net/kkesk/2/

example 1 uses float: left; example 2 uses padding.

Mark
  • 5,680
  • 2
  • 25
  • 26
  • In your second example, the text is not centered. All elements were centered. – Johnny Everson Sep 21 '12 at 09:32
  • @JhonnyEverson the text is centered in both examples. I just have added padding to the span to demonstrate that you can have padding on inline elements. – Mark Sep 21 '12 at 12:37