Various ways to place characters in the center with CSS

When creating buttons and headings with CSS, I think there are many opportunities to center the text inside the box. I also had an opportunity to write CSS lately. For my notes, I wrote it down here how to place where you want it.

Centering with text-align and padding

This is the method to center the characters and add same size padding on the top and bottom. The problem is that the height of elements and amount of characters are affected by font-size and the number of lines.

Every methods I noted on this article obtain same outcome like above. Therefore, I do not show same image of result for short.

Centering with height and line-height

This method set the height to fix the height and also set the same value of line-heigh after centering characters. The problem with this method is that it can only be used when the text is on one line. This method is easy for elements such as buttons that have a fixed text on one line. As an application, it is possible to express the character in the circle if you make height and width the same size and border-radius 50%.

Centering with table layout and vertical-align

Enclose the elements which you want to place it in the center.
Set height to the parent element and set display: table; after centering characters. And then, set display: table-cell; to the child element, and align it vertically-align: middle; in the center of the top and bottom. However, if the height of parent element is larger than child element, the parent element is affected size of child element.

Centering with position and translate

Enclose the elements which you want to place it in the center.
Set height to the parent element and set position: relative; after centering characters.
Place child element to the position of top: 50%; left: 50% with position: absolute;
At this time, the position of the child element is such that “the upper right part of the child element” comes to the center of the parent element.
To solve this, you need to adjust the position with transform: translate (-50%, -50%) ;.
Even if the child element is larger than the height of the parent element, it will not be affected with this method. (Looks like sticking out.)

Centering with flexbox

It can be also centering with Flexbox.
Set display: flex; to the parent element and position it in the vertical center with justify-content: center; and place it in the horizontal center with align-items: center.
If you have several child elements, it can be arrange vertically with flex-direction: column;

Centering with CSS Grid layout

Although this is not compatible method with IE11, it is possible to place element in the center of the top, bottom, right and left with CSS Grid.
The code is clean and easy because it can be implemented just by specifying two lines: display: grid; place-items: center ;.

It depends on the project, device and design which type of method is better, however I think that you can handle it flexibly by knowing various type of methods !!

I would be happy if this note was useful for you. Happy coding !!

80% is the creation, the rest is depression. Frontend developer and data scientist, designer. Looking for Physics Ph.D Twitter: @T_I_SHOW

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store