Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRichard Contreras
7,026 PointsHow do you horizontally center multiple lines of text in a flexbox?
Please see this codepen: https://codepen.io/pdxcoder/pen/vROdvm
I basically want a table of text using CSS grid and I want all the text to be centered horizontally and vertically. Using justify-content and align-items works when my text only takes up a single line, but when it runs over to multiple lines the horizontal centering is lost.
1 Answer
Samuel Zhen
33,571 PointsAny reason why you want to set the display of column to flex? You can just do this
.column {
text-align: center;
}
When you set the column to flex and justify-content to center, it only centers the p element not the text inside it. If you want to keep the flex display then do this
.column {
border: 1px solid #333;
display: flex;
}
p {
flex: 1; /* This make p take the full width of column*/
text-align: center;
}
Richard Contreras
7,026 PointsRichard Contreras
7,026 PointsThanks, that is very helpful!
I think I do need to keep the flex display because I want the text to be vertically centered in the grid item as well.