HTML Table Colgroup
In this page:
<table>
<colgroup>
<col span="number" style="background-color: colorvalue;">
</colgroup>
<tr><td>Cell</td></tr>
</table>
colgroup से Columns को Group करना
<colgroup> एक structural wrapper है जिसे opening <table> tag के ठीक बाद, किसी भी row से पहले रखा जाता है, जिसमें उस हर column के लिए एक <col> element होता है जिसे आप individually reference या style करना चाहते हैं, बिना किसी single row के markup को छुए।
- colgroup को table का बिल्कुल पहला child रखें, thead या tbody से पहले, वरना ब्राउज़र इसे ignore कर देंगे।
- Accepted attributes:
- span — positive integer; columns की संख्या (जब कोई col children न हों)
- Global attributes (class, id, style)
उदाहरण: Grouping Columns with colgroup
<table>
<colgroup>
<col>
<col>
</colgroup>
<tr>
<td>A</td>
<td>B</td>
</tr>
</table>
col Element से एक Column को Size करना
किसी individual <col> element पर width सेट करना उस पूरे column को एक ही जगह size करता है, न कि हर row में उसी column के नीचे हर single cell पर वही width सेट करना -- wide tables के लिए यह कहीं ज़्यादा maintainable approach है।
- consistent column sizing के लिए col width settings का उपयोग करें जो बाद में rows जोड़े या हटाए जाने पर भी सही बनी रहे।
- Accepted attributes:
- span — positive integer
- style/class — CSS से width सेट करें
- width — obsolete
उदाहरण: Sizing a Column with the col Element
<table>
<colgroup>
<col style="width: 70%;">
<col style="width: 30%;">
</colgroup>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
</table>
span से कई Columns को Span करना
किसी single <col> element पर span attribute उसे एक साथ कई consecutive columns represent करने देता है, हर column के लिए एक अलग <col> लिखने के बजाय -- किसी एक col element पर span="3" एक row में तीन plain col elements लिखने के बराबर है।
- जब कई consecutive columns में exact एक जैसी width या styling हो, तो colgroup markup को छोटा करने के लिए span का उपयोग करें।
- Accepted values:
- span — positive integer (default 1)
- col और colgroup पर valid
उदाहरण: Spanning Multiple Columns with span
<table>
<colgroup>
<col span="3" style="width: 33%;">
</colgroup>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>
किसी Column को Visually Highlight करना
width के अलावा, किसी col element को CSS के साथ target करके पूरे column पर एक साथ background color apply किया जा सकता है, जो किसी महत्वपूर्ण column, जैसे "Total" column, को visually highlight करने के लिए उपयोगी है, बिना उसमें हर individual cell में class जोड़े।
उदाहरण: Highlighting a Column Visually
<table>
<colgroup>
<col>
<col style="background-color: yellow;">
</colgroup>
<tr>
<td>Item</td>
<td>Total</td>
</tr>
</table>
colgroup बनाम Per-Cell Styling कब उपयोग करें
colgroup को column-wide sizing और simple background highlighting के लिए ही बचाकर रखना बेहतर है; ज़्यादा complex per-column styling (जैसे text-align, borders, या font styling) के लिए, td और th cells पर apply किया गया ज़्यादा व्यापक रूप से supported CSS :nth-child(n) selector आमतौर पर ज़्यादा reliable choice है।
उदाहरण: When to Use colgroup vs Per-Cell Styling
<table>
<colgroup>
<col style="width: 50%;">
</colgroup>
<tr>
<td style="text-align: right;">Complex per-cell style</td>
<td>Simple width</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