As long as CSS has been around, centering elements vertically has always been a frustrating task for many front-end web developers.
Unlike horizontal alignments, which can be achieved easily using the text-align
property, vertical alignments are often much more tricky to put into action.
Nowadays, vertically centering text or any element using CSS is a simple task. This article demonstrates various CSS vertical alignment techniques: Using a Flexbox, using positioning + transform, using vertical padding, and using line-height.
Word Table Cell Vertical Alignment Tool
CSS Vertical Align using Flex
Nov 07, 2019 To align text vertically on a page, head over to the “Layout” tab and select the small icon in the bottom-right corner of the “Page Setup” group. This opens the “Page Setup” dialog box. Select the “Layout” tab and then click the arrow next to “Vertical Alignment” in the “Page” section. Middle align text vertically in table cell in Word. Middle align text vertically in table cell in Word. How to Align Text in a Microsoft Word Table. With Microsoft Word, you can create an numerous tables that look different from each other. When you start entering text into a table or convert text to a table, Word will automatically align the text to the upper left portion of the cell. Select the cells, columns, or rows, with text that you want to align (or select your entire table). Go to the (Table Tools) Layout tab. Click an Align button (you may have to click the Alignment button first, depending on the size of your screen). Formatting and aligning text in tables. You can change the font and font attributes of text in a cell, row, column, or table. You can also apply other formatting, such as highlighting, indenting, drop caps and so on. For more information, see “Formatting text.” WordPerfect lets you change the alignment of text in a cell, column, or table.
Introduced with the CSS3 specification, the display: flex
property/value makes it easier to design flexible responsive layout structures without using floats or positioning.
Along with display:flex
, you can easily align anything from table cells to inline elements with the align-items
, align-self
, and justify-content
properties.
Click here to view the demo for this technique. For the HTML part, we only need a simple container, so let’s consider the following:
Here is the CSS code for vertically centering the text:
Let’s see how it works:
- On line 4 of the CSS code, I define the display as
flex
, which enables the flexbox layout for the container. - Line 5 vertically centers the text in the flexbox, using the
align-items
CSS property andcenter
as a value.
Word Table Cell Vertical Alignment Components
This technique is very reliable and works well in a responsive web design context. Unfortunately, CSS Flexbox isn’t supported by IE9 and earlier versions.
CSS Vertical Align Using The Transform Property
This is an interesting technique, as it allows you to easily center vertically any HTML element: Paragraphs, images, etc.
To make it work, we need two containers in our HTML: The following examples use a div
as a parent, and a paragraph containing the text I want to vertically align:
Here’s the related CSS code, that will make our paragraph vertically (and horizontally) centered:
Let’s have a look at the properties used to make it work (You can check out the demo if you want to see this technique in action):
- The
.vertical-align
class defines a fixed 200px height. This can be changed to fit your site’s needs. Also, note the use ofposition: relative
, which is used to prevent display problems of the child element, which has aposition: absolute
defined. - The child element (
.vertical-align p
) uses atop: 50%;
to display the paragraph at 50% of the parent element’s top. This alone won’t center the content vertically. - The
transform
property on line 13 is used to compensate the extra space created by bothtop
andleft
.
This is what I’ve used to make WebDevBlog’s post title and meta vertically centered. It’s a simple, quick and efficient technique. The only problem is that the transform
property is not supported in IE8 and earlier versions.
CSS Vertical Alignment Using Vertical Padding
Using padding, we can create a centered text. This technique is very widely used, for example when creating buttons and navigation menu items.
Consider this simple bit of HTML below, or have a look at the demo.
Here is the CSS class we’re using:
As you can see, I specified a top and bottom padding of 2 rems (On line 7). This creates space at the top and bottom of the element, which will look vertically centered.
CSS Vertical Alignment Using Line Height
This technique is very similar to the padding technique explained above. The only difference is that we will be using the line-height
CSS property to vertically center our element.
Let’s use the same HTML code we’ve used in the previous example. As before, there’s a demo available for you to test and experiment with this technique.
The CSS code isn’t much different from the one used in the previous example:
In this technique, we have used the line-height
CSS property to vertically center the text of a button.
While this technique works well with buttons or navigation items, you have to use it very carefully: If for some reason your text goes on multiple lines, the whole thing will look completely broken. This is why I tend to use the padding technique instead of this when vertically centering text on a button.
CSS Vertical Alignment Using display:table-cell
This technique requires 2 containers, and uses display: table
on a parent element and display: table-cell
on its child.
This allows you to replicate how text and content are aligned on a HTML table cell. Let’s consider the following HTML code:
Here is the related CSS:
Nothing complicated here: The parent element is assigned a table
display, and its child uses display: table-cell
. The content of the child element is then vertically centered using the vertical-align: middle
property.
If you also need to align the content horizontally, you simply need to use text-align: center
on the parent element.
This technique works great in all modern browsers. Please note that display: table
and display: table-cell
are not supported on IE7 and earlier versions.
Frequently Asked Questions
Can I Vertically Center an Element Using vertical-align?
A common misconception is that the vertical-align: middle
CSS property can be used to vertically center an element within its parent. While vertical-align: middle
can be used to vertically align the content of a table cell, it won’t work on other elements.
How Do You Center Elements Vertically in CSS?
Depending on the type of element you’d like to vertically align, you can either use a flexbox, transforms, vertical padding or line-height, as detailed in the article.
If you create a table in Word and type some contents in it, you’ll find the text is automatically aligned left just like in Excel. It’s the default setting. If you don’t like it and want to center the text in each cell of Word tables, there are methods you can try.
First, select all the content in the table.
Then switch to Home tab and click Center in Paragraph section. The text will be centered immediately as below. But it’s still at the upper place of each cell.
If you want to center the text in Word tables both horizontally and vertically, you should select all the text at first as well. Then right-click the selected text and choose Table Properties…
Switch to Cell tab and choose Center in Vertical alignment. Hit OK to implement it.
Now check the table again. You’ve centered the text completely.