Search code examples
htmlcssvertical-alignment

Vertically Align Divs


I am trying to vertically align a div in my code but with no success. This div contains sub divs. The first one

I want this to look like this :

enter image description here

but at the moment it is not aligned. This is my HTML code :

body {
  display: block;
  margin-left: auto;
  margin-right: auto;
  background: #f1f1f1;
}
.content {
  float: left;
  margin: 20px auto;
  text-align: center;
  width: 300px;
}
.content h1 {
  text-transform: uppercase;
  font-weight: 700;
  margin: 0 0 40px 0;
  font-size: 25px;
  line-height: 30px;
}
.circle {
  width: 100px;
  height: 100px;
  line-height: 100px;
  border-radius: 50%;
  /* the magic */
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  text-align: center;
  color: white;
  font-size: 16px;
  text-transform: uppercase;
  font-weight: 700;
  margin: 0 auto 20px;
}
.blue {
  background-color: #052D72;
}
.green {
  background-color: #16a085;
}
.red {
  background-color: #e74c3c;
}
<div class="content">
  <div class="circle blue"></div>
</div>
<div class="content">
  <div class="circle blue">Blue</div>
  <div class="circle blue"></div>
  <div class="circle blue"></div>
  <div class="circle blue"></div>
</div>
<div class="content">
  <div class="circle blue"></div>
  <div class="circle blue"></div>
</div>

So, in the .content I tried adding this :

vertical-align:baseline;

but I saw no difference.


Solution

  • Add display:inline-block & Remove float for #content

    .content {
        display: inline-block;
        margin: 20px auto;
        text-align: center;
        vertical-align: middle;
        width: 200px;
    }
    

    https://jsfiddle.net/k0fx384a/1/

    EDIT with class: https://jsfiddle.net/k0fx384a/2/