HTML Tables
In this page:
<table>
<tr>
<th>Header</th>
<th>Header</th>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
बुनियादी Table Elements (table, tr, td)
एक HTML table बनाने के लिए, आप तीन nested elements का उपयोग करके grid system define करते हैं: table container, table rows के लिए tr, और table data cells के लिए td।
- tr को एक horizontal row container की तरह सोचें, और td को उस row के अंदर side-by-side individual cells की तरह।
- Accepted attributes:
- td colspan — cell कितने columns में फैला है
- td rowspan — cell कितनी rows में फैला है
- td headers — th ids की space-separated list
- table पर border, cellpadding, cellspacing, align, width obsolete हैं; CSS का उपयोग करें
उदाहरण: Basic Table Elements (table, tr, td)
<table>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
</table>
Table Headings (th) Declare करना
Table headings को regular data से अलग दिखना चाहिए। अपने header row के लिए td के बजाय th tag का उपयोग करके, ब्राउज़र स्वचालित रूप से text को bold और center-aligned format में render करेंगे।
- table columns को पढ़ना बहुत आसान बनाने के लिए अपनी पहली row पर th tag का उपयोग करें।
- Accepted attributes:
- scope — row | col | rowgroup | colgroup | auto
- colspan / rowspan — positive integers
- headers — th ids की space-separated list
- abbr — header cell के लिए छोटा label
उदाहरण: Declaring Table Headings (th)
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>30</td>
</tr>
</table>
Cells को Merge करना (colspan और rowspan)
कभी-कभी आपको किसी heading या summary row को कई columns में फैलाने या cells को vertically merge करने की ज़रूरत होती है। colspan attribute cells को columns के आर-पार horizontally merge करता है, और rowspan attribute उन्हें rows के नीचे vertically merge करता है।
- span attributes का उपयोग करते समय table alignment issues से बचने के लिए अपनी cell counts को दोबारा जांचें।
- Accepted attributes:
- colspan — positive integer (default 1)
- rowspan — non-negative integer (default 1; 0 row group के अंत तक फैलता है)
- td और th पर valid
उदाहरण: Merging Cells (colspan and rowspan)
<table>
<tr>
<td colspan="2">Merged across columns</td>
</tr>
<tr>
<td rowspan="2">Merged down rows</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
</tr>
</table>
Semantic Table Structure (thead, tbody, tfoot)
complex tables के लिए, आप rows को semantic containers में group कर सकते हैं: thead (table header), tbody (table body), और tfoot (table footer)। इससे specific visual styles apply करना और लंबी tables को कई pages में print करना आसान हो जाता है।
उदाहरण: Semantic Table Structure (thead, tbody, tfoot)
<table>
<thead>
<tr><th>Item</th></tr>
</thead>
<tbody>
<tr><td>Apple</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td></tr>
</tfoot>
</table>
captions और scopes के साथ Accessible Tables
जब तक आप accessibility helpers न जोड़ें, screen readers के लिए tables को interpret करना मुश्किल हो सकता है। caption tag table के लिए एक visible title description define करता है, और scope attribute assistive devices को बताता है कि कोई th किसी column या row का header है या नहीं।
- अधिकतम accessibility के लिए अपने th tags के अंदर हमेशा scope="col" या scope="row" specify करें।
- Accepted attributes:
- caption — बिना special attributes वाला element; table का पहला child
- scope — row | col | rowgroup | colgroup | auto (th पर)
- headers — th ids की space-separated list (td/th पर)
उदाहरण: Accessible Tables with captions and scopes
<table>
<caption>Monthly Sales</caption>
<tr>
<th scope="col">Month</th>
<th scope="col">Sales</th>
</tr>
</table>
Column और Row Spanning
colspan attribute किसी cell को कई columns में फैलाता है। rowspan attribute किसी cell को कई rows में फैलाता है। दोनों numbers हैं जो दर्शाते हैं कि कितनी cells span करनी हैं।
- layout टूटने से बचने के लिए colspan का उपयोग करते समय अपने columns को ध्यान से count करें।
- Accepted attributes:
- colspan — positive integer (default 1)
- rowspan — non-negative integer (default 1)
- headers — th ids की space-separated list
उदाहरण: Column and Row Spanning
<table>
<tr>
<td colspan="3">Spans 3 columns</td>
</tr>
<tr>
<td rowspan="2">Spans 2 rows</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
Table Structure Elements
header rows के लिए thead, main data rows के लिए tbody, और summary rows के लिए tfoot का उपयोग करें। यह आपकी table को semantic meaning देता है और screen readers को इसे सही तरीके से navigate करने में मदद करता है।
उदाहरण: Table Structure Elements
<table>
<thead>
<tr><th>Product</th></tr>
</thead>
<tbody>
<tr><td>Book</td></tr>
</tbody>
<tfoot>
<tr><td>Summary</td></tr>
</tfoot>
</table>
Colgroup और Col Elements
colgroup और col elements आपको हर एक cell को छुए बिना table के पूरे column पर styling apply करने देते हैं। किसी column की हर td में class जोड़ने के बजाय, आप table के ऊपर एक बार column का वर्णन कर देते हैं।
- जब आप हर row पर CSS classes दोहराए बिना कोई पूरा column, जैसे totals column, highlight करना चाहें, तो colgroup का उपयोग करें।
- Accepted attributes:
- span — covered columns की positive integer संख्या (col और colgroup पर)
- width, align, bgcolor — obsolete; CSS का उपयोग करें
- बाकी सिर्फ global attributes (class, id, style)
उदाहरण: Colgroup and Col Elements
<table>
<colgroup>
<col style="background-color: yellow;">
<col>
</colgroup>
<tr>
<td>Total</td>
<td>100</td>
</tr>
</table>
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- HTML Favicon
- HTML Page Title
- HTML Tables
- HTML Lists
- HTML Block & Inline
- HTML Comments
- HTML Colors
- HTML CSS
- HTML Links
- HTML Images
- HTML RGB Colors
- HTML HEX Colors
- HTML HSL Colors
- HTML Link Colors
- HTML Link Bookmarks
- HTML Background Images
- HTML Table Borders
- HTML Table Sizes
- HTML Table Headers
- HTML Table Padding & Spacing
- HTML Colspan & Rowspan
- HTML Table Styling
- HTML Table Colgroup
- HTML Unordered Lists
- HTML Ordered Lists
- HTML Other Lists / Description Lists