0

我试图让这个块元素在中间水平对齐,但同时我希望宽度是内部内容的最小可能。但我不认为这是可能的,或者我自己无法做到......

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;
}

这是我当前的代码......这背后的想法是,该标签的文本可能会稍大或稍小,具体取决于用户语言以及同一句子对于该特定用户的语言有多少字符。我无法控制,但我仍然想让这个元素水平对齐到中心。

这可能与 CSS 吗?

4

1 回答 1

3

使用display:table.

更新

<!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>

现场演示

于 2010-04-01T22:14:12.783 回答