Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮     
     ❯   

CSS Tutorial

CSS HOMECSS IntroductionCSS SyntaxCSS SelectorsCSS How ToCSS CommentsCSS ErrorsCSS ColorsCSS BackgroundsCSS BordersCSS MarginsCSS PaddingCSS Height/WidthCSS Box ModelCSS OutlineCSS TextCSS FontsCSS IconsCSS LinksCSS ListsCSS TablesCSS DisplayCSS Max-widthCSS PositionCSS Z-indexCSS OverflowCSS FloatCSS Inline-blockCSS AlignCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS OpacityCSS Navigation BarsCSS DropdownsCSS Image GalleryCSS Image SpritesCSS Attr SelectorsCSS FormsCSS CountersCSS UnitsCSS InheritanceCSS SpecificityCSS !importantCSS Math FunctionsCSS OptimizationCSS AccessibilityCSS Website Layout

CSS Advanced

CSS Rounded CornersCSS Border ImagesCSS BackgroundsCSS ColorsCSS Color KeywordsCSS GradientsCSS ShadowsCSS Text EffectsCSS Custom FontsCSS 2D TransformsCSS 3D TransformsCSS TransitionsCSS AnimationsCSS TooltipsCSS Image StylingCSS Image ModalCSS Image CenteringCSS Image FiltersCSS Image ShapesCSS object-fitCSS object-positionCSS MaskingCSS ButtonsCSS PaginationCSS Multiple ColumnsCSS User InterfaceCSS VariablesCSS @propertyCSS Box SizingCSS Media QueriesCSS MQ Examples

CSS Flexbox

Flexbox IntroFlex ContainerFlex ItemsFlex Responsive

CSS Grid

Grid IntroGrid ContainerGrid ItemsGrid 12-column LayoutCSS @supports

CSS Responsive

RWD IntroRWD ViewportRWD Grid ViewRWD Media QueriesRWD ImagesRWD VideosRWD FrameworksRWD Templates

CSS SASS

SASS Tutorial

CSS Examples

CSS TemplatesCSS ExamplesCSS EditorCSS SnippetsCSS QuizCSS ExercisesCSS WebsiteCSS SyllabusCSS Study PlanCSS Interview PrepCSS BootcampCSS Certificate

CSS References

CSS ReferenceCSS SelectorsCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS At-rulesCSS FunctionsCSS Reference AuralCSS Web Safe FontsCSS AnimatableCSS UnitsCSS PX-EM ConverterCSS ColorsCSS Color ValuesCSS Default ValuesCSS Browser Support

CSSCentering 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 usemargin: auto; ordisplay: flex;.

1. Using margin: auto

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

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

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

Here is a horizontally centered image usingmargin: auto:

Paris

Example

Horizontally centered image using margin: auto:

img {
  display: block;
  margin: auto;
 width: 50%;
}
Try it Yourself »

2. Using display: flex

Another way to center an image horizontally on a page is to usedisplay: 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 awidth for the image. The width of the image must be smaller than the width of the page.

Here is a horizontally centered image usingdisplay: flex:

Paris

Example

Horizontally centered image using display: flex:

div {
  display: flex;
  justify-content: center;
}

img {
  width: 50%;
}
Try it Yourself »


Vertical and Horizontal Centering

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

1. Using display: flex

To display an image that is both vertically and horizontally centered withflexbox, 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 awidth for the image (must be smaller than the width of the container).

Here is a vertically and horizontally centered image with flexbox:

Paris

Example

True centering using display: flex:

div {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 600px;
  border: 1px solid black;
}

img {
 width: 50%;
}
Try it Yourself »

2. Using display: grid

To display an image that is both vertically and horizontally centered withgrid, 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 awidth for the image (must be smaller than the width of the container).

Here is a vertically and horizontally centered image with grid:

Paris

Example

True centering using display: grid:

div {
 display: grid;
  place-items: center;
  height: 600px;
  border: 1px solid black;
}

img {
 width: 50%;
}
Try it Yourself »




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp