32

This is the idea of what I want to do:

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-2"></div>
        <div class="col-sm-4"></div>
        <div class="col-sm-4"></div>
        <div class="col-sm-2"></div>
    </div>
</div>

How would it work using offset?

Philipp Kief
  • 8,172
  • 5
  • 33
  • 43
sumguy
  • 321
  • 1
  • 3
  • 5

5 Answers5

50

The offset works like a blank column that will stay before your column. For example, if you want a column that will have half of the size of the screen and will be exactly in the middle, you will have to do:

<div class="col-sm-6 col-sm-offset-3"></div>

A full row has 12 columns. This way you will have 6 columns (half of the row) and a offset of 3 columns. It will be exactly in the middle of the screen.

Take a look on Bootstrap documentation.

Luiz Henrique
  • 877
  • 8
  • 25
25

In Bootstrap 3, like this..

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-4 col-sm-offset-2"></div>
        <div class="col-sm-4"></div>
    </div>
</div>

http://codeply.com/go/7wofwfzrH3

In Bootstrap 4, like this..

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-4 offset-sm-2"></div>
        <div class="col-sm-4"></div>
    </div>
</div>

https://codeply.com/go/t5DTGwero8

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • Thank you!! How come the second col-sm-4 didn't need an offset if I was trying to do "2 4 4 2"? – sumguy Jan 13 '17 at 01:28
  • @sumguy Offset columns add margins to left so in this case, first there is margin/space of col-2 then two div's of col-4 and then the remaining col-2 is the remaining space. Consider a parent of 100% and a child of 70%(random), so now you have 30% of free space/ empty column to right side. – divy3993 Jan 13 '17 at 03:12
8

The above doesn't work for me as I think the bootstrap classes have changed slightly.

<div class="col-md-4 offset-md-2">

Does work for me.

Hope this helps

Dave

demsley
  • 319
  • 3
  • 6
1

I am using bootstrap 3. Below code is worked for me to shift div right.

<div class="col-lg-4 offset-lg-4"></div>
0

An example for Bootstrap 5 is below.

But note that offset is used for margin-left alignment. If you want to align items with margin-left, you can use offset classes. There are two types of offsets in Bootstrap 5: First is offset from 1-12 and second is responsive classes like offset-sm, offset-lg, offset-md.

It looks like this:

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-2 offset-sm-2"></div>
        <div class="col-sm-4 offset-sm-4"></div>
    </div>
</div>
Michael M.
  • 10,486
  • 9
  • 18
  • 34
IMRAN H
  • 11
  • 4