在Bootstrap中在div内左对齐并右对齐

use*_*455 236 user-interface twitter-bootstrap bootstrap-4

有什么常见的方法可以左对齐一些文本并在引导程序中对div容器中的其他文本进行右对齐?

例如

Total cost                   $42
Run Code Online (Sandbox Code Playgroud)

以上总费用应为左对齐文本,42美元为右对齐文本

Zim*_*Zim 542

2018年更新......

Bootstrap 4.1+

  • pull-right 就是现在 float-right
  • text-right 与3.x相同,适用于内联元素
  • 两者float-*text-*响应为在不同宽度的不同比对(即:float-sm-right)

flexbox utils(例如:) justify-content-between也可用于对齐:

<div class="d-flex justify-content-between">
      <div>
         left
      </div>
      <div>
         right
      </div>
 </div>
Run Code Online (Sandbox Code Playgroud)

或者,ml-auto任何弹性箱容器中的自动边距(例如:: )(行,导航栏,卡片,d-flex等...)

<div class="d-flex">
      <div>
         left
      </div>
      <div class="ml-auto">
         right
      </div>
 </div>
Run Code Online (Sandbox Code Playgroud)

Bootstrap 4 Align Demo
Bootstrap 4右对齐示例(float,flexbox,text-right等...)


Bootstrap 3

使用pull-right班级..

<div class="container">
  <div class="row">
    <div class="col-md-6">Total cost</div>
    <div class="col-md-6"><span class="pull-right">$42</span></div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Bootstrap 3演示

你也可以使用这样的text-right类:

  <div class="row">
    <div class="col-md-6">Total cost</div>
    <div class="col-md-6 text-right">$42</div>
  </div>
Run Code Online (Sandbox Code Playgroud)

Bootstrap 3演示2

  • 对于Bootstrap 3,对于专栏,`text-right`对我有用。 (2认同)
  • @FernandoKosh 从左/右更改为开始/结束是因为它增加了对从右到左语言的支持,以及填充、边距和文本对齐方式,甚至当页面指定读取的语言时,列的顺序会自动翻转从右边。 (2认同)

小智 59

而不是使用pull-right类,最好text-right在列中使用类,因为pull-right有时在调整页面大小时会产生问题.


Car*_*ard 16

引导5.0

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

<div class="row">
  <div class="col-sm-6"><p class="float-start">left</p></div>  
  <div class="col-sm-6"><p class="float-end">right</p></div>  
</div>
Run Code Online (Sandbox Code Playgroud)

一整列可以容纳 12 个,6 个(col-sm- 6)正好是一半,在这一半中,一个在左边(float-start),一个在右边(float-end)。

更多例子

  • fontawesome按钮

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
    
    <div class="row">
      <div class=col-sm-6>
        <p class="float-start text-center">  <!-- text-center can help you put the icon at the center -->
          <a class="text-decoration-none" href="https://www.google.com/"
          ><i class="fas fa-arrow-circle-left fa-3x"></i><br>Left
          </a>
        </p>
      </div>
      <div class=col-sm-6>
        <p class="float-end text-center">
          <a class="text-decoration-none" href="https://www.google.com/"
          ><i class="fas fa-arrow-circle-right fa-3x"></i><br>Right
          </a>
        </p>
      </div>  
    Run Code Online (Sandbox Code Playgroud)


Eri*_*nig 15

在Bootstrap 4中,正确的答案是使用text-xs-right该类.

这有效,因为xs表示BS中的最小视口大小.如果您愿意,可以通过使用仅在视口为中等或更大时应用对齐text-md-right.

在最新的alpha中,text-xs-right已被简化为text-right.

<div class="row">
    <div class="col-md-6">Total cost</div>
    <div class="col-md-6 text-right">$42</div>
</div>
Run Code Online (Sandbox Code Playgroud)


Wil*_*ken 9

Bootstrap v4引入了flexbox支持

<div class="d-flex justify-content-end">
  <div class="mr-auto p-2">Flex item</div>
  <div class="p-2">Flex item</div>
  <div class="p-2">Flex item</div>
</div>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,访问https://v4-alpha.getbootstrap.com/utilities/flexbox/


小智 7

我们可以通过 Bootstrap 4 Flexbox 实现:

<div class="d-flex justify-content-between w-100">
<p>TotalCost</p> <p>$42</p>
</div>

d-flex // Display Flex
justify-content-between // justify-content:space-between
w-100 // width:100%
Run Code Online (Sandbox Code Playgroud)

示例:JSFiddle