Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 622 Bytes

centering-elements.md

File metadata and controls

30 lines (22 loc) · 622 Bytes

Centering elements

Centering elements horizontally or vertically in CSS is notoriously unstraightforward. Here is a simple example on how to center text horizontally and vertically in a box.

HTML:

<div class="parent">
  <div class="child">hello world</div>
</div>

CSS:

.parent {
  width: 600px;
  height: 100px;
  margin: 0 auto;
  background-color: red;
}

.child {
  text-align: center;
  line-height: 100px; /* Note this needs to be set same as the height of parent */
}

For more recipes on vertical centering, refer to this tutorial