CSS Vertical Alignment Using display:table-cell This is why I tend to use the padding technique instead of this when vertically centering text on 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. In this technique, we have used the line-height CSS property to vertically center the text of a button.
#Css how to vertically align text center code#
The CSS code isn’t much different from the one used in the previous example: As before, there’s a demo available for you to test and experiment with this technique. Let’s use the same HTML code we’ve used in the previous example. The only difference is that we will be using the line-height CSS property to vertically center our element. This technique is very similar to the padding technique explained above. This creates space at the top and bottom of the element, which will look vertically centered. This technique is very widely used, for example when creating buttons and navigation menu items.Ĭonsider this simple bit of HTML below, or have a look at the demo.Īs you can see, I specified a top and bottom padding of 2 rems (On line 7). Using padding, we can create a centered text. CSS Vertical Alignment Using Vertical Padding The only problem is that the transform property is not supported in IE8 and earlier versions. It’s a simple, quick and efficient technique. This is what I’ve used to make WebDevBlog’s post title and meta vertically centered.
CSS Vertical Align Using The Transform Property Unfortunately, CSS Flexbox isn’t supported by IE9 and earlier versions. This technique is very reliable and works well in a responsive web design context. Line 5 vertically centers the text in the flexbox, using the align-items CSS property and center as a value.On line 4 of the CSS code, I define the display as flex, which enables the flexbox layout for the container.Here is the CSS code for vertically centering the text: For the HTML part, we only need a simple container, so let’s consider the following: Introduced with the CSS3 specification, the display: flex property/value makes it easier to design flexible responsive layout structures without using floats or positioning.Īlong with display:flex, you can easily align anything from table cells to inline elements with the align-items, align-self, and justify-content properties.Ĭlick here to view the demo for this technique. This article demonstrates various CSS vertical alignment techniques: Using a Flexbox, using positioning + transform, using vertical padding, and using line-height.
Nowadays, vertically centering text or any element using CSS is a simple task. Unlike horizontal alignments, which can be achieved easily using the text-align property, vertical alignments are often much more tricky to put into action. As long as CSS has been around, centering elements vertically has always been a frustrating task for many front-end web developers.