我在这里有一个简单的演示:

<ul>
    <li>One <input class="btn pull-right" value="test"></li>
    <li>Two <input class="btn pull-right" value="test2"></li>
</ul>

我有一个无序的列表和每个列表项目,我希望有文本在左边,然后一个右对齐按钮。我曾尝试使用拉右,但这完全混乱了对齐。我做错了什么?

<链接href=“http://twitter.github.com/bootstrap/assets/cssstrap.css”“terlesheet”/> <德> <li>One <输入级=“btn pullright”值=“测试”></li> <li> 2 < <输入级=“btn -right引体”值=“test2”</li> < /德>


当前回答

使用Bootstrap右拉帮助器并不适用于我们,因为它使用float: right,这迫使内联块元素变成块。当.btns变成块时,它们失去了内联块作为准文本元素提供的自然边距。

所以我们使用direction: rtl;在父元素上,这导致该元素内的文本从右向左布局,这也导致内联块元素从右向左布局。你可以像下面这样使用LESS来防止孩子也被放置rtl:

/* Flow the inline-block .btn starting from the right. */
.btn-container-right {
  direction: rtl;

  * {
    direction: ltr;
  }
}

像这样使用它:

<div class="btn-container-right">
    <button class="btn">Click Me</button>
</div>

其他回答

很抱歉回答一个已经回答过的老问题,但我想我应该指出你的jsfiddle不能工作的几个原因,以防其他人检查它并想知道为什么在接受的答案中描述的拉右类在那里不能工作。

the url to the bootstrap.css file is invalid. (perhaps it worked when you asked the question). you should add the attribute: type="button" to your input element, or it won't be rendered as a button - it will be rendered as an input box. Better yet, use the <button> element instead. Additionally, because pull-right uses floats, you will get some staggering of the button layout because each LI does not have enough height to accommodate the height of the button. Adding some line-height or min-height css to the LI would address that.

工作提琴:http://jsfiddle.net/3ejqufp6/

<ul>
  <li>One <input type="button" class="btn pull-right" value="test"/></li>
  <li>Two <input type="button" class="btn pull-right" value="test2"/></li>
</ul>

(我还添加了一个最小宽度的按钮,因为我不能忍受一个粗糙的右对齐的按钮,因为不同的宽度:))

添加到公认的答案,当在容器和列中工作时,从bootstrap中内置了填充,我有时会有一个完整的拉伸列,其中有一个子div,它可以进行拉取。

<div class="row">
  <div class="col-sm-12">
      <div class="pull-right">
            <p>I am right aligned, factoring in container column padding</p>
      </div>
  </div>
</div>

或者,将所有列相加为网格列的总数(默认为12),并偏移第一列。

<div class="row">
  <div class="col-sm-4 col-sm-offset-4">
        This content and its sibling..
  </div>
  <div class="col-sm-4">
        are right aligned as a whole thanks to the offset on the first column and the sum of the columns used is the total available (12).
  </div>
</div>

从Bootstrap V3.3.1开始,下面的CSS样式将解决这个问题

.modal-open{
    padding-right: 0 !important;
}

注意:我尝试了上面帖子中的所有建议,所有地址都是旧版本,不提供对新集引导版本的修复。

“右拉”类可能不是正确的方式,因为它使用“float: right”而不是文本对齐。

检查引导3 css文件,我发现“文本权利”类在第457行。这个类应该是将文本向右对齐的正确方式。

一些代码: 对于Bootstrap 3和4

<div class="row">
    <div class="col-xs-12">
        <div class="text-right">
            <button type="button" class="btn btn-default">Default</button>
        </div>
    </div>
</div>

自举5

<div class="row">
    <div class="col">
        <div class="text-end">
            <button type="button" class="btn btn-secondary">Default</button>
        </div>
    </div>
</div>

在class属性中插入pull-right,让bootstrap来排列按钮。

对于Bootstrap 2.3,请参见:http://getbootstrap.com/2.3.2/components.html#misc >帮助类> .pull-right。


关于引导3,请参见:https://getbootstrap.com/docs/3.3/css/#helper-classes > Helper类。


关于Bootstrap 4,请参见:https://getbootstrap.com/docs/4.0/utilities/float/#responsive

右拉命令被移除并替换为右浮动命令或一般的float-{sm,md,lg,xl}-{左,右,none}


Boostrap 5参见:https://getbootstrap.com/docs/5.0/utilities/float/

最接近的解决方案是浮动端。