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
  • 31,464
  • 27
  • 87
  • 113
Alex
  • 64,868
  • 164
  • 416
  • 621

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
  • 1,964
  • 18
  • 23
2

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

Alex
  • 34,121
  • 5
  • 74
  • 88
  • 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
  • 40,424
  • 13
  • 77
  • 74