📘 Lesson  ·  Lesson 12

Text Align and Decoration

Text Align और Decoration

The Centering Problem — CSS's most-Googled question

"How to center a div" is famously the most-searched CSS question of all time — because there isn't ONE answer. The right method depends on WHAT you're centering: text inside a box, a box on the page, or content both horizontally AND vertically. Once you know which tool fits which situation, centering stops being frustrating. This chapter gives you the complete map — the three methods and exactly when to use each.

Center Text and Inline Content — text-align

.box {
    text-align: center;
}
This text is centered inside its box

For centering TEXT (and other inline content like images or buttons) inside a container, text-align: center is the tool — you met it in the text chapter. Remember the key limitation: it centers the CONTENT inside the box, not the box itself. If you just want words centered in a heading or paragraph, this is all you need. For centering the box itself, read on.

Center a Block Horizontally — margin auto

.box {
    width: 300px;
    margin: 0 auto;    /* top/bottom 0, left/right AUTO */
}
This 60%-wide box is centered with margin auto
The classic technique for centering a block element horizontally. margin: 0 auto means "0 space top and bottom, but AUTO left and right" — and auto tells the browser to split the leftover horizontal space equally on both sides, pushing the box to the center. Crucial requirement: the element MUST have a set width (less than its container), or there's no leftover space to distribute. This is THE way to center a fixed-width container, card, or content column on a page.

Center Anything — flexbox (the modern way)

.container {
    display: flex;
    justify-content: center;   /* horizontal centering */
    align-items: center;       /* vertical centering */
    height: 300px;
}
Perfectly centered both ways
The modern, powerful answer — and it solves the hardest case: centering both horizontally AND vertically at once. Make the container display: flex, then justify-content: center handles horizontal, align-items: center handles vertical. Three lines, and the child is perfectly centered in the middle of the container. This is why flexbox gets its own full chapters ahead — it makes layout tasks that were once painful trivially easy. For any "center this in the middle" need, reach for flexbox.

Vertical Centering — why it used to be hard

For years, vertical centering was CSS's biggest headache — text-align and margin auto only handle horizontal, and old tricks were ugly. Flexbox changed everything: align-items: center centers vertically in one line. That's why the flexbox method above is now the go-to for true center-of-the-screen content (login boxes, hero text, modals). If you only remember one centering method, remember the flexbox trio: display: flex; justify-content: center; align-items: center;.

Exam Corner

Q: How do you center text inside a box? text-align: center;

Q: How do you center a fixed-width block horizontally? margin: 0 auto; (the element needs a set width).

Q: Why must margin auto have a width to work? Without a width the box fills the container, so there's no leftover space to distribute.

Q: How do you center something both horizontally and vertically? Flexbox: display: flex; justify-content: center; align-items: center;

Q: Which flexbox property centers vertically? align-items: center.

Centering की समस्या — CSS का सबसे Google किया सवाल

"How to center a div" मशहूर रूप से हर समय का सबसे ज़्यादा search किया CSS सवाल है — क्योंकि EK जवाब नहीं है. सही method इस पर निर्भर करता है आप KYA center कर रहे हैं: box के अंदर text, page पर box, या content horizontally AUR vertically दोनों. एक बार पता चल जाए कौन-सा tool किस situation में fit है, centering परेशान करना बंद कर देती है. यह chapter पूरा नक्शा देता है — तीन methods और ठीक कब कौन-सा.

Text और Inline Content Center करना — text-align

.box {
    text-align: center;
}
Yeh text apne box ke andar centered hai

Container के अंदर TEXT (और images या buttons जैसे अन्य inline content) center करने को text-align: center tool है — text chapter में मिल चुका. मुख्य सीमा याद रखिए: यह box के अंदर CONTENT center करता है, box को नहीं. अगर बस heading या paragraph में शब्द center चाहिए, इतना ही काफी. Box को खुद center करने के लिए आगे पढ़िए.

Block को Horizontally Center करना — margin auto

.box {
    width: 300px;
    margin: 0 auto;    /* top/bottom 0, left/right AUTO */
}
Yeh 60%-chaudा box margin auto se centered hai
Block element को horizontally center करने की classic तकनीक. margin: 0 auto मतलब "top और bottom 0 space, पर left और right AUTO" — और auto browser को कहता है बची horizontal space दोनों तरफ बराबर बांटे, box को center की ओर धकेलते हुए. ज़रूरी शर्त: element की set width होनी चाहिए (container से कम), वरना बांटने को कोई बची space नहीं. यह page पर fixed-width container, card, या content column center करने का तरीका है.

कुछ भी Center करना — flexbox (modern तरीका)

.container {
    display: flex;
    justify-content: center;   /* horizontal centering */
    align-items: center;       /* vertical centering */
    height: 300px;
}
Dono taraf se perfectly centered
Modern, ताकतवर जवाब — और यह सबसे मुश्किल case हल करता है: horizontally AUR vertically एक साथ center करना. Container को display: flex बनाइए, फिर justify-content: center horizontal संभालता है, align-items: center vertical. तीन lines, और child container के बीचोबीच perfectly centered. इसीलिए flexbox को आगे अपने पूरे chapters मिलते हैं — यह पहले दर्दनाक layout tasks को मामूली आसान बना देता है. किसी भी "इसे बीच में center करो" ज़रूरत के लिए flexbox लीजिए.

Vertical Centering — पहले मुश्किल क्यों थी

सालों तक vertical centering CSS का सबसे बड़ा सिरदर्द था — text-align और margin auto सिर्फ horizontal संभालते हैं, और पुराने tricks भद्दे थे. Flexbox ने सब बदल दिया: align-items: center एक line में vertically center करता है. इसीलिए ऊपर वाला flexbox method अब सच्चे center-of-screen content (login boxes, hero text, modals) के लिए go-to है. अगर सिर्फ एक centering method याद रखें, flexbox तिकड़ी याद रखिए: display: flex; justify-content: center; align-items: center;.

Exam Corner

Q: Box के अंदर text कैसे center करते हैं? text-align: center;

Q: Fixed-width block को horizontally कैसे center करते हैं? margin: 0 auto; (element की set width चाहिए).

Q: margin auto को चलने के लिए width क्यों चाहिए? बिना width box container भर देता है, तो बांटने को बची space नहीं.

Q: कुछ को horizontally और vertically दोनों कैसे center करते हैं? Flexbox: display: flex; justify-content: center; align-items: center;

Q: कौन-सी flexbox property vertically center करती है? align-items: center.
← Back to CSS Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
👁 Live Preview
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का console.log देखने के लिए F12 दबाइए.