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

CSSHorizontal & Vertical Align


Center elements
horizontally and vertically


CSS Centering Elements

With CSS, you can center elements (horizontally, vertically, or both) with several methods, depending on the type of element.


Center Align Block Elements

Usemargin: auto;, to horizontally center a block-level element (like <div>).

Also specify awidth for the element, to prevent it from stretching out to the edges of its container.

Note: Center aligning has no effect on block-level elements if thewidth property is not set (or set to 100%).

Below, the <div> element is centered and has a width of 50% (and the remaining space will be split equally between the left and right margins):

This div element is centered.

Example

.center{
  margin: auto;
 width: 50%;
 border: 3px solid green;
  padding: 10px;
}
Try it Yourself »

Center Align Text

To center the text inside a block-level element, usetext-align: center;.

This text is centered.

Example

p {
  text-align: center;
}
Try it Yourself »

Tip: For more examples on how to align text, see theCSS Text chapter.



Center Align an Image

To center an image, setmargin-left andmargin-right toauto, and also turn the image into ablock element:

Paris

Example

img{
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 40%;
}
Try it Yourself »

Center Align with Flexbox

WithCSS flexbox you can center elements, both horizontally and vertically, within a flex container.

A flex container with both thejustify-content and thealign-items properties set tocenter will align the item(s) in the center (in both axis):

I am vertically and horizontally centered.

Example

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  border: 3px solid green;
}
Try it Yourself »

Center Align with Grid

WithCSS grid you can center elements, both horizontally and vertically, within a grid container.

A grid container with theplace-items property set to center, will align the item(s) in the center (in both axis).

I am vertically and horizontally centered.

Example

.center {
  display: grid;
  place-items: center;
  height: 200px;
  border: 3px solid green;
}
Try it Yourself »

Left and Right Align - Using position

Another method for aligning elements is to useposition: absolute;:

Note: Absolute positioned elements are removed from the normal flow, and can overlap other elements.

This <div> element is positioned to the right, with the position: absolute property.

Example

.right{
  position: absolute;
 right: 0px;
  width: 300px;
  border: 3px solid green;
  padding: 10px;
}
Try it Yourself »

Left and Right Align - Using float

Another method for aligning an element to the left or right, is to use thefloat property:

Example

.right{
  float: right;
 width: 300px;
  border: 3px solid green;
  padding: 10px;
}
Try it Yourself »

Center Align with position and transform

If you deal with elements of unknown or dynamic dimensions, it is a common technique to useposition: absolute; combined withtransform: translate(); to center an element:

I am vertically and horizontally centered.

Example

.container p {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
Try it Yourself »

Tip: You will learn more about the transform property in our2D Transforms Chapter.





×

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