- How To Center or Align Text and Images on Your Webpage with HTML
- Tutorial Series: How To Build a Website with HTML
- Web Style Sheets CSS tips & tricks
- Centering a block or image
- Centering vertically
- Centering vertically in CSS level 3
- Centering vertically and horizontally in CSS level 3
- Centering in the viewport in CSS level 3
- Nicely centered
- Site navigation
- CSS Vertical Align – How to Center a Div, Text, or an Image [Example Code]
- Why is Centering CSS Elements So Hard?
- How to Center an Element with the CSS Position Property
- How to center text with CSS positioning
- How to center an image with CSS positioning
- How to Center an Element with Flexbox in CSS
- How to center text with Flexbox
- How to center an image with Flexbox
- How to Center an Element with CSS Grid
- How to center text with CSS Grid
- How to center an Image with CSS Grid
- How to Center a Standalone Div, Text, or Image in CSS
- How to center a standalone div in CSS
- How to center standalone text in CSS
- How to center a standalone image in CSS
- Conclusion
How To Center or Align Text and Images on Your Webpage with HTML
Aligning content to the center, left, or right can be useful for arranging content on your page. In this tutorial, we’ll learn how to align text using HTML.
To align text on a webage, we can use the style attribute and the property text-align.
For example, the following code snippet would center the text “Sample text”:
p style="text-align:center;">Sample textp>
To align your HTML content to the left or right, you would replace center with left or right .
In this tutorial, we’ll go through the process of using the text-align property to center the images and text in the top section of our webpage as illustrated in our demonstration website.
To center this content, we’ll add the text-align property to the element that contains the background image, profile image, title, subtitle, and link in the top section of the homepage.
Locate this element in your index.html file and add the highlighted text like so:
. div style="background-image: url('https://html.sammy-codes.com/images/background.jpg'); background-size: cover; height:480px; padding-top: 80px; text-align: center;"> img src="https://html.sammy-codes.com/images/small-profile.jpeg" style="height:150px; border-radius: 50%; border: 10px solid #FEDE00;"> h1 style="font-size:100px; color:white; margin:10px;">Sammy the Sharkh1> p style="font-size:30px; color: white;">em>Senior Selachimorpha at DigitalOceanem>p> p style="font-size: 20px; color:#1F9AFE;">a href="Webpage FilePath";>About this sitea>p> div> .
Only copy and add the highlighted text-align attribute as other parts of this HTML code will not be specific to your project. Save your file and reload it in the browser. You should receive something like this:
You should now understand how to center and align text and have a section that looks like the top section of the demonstration site. In the next tutorial, we will recreate the middle section of the demonstration site.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Tutorial Series: How To Build a Website with HTML
This tutorial series will guide you through creating and further customizing this website using HTML, the standard markup language used to display documents in a web browser. No prior coding experience is necessary but we recommend you start at the beginning of the series if you wish to recreate the demonstration website.
At the end of this series, you should have a website ready to deploy to the cloud and a basic familiarity with HTML. Knowing how to write HTML will provide a strong foundation for learning additional front-end web development skills, such as CSS and JavaScript.
Web Style Sheets CSS tips & tricks
The most common and (therefore) easiest type of centering is that of lines of text in a paragraph or in a heading. CSS has the property ‘text-align’ for that:
renders each line in a P or in a H2 centered between its margins, like this:
The lines in this paragraph are all centered between the paragraph’s margins, thanks to the value ‘center’ of the CSS property ‘text-align’.
Centering a block or image
Sometimes it is not the text that needs to be centered, but the block as a whole. Or, phrased differently: we want the left and right margin to be equal. The way to do that is to set the margins to ‘auto’. This is normally used with a block of fixed width, because if the block itself is flexible, it will simply take up all the available width. Here is an example:
This rather narrow block of text is centered. Note that the lines inside the block are not centered (they are left-aligned), unlike in the earlier example.
This is also the way to center an image: make it into block of its own and apply the margin properties to it. For example:
The following image is centered:
Centering vertically
CSS level 2 doesn’t have a property for centering things vertically. There will probably be one in CSS level 3 (see below ). But even in CSS2 you can center blocks vertically, by combining a few properties. The trick is to specify that the outer block is to be formatted as a table cell, because the contents of a table cell can be centered vertically.
DIV.container < min-height: 10em; display: table-cell; vertical-align: middle >.This small paragraph.
This small paragraph is vertically centered.
Centering vertically in CSS level 3
CSS level 3 offers other possibilities. At this time (2014), a good way to center blocks vertically without using absolute positioning (which may cause overlapping text) is still under discussion. But if you know that overlapping text will not be a problem in your document, you can use the ‘transform’ property to center an absolutely positioned element. For example:
This paragraph is vertically centered.
For a document that looks like this:
the style sheet looks like this:
div.container3 < height: 10em; position: relative > /* 1 */ div.container3 p < margin: 0; position: absolute; /* 2 */ top: 50%; /* 3 */ transform: translate(0, -50%) > /* 4 */
- Make the container relatively positioned, which declares it to be a container for absolutely positioned elements.
- Make the element itself absolutely positioned.
- Place it halfway down the container with ‘top: 50%’. (Note that 50%’ here means 50% of the height of the container.)
- Use a translation to move the element up by half its own height. (The ‘50%’ in ‘translate(0, -50%)’ refers to the height of the element itself.)
Recently (since about 2015), another technique has also become available in several CSS implementations. It is based on the new ‘flex’ keyword for the ‘display’ property. This keyword is meant for use in graphical user interfaces (GUIs), but nothing stops you from using it in a document, if the document happens to have the right structure.
This paragraph is vertically centered.
the style sheet looks like this:
div.container5 < height: 10em; display: flex; align-items: center > div.container5 p
Centering vertically and horizontally in CSS level 3
We can extend both methods to center horizontally and vertically at the same time.
A side-effect of making the paragraph absolutely positioned is that it is then only as wide as it needs to be (unless we give it an explicit width, of course). In the example below, that’s precisely what we want: We center a paragraph with just one word (“Centered!”), so the width of the paragraph should be exactly the width of that word.
The yellow background is there to show that the paragraph is indeed only as wide as its contents. We assume the same mark-up as before:
The style sheet is similar to the previous example with respect to the vertical centering. But we now move the element halfway across the container as well, with ‘left: 50%’, and at the same time move it leftwards by half its own width in the ‘translate’ transformation:
div.container4 < height: 10em; position: relative >div.container4 p < margin: 0; background: yellow; position: absolute; top: 50%; left: 50%; margin-right: -50%; transform: translate(-50%, -50%) >
The next example below explains why the ‘margin-right: -50%’ is needed.
When the CSS formatter supports ‘flex’, it’s even easier:
div.container6 < height: 10em; display: flex; align-items: center; justify-content: center > div.container6 p
i.e., the only addition is the ‘justify-content: center’. Just like ‘align-items’ determines the vertical alignment of the container’s contents, ‘justify-content’ determines the horizontal alignment. (It’s actually a bit more complex, as their names suggest, but in a simple case that’s how it works.) A side-effect of ‘flex’ is that the child element, the P in this case, is automatically made as small as possible.
Centering in the viewport in CSS level 3
The default container for absolutely positioned elements is the viewport. (In case of a browser, that means the browser window). So centering an element in the viewport is very simple. Here is a complete example. (This example uses HTML5 syntax.)
Nicely centered
This text block is vertically centered.
Horizontally, too, if the window is wide enough.
You can see the result in a separate document.
The ‘margin-right: -50%’ is needed to compensate the ‘left: 50%’. The ‘left’ rule reduces the available width for the element by 50%. The renderer will thus try to make lines that are no longer than half the width of the container. By saying that the right margin of the element is further to the right by that same amount, the maximum line length is again the same as the container’s width.
Try resizing the window: You’ll see that each sentence is on one line when the window is wide enough. Only when the window is too narrow for the whole sentence will the sentence be broken over several lines. When you remove the ‘margin-right: -50%’ and resize the window again, you’ll see that the sentences will be broken already when the window is still twice as wide as the text lines.
Site navigation
Bert Bos, style activity lead
Copyright © 1994–2021 W3C ® Privacy policy
Created 5 May 2001;
Last updated Wed 06 Jan 2021 05:40:49 AM UTC
CSS Vertical Align – How to Center a Div, Text, or an Image [Example Code]
Kolade Chris
Even with helpful tools like CSS Grid and Flexbox, centering elements in CSS remains notoriously challenging.
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 margin: 0 auto 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 Property
The 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
I'm a Camper, and I'm vertically centered
* < margin: 0; padding: 0; box-sizing: border-box; >.container < position: relative; height: 400px; border: 2px solid #006100; >.centered-element
How to center an image with CSS positioning
* < margin: 0; padding: 0; box-sizing: border-box; >.container < position: relative; height: 400px; border: 2px solid #006100; >.centered-element
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.
* < margin: 0; padding: 0; box-sizing: border-box; >.container < position: relative; height: 400px; border: 2px solid #006100; >.centered-element
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. translateY() pushes it to the center vertically and translate on both the X and Y-axis ( translate() ) pushes it to the center vertically and horizontally.
How to Center an Element with Flexbox in CSS
CSS 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
I'm a Camper, and I'm vertically centered
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.
I'm a Camper, I'm now vertically and horizontally centered
The text now looks like this:
And the image like this:
How to Center an Element with CSS Grid
With 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
I'm a Camper, and I'm vertically centered
How to center an Image with CSS Grid
The above examples takes care of vertical centering for you. To get the text and image centered horizontally too, replace the align items with place items – a combination of both align-items and justify-content :
The text now looks like this:
And the image like this:
How to Center a Standalone Div, Text, or Image in CSS
The 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
How to center standalone text in CSS
I'm a Camper, and I'm centered
How to center a standalone image in CSS
img < display: block; margin: 0 auto; >/* Applies a display of block, a margin 0f 0 at the top and bootom, and auto on the left and right */
Conclusion
I 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.