CSS Styling Table
CSSÂ Styling Table
In this tutorial we will learn, how to style the HTML Table with CSS. We can style and design the HTML Table to improve the looks of Tables. Here we learn how to style table with CSS properties such as collapsing table border, padding, table aligning text content, layout and width of table, striped rows and coloring the table rows and columns.
If you haven’t read the HTML Table, how to make table in HTML basic knowledge to make table in HTML, you can visit and Learn HTML Table Full Tutorial.
CSS Table Border
In table we can sets the border by using the border CSS property, below the programming example how to set border in HTML Table.
Programming Example.
<!DOCTYPE html>
<html lang="en">
<head>
 <title>CSS Table Border</title>
 <style type="text/css">
   table, td,th{border: 1px
solid red;} Â
</style>
</head>
<body>
<p>Programming Example CSS Border
Table</p>
 <table>
   <thead>
     <tr>
       <th>Roll No</th>
       <th>Name</th>
       <th>Subject</th>
     </tr>
   </thead>
   <tbody>
     <tr>
       <td>07</td>
       <td>Shubham</td>
       <td>BCA</td>
     </tr>
     <tr>
       <td>20</td>
       <td>Mahid</td>
       <td>BCA</td>
     </tr>
     <tr>
       <td>19</td>
       <td>Sonu</td>
       <td>BCA</td>
     </tr>
     <tr>
       <td>25</td>
       <td>Bipul</td>
       <td>BCA</td>
     </tr>
   </tbody>
 </table>
</body>
</html>
|
Output of programming example.
Above programming output, you can see the border in the table, here we use the border property of CSS.
CSS Collapsing Table border
We can collapse the border of table by using the border-collapse: collapse property of CSS, now you see the programming example how to collapse the table border.
Programming Example.
<!DOCTYPE html> <html lang="en"> <head> Â <title>HTML & CSS Table</title> Â <style type="text/css"> Â Â Â table, th, td{ Â Â Â Â Â border: 1px solid blue; Â Â Â } Â Â Â table{ Â Â Â border-collapse: collapse; Â Â Â } Â </style> </head> <body> Â <p>Table padding & text align</p> Â <table> Â Â Â <thead> Â Â Â Â <tr> Â Â Â Â Â Â <th>Roll No</th> Â Â Â Â Â Â <th>Name</th> Â Â Â Â Â Â <th>Subject</th> Â Â Â Â </tr> Â </thead> Â Â Â Â <tbody> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>07</td> Â Â Â Â Â Â Â Â <td>Shubham</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>20</td> Â Â Â Â Â Â Â Â <td>Mahid</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>17</td> Â Â Â Â Â Â Â Â <td>Bipul</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>42</td> Â Â Â Â Â Â Â Â <td>sonu</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â </tr> Â Â Â Â </tbody> Â Â Â </table> </body> </html> |
Output of programming example.
|
Above output of programming, you can see we collapsed the table border by using the CSS property.
CSS Table Padding & Text Alignment
To add space between a table sell’s border and content we need to use the padding CSS property. And we can also set the text alignment inside the table cell using the text-align property of CSS. Now you see below the programming example.
Programming Example.
<!DOCTYPE html> <html lang="en"> <head> Â <title>HTML & CSS Table</title> Â <style type="text/css"> Â Â Â table, th, td{ Â Â Â Â Â border: 2px solid blueviolet;} Â Â Â table{ Â Â Â border-collapse: collapse; Â Â th, td{ padding: 5px; Â Â text-align: center;} </style> </head> <body> Â <p>Table border collapse</p> Â <table> Â Â Â <thead> Â Â Â Â <tr> Â Â Â Â Â Â <th>Roll No</th> Â Â Â Â Â Â <th>Name</th> Â Â Â Â Â Â <th>Subject</th> Â Â Â Â </tr> </thead> Â Â Â Â <tbody> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>07</td> Â Â Â Â Â Â Â Â <td>Shubham</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>20</td> Â Â Â Â Â Â Â Â <td>Mahid</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>17</td> Â Â Â Â Â Â Â Â <td>Bipul</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>42</td> Â Â Â Â Â Â Â Â <td>sonu</td> Â Â Â Â Â Â Â Â <td>BCA</td> Â Â Â Â Â Â </tr> Â Â Â Â </tbody> Â </table> </body> </html> |
Output of programming example.
Above output of programming example, you can see the text align is center because we used text align center property and you also see the space between the table cell, here we used padding 5px.
CSS Table – Layout, Width and Color
We also set the Layout and width of table; it is the default value of table the width of table resizes according to their content size.
To set table layout we need to used table-layout property. Below example we used :nth-child class to individually select the <th> element.
It is generally a good idea to set the table-layout property to fix the table layout, we can set the size of column by setting the table’s heading width.
Programming Example.
<!DOCTYPE html> <html lang="en"> <head> Â <title>HTML & CSS Table</title> Â <style type="text/css"> Â Â Â table, th, td{ Â Â Â Â Â border: 2px solid brown; Â Â Â } Â Â Â td{color: blue;} Â Â Â th{color: red;} Â Â Â table{ Â Â Â Â Â border-collapse: collapse; Â Â Â Â Â table-layout: fixed; Â Â Â Â Â width: 100%; Â Â Â Â Â font-family: cursive; Â Â Â } Â Â Â th, td{ Â Â Â Â Â padding: 5px; Â Â Â Â Â text-align: center; Â Â Â } thead th:nth-child(1){width: 20%;} thead th:nth-child(2){width: 30%;} thead th:nth-child(3){width: 40%;} </style> </head> <body> Â <p>Table Layout & Width</p> Â <table> Â Â Â <thead> Â Â Â Â <tr> Â Â Â Â Â Â <th>Lorem Heading</th> Â Â Â Â Â Â <th>Lorem Heading</th> Â Â Â Â Â Â <th>Lorem Heading</th> Â Â Â Â </tr> </thead> Â Â Â Â <tbody> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>Lorem15</td> Â Â Â Â Â Â Â Â <td>Lorem15</td> Â Â Â Â Â Â Â Â <td>Lorem15</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â </tbody> Â </table> </body> </html> |
Output of programming Example.
Above output of programming example, you can see the color of table heading and table data is changes, and the width of all cells is different.
CSS Table Striped Rows
We can color the rows alternative to make table data easier to read and shown. To do this we need to select both even and odd rows than style them differently. Here we also used the :nth-child pseudo-class.
You see below the example how to do this and how looks table after doing this.
Programming Example.
<!DOCTYPE html> <html lang="en"> <head> Â <title>HTML & CSS Table</title> Â <style type="text/css"> Â Â Â table, th, td{ Â Â Â Â Â border: 2px solid green; Â Â Â } Â Â Â td{color: blue;} Â Â Â th{color: red; Â Â Â Â background-color: lightyellow} Â Â Â table{ Â Â Â Â Â border-collapse: collapse; Â Â Â Â Â width: 100%; Â Â Â Â Â font-family: cursive; Â Â Â } Â Â Â th, td{ Â Â Â Â Â padding: 5px; Â Â Â Â Â text-align: center; Â Â Â } Â Â Â tr:nth-child(odd){background: lightgreen;} Â Â Â tr:nth-child(even){background: lightskyblue;} </style> </head> <body> Â <p>Table Background Color</p> Â <table> Â Â Â <thead> Â Â Â Â Â <tr> Â Â Â Â Â Â Â <th>First Name</th> Â Â Â Â Â Â Â <th>Last Nmae</th> Â Â Â Â Â Â Â <th>Gender</th></tr> </thead> Â Â Â Â <tbody> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â <td>Shubham</td> Â Â Â Â Â Â Â <td>Verma</td> Â Â Â Â Â Â Â <td>Male</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>Sunidhi</td> Â Â Â Â Â Â Â Â <td>Verma</td> Â Â Â Â Â Â Â Â <td>Female</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>Mahid</td> Â Â Â Â Â Â Â Â <td>Sinha</td> Â Â Â Â Â Â Â Â Â <td>Male</td> </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>Samridhi</td> Â Â Â Â Â Â Â Â <td>Kumari</td> Â Â Â Â Â Â Â Â <td>Female</td> Â Â Â Â Â Â </tr> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â <td>Bipul</td> Â Â Â Â Â Â Â Â <td>Sahu</td> Â Â Â Â Â Â Â Â <td>Male</td> Â Â Â Â Â Â </tr> Â Â Â </tbody> Â </table> </body> </html> |
Output of programming example,
Above you see the programming example of colorful table we used the even & odd property to use the color in table.
In this tutorial you learned the CSS Table Styling, how to style the table such as Table Border, Collapsing Border, Table Padding, Table content text alignment, Layout and Width of Table, Background and Color of table, and Striped Rows of table. I hope you read these all property and see the programming example of all properties. If you have any doubt in this CSS Table Tutorial, you ask in the comment section.
Also Read-
2 Comments
zoritoler imol · June 30, 2022 at 11:46 am
Merely wanna say that this is handy, Thanks for taking your time to write this.
Julienne Dukeshier · September 23, 2022 at 12:15 pm
Excellent read, I just passed this onto a friend who was doing some research on that. And he just bought me lunch as I found it for him smile Therefore let me rephrase that: Thanks for lunch!