Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left align and right align within div in Bootstrap

What are some of the common ways to left align some text and right align some other text within a div container in bootstrap?

e.g.

Total cost                   $42 

Above total cost should be left aligned text and $42 is right aligned text

like image 546
user462455 Avatar asked Sep 07 '13 10:09

user462455


People also ask

How Left and Right align text within a div in Bootstrap?

Answer: Use the justify-content-between Class You can simply use the class . justify-content-between in combination with the class . d-flex to left align and right align text content within a <div> container in Bootstrap 4.

How do you set a right and left div tag?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.


2 Answers

2021 Update...

Bootstrap 5 (beta)

For aligning within a flexbox div or row...

  • ml-auto is now ms-auto
  • mr-auto is now me-auto

For text align or floats..

  • text-left is now text-start
  • text-right is now text-end
  • float-left is now float-start
  • float-right is now float-end

Bootstrap 4+

  • pull-right is now float-right
  • text-right is the same as 3.x, and works for inline elements
  • both float-* and text-* are responsive for different alignment at different widths (ie: float-sm-right)

The flexbox utils (eg:justify-content-between) can also be used for alignment:

<div class="d-flex justify-content-between">       <div>          left       </div>       <div>          right       </div>  </div> 

or, auto-margins (eg:ml-auto) in any flexbox container (row,navbar,card,d-flex,etc...)

<div class="d-flex">       <div>          left       </div>       <div class="ml-auto">          right       </div>  </div> 

Bootstrap 4 Align Demo
Bootstrap 4 Right Align Examples(float, flexbox, text-right, etc...)


Bootstrap 3

Use the pull-right class..

<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> 

Bootstrap 3 Demo

You can also use the text-right class like this:

  <div class="row">     <div class="col-md-6">Total cost</div>     <div class="col-md-6 text-right">$42</div>   </div> 

Bootstrap 3 Demo 2

like image 113
Zim Avatar answered Sep 20 '22 02:09

Zim


Instead of using pull-right class, it is better to use text-right class in the column, because pull-right creates problems sometimes while resizing the page.

like image 30
Arun Agarwal Avatar answered Sep 23 '22 02:09

Arun Agarwal