Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
"How do I center something in CSS?" is famously the most searched question in all of web development! 🎯
Every beginner has faced this frustration:
text-align: center; to a card, but the card stubbornly stays glued to the left of your monitor.margin: 0 auto; to a button, and nothing happens at all!Centering in CSS is not difficult once you know the Two Golden Rules.
In this lesson, you will master:
text-align: centermargin: 0 autoTo never confuse the two rules, visualize your School Classroom:
+-------------------------------------------------------------------------+ | THE TWO GOLDEN RULES OF CENTERING | +-------------------------------------------------------------------------+ | RULE 1: text-align: center (THE TEACHER'S BLACKBOARD) | | The teacher writes "DAILY HOMEWORK" on the blackboard. The blackboard | | itself spans the entire front wall, but the CHALK WORDS are aligned | | in the center of that board. | | * You apply text-align: center to the PARENT BOARD! | | | | RULE 2: margin: 0 auto (PLACING A TROPHY SHELF IN THE HALLWAY) | | The sports master buys a wooden trophy cabinet that is 4 feet wide. | | To center the cabinet in a 12-foot corridor, they measure 4 feet of | | empty space on the left and 4 feet on the right. | | * You apply margin: 0 auto directly to the CABINET ITSELF! | +-------------------------------------------------------------------------+
text-align: centerUse text-align: center when you want to center:
<h1> - <h6>), and quotes<span>, <strong>, <em>)<a>) and inline-block buttons<img> tags, which are inline by default!)You must apply text-align: center; to the PARENT CONTAINER, not the text or image itself!
+-------------------------------------------------------------+ | .hero-banner (text-align: center;) | | | | [ Welcome to Class 10 ] | | [ Start Your Coding Journey Today ] | | [ Enroll Button ] | | | +-------------------------------------------------------------+
margin: 0 autoUse margin: 0 auto when you want to center an entire container box itself (such as a card, form, or article container):
+-------------------------------------------------------------+ | BROWSER WINDOW (1200px wide) | | | | [ auto margin: 400px ] +-----------------+ [ auto: 400] | | | .profile-card | | | | (width: 400px) | | | +-----------------+ | | | +-------------------------------------------------------------+
margin: 0 auto:display: block (like <div>, <form>), but has ZERO effect on inline elements like <span> or <a>.width: 100%, there is no leftover space to distribute, so auto margins do nothing! Always specify a width or max-width (e.g. max-width: 600px;).Look at this common bug:
Vertical centering historically challenged web designers. Here are two foolproof beginner techniques:
The simplest way to vertically center text inside a banner or header is equal top and bottom padding:
line-height Equal to height (Single-line buttons)If a button or pill badge has a fixed height, setting line-height equal to that height vertically centers a single line of text:
| What do you want to center? | CSS Solution | Where do you apply it? |
|---|---|---|
| Paragraph text, titles, links, icons | text-align: center; | On the Parent container |
| Whole Card, Form, or Article box | margin: 0 auto; (with max-width) | On the Element itself |
An <img> element | text-align: center; OR display: block; margin: 0 auto; | Parent OR image directly |
| Single line button text | line-height equal to height | On the button |
<div> horizontally on the screen?margin: 0 auto; to a <div> that does NOT have a specified width or max-width?<p> and a button <a> inside a header banner, where should text-align: center; be placed?margin: 0 auto; center an inline <span> or <a> tag by default?<img> tag that has been given display: block; width: 200px;?centering-mastery-lab.html.In the next lesson, we will continue your web styling journey with Intro to Flexbox: The Modern 1D Layout Powerhouse (Basic Layout Techniques).
👉 Continue to Next Lesson: Intro to Flexbox: The Modern 1D Layout Powerhouse →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.