Sunday, 20 September 2015

CSS Table

CSS Table

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:
  • border
  • border-collapse
  • padding
  • width
  • height
  • text-align
  • color
  • background-color

CSS Table Border

We can set border for the table, th and td tags using the CSS border property.
  1. <style>  
  2. table, th, td {  
  3.     border: 1px solid black;  
  4. }  
  5. </style>  

Output:
First_NameLast_NameMarks
SonooJaiswal60
JamesWilliam80
SwatiSironi82
ChetnaSingh72

CSS Table Border Collapse

By the help of border-collapse property, we can collapse all borders in one border only.
  1. <style>  
  2. table, th, td {  
  3.     border: 2px solid black;  
  4.     border-collapse: collapse;  
  5. }  
  6. </style>  

Output:
NameLast NameMarks
SonooJaiswal60
JamesWilliam80
SwatiSironi82
ChetnaSingh72

CSS Table Padding

We can specify padding for table header and table data using the CSS padding property.
  1. <style>  
  2. table, th, td {  
  3.     border: 1px solid black;  
  4.     border-collapse: collapse;  
  5. }  
  6. th, td {  
  7.     padding: 10px;  
  8. }  
  9. </style>  

Output:
NameLast NameMarks
SonooJaiswal60
JamesWilliam80
SwatiSironi82
ChetnaSingh72

CSS Table: Styling even and odd cells

We can style even and odd table cells for better look and feel. In this code, we are displaying different background colors on even and odd cells. Moreover, we have changed the background-color and color of <th> tag.
CSS code:
  1. <style>  
  2. table, th, td {  
  3.     border: 1px solid black;  
  4.     border-collapse: collapse;  
  5. }  
  6. th, td {  
  7.     padding: 10px;  
  8. }  
  9. table#alter tr:nth-child(even) {  
  10.     background-color: #eee;  
  11. }  
  12. table#alter tr:nth-child(odd) {  
  13.     background-color: #fff;  
  14. }  
  15. table#alter th {  
  16.     color: white;  
  17.     background-color: gray;  
  18. }  
  19. </style>  

Output:
CSS table even and odd

No comments:

Post a Comment