- 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 Image Centering – How to Center an Image in a Div
- How to Center a div using CSS
- How to Center an Image in a Div Horizontally with Text-align
- How to Center an Image in a Div Horizontally with Margin-auto
- How to Center an Image in a Div Horizontally with the Position and Transform Properties
- How to Center an Image in a Div Horizontally with Display-Flex
- How to Center an Image in a Div Vertically with Display-Flex
- How to Center an Image in a Div Vertically with the Position and Transform Properties
- How to Center an Image in a Div Horizontally and Vertically with Display-Flex
- How to Center an Image in a Div Horizontally and Vertically with the Position and Transform Properties
- Wrapping Up
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 Image Centering – How to Center an Image in a Div
Joel Olawanle
When you’re working on the front-end of a web page, you sometimes need to center an image within a div (container).
This can become tricky at times. And based on certain conditions, a particular method may not work at some point, leaving you searching for alternatives.
In this article, you will learn how to center an image in a div with CSS.
How to Center a div using CSS
You center an image in a div in two ways: horizontally and vertically. When you put these two methods together, you will have an entirely centered image:
By default, web content always begins from the top-left corner of the screen and moves from ltr (left to right) – except for certain languages like Arabic, which goes from rtl (right to left).
Let’s start by seeing how to center an image within a div horizontally. Then we’ll see how to center vertically. Finally, we’ll see how you can do both together.
How to Center an Image in a Div Horizontally with Text-align
Suppose you have a div in which you place your image this way:
And apply basic CSS styling so your image is visible:
The text-align method won’t work in all cases, as you typically use it to center text. But when you have your images within a block level container like a div , then this method will work:
This works by adding the text-align property alongside its value of center to the container and not the image itself.
How to Center an Image in a Div Horizontally with Margin-auto
Another method that you can use to horizontally center an image within a div (container) is the margin property with the value of auto .
The element will then take up the specified width , and the remaining space will be split equally between the left and right margins.
You would usually apply this method to the image itself and not the container. But unfortunately, this property alone doesn’t work. You also need to specify the width the image will take first. This lets the margin know the remaining width the container has so that it can be split equally.
Secondly, img is an inline element, and the margin-auto property set does not affect inline-level elements. This means you must first convert it to a block-level element with display property set as block .
How to Center an Image in a Div Horizontally with the Position and Transform Properties
Another method you can use to position an image horizontally is the position property alongside the transform property.
This method can be very tricky, but it works. You must first set the container’s position to relative , then the image to absolute .
Once you do this, you can now move the image to whichever position you wish using either the left , top , bottom , or right properties on the image.
In this case, you only want to move the image to the center horizontally. This means you would move the image via the left to 50% or right to -50%:
But when you check your image, you will notice that the image is still not perfectly placed in the center. This is because it started from the 50% mark, which is the center position.
In this case, you need to use the transform-translateX property to adjust it to get the perfect center horizontally:
How to Center an Image in a Div Horizontally with Display-Flex
CSS flexbox makes it easier for you to design flexible, responsive layout structures without using float or positioning. We can also use this to place an image in the center horizontally of a container using the display property with flex as its value.
But this alone doesn’t work. You also need to define the position where you want your image. This could be center, left or maybe right :
Note: The display: flex property is not supported in older versions of browsers. You can read more here. You’ll also notice that the width and height of the image are defined to ensure the image doesn’t shrink.
Let’s now learn how to center an image in a div vertically. Later we’ll see how to center an image in a div horizontally and vertically together, making it a perfect center.
How to Center an Image in a Div Vertically with Display-Flex
Just like how you were able to center the image horizontally with the display-flex method, you can also do the same vertically.
But this time around, you won’t need to use the justify-content property. Rather you’ll use the align-items property:
For this method to work, the container must have a specified height which you will use to calculate the height and know where the center is located.
How to Center an Image in a Div Vertically with the Position and Transform Properties
Similar to how you used the position and transform properties earlier to place your image in the center horizontally, you can also do the same vertically.
But this time around, you won’t use left or right, . Instead you will use top or bottom alongside translateY rather than translateX :
You have learned how to center an image within a div horizontally and vertically using all possible methods. Let’s now learn how to center both horizontally and vertically.
How to Center an Image in a Div Horizontally and Vertically with Display-Flex
The display-flex property is a combination of how you’d center the image vertically and horizontally.
For the flex method, this means you will use both the justify-content and align-items properties set to center :
How to Center an Image in a Div Horizontally and Vertically with the Position and Transform Properties
This is also very similar, as all you have to do is combine both ways you were able to center vertically and horizontally:
You can also combine the translateX and translateY by using translate(x,y) :
Wrapping Up
In this article, you have learned how to center an image in a div vertically, horizontally, or both.
You will often use the Flexbox method when moving an image to the center because the position method can distort your web page and works very trickily.
You can learn more about the CSS position method here and then more about the flexbox method here.