html - Css 强制文本根据下一个元素换行

标签 html css word-wrap

我有这个代码:

HTML:

<div>
    <h1>long string </h1>
    <div class="button">
        <a>button1</a>
        <a>button2</a>
        <a>buttonX</a>
    </div>
</div>

CSS:

h1 {
    float:left;
    vertical-align:top;
}
.button {
    float:right;
}

.button a {
    display:inline-block;
    width:100px;
    background-color:red;
    vertical-align:top;
    margin:5px;
}

这是一个 jsFiddle:http://jsfiddle.net/a88rB/1/

为了简单起见,我只保留了最少的标签。

目前,两者(带有“按钮”的 h1 和 div)都在同一行上。但是如果 h1 因为像

这样的真正的长字符串
<h1>long string long string long string long string long string long string long string long string long string long string </h1>

http://jsfiddle.net/a88rB/2/

按钮在 h1 下方。

我并不总是知道我将拥有多少个按钮。

有没有办法将 div.button 的宽度设置为它的内容,并将 h1 的宽度设置为父级的其余部分,这样如果 h1 足够长,它会自动换行并将按钮保留在顶部?

附言。我不需要 ie7 的支持

最佳答案

Fiddle!您可以重新排列 html,使按钮位于顶部:

<div>
    <div class="button">
        <a>button1</a>
        <a>button2</a>
        <a>buttonX</a>
    </div>
    <h1>long string long string long string long string long string long string long string long string long string long string </h1>
</div>

如果你想要 <h1>留在顶部,从同一条“线”开始,删除 h1 { float:left; }规则。

关于html - Css 强制文本根据下一个元素换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19143068/

相关文章:

html - 有没有办法在卡片内的 Material-UI Typography 元素中加粗一个词,而无需渲染?

javascript - jQuery — 带有滚动边栏的固定内容

css 在其父元素之间定位一个框

jquery - 页面重定向后显示特定的 div

javascript - 小屏幕上的自动换行不适用于我的代码中的一句话

javascript - 使用 Bootstrap Tags 输入以编程方式生成标签

html - 流体布局与网格?

github - 将 GitHub 的 'soft wrap' 选项设置为默认开启

java - Java 中的国际化自动换行

html - laravel 5 的简单 html dom 解析器