CSS Table

The CSS table properties are used to style and control the layout of HTML tables. This includes setting borders, spacing, alignment, and appearance of rows, columns, and cells. With CSS, you can create visually appealing and responsive tables for your webpage.

We can apply style on HTML tables for better look and feel. There are some CSS properties that are widely used in designing table using CSS:

Key Points on CSS Tables:

Syntax for CSS Table:

Syntax Example

/* Basic table styling */
        table {
            border-collapse: collapse;
            width: 100%;
        }
        
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
        }
        
        th {
            background-color: #f2f2f2;
            text-align: left;
        }

Examples of CSS Tables:

Code Example: Basic Table

table {
            border-collapse: collapse;
            width: 100%;
            margin: 10px 0;
        }
        
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: center;
        }
        
        th {
            background-color: lightblue;
        }

Output

Header 1 Header 2 Header 3
Row 1 Col 1 Row 1 Col 2 Row 1 Col 3
Row 2 Col 1 Row 2 Col 2 Row 2 Col 3

Code Example: Zebra-Striped Table

table {
            border-collapse: collapse;
            width: 100%;
        }
        
        th, td {
            border: 1px solid black;
            padding: 10px;
        }
        
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        
        th {
            background-color: lightblue;
            font-weight: bold;
        }

Output

Header 1 Header 2 Header 3
Row 1 Col 1 Row 1 Col 2 Row 1 Col 3
Row 2 Col 1 Row 2 Col 2 Row 2 Col 3

Common Table Properties:

CSS tables are a powerful tool for organizing and displaying tabular data in a structured and visually appealing format. Customize table properties to fit the design and functional requirements of your webpage.