Designing Table using CSS

M2-R5.1 · Chapter 4: CSS · 2 min read

Designing Tables Through CSS

HTML में <table> का उपयोग data को rows और columns के रूप में दिखाने के लिए किया जाता है। CSS की मदद से हम table को attractive, readable और professional बना सकते हैं।

CSS से हम table में:

  • Borders लगा सकते हैं।

  • Cells के बीच borders को merge कर सकते हैं।

  • Alternate rows में अलग-अलग background colors दे सकते हैं।

  • Mouse hover करने पर row को highlight कर सकते हैं।

  • Text alignment, spacing और table width को control कर सकते हैं।

इसी प्रक्रिया को Designing Tables Through CSS कहा जाता है।

1. Adding Border to Table

Table में border लगाने के लिए CSS की border property का उपयोग किया जाता है।

Example:

table, th, td {
    border: 1px solid black;
}

यहाँ:

  • table → पूरी table को target करता है।

  • th → Table header cell को target करता है।

  • td → Table data cell को target करता है।

  • 1px → Border की thickness।

  • solid → Border का style।

  • black → Border का color।

Output:

NameAge
Rahul20

2. border-collapse Property

जब table और उसके cells पर borders लगाए जाते हैं, तो उनके बीच अलग-अलग borders दिखाई दे सकते हैं। इन्हें control करने के लिए border-collapse property का उपयोग किया जाता है।

इसकी दो मुख्य values हैं:

A. collapse

Adjacent borders को merge करके single border बना देता है।

table {
    border-collapse: collapse;
}

इससे table साफ और व्यवस्थित दिखाई देती है।

B. separate

यह default value है। इसमें adjacent cells के borders अलग-अलग रहते हैं। Cells के बीच spacing भी रखी जा सकती है।

table {
    border-collapse: separate;
}

याद रखें:

  • collapse → Single merged borders

  • separate → Separate borders

3. Making a Striped Table

Striped table में alternate rows के background colors अलग-अलग होते हैं। इससे अधिक rows वाली table को पढ़ना आसान हो जाता है।

इसके लिए :nth-child() pseudo-class का उपयोग किया जाता है।

Even Rows

tr:nth-child(even) {
    background-color: lightgray;
}

यह even-numbered rows को select करता है।

Odd Rows

tr:nth-child(odd) {
    background-color: lightgray;
}

यह odd-numbered rows को select करता है।

Example:

Row 1

Rahul

Row 2

Aman

Row 3

Riya

Row 4

Priya

Row 5

Neha

याद रखें:

  • even → 2, 4, 6, 8...

  • odd → 1, 3, 5, 7...

ध्यान दें: यदि selector सभी tr elements को target करता है, तो header row भी counting में शामिल होती है। केवल data rows को style करने के लिए tbody tr:nth-child(even) का उपयोग किया जा सकता है।

4. Making a Hoverable Table

Table को interactive बनाने के लिए :hover pseudo-class का उपयोग किया जाता है।

जब mouse pointer किसी row के ऊपर जाता है, तो उस row का background color बदल जाता है।

tr:hover {
    background-color: #ddd;
}

Example:

Demo: किसी row पर pointer ले जाएँ या उसे select करें।

Name

Age

Course

Rahul20BCAAman21B.TechRiya20BCA

यहाँ :hover selector mouse pointer के ऊपर होने पर style लागू करता है। इससे user को यह पहचानने में आसानी होती है कि वह किस row को देख रहा है।

5. Complete Example: Designing a Table Using CSS

इस example में हम border, border-collapse, nth-child() और hover जैसी properties का उपयोग करेंगे।


<!DOCTYPE html>
<html>
<head>
    <title>CSS Table Design</title>

    <style>
        table {
            width: 70%;
            border-collapse: collapse;
            margin: 20px auto;
            font-family: Arial, sans-serif;
        }

        th, td {
            border: 1px solid black;
            padding: 12px;
            text-align: center;
        }

        th {
            background-color: #333;
            color: white;
        }

        tbody tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tbody tr:hover {
            background-color: #ddd;
        }
    </style>
</head>

<body>

    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Course</th>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td>Rahul</td>
                <td>20</td>
                <td>BCA</td>
            </tr>

            <tr>
                <td>Aman</td>
                <td>21</td>
                <td>B.Tech</td>
            </tr>

            <tr>
                <td>Riya</td>
                <td>20</td>
                <td>BCA</td>
            </tr>

            <tr>
                <td>Priya</td>
                <td>22</td>
                <td>MCA</td>
            </tr>
        </tbody>
    </table>

</body>
</html>

Code की Important Properties

Property / Selector

कार्य

width: 70%

Table की width को parent container की 70% रखता है।

border-collapse: collapse

Adjacent borders को merge करता है।

border: 1px solid black

Black solid border लगाता है।

padding: 12px

Cell के content और border के बीच space देता है।

text-align: center

Text को horizontal direction में center करता है।

tbody tr:nth-child(even)

Even-numbered data rows को select करता है।

tbody tr:hover

Hover होने पर data row का style बदलता है।

background-color

Element का background color set करता है।