Matthew C.
—You want to center text vertically using CSS, how do you do this?
Multiple methods can be used to center text vertically.
Say you have a <p>
tag element containing text in a container element:
<div class="container"> <p>text</p> </div>
You can make the container a flexbox
and center the text vertically using the align-items
property:
.container { display: flex; align-items: center; width: 150px; height: 150px; border: 1px solid black; }
You can use CSS grid to center text in a container in a similar way to using flexbox. The difference is that you set the display
property to grid
:
.container { display: grid; align-items: center; width: 150px; height: 150px; border: 1px solid black; }
You can set the position
of the text element to absolute
relative to the text container and then center the text vertically using the top
and transform
CSS properties:
* { margin: 0; } .container { width: 150px; height: 150px; border: 1px solid black; position: relative; } .container p { position: absolute; top: 50%; transform: translateY(-50%); }
The margin of all elements is set to 0 to remove the default margin on elements such as the <p>
tag, which affects centering text vertically using this method. We move the text element 50% from the top of the container element. This makes the top of the text vertically centered in the container. To make the middle of the text the vertical center, we use the translateY()
CSS function of the transform
CSS property to move the text element up by 50% of its height.
To vertically align text in an inline, inline-block, or table-cell element, you can use the vertical-align
property. An example of where this is useful is when you have a button with text and an icon that you want to vertically align:
<button class="button"> <span class="icon">💾</span> <span class="button-text">Save</span> </button>
If the icon and text have a different size, you can vertically align them using the vertical-align
property:
.button { padding: 12px 20px; font-size: 1rem; text-align: center; } .icon { vertical-align: middle; font-size: 1.5rem; } .button-text { vertical-align: middle; margin-left: 0.3rem; }
There are other methods that you can use such as setting the line height to the same height as the text container height or making the padding top and bottom equal if the text container height is fixed. These methods are not as flexible as the methods described above.
If you’re looking to get a deeper understanding of how web performance optimization works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.