How to Vertically Align Text Inside a Flexbox Using align-items
When using Flexbox to vertically align content within an
Unlike align-self, which applies to flex items, align-items is applicable to flex containers. In this case, the li element is the flex container, allowing align-items: center to vertically center its child elements.
Here's an updated version of your code with the necessary adjustment:
ul { height: 100%; } li { display: flex; justify-content: center; /* Remove align-self: center */ align-items: center; /* Add align-items: center */ background: silver; width: 100%; height: 20%; }
Using align-items: center eliminates the need for a wrapper div. It allows you to vertically align the text within the li element without introducing unnecessary elements into your code.
The above is the detailed content of How to Vertically Center Text within a Flexbox `` Element?. For more information, please follow other related articles on the PHP Chinese website!