css - Bootstrap 3 : Span elements floating right in list-group-item do not consume vertical space

标签 css html css-float twitter-bootstrap-3

在 Bootstrap 3 list-group-item 中,我有一个图标、一些文本和两个应该向右浮动的图标/按钮。

我试过这个:

<a class="list-group-item" ng-click="handleClick()">
    <span class="glyphicon glyphicon-file"></span> 
    Some text goes here
    <button class="btn btn-xs btn-warning pull-right" ng-click="handleButtonClick()"><span class="glyphicon glyphicon-trash"></span></button>
    <some:custom:span></some:custom:span> 
</a>

如果结果适合一行,这会很好用:

rendering with wide window

当窗口太薄以至于实际文本无法放在一行中时,它也可以工作:

the text wrapped, leaving a place for the pull-right spans

但是,如果窗口允许文本保留在一行中,但没有足够的空间用于向右拉的跨度,事情就会变得一团糟:

float sticks out below

我真正想要的是 pull-right 跨度开始一个新行并右对齐,并且 list-group-item 垂直延伸到它们合身。我怎样才能做到这一点?

最佳答案

为了保持按钮对齐,在它们周围包裹一个新元素并 float 包裹元素。然后使用 .clearfix 类清除列表项上的 float 以固定它们的高度。

<div class="list-group">
  <a href="#" class="list-group-item clearfix">
    <span class="glyphicon glyphicon-file"></span>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit.
    <span class="pull-right">
      <button class="btn btn-xs btn-info">CCS</button>
      <button class="btn btn-xs btn-warning">
        <span class="glyphicon glyphicon-trash"></span>
      </button>
    </span>
  </a>
</div>

在此处查看实例:http://jsfiddle.net/cdog/EpG7x/ .

但是,根据 HTML5 specification from W3C,将按钮放置在链接内是无效的.

The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links).

使用带有表格的面板也可以获得类似的结果。

<div class="panel panel-default">
  <table class="table table-hover">
    <tbody>
      <tr>
        <td>
          <span class="glyphicon glyphicon-file"></span>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit.
        </td>
        <td class="text-right text-nowrap">
          <button class="btn btn-xs btn-info">CCS</button>
          <button class="btn btn-xs btn-warning">
            <span class="glyphicon glyphicon-trash"></span>
          </button>
        </td>
      </tr>
    </tbody>
  </table>
</div>

要防止单元格内的内容换行,您可以使用 .text-nowrap 类(在 Bootstrap v3.2.0 中添加)。

在此处查看实例:http://jsfiddle.net/cdog/6mFDH/ .

关于css - Bootstrap 3 : Span elements floating right in list-group-item do not consume vertical space,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868664/

相关文章:

java - 找不到将 CSS 导入 JavaFX 的源

css - 文字溢出 float 的div

html - 我可以在一个 CSS 选择器中同时使用百分比 (100%) 和像素 (-30px)

javascript - 为什么手机使用 jQuery 检测疯狂的宽度 ('body' ).width();

html - 居中水平对齐的标题

jquery - 将 Flickity 与 Bootstrap 4 轮播集成

javascript - jQuery html() 和 append() 的意外行为

html - 在相对定位的 div 周围环绕文本

css - 让第二个 float div 填充容器 div 内其余水平空间的问题

html - 在 firefox 中首次进入不容易点击问题,一个错误?