1

The element has a variable width, so I don't know it.

Is it possible to center this element inside its wrapper element?

The wrapper has a fixed width of 960px...

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Alex
  • 66,732
  • 177
  • 439
  • 641

3 Answers3

5

Add text-align: center; to the wrapper, and then display: inline-block; to the child

Or add display: table; andmargin: 10px auto; to the child element

Or position: relative; left: 50%; float: left;

Max
  • 2,082
  • 19
  • 25
2

Yes, as long as you have margin:0 auto; with the variable width specified...

Alex
  • 34,899
  • 5
  • 77
  • 90
  • thats the problem, i can specify the width because depends on the text inside the element.. – Alex Feb 06 '12 at 02:09
  • you could write some `JS` that, on page load, retrieves the `outer width` and sets the `CSS` style property to the `outer width` detected in the script – Alex Feb 06 '12 at 02:12
  • 1
    depends on the element containing the text but using `

    ` or `

    ` will work with this solution without additional js, [here](http://jsfiddle.net/qGKDb/1/) is an example where the width is randomly set
    – T I Feb 06 '12 at 02:27
1

Here is an example: http://jsfiddle.net/traMU/

stewe
  • 41,820
  • 13
  • 79
  • 75