Place button group at the far right side of a header with Bootstrap

amr*_*ree 55 css twitter-bootstrap

I've managed to place a button group at the far side of a header (H1/H2/H3). However, I don't know how to make the button float vertically in the middle. It just stays at the top. I tried using margin-top but since it's a fixed value it won't be dynamically placed in the middle.

Here's the screenshot:

enter image description here

Here's the code:

<div class="container">
  <section>
    <div class="page-header">
      <h1 class="pull-left">
        The header
      </h1>
      <div class="pull-right">
        <div class="btn-group">
          <button class="btn btn-primary">
            Actions
          </button>
          <button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
            <span class="caret"></span>
          </button>
          <ul class="dropdown-menu pull-right">
            <li>
              <a href="#">Action</a>
            </li>
            <li>
              <a href="#">Another Action</a>
            </li>
          </ul>
        </div>
      </div>
      <div class="clearfix"></div>
    </div>
    Contents
  </section>
</div>
Run Code Online (Sandbox Code Playgroud)

Thanks in advance.

jsFiddle: http://jsfiddle.net/aurorius/PKrUC/

joh*_*ino 166

这是如何在没有任何额外CSS的情况下在Bootstrap 3.2,3.3中获得与标题对齐的按钮,以防有人从谷歌来到这里.

选项1

http://jsfiddle.net/ypaL5nko/

<div class='page-header'>
  <div class='btn-toolbar pull-right'>
    <div class='btn-group'>
      <button type='button' class='btn btn-primary'>Button Text</button>
    </div>
  </div>
  <h2>Header Text</h2>
</div>
Run Code Online (Sandbox Code Playgroud)

如果您只使用一个按钮,则按钮组是可选的.

选项2

http://jsfiddle.net/sLdnae3q/

<h1>
  <span>Header Text</span>
  <button class='btn btn-primary pull-right'>Button Text</button>
</h1>
<hr/>
Run Code Online (Sandbox Code Playgroud)

  • 这应该标记正确的答案 (15认同)
  • 选项2在语义上也不正确,我想知道哪些屏幕阅读器最终会阅读... (4认同)
  • 对于我来说,在靠近井的右上方放置一个按钮也很有效.它不是在角落里,而是在拐角处. (2认同)
  • 由于长标题文本或小屏幕,选项1将在重叠时显示得更好. (2认同)

Sel*_*ani 8

试试这个css代码

.btn-group.my-class{
  bottom: 15px;
  position: relative;
  top: 15px;
}
Run Code Online (Sandbox Code Playgroud)

H1/H2/H3上的动态标题可能是H4

演示:http://jsfiddle.net/PKrUC/2/

  • 这段代码可能(并且可能)搞砸HTML中的任何其他`.btn-group`元素.至少将特定类添加到它的特定实例,并将其用作CSS选择器. (16认同)