18

我需要在同一行上的标题和按钮,如有必要,使用省略号。

这是一个小提琴: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] |
|                                                       |
---------------------------------------------------------
4

4 回答 4

22

也许这样的事情有帮助?http://jsfiddle.net/epyFT/3/

我不确定如何使按钮与容器的未包装宽度对齐,并且我只使用单元格的百分比宽度。

新代码:

.container {
  width: 100%;
  display: table;
  table-layout: fixed;
}

.container>div {
  display: table-cell;
}

.cell1 {}

.wrap {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  word-break: break-all;
  word-wrap: break-word;
}

.cell2 {
  width: 60%;
}

h2 {
  display: inline;
  min-width: 200px;
}

button {
  width: 100px;
}
<div class="container">
  <div class="cell1">
    <div class="wrap">
      <h2>This is the header that should never wrap and elipse if it doesn't fit</h2>
    </div>
  </div>
  <div class="cell2">
    <button>Button</button>
  </div>
</div>
<div class="container">
  <h2>Header</h2>
  <button>Button</button>
</div>

于 2013-04-20T22:06:15.133 回答
7

我想我找到了一个更好的解决方案:http: //jsfiddle.net/epyFT/9/

HTML:

<div class="container">
    <h2>This is a very long heading that should wrap with ellipsis when too long, but have a button to it's right.</h2>
    <button>Hello.</button>
</div>

<div class="container">
    <h2>This is short.</h2>
    <button>Sup</button>
</div>

CSS:

.container {
    display: inline-block;
   max-width:100%;
   position: relative;
}
h2 {  
    padding-right: 200px;
    box-sizing: border-box;
    width: 100%;
    display: inline-block; 
    overflow: hidden; 
    white-space: nowrap; 
    text-overflow: ellipsis; 
    word-break: break-all; 
    word-wrap: break-word; 
}

button {
    position: absolute;
    width: 100px;
    right: 95px;
    top: 2em;
}
于 2013-04-20T22:49:20.077 回答
1

这是另一个

HTML:

<div class="container">
     <h2 class="oneline">This is the header that should never wrap and elipse if it doesn't fit</h2>
    <button>Button</button>
</div>
<div class="container">
     <h2 class="oneline">Header</h2>
    <button>Button</button>
</div>

CSS:

.container {
    width: 100%;
}
.oneline {
    display: inline-block;
    vertical-align: middle;
    max-width: 80%;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

JSFiddle

要对齐右侧的按钮,请width: 80%;添加.oneline.

于 2013-04-20T22:30:48.223 回答
0

我采用了 Dan 的出色答案(仅与 Webkit 浏览器兼容),并使其与 Firefox 和 Internet Explorer 8+ 兼容。这是小提琴:http: //jsfiddle.net/hammerbrostime/9t5bX/1/

.container {
  display: inline-block;
  max-width: 100%;
  position: relative;
}

h2 {
  padding-right: 200px;
  box-sizing: border-box;
  width: 100%;
  display: inline-block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  word-break: break-all;
  /* word-wrap: break-word;  taken out, caused issues in IE */
  max-width: -moz-calc(100% - 200px);
  /* FF hack */
}

button {
  position: absolute;
  width: 100px;
  right: 95px;
  top: 2em;
}
<div class="container">
  <h2>This is a very long heading that should wrap with ellipsis when too long, but have a button to it's right.</h2>
  <button>Hello.</button>
</div>

<div class="container">
  <h2>This is short.</h2>
  <button>Sup</button>
</div>

于 2013-12-03T20:30:36.400 回答