当文本太长时,如何制作带有边距的流体文本居中框?

时间:2013-10-31 07:33:06

标签: html css

我有 HTML

<header class="title">
    <h1>Title</h1>
    <a href="#" class="back">Back</a>
</header>

CSS

.title {
    height: 36px;
    line-height: 36px;
    position: relative;
    overflow: hidden;
    background-color: #eee;
    margin-bottom: 30px;
}

.back {
    position: absolute;
    top: 0;
    left: 0;
    margin: 0 15px;
    height: 36px;
    line-height: 36px;
    width: 42px;
    overflow: hidden;
    background-color: #ccc;
}

.title h1 {
    margin: 0;
    font-size: 24px;
    text-align: center;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

请参阅http://jsfiddle.net/EZwaQ/:)

我想(1)文本中心在短时间内对齐,但(2)当它太长时,不要覆盖左侧的后退按钮。

example

正如你在jsfiddle中看到的,我在第二个例子中添加了margin-left

是否有任何通用的CSS方法可以解决这个问题?

2 个答案:

答案 0 :(得分:1)

试试这个我更新你的jsfiddle看看这个

jsfiddlehttp://jsfiddle.net/EZwaQ/3/

答案 1 :(得分:0)

试试这个。

<div class="header">
    <h1>Title</h1>
</div>
.header{margin:0 15px 0 72px;} //margin-left-->42px of width and 30px of margin for back

Fiddle here.

希望它对你有所帮助。

相关问题