Sentry Answers>CSS>

How do I vertically center text with CSS?

How do I vertically center text with CSS?

Matthew C.

The ProblemJump To Solution

You want to center text vertically using CSS, how do you do this?

The Solution

Multiple methods can be used to center text vertically.

Using Flexbox

Say you have a <p> tag element containing text in a container element:

Click to Copy
<div class="container"> <p>text</p> </div>

You can make the container a flexbox and center the text vertically using the align-items property:

Click to Copy
.container { display: flex; align-items: center; width: 150px; height: 150px; border: 1px solid black; }

Using Grid

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:

Click to Copy
.container { display: grid; align-items: center; width: 150px; height: 150px; border: 1px solid black; }

Using absolute positioning and CSS transforms

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:

Click to Copy
* { 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.

Using vertical alignment

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:

Click to Copy
<button class="button"> <span class="icon">&#128190;</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:

Click to Copy
.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; }

Other methods

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.

Further Reading

If you’re looking to get a deeper understanding of how web performance optimization works, take a look at the following articles:

  • Syntax.fmHasty Treat - CSS Grid Masonry (Grid Level 3)
  • Syntax.fmHasty Treat - CSS Grid Level 2 aka Subgrid
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “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.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.