views:

49

answers:

1

Hi,

I'm trying to have this block element to be horizontally aligned in the middle but at the same time I would like the width to be the minimum possible for the contents inside. But I don't think it's possible or I'm not able to do it myself...

a#button-link {
    background: url("images/button-link.png") no-repeat scroll center left;
    margin: 12px auto 0 auto;
    display: block;
    width: 125px;
    height: 32px;
    line-height: 32px;
    padding-left: 35px;
    font-weight: bold;
}

This is my current code... The idea behind this is that the text for that tag could be slightly bigger or smaller depending on the user language and how much characters the same sentence has for that specific user's language. There's no way I can control but I still would like to have this element horizontally aligned to center.

Is this possible with CSS?

+1  A: 

Use display:table.

Update

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>display:table</title>
<style>
div
    {
        display:            table;
        border-collapse:    collapse;
        border:             1px solid #950;
        margin:             100px auto;
    }
* html div
    { /* IE 6. If anybody finds a good solution for IE 7: Tell me! */
        width:              .1em;
    }
* + html div
    { /* IE 7 Hack. Not perfect. */
        float:              left;
        margin:             100px auto 100px 45%;
    }
</style>

<div>Some Text.</div>

Live Demo

toscho
That won't work in IE(6?) though, will it?
crimson_penguin
Even if it works, is it OK to do it? I mean, I'm using divs and CSS so I avoid tables and now I set the display property to a table? A bit ironic...
Nazgulled
Just checked, it doesn't work with IE6 but I don't I care. IE9 it's already in the works, that's 3 new versions, time to update people.
Nazgulled
@Nazgulled CSS never changes the semantics of Text, while markup does. That’s the difference between HTML tables and the CSS property.
toscho