How To Style a Table with CSS? Last Updated : 20 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A table organizes data in rows and columns, making information easy to read and compare, like a spreadsheet or chart. To style a table with CSS, use properties like border for cell borders, padding for spacing, text-align for alignment, and background-color to color rows or headers for clarity.Approach to Style Table with CSSUse the <link> tag within the <head> section of your HTML document to link an external CSS file.Use the <table>, <thead>, <tbody>, <tr>, <th>, and <td> tags to structure your table and its content.Define styles in your CSS file (style.css) to customize the appearance of the table, headers (<th>), and data cells (<td>).Example 1: Styling a table using External CSS. HTML <link rel="stylesheet" href="index.css"> <body> <table> <thead> <tr> <th class="cl1">Firstname</th> <th class="cl2">Lastname</th> <th class="cl3">Age</th> </tr> </thead> <tbody> <tr> <td class="cl4">Bahadur</td> <td class="cl5">Lodhi</td> <td class="cl6">20</td> </tr> <tr> <td class="cl7">Shubham</td> <td class="cl8">Ji</td> <td class="cl9">25</td> </tr> <tr> <td class="cl10">Michael</td> <td class="cl11">John</td> <td class="cl12">40</td> </tr> <tr> <td class="cl13">Aman</td> <td class="cl14">Sign</td> <td class="cl15">20</td> </tr> </tbody> </table> </body> CSS /* Write CSS Here */ table { width: 40%; margin: 0 auto; } body { padding-top: 8%; } th, td { border: 1px solid #ddd; padding: 10px; text-align: center; } th { background-color: #f2f2f2; } .cl1, .cl12 { background-color: chartreuse; } .cl3, .cl4 { background-color: darkgoldenrod; } .cl5, .cl6 { background-color: darkseagreen; } .cl7, .cl14 { background-color: deepskyblue; } .cl9, .cl10 { background-color: rgb(251, 251, 128); } .cl11, .cl2 { background-color: crimson; } .cl13, .cl8 { background-color: darkmagenta; } .cl15 { background-color: rgb(86, 79, 146); } OutputStyled TableExample 2: Style a table using Internal CSS. HTML <!DOCTYPE html> <html lang="en"> <head> <style> /* Style for the table */ table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid black; padding: 8px; text-align: left; } /* Header styling */ th { background-color: #f2f2f2; font-weight: bold; } /* Color the rows */ tr:nth-child(1) { background-color: #cfe2f3; } tr:nth-child(2) { background-color: #d9ead3; } tr:nth-child(3) { background-color: #f9cb9c; } tr:nth-child(4) { background-color: #f4cccc; } tr:hover { background-color: #ddd; } </style> </head> <body> <table> <thead> <tr> <th>Employee ID</th> <th>Name</th> <th>Department</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>001</td> <td>John Doe</td> <td>Marketing</td> <td>[email protected]</td> </tr> <tr> <td>002</td> <td>Jane Smith</td> <td>Development</td> <td>[email protected]</td> </tr> <tr> <td>003</td> <td>Sam Brown</td> <td>Human Resources</td> <td>[email protected]</td> </tr> <tr> <td>004</td> <td>Lisa White</td> <td>Finance</td> <td>[email protected]</td> </tr> </tbody> </table> </body> </html> OutputStyled Table Comment More infoAdvertise with us Next Article How to create a table row using HTML? B bahadurl91x7 Follow Improve Article Tags : HTML CSS-Questions HTML Table Similar Reads How to Center a Table with CSS ? Here are different ways to center table in CSS.1. Center a Table using margin auto propertyThe CSS margin is used to set the space around the element. The margin auto enables dynamic and equal margins from both sides and center the element. Syntaxtable { margin: auto;}By setting the margin auto the 2 min read How to create a zebra stripped table with CSS ? Creating a zebra-striped table with CSS makes your table easier to read by adding alternating background colors to the rows. This design helps to visually separate the rows, making it simpler for users to follow the data.These are the following methods to create a zebra-stripped table with CSS Table 2 min read How to Create Table in HTML with Border without CSS ? Creating tables in HTML remains an Important skill for over 80% of beginner web developers, especially when it comes to displaying structured data clearly. Tables help organize information into rows and columns, enhancing readability and layout. In this article, weâll explore how to create a basic H 2 min read How to create a table row using HTML? Define a row in a table by using a <tr> tag in a document. This tag is used to define a row in an HTML table. The tr element contains multiple th or td elements. Syntax:<tr> ... </tr>Example: In this example, a table row in HTML5, utilizes the <tr> tag within the <tbody 1 min read How To Create a Responsive Table in CSS ? A responsive table in CSS automatically changes to fit any screen size. It stays easy to read on small screens, like phones, by scrolling or adjusting columns as needed. To make a responsive table in CSS, place it inside a <div> with overflow-x: auto;. This lets the table scroll sideways on sm 3 min read How create table without using <table> tag ? HyperText Markup Language (HTML) is the standard markup language used to create web pages. HTML allows table creation using the <table> tag. However, tables can also be created in HTML without using <table> tag with the help of Cascading Style Sheet (CSS). CSS is a mechanism for adding s 3 min read Like