Text Align and Decoration
Text Align और Decoration
The Centering Problem — CSS's most-Googled question
Center Text and Inline Content — text-align
.box {
text-align: center;
}
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 */
}
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;
}
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
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 किया सवाल
Text और Inline Content Center करना — text-align
.box {
text-align: center;
}
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 */
}
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;
}
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
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.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.console.log देखने के लिए F12 दबाइए.