Even with helpful tools like CSS Grid and Flexbox, centering elements in CSS remains notoriously challenging. Show
It's been the subject of many jokes and memes, and when you successfully center something, you'll want to brag about it. Why is Centering CSS Elements So Hard?CSS can be tricky to work with. For example, if you're trying to align something horizontally OR vertically, it's not that difficult. You can just set text-align to center for an inline element, and
3 would do it for a block-level element. But issues arise on multiple fronts if you're trying to combine both vertical and horizontal alignments. In this tutorial, I will introduce you to three different methods to correctly center a div, text, or image in CSS. How to Center an Element with the CSS Position PropertyThe CSS position property takes relative, absolute, fixed, and static (the default) as values. When set, you will be able to apply the top, right, bottom, and left properties to the element. The combination of relative and absolute values can get a lot of things done, and so you can use it to center anything. Take a look at the snippets below to see some examples. How to center text with CSS positioning
How to center an image with CSS positioning
The above code has made the text and image centered vertically. To take care of both vertical and horizontal centering, we need to make a little tweak in the CSS. We'll set the top property to 50%, and we'll add a transform on both the X and Y axes.
The text now looks like this: And the image like this: Note that I applied the transform property because the child (with the class of centered-element) was slightly off-center.
4 pushes it to the center vertically and translate on both the X and Y-axis (
How to Center an Element with Flexbox in CSSCSS Flexbox handles layouts in one dimension (row or column). With Flexbox, it is pretty easy to center a div, text, or image in just three lines of code. Check the snippets below for examples. How to center text with Flexbox
How to center an image with Flexbox
We took care of the vertical alignment in just two lines of code. To make the image and text horizontally centered, add in justify-content: center.
1 The text now looks like this: And the image like this: How to Center an Element with CSS GridWith Flexbox, it is pretty easy to center anything, right? But with CSS Grid, it is really easy to center anything, because two lines of code are enough to do it for you. How to center text with CSS Grid
3 How to center an Image with CSS Grid
3 The above examples takes care of vertical centering for you. To get the text and image centered horizontally too, replace the align items with
6 – a combination of both
7 and
8:
6 The text now looks like this: And the image like this: How to Center a Standalone Div, Text, or Image in CSSThe three methods above let you center a div, text, or image in a container. You can also center a standalone div, text, or image. Let's see how to do that now. How to center a standalone div in CSS
7
8 How to center standalone text in CSS
9
0 How to center a standalone image in CSS
1
2 ConclusionI hope this tutorial gives you enough knowledge about vertical alignment and how to center elements in CSS so it's less of a hassle for you in your next project. Thank you for reading, and keep coding. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started How do you put text on top of a div?Position the div relatively ( position: relative; ), and then the text absolutely ( position:absolute; ). Make sure the text is inside an element inside the div, and then set the nested element's top position to a negative number until it is positioned to your liking. How do I align the content of a div to the top?How to Center a Div Vertically. Give the div a CSS class like center.. In your CSS code, type your .center CSS selector and open the style brackets.. Set the position of your div to absolute so that it's taken out of the normal document flow.. Set the top property to 50%.. How do I align text in a div?Using Text Align: Set the parent container to text-align: center. This centers the child element horizontally within the parent container. Use line-height: <height> to center the child element vertically within the parent container. The value of <height> should be equal to the height of the parent container. How to text align top in HTML?The HTML <td> valign attribute specifies the vertical alignment of content within a table cell. It can accept values such as top, middle, bottom, or baseline to control the positioning of content. If not explicitly set, the content in a table cell is vertically aligned to the middle by default. |