Skip to content

Instantly share code, notes, and snippets.

@jonohayon
Created March 2, 2016 14:39
Show Gist options
  • Save jonohayon/513579790ad74b36010a to your computer and use it in GitHub Desktop.
Save jonohayon/513579790ad74b36010a to your computer and use it in GitHub Desktop.
Two ways to center an element both horizontally and vertically

#Option 1: Using flexbox

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

#Option 2: Using tables (for inline-block and block elements)

.parent {
  display: table;
}

.child {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.child > .child-of-child {
  margin-left: auto;
  margin-right: auto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment