Search
Close this search box.
Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

1. Table Borders (border, border-collapse, border-spacing):

Definition:

Sets the border properties of the table.

Syntax:

table {
border: 1px solid #ddd; /* Border around the table */
border-collapse: collapse; /* Collapse border spacing into a single border */
border-spacing: 0; /* No space between table cells */
}

 

2. Table Cell Padding and Spacing (padding, cellpadding, cellspacing):

Definition:

Sets padding and spacing for cells in the table.

Syntax:

td, th {
padding: 8px; /* Padding inside each cell */
}

/* Deprecated, but you might still encounter it in older code: */
table {
cellpadding: 8px; /* Padding inside each cell */
cellspacing: 0; /* No space between table cells */
}

3. Table Width and Height (width, height):

Definition:

Sets the width and height of the table.

Syntax:

table {
width: 100%; /* Table takes up 100% of the container width */
height: 300px; /* Fixed height for the table */
}

4. Table Header and Footer Styles (thead, tfoot, th):

Definition:

Styles the table header and footer.

Syntax:

thead, tfoot {
background-color: #f2f2f2; /* Background color for header and footer */
}

th {
font-weight: bold; /* Bold text for header cells */
}

5. Table Row Styles (tr):

Definition:

Styles the table rows.

Syntax:

tr {
background-color: #f9f9f9; /* Background color for alternate rows */
}

 

6. Table Cell Styles (td):

Definition:

Styles the individual table cells.

Syntax:

td {
border: 1px solid #ddd; /* Border around each cell */
}

 

7. Text Alignment in Cells (text-align):

Definition:

Sets the horizontal alignment of text within cells.

Syntax:

td, th {
text-align: center; /* Center align text in cells */
}

 

8. Vertical Alignment in Cells (vertical-align):

Definition:

Sets the vertical alignment of content within cells.

Syntax:

td, th {
vertical-align: middle; /* Align content vertically in the middle */
}

 

9. Table Caption Styles (caption):

Definition:

Styles the table caption.

Syntax:

caption {
font-size: 1.2em; /* Set the font size for the caption */
color: #333; /* Text color for the caption */
}

 

Now Let’s Create a Table using CSS properties:

Example:

<!DOCTYPE html>
<html lang=”en”>
<head>
  <meta charset=”UTF-8″>
  <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
  <style>
    table {
      width: 100%;
      border-collapse: collapse;
      margin-bottom: 20px;
    }
    th, td {
      border: 1px solid #ddd;
      padding: 8px;
      text-align: left;
    }
    th {
      background-color: #f2f2f2;
    }
    tr:hover {
      background-color: #f5f5f5;
    }
  </style>
  <title>Computer Science College Students</title>
</head>
<body>
  <h2>Computer Science College Students</h2>
  <!– Table for Students –>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Major</th>
        <th>Year</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>John Doe</td>
        <td>Computer Science</td>
        <td>2nd Year</td>
      </tr>
      <tr>
        <td>2</td>
        <td>Jane Smith</td>
        <td>Computer Science</td>
        <td>3rd Year</td>
      </tr>
      <tr>
        <td>3</td>
        <td>Alice Johnson</td>
        <td>Information Technology</td>
        <td>1st Year</td>
      </tr>
      <!– Add more rows for additional students –>
    </tbody>
  </table>
  <!– Your page content goes here –>
</body>
</html>
 

Output: