【发布时间】:2013-04-20 21:39:44
【问题描述】:
我需要一个标题和按钮在同一行,必要时使用省略号。
这是一个小提琴:http://jsfiddle.net/epyFT/1/
我希望输出如下所示:
_________________________________________________________
| |
| Header goes here [button] |
| |
---------------------------------------------------------
或者
_________________________________________________________
| |
| Super, super, super, super long header... [button] |
| |
---------------------------------------------------------
或者使用更小的窗口:
____________________________
| |
| Header goes... [button] |
| |
----------------------------
按钮不应该浮动到下一行。我该怎么做?
HTML
<div class="container">
<h2>This is the header that should never wrap and elipse if it doesn't fit</h2>
<button>Button</button>
</div>
<div class="container">
<h2>Header</h2>
<button>Button</button>
</div>
CSS
.container {
width:100%;
}
h2 {
display:inline;
min-width:200px;
overflow: hidden; white-space: nowrap; text-overflow: ellipsis; word-break: break-all; word-wrap: break-word;
}
button {
width:100px;
}
奖金
获得第二个(固定宽度)按钮向右拉的额外功劳。
_________________________________________________________
| |
| Header goes here [button1] [button2] |
| |
| |
| Super, super, super, super long... [button] [button2] |
| |
---------------------------------------------------------
【问题讨论】:
-
您对目标浏览器有什么要求吗?
-
不幸的是,
text-overflow:ellipsis;仅适用于块容器。 dev.w3.org/csswg/css-ui/#text-overflow -
@Dan,常见的现代版本——Chrome、Safari、IE 8、Firefox 等
-
@SonuJoshi,我可以模拟块容器吗?我为 h2 尝试了 inline-block 和 table-cell。你有什么建议?
-
Dan 已经解决了。我正在做类似的事情。