bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/CSS/Advanced Styling
CSS•Advanced Styling

CSS Centering Images

Centering Images

With CSS, you can center images with several methods.

An image can be centered horizontally, vertically, or both.

Center an Image Horizontally

To display a horizontally centered image, we can use margin: auto; or display: flex; .

Using margin: auto

One way to center an image horizontally on a page is to use margin: auto .

Since the <img> element is an inline element by default (and margin: auto does not have any effect on inline elements) we must convert the image to a block element, with display: block .

In addition, we have to define a width . The width of the image must be smaller than the width of the page.

Here is a horizontally centered image using margin: auto :

Example

Formatted code
img {
  display: block;
  margin: auto;
  width: 50%;
}

Live preview

Using display: flex

Another way to center an image horizontally on a page is to use display: flex .

Here, we put the <img> element inside a <div> container.

We add the following CSS to the div container:

  • display: flex
  • justify-content: center (centers the image horizontally in the div container)

Then, we set a width for the image. The width of the image must be smaller than the width of the page.

Here is a horizontally centered image using display: flex :

Example

Formatted code
div {
  display: flex;
  justify-content: center;
}
img {
  width: 50%;
}

Live preview

Vertical and Horizontal Centering

To display an image that is both vertically and horizontally centered (true centering), we can use display: flex; or display: grid; .

Using display: flex

To display an image that is both vertically and horizontally centered with flexbox , we use a combination of:

  • display: flex;
  • justify-content: center; (centers the image horizontally in the container)
  • align-items: center; (centers the image vertically in the container)
  • height: 600px; (the height of the container)

Here, we also put the <img> element inside a <div> container.

Then, we set a width for the image (must be smaller than the width of the container).

Here is a vertically and horizontally centered image with flexbox:

Example

Formatted code
div {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 600px;
  border: 1px solid
  black;
}
img {
  width: 50%;
}

Live preview

Using display: grid

To display an image that is both vertically and horizontally centered with grid , we use a combination of:

  • display: grid;
  • place-items: center; (centers the image horizontally and vertically in the container)
  • height: 600px; (the height of the container)

Here, we also put the <img> element inside a <div> container.

Then, we set a width for the image (must be smaller than the width of the container).

Here is a vertically and horizontally centered image with grid:

Example

Formatted code
div {
  display: grid;
  place-items: center;
  height: 600px;
  border: 1px solid
  black;
}
img {
  width: 50%;
}

Live preview

Previous

CSS Responsive Modal Images

Next

CSS Image Filter Effects

This chapter

Overview
53

Lessons

154m

Read time

1. CSS Rounded Corners2. Sass Tutorial3. CSS Border Images4. CSS Multiple Backgrounds5. CSS Background Size6. CSS Background Origin7. CSS Background Clip8. CSS Colors9. CSS Color Keywords10. CSS Gradients11. CSS Radial Gradients12. CSS Conic Gradients13. CSS Shadow Effects14. CSS Box Shadow15. CSS Text Effects16. CSS Custom Fonts17. CSS 2D Transforms18. CSS 2D Transform Scale19. CSS 2D Transform Skew / Matrix20. CSS 3D Transforms21. CSS Transitions22. CSS Transition Timing23. CSS Animations24. CSS Animation Timing25. CSS Animation Properties26. CSS Tooltip27. CSS Tooltip Arrows28. CSS Styling Images29. CSS Image Effects30. CSS Image Hover Overlays31. CSS Responsive Modal Images32. CSS Centering Images33. CSS Image Filter Effects34. CSS Image Shapes35. CSS object-fit Property36. CSS object-position Property37. CSS Masking38. CSS Gradient Mask Layers39. CSS SVG Mask Layers40. CSS Styling Buttons41. CSS Button Hover Effects42. CSS Button Groups43. CSS Pagination Styles44. CSS Multiple Columns45. CSS Column Rules46. CSS User Interface47. CSS Variables - var() Function48. CSS Overriding Variables49. CSS Variables and JavaScript50. CSS Variables in Media Queries51. CSS @property Rule52. CSS Box Sizing53. CSS Media Queries

On this page

Centering ImagesCenter an Image HorizontallyUsing margin: autoUsing display: flexVertical and Horizontal CenteringUsing display: flexUsing display: grid