Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Centering content is one of the trickiest and more difficult things to do in CSS layout when using properties like float, display or position. In this video, you'll learn why flexbox is the smartest solution when it comes to centering content.
Resources
- A Complete Guide to Flexbox
- Flexbox - latest browser support
- Flexbox Froggy - A fun way to practice flexbox
Three ways to center-align elements using flexbox
Set the flex container's justify-content
and align-items
values to center
:
.container {
justify-content: center;
align-items: center;
}
Set the flex container's justify-content
value to center
, while setting the flex item's align-self
value to center
:
.container {
justify-content: center;
}
.item {
align-self: center;
}
Set the flex item's margin
value to auto
:
.item {
margin: auto;
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up