← Back to HTML Course | Chapter 2: Text, Formatting & Media | Lesson 22 of 26

HTML Table Styling

एक bare, unstyled HTML table functional तो है लेकिन visually भूलने योग्य है -- plain white grid पर plain black text। Table styling borders, padding, alternating row colors, hover effects, और typography choices को एक single cohesive design में एक साथ लाती है, एक plain data grid को किसी ऐसी चीज में बदल देती है जो बाकी की polished website से मेल खाती है।
Syntax
markup
<style>
  table { border-collapse: collapse; }
  tr:nth-child(even) { background-color: colorvalue; }
</style>

एक Clean Base Style बनाना

एक ठोस table styling foundation grid lines merge करने के लिए border-collapse: collapse, breathing room के लिए हर cell पर consistent padding, और एक neutral border color से शुरू होती है -- सिर्फ ये तीन basics ही किसी raw, unstyled table को एक clean और professional-looking चीज़ में बदल देते हैं।

Note: color और effects लगाने से पहले हर table design इसी clean base (collapse, padding, subtle border) से शुरू करें।
Warning: अन्य styling जोड़ने से पहले border-collapse को skip करना बाद के border-radius और border-color choices को असंगत दिखा सकता है।

उदाहरण: Building a Clean Base Style

markup
<style>
  table {
    border-collapse: collapse;
  }
  td, th {
    padding: 10px;
    border: 1px solid #ccc;
  }
</style>

Header Row को Style करना

एक अलग header style, आमतौर पर white या dark contrasting text के साथ एक solid background color, readers को तुरंत बताती है कि labels कहां हैं और data कहां से शुरू होता है, जिससे पूरी table को एक नज़र में समझना कहीं तेज़ हो जाता है।

Note: एक header background color चुनें जिसमें उसके text के मुकाबले मज़बूत contrast हो (contrast ratio चेक करें) ताकि labels सभी users के लिए readable रहें।
Warning: text और background के बीच अपर्याप्त contrast वाली header style column labels को पढ़ना मुश्किल बना सकती है, खासकर visually impaired users के लिए।

उदाहरण: Styling the Header Row

markup
<style>
  th {
    background-color: #333;
    color: white;
  }
</style>
<table>
  <tr><th>Name</th></tr>
</table>

Rows में Zebra Striping

:nth-child(even) या :nth-child(odd) selector से apply की गई हर दूसरी row पर alternating background colors एक "zebra stripe" pattern बनाते हैं जो किसी wide table में गलती से पड़ोसी row पर कूदे बिना एक row को follow करना बहुत आसान बना देता है।

Note: zebra stripe colors को subtle रखें (एक बहुत हल्का gray अच्छा काम करता है) ताकि वे असल data से मुकाबला किए बिना आंख को guide करें।
Warning: आपस में बहुत ज्यादा contrast वाले दो zebra stripe colors table को genuinely scan करने में आसान होने के बजाय busy महसूस करा सकते हैं।

उदाहरण: Zebra Striping Rows

markup
<style>
  tr:nth-child(even) {
    background-color: #f2f2f2;
  }
</style>
<table>
  <tr><td>Row 1</td></tr>
  <tr><td>Row 2</td></tr>
</table>

Interactive Tables के लिए Hover Effects

tr:hover पर एक subtle background color बदलाव users को स्पष्ट visual feedback देता है कि उनका mouse अभी किस row के ऊपर है, जो कई rows वाली wide tables पर वाकई उपयोगी बन जाता है जहां horizontally पढ़ते हुए track खोना आसान होता है।

Note: highlight को अचानक flash की बजाय smooth महसूस कराने के लिए background-color पर एक छोटे CSS transition के साथ hover effect combine करें।
Warning: zebra stripe color के बहुत करीब का hover color striped rows पर विशेष रूप से hover effect को लगभग invisible बना सकता है।

उदाहरण: Hover Effects for Interactive Tables

markup
<style>
  tr:hover {
    background-color: #ddd;
    transition: background-color 0.2s;
  }
</style>
<table>
  <tr><td>Hover me</td></tr>
</table>

Responsive Table Design

narrow mobile screens पर, अगर सिर्फ सिकुड़ने के लिए छोड़ दिया जाए तो एक wide multi-column table वाकई unusable हो सकती है।

आम responsive strategies में एक contained wrapper के अंदर horizontal scroll allow करना, या छोटे breakpoints पर CSS के साथ cells को vertically stack करके restructure करना शामिल है ताकि हर row एक cramped grid की बजाय एक labeled card जैसी पढ़ी जाए।

Note: table design को finished मानने से पहले किसी भी styled table को आम mobile widths (375px, 414px) पर test करें।
Warning: बिना किसी responsive strategy के shrink होने पर मजबूर table अपने text और numbers को इस हद तक compress कर सकती है कि वे phones पर unreadable हो जाएं।

उदाहरण: Responsive Table Design

markup
<div style="overflow-x: auto;">
  <table>
    <tr><td>Wide table content</td></tr>
  </table>
</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. #}

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.