← Back to HTML Course | Chapter 3: Page Structure | Lesson 8 of 12

HTML Layout

सोचिए आप एक newspaper पढ़ने की कोशिश कर रहे हैं, जिसमें कोई columns, borders, headers, या sections नहीं हैं—बस ऊपर से नीचे तक एक लगातार text की दीवार है। इसे navigate करना नामुमकिन होगा। HTML layouts किसी page पर structural columns की तरह काम करते हैं। अपने webpage को headers, footers, main content grids, और sidebars जैसे defined zones में divide करके, आप अपने layouts को organized रखते हैं। इन zones को बनाने के लिए semantic tags का उपयोग करना search engines और screen readers को बताता है कि आपकी site की structure बिल्कुल कैसी है।
Syntax
markup
<header>Header</header>
<nav>Navigation</nav>
<main>
  <section>Content</section>
  <aside>Sidebar</aside>
</main>
<footer>Footer</footer>

Semantic Headers और Footers

header element introductory content को represent करता है, जिसमें आमतौर पर logos, search inputs, या navigation menus होते हैं। footer element closing content, जैसे copyright info, contacts, या system links को mark करता है।

Note: अपने pages की शुरुआत और अंत को साफ तरीके से define करने के लिए header और footer tags इस्तेमाल करें।
Warning: अपने पूरे page को header tag के अंदर wrap न करें; इसे सख्ती से introductory elements के लिए ही उपयोग करें।

उदाहरण: Semantic Headers and Footers

markup
<header>Site Logo</header>
<footer>&copy; 2024 My Site</footer>

Semantic Navigation (nav)

nav element एक खास container है जो website की मुख्य navigation links को group करने के लिए इस्तेमाल होता है। इस tag का इस्तेमाल करने से search engine crawlers और screen readers को संकेत मिलता है कि यह block page routing को handle करता है।

Note: navigation को साफ रखने के लिए nav container के अंदर सिर्फ अपनी मुख्य menu links रखें।
Warning: single, isolated footnotes या help links को nav element में wrap न करें, क्योंकि इससे assistive devices confuse हो सकते हैं।

उदाहरण: Semantic Navigation (nav)

markup
<nav>
  <a href="/">Home</a>
  <a href="/about">About</a>
</nav>

article और section से Main Content को Structure करना

main tag आपके page के unique, core topic को represent करता है। इसके अंदर, आप content को semantically group कर सकते हैं, article tag (blog posts जैसे standalone, self-contained elements के लिए) और section tag (जुड़े हुए features को group करने के लिए) का इस्तेमाल करके।

Note: व्यवस्थित hierarchy बनाए रखने के लिए हर section container में हमेशा एक descriptive heading शामिल करें।
Warning: search engine bots को confuse होने से बचाने के लिए एक page पर main tag का उपयोग कभी भी एक से ज्यादा बार न करें।

उदाहरण: Structuring Main Content with article and section

markup
<main>
  <article>Blog post content</article>
  <section>Related features</section>
</main>

Sidebars और Callouts (aside)

aside element ऐसे content को represent करता है जो main page content से थोड़ा-बहुत related होता है, जैसे popular tools दिखाने वाला sidebar, quick tips, या callout advertisements।

Note: अपने मुख्य reading flow को बिखरने से बचाने के लिए secondary links और tips के लिए aside tag इस्तेमाल करें।
Warning: essential page actions को aside tag के अंदर न रखें, क्योंकि fast navigation के दौरान screen readers इसे skip कर सकते हैं।

उदाहरण: Sidebars and Callouts (aside)

markup
<aside>Popular tools this week</aside>

Generic Dividers बनाम Semantic Tags

header, nav, और article जैसे semantic HTML tags आने से पहले, developers को हर section के लिए custom class names (जैसे div class="header") वाले generic div containers इस्तेमाल करने पड़ते थे।

हालांकि standard divs layout cards को style करने के लिए अभी भी बेहद उपयोगी हैं, लेकिन accessibility और search engine optimization के लिए semantic tags का इस्तेमाल करना कहीं बेहतर है।

Note: अपने layout को structure करने के लिए semantic elements इस्तेमाल करें, और standard div elements को visual styling और spacing के लिए बचाकर रखें।
Warning: semantic containers की जगह plain div tags का ज्यादा उपयोग आपकी site की SEO ranking पर नकारात्मक असर डाल सकता है।

उदाहरण: Generic Dividers vs. Semantic Tags

markup
<div class="header">Old approach</div>
<header>Semantic approach</header>

Layout के लिए CSS Box Model

हर HTML element चार layers से बना एक rectangular box होता है: खुद content, content के चारों ओर padding, padding के चारों ओर एक border, और border के बाहर margin जो इसे दूसरे elements से अलग करता है।

किसी भी layout को सही तरीके से बनाने के लिए इस model को समझना सबसे बुनियादी बात है।

Note: box-sizing: border-box को globally सेट करें, ताकि padding और border आपके तय किए गए width के अंदर ही शामिल हों, अचानक उसमें जुड़ने के बजाय।
Warning: यह भूल जाना कि margin border के बाहर होता है, layouts के उम्मीद से ज्यादा चौड़े या spaced out दिखने का एक बहुत आम कारण है।

उदाहरण: CSS Box Model for Layout

markup
<div style="padding: 10px; border: 1px solid black; margin: 20px;">
  Content
</div>

Flexbox की बुनियादी बातें

Flexbox एक CSS layout method है जो items को एक ही row या column में arrange करने और उनके बीच space को automatically बांटने के लिए बनाया गया है।

किसी container पर display: flex सेट करने से उसके direct children flexible items बन जाते हैं जो उस एक दिशा में grow, shrink, या align हो सकते हैं।

Note: main direction में spacing control करने के लिए justify-content और दूसरी direction में alignment control करने के लिए align-items इस्तेमाल करें।
Warning: Flexbox one-dimensional layouts — rows या columns — के लिए डिज़ाइन किया गया है, न कि पूरी two-dimensional grids के लिए जिसमें rows और columns दोनों साथ align हों।

उदाहरण: Flexbox Basics

markup
<div style="display: flex; justify-content: space-between;">
  <div>Item 1</div>
  <div>Item 2</div>
</div>

CSS Grid की बुनियादी बातें

CSS Grid two-dimensional layouts के लिए बना एक layout method है, जिससे आप rows और columns दोनों एक साथ define कर सकते हैं।

किसी container पर display: grid के साथ grid-template-columns सेट करने से आप items को एक precise grid structure में रख पाते हैं, जिसे flexbox आसानी से replicate नहीं कर सकता।

Note: percentages खुद calculate किए बिना तीन बराबर columns बनाने के लिए grid-template-columns: repeat(3, 1fr) इस्तेमाल करें।
Warning: Grid और flexbox अलग-अलग समस्याएं solve करते हैं — जब आपको सिर्फ एक simple single row चाहिए हो, तब grid का उपयोग करना अक्सर अनावश्यक complexity जोड़ देता है।

उदाहरण: CSS Grid Basics

markup
<div style="display: grid; grid-template-columns: repeat(3, 1fr);">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

सही Layout Method चुनना

एक row या column में items को arrange करने के लिए, जैसे कोई navigation bar या buttons की list, आमतौर पर flexbox सबसे सरल विकल्प होता है।

एक पूरे page structure को rows और columns के साथ लेआउट करने के लिए, जैसे sidebar, header, और main content area वाला कोई dashboard, CSS Grid आमतौर पर ज्यादा स्पष्ट और शक्तिशाली होता है।

Note: दोनों को एक साथ इस्तेमाल करना पूरी तरह सामान्य और अक्सर सबसे अच्छा है — पूरे page structure के लिए grid, और हर grid area के अंदर items को align करने के लिए flexbox।
Warning: केवल flexbox का उपयोग करके एक complex two-dimensional dashboard layout को force करने की कोशिश अक्सर fragile, hacky CSS की ओर ले जाती है जो अलग-अलग screen sizes पर टूट जाती है।

उदाहरण: Choosing the Right Layout Method

markup
<div style="display: grid; grid-template-columns: 200px 1fr;">
  <nav>Sidebar</nav>
  <div style="display: flex;">
    <button>Save</button>
    <button>Cancel</button>
  </div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.