26

How can I center an inline div (not its content) using css?

VERY IMPORTANT: the width of the div is unknown (I can not specify it)

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Mihai Om
  • 261
  • 1
  • 3
  • 4
  • 4
    You're not crazy, they seriously missed something in the CSS (2&3) spec on this one, when the `
    ` tag was deprecated a style rule should have been added to replace it immediately.
    – Nick Craver Sep 06 '10 at 21:31
  • Did anyone ever figure out how to do this or is CSS still broken? – ThatAintWorking Oct 11 '12 at 20:54

5 Answers5

21

If by inline you mean, its CSS display property is set to inline then you can center it by giving the immediate parent container a text-align:center

If you mean a div within another div, you can follow this approach if you can add a wrapper and float both:

How to horizontally center a floating element of a variable width?

Community
  • 1
  • 1
Moin Zaman
  • 25,281
  • 6
  • 70
  • 74
  • 1
    If the intent is to set it to `inline`...why use a `
    `? It seems like this solution wouldn't work in 99% of cases, since you're completely changing the layout by doing this.
    – Nick Craver Sep 06 '10 at 21:34
  • its not working. if I set on the container text-align:center, my div's content get centered even if I overwrite on my div the text-align style. – Mihai Om Sep 06 '10 at 21:37
  • @Nick: agree setting a block element to inline is a bit pointless, but that's what the Mihai seems to be asking. – Moin Zaman Sep 06 '10 at 21:38
  • @Mihai: Check the specificity of your CSS for the div and its content. Make it more specific by using an ID or more selectors and the div's content should not be centered. If that doesn't work, use `text-align:left !important;` – Moin Zaman Sep 06 '10 at 21:40
  • I have something like this:
    text
    some longer text
    some longer longer text
    . All i want is to center my div on the page with all the images one under another and the text on the right. The images are some small icons. I made the div inline only not to spread it across the whole width of the page :(
    – Mihai Om Sep 06 '10 at 21:43
  • @Mihai: the code didn't come through in your last comment. use the '`' characters around your code or just add it to the question – Moin Zaman Sep 06 '10 at 21:44
  • and the problem is the the contet is centered and the images at the beginning of the lines are not one under the other anymore – Mihai Om Sep 06 '10 at 21:47
  • @Mihai: I think you need to rethink the markup and approach to layouting this with CSS. Can you post a screenshot of what you have now and what the desired result is? – Moin Zaman Sep 06 '10 at 21:49
  • Also from what I understand, I think you are better off wrapping each icon / text in one container. In fact what you're describing sounds very much like a list. Have you looked at using a `
      ` and using your image icons for the bullets?
    – Moin Zaman Sep 06 '10 at 21:51
  • I'll try it as a list. But I have to put the image in list item because are different images. Thank you for your effort guys. – Mihai Om Sep 06 '10 at 21:54
  • u can give each `
  • ` a different class and use the different icons
  • – Moin Zaman Sep 06 '10 at 21:55
  • I know. But I hate using background-image on li when is more natural to use list-style-image on ul. And I have to wrap the icons as links, so there must go in the list item. Thank you again. – Mihai Om Sep 06 '10 at 21:58