Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

CSS Tutorial

CSS HOME CSS Introduction CSS Syntax CSS Selectors CSS How To CSS Comments CSS Errors CSS Colors CSS Backgrounds CSS Borders CSS Margins CSS Padding CSS Height/Width CSS Box Model CSS Outline CSS Text CSS Fonts CSS Icons CSS Links CSS Lists CSS Tables CSS Display CSS Max-width CSS Position CSS Z-index CSS Overflow CSS Float CSS Inline-block CSS Align CSS Combinators CSS Pseudo-classes CSS Pseudo-elements CSS Opacity CSS Navigation Bars CSS Dropdowns CSS Image Gallery CSS Image Sprites CSS Attr Selectors CSS Forms CSS Counters CSS Website Layout CSS Units CSS Specificity CSS !important CSS Math Functions CSS Performance CSS Accessibility

CSS Advanced

CSS Rounded Corners CSS Border Images CSS Backgrounds CSS Colors CSS Color Keywords CSS Gradients CSS Shadows CSS Text Effects CSS Web Fonts CSS 2D Transforms CSS 3D Transforms CSS Transitions CSS Animations CSS Tooltips CSS Image Styling CSS Image Centering CSS Image Filters CSS Image Shapes CSS object-fit CSS object-position CSS Masking CSS Buttons CSS Pagination CSS Multiple Columns CSS User Interface CSS Variables CSS @property CSS Box Sizing CSS Media Queries CSS MQ Examples

CSS Flexbox

Flexbox Intro Flex Container Flex Items Flex Responsive

CSS Grid

Grid Intro Grid Columns/Rows Grid Lines Grid Container Grid Item CSS @supports

CSS Responsive

RWD Intro RWD Viewport RWD Grid View RWD Media Queries RWD Images RWD Videos RWD Frameworks RWD Templates

CSS SASS

SASS Tutorial

CSS Examples

CSS Templates CSS Examples CSS Editor CSS Snippets CSS Quiz CSS Exercises CSS Website CSS Syllabus CSS Study Plan CSS Interview Prep CSS Bootcamp CSS Certificate

CSS References

CSS Reference CSS Selectors CSS Combinators CSS Pseudo-classes CSS Pseudo-elements CSS At-rules CSS Functions CSS Reference Aural CSS Web Safe Fonts CSS Animatable CSS Units CSS PX-EM Converter CSS Colors CSS Color Values CSS Default Values CSS Browser Support

CSS Horizontal & 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

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

Also specify a width 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 the width 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, use text-align: center;.

This text is centered.

Example

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

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



Center Align an Image

To center an image, set margin-left and margin-right to auto, and also turn the image into a block element:

Paris

Example

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

Center Align with Flexbox

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

A flex container with both the justify-content and the align-items properties set to center 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

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

A grid container with the place-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 use position: 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 the float 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 use position: absolute; combined with transform: 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 our 2D 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 our terms of use, cookie and privacy policy.

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