tom
tom

Reputation: 165

How to center divs in row in bootstrap?

I want to center my 7 divs and I don't know really how to do it. Any idea? I don't want to change margin all elements individually, as you can see on the picture divs are not centered compared to my buttons and there's a 12 column bootstrap problem

screenshot

<style type="text/css">
.properbut{
height: 180px;
display: block;
margin-left: auto;
margin-right: auto;
display: table-cell;
vertical-align: middle;

}

.fonts{
font-family:Georgia,serif;
color:#4E443C;
font-variant: small-caps; 
text-transform: none; 
font-weight: 100; 
font-size:   30px
}
</style>

<div class="container">
<br><br><br><br>

<div class="jumbotron">
    <br><br><br><br><br><br>
    <div class="row">
                    <div class ="col-md-2">
                    </div>

                    <div class ="col-md-1 ">
                       <img src="<?php echo asset_url();?>media/img/kalendarz/pon.png" style="height: 60px;">
                    </div>

                    <div class ="col-md-1 ">
                        <img src="<?php echo asset_url();?>media/img/kalendarz/wto.png"  style="height: 60px">
                    </div>


                    <div class ="col-md-1 ">
                        <img src="<?php echo asset_url();?>media/img/kalendarz/sro.png" style="height: 60px">
                    </div>


                    <div class ="col-md-1 ">
                        <img src="<?php echo asset_url();?>media/img/kalendarz/czw.png" style="height: 60px">
                    </div>


                    <div class ="col-md-1 ">
                        <img src="<?php echo asset_url();?>media/img/kalendarz/pia.png" style="height: 60px">
                    </div>


                    <div class ="col-md-1 ">
                        <img src="<?php echo asset_url();?>media/img/kalendarz/sob.png" style="height: 60px">
                    </div>


                    <div class ="col-md-1">
                        <img src="<?php echo asset_url();?>media/img/kalendarz/niedz.png" style="height: 60px">
                    </div>

                    <div class ="col-md-3">
                    </div>

    </div>
    <br><br><br><br><br><br>

    <div class="row container" >
            <div> 
                <button class="properbut btn btn-primary col-md-3 fonts" type="submit" style="margin-left: 130px;">Powtórki</button>
            </div>
            <div class="col-md-3" >

                    <p class="text-center fonts" style="margin-top: 7px; font-size: 30px;">Co dzisiaj zrobisz aby osiągnąć swój cel?</p>

            </div>

            <div> 
                <button class="properbut btn btn-warning col-md-3 fonts"  type="submit">Lekcje audio</button>
            </div>
    </div>
    <br><br><br><br><br><br><br><br>
    <br><br><br><br>

</div>

Upvotes: 8

Views: 80574

Answers (4)

user3589620
user3589620

Reputation:

First of all. If you want to have 7 pictures in the center, you need 14 columns! Go to http://getbootstrap.com/customize/#grid-system. The number of the columns is 14. (Or 28 to make a more precise grid-system). Than download it and use this css.

In the standard configuration:

  • col-xs-... Phones (< 768px). For example col-xs-12 is the complete width in devices < 768px

  • col-sm-... Small devices >= 768px and < 992px. For example col-sm-6 means the half of the devicewidth in this area

  • col-md-... Medium devices Desktops >= 992px and < 1200px

  • col-lg-... Large devices Desktops > 1200px

http://getbootstrap.com/css/#grid-options

This information you have to know, if you work with Bootstrap. Bootstrap works with offset, so that you don't need to use margin-left, margin-right.

No need to use

display: block;
margin-left: auto;
margin-right: auto;
display: table-cell;

in the class "properbut"!

http://getbootstrap.com/css/#grid-offsetting

Instead of <br><br><br>.... use padding.

Here is a working example with a 14-column-grid:

CSS

.properbut {
  height: 180px;
}

.p50 {
  padding: 50px;
}

.jumbotron img {
  height: 60px;
}

.fonts {
  font-family: Georgia,serif;
  color: #4E443C;
  font-variant: small-caps; 
  text-transform: none; 
  font-weight: 100; 
  font-size: 30px;
}

HTML

<div class="container">
<div class="jumbotron">
  <div class="row text-center p50">

    <div class ="col-xs-2 col-sm-2 col-md-2 col-lg-2 ">
     <img src="<?php echo asset_url();?>media/img/kalendarz/pon.png" >
   </div>

   <div class ="col-xs-2 col-sm-2 col-md-2 col-lg-2 ">
    <img src="<?php echo asset_url();?>media/img/kalendarz/wto.png" >
  </div>


  <div class ="col-xs-2 col-sm-2 col-md-2 col-lg-2 ">
    <img src="<?php echo asset_url();?>media/img/kalendarz/sro.png">
  </div>


  <div class ="col-xs-2 col-sm-2 col-md-2 col-lg-2 ">
    <img src="<?php echo asset_url();?>media/img/kalendarz/czw.png">
  </div>


  <div class ="col-xs-2 col-sm-2 col-md-2 col-lg-2 ">
    <img src="<?php echo asset_url();?>media/img/kalendarz/pia.png">
  </div>


  <div class ="col-xs-2 col-sm-2 col-md-2 col-lg-2 ">
    <img src="<?php echo asset_url();?>media/img/kalendarz/sob.png">
  </div>


  <div class ="col-xs-2 col-sm-2 col-md-2 col-lg-2">
    <img src="<?php echo asset_url();?>media/img/kalendarz/niedz.png">
  </div>
</div><!-- row -->


<div class="row p50">
  <div> 
  <button class="properbut btn btn-primary col-xs-10 col-xs-offset-2 col-sm-8 col-sm-offset-3 col-md-5 col-md-offset-0 col-lg-3 col-lg-offset-1  fonts" type="submit">Powtórki</button>
  </div>
  <div class="col-xs-10 col-xs-offset-2 col-sm-8 col-sm-offset-3 col-md-4 col-md-offset-0 col-lg-4 col-lg-offset-1">

    <p class="fonts" style="font-size: 30px;">Co dzisiaj zrobisz aby osiągnąć swój cel?</p>

  </div>

  <div> 
    <button class="properbut btn btn-warning col-xs-10 col-xs-offset-2 col-sm-8 col-sm-offset-3 col-md-5 col-md-offset-0 col-lg-3 col-lg-offset-1 fonts"  type="submit">Lekcje audio</button>
  </div>
</div><!-- row -->

In the firefox browser ctrl+shift+m to test in different screensizes. You can see how it works with the columns and the offsets!

Upvotes: 2

michaelsangma
michaelsangma

Reputation: 346

CSS Style:

/* centered columns styles */
.row-centered {
text-align:center;
}
.col-centered {
display:inline-block;
float:none;
/* reset the text-align */
text-align:left;
/* inline-block space fix */
margin-right:-4px;
}

HTML:

<div class="row row-centered">

<div class ="col-md-1 col-centered">
   <img src="http://placehold.it/100x100" style="height: 60px;">
</div>

<div class ="col-md-1 col-centered">
   <img src="http://placehold.it/100x100" style="height: 60px;">
</div>

<div class ="col-md-1 col-centered">
   <img src="http://placehold.it/100x100" style="height: 60px;">
</div>

<div class ="col-md-1 col-centered">
   <img src="http://placehold.it/100x100" style="height: 60px;">
</div>

<div class ="col-md-1 col-centered">
   <img src="http://placehold.it/100x100" style="height: 60px;">
</div>

<div class ="col-md-1 col-centered">
   <img src="http://placehold.it/100x100" style="height: 60px;">
</div>

<div class ="col-md-1 col-centered">
   <img src="http://placehold.it/100x100" style="height: 60px;">
</div>

</div><!-- row -->

enter image description here

This should help you fix it.

Upvotes: 16

Boris
Boris

Reputation: 541

Do you need something like this?

.row {
    text-align: center;
}
.col-md-1 {
    display:inline-block;
}

Upvotes: 8

nanndoj
nanndoj

Reputation: 6770

I can see you are using vertical-align property. Try also using text-align: center;

.row {
  text-align: center;
}

Upvotes: 0

Related Questions