CSSCSS23 min read

CSS Center Alignment

Learn all common ways to center text and elements in CSS, including margin auto and text alignment, explained simply.

David Miller
January 2, 2026
2.3k62
  Centering is one of the most common beginner problems.
  
  ## Types of centering
  - Text centering
  - Block centering
  - Vertical centering
  
  ## Center text
  ```css
  p {
    text-align: center;
  }
  ```
  
  ## Center block element
  ```css
  .box {
    width: 300px;
    margin: auto;
  }
  ```
  
  ## UI Visualization
  - Text appears in center
  - Box stays centered horizontally
  
  ## Real-world example
  - Login pages
  - Banners
  - Error messages
  
  ## Key rule
  Margin auto works only when width is defined.
  
#CSS#Beginner