13

我有两个按钮浮动到屏幕的相对两侧,如果屏幕缩小,我希望它们能很好地折叠。从视觉上看,这就是我想要的:

________________________________________________________________
|                                                               |
|   |LongTextButtonName|                 |LongTextButtonName|   | When the screen
|_______________________________________________________________| is large

_______________________________________
|                                      |
|   |LongTextBu...|  |LongTextBu...|   | When the screen is small
|______________________________________|

不幸的是,现在我得到了这个:

________________________________________
|                                       |
|   |LongTextButtonName|                |
|                |LongTextButtonName|   | 
|_______________________________________|

我想要一个纯 CSS 的解决方案。

这是我当前的 html 和 css(这是一个小提琴):

<div class="button-container">
  <div class="left-button"><a>LongTextButtonName</a></div>
  <div class="right-button"><a>LongTextButtonName</a></div>
</div>

.button-container {
  min-width:320px;
}
.button-container > div {
  border: 1px solid gray;
  background-color: orange;
  min-width: 160px;
  padding: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.left-button {
  float: left;
}
.right-button {
  float: right;
}
4

1 回答 1

12

下面的小提琴基于以下工作:

  • 容器需要一个宽度才能触发溢出和省略号。
  • 为了让盒子不断折叠,我使用了百分比宽度。
  • 考虑到我使用 CSS3 的填充box-sizing:border-box;

请注意,由于box-sizing:border-box;,此解决方案不适用于较旧的浏览器。

.button-container {
  min-width:320px;
}
.button-container > div {
  border: 1px solid gray;
  background-color: orange;
  max-width: 50%;
  padding: 8px;
  white-space: nowrap;
  overflow: hidden;
  box-sizing:border-box;
  text-overflow: ellipsis;
}
.left-button {
  float: left;
}
.right-button {
  float: right;
}

http://jsfiddle.net/UfxgZ/1/

于 2013-01-06T16:05:09.037 回答