0

I am using bootstrap and here are my HTML code:

    <div class="row">
          <div class="col-md-3">
              <div class="a">
                  <p>a</p>
              </div>
          </div>
          <div class="col-md-3">
              <div class="b">
                  <p>b</p>
              </div>
          </div>
          <div class="col-md-3">
              <div class="c">
                  <p>c</p>
              </div>
          </div>
          <div class="col-md-3">
              <div class="d">
                  <p>d</p>
              </div>
          </div>
    </div>

And for each class, a,b,c,d, the CSS code is like this:

.a{
    background-image: url('a.jpg');
    width: 300px;
    height: 300px;
    margin-top: 5%;
    border-radius: 10px;
}

The problem is I want to position these div(class a,b,c,d) to center of each class(col-md-3). Could anyone help me?

user6142261
  • 627
  • 1
  • 8
  • 14

1 Answers1

0

you can horizontally center an element using auto margins:

.a, .b, .c, .d {
    margin: 0 auto;
}

You can also achieve centering both horizontally and vertically using flexbox on the parent elements:

.col-md-3 {
    display: flex;
    align-items: center;
    justify-content: center;
}
Adam Karacsony
  • 151
  • 1
  • 9
  • Thanks~ I will accept this answer~ Because stackoverflow says that I cannot accept an answer within 8 min... – user6142261 Feb 21 '17 at 17:01
  • Thanks :) That timeout is to make sure others also have the chance to submit potentially better answers before you decide which answer will you accept. – Adam Karacsony Feb 22 '17 at 09:50