×

CSS Table

CSS table provides better look and feel. There are various properties of CSS table that are given below.
  • border
  • border-collapse
  • padding
  • width
  • height
  • text-align
  • color
  • background-color

CSS Table Border

You can set the border of the table by using table tag th and td. Example
<!DOCTYPE>  
<html>    
<head>  
<style>    
table, th, td {    
    border: 1px solid black;    
}    
</style>    
</head>  
<body>    
<table>    
<tr><th>NAME</th><th>CLASS</th><th>ROLL NO</th></tr>    
<tr><td>Rahul</td><td>PHD</td><td>1</td></tr>    
<tr><td>James</td><td>MBBS</td><td>2</td></tr>    
<tr><td>Frank</td><td>MCA</td><td>3</td></tr>    
<tr><td>Jack</td><td>BCA</td><td>4</td></tr>    
</table>    
</body>  
</html>
Try Now

Collapse Table Borders

You can collapse the table border by using its property. Example
<!DOCTYPE html>  
<html>  
<head>  
<style>  
table {  
    border-collapse: collapse;  
}  
table, td, th {  
    border: 1px solid black;  
}  
</style>  
</head>  
<body>  
<h2> Example borders collapse</h2>  
<table>  
  <tr>  
    <th>Firstname</th>  
    <th>Lastname</th>  
    <th>Address</th>  
  </tr>      
  <tr>  
    <td>ABC</td>  
    <td>DEM</td>  
    <td>Noida</td>  
  </tr>  
  <tr>  
    <td>Lois</td>  
    <td>Griffin</td>  
   <td>Delhi</td>  
   </tr>  
</table>  
</body>  
</html>
Try Now

CSS Table Padding

You can specify the padding of table header and table data by using CSS padding property.
<!DOCTYPE>  
<html>    
<head>  
<style>    
table, th, td {    
    border: 1px solid black;    
    border-collapse: collapse;    
}    
th, td {  
    padding: 7px;  
}  
</style>    
</head>  
<body>    
<table>    
<tr><th>Product_Name</th><th>Price(rs)</th><th>Total_Product</th></tr>    
<tr><td>TV</td><td>35,000</td><td>60</td></tr>    
<tr><td>Computer</td><td>56.000</td><td>80</td></tr>    
<tr><td>Laptop</td><td>3,500</td><td>82</td></tr>    
<tr><td>Cooler</td><td>20,000</td><td>72</td></tr>    
</table>    
</body>  
</html>
Try Now

Responsive table

A responsive table is used to create responsive table. It will display horizontal scroll bar if the screen is too small. You should use only overflow-x: auto properties.
<!DOCTYPE html>  
<html>  
<head>  
<style>  
table {  
    border-collapse: collapse;  
    width: 100%;  
}  
th, td {  
    text-align: left;  
    padding: 8px;  
}  
tr:nth-child(even){background-color: #f2f2f2}  
</style>  
</head>  
<body>    
<div style="overflow-x:auto;">  
  <table>  
    <tr>  
      <th>Reg No</th>  
      <th>Fname</th>  
      <th>Lname</th>  
      <th>Address</th>  
      <th>Pin</th>  
    </tr>  
    <tr>  
       <td>1</td>  
       <td>Jill</td>  
       <td>Smith</td>  
       <td>Noida</td>  
      <td>50</td>  
    </tr>  
    <tr>  
    <td>2</td>  
      <td>Eve</td>  
      <td>Jackson</td>  
       <td>Delhi</td>  
      <td>94</td>  
   </tr>  
    <tr>  
     <td>3</td>  
      <td>Adam</td>  
      <td>Johnson</td>  
       <td>Patna</td>  
      <td>67</td>  
    </tr>  
  </table>  
</div>  
</body>  
</html>
Try Now

CSS Table Properties

Property Description
Border It is used to set the border properties in one declaration.
border-collapse It specifies whether table borders should be collapsed.
border-spacing It specifies the distance between the borders of adjacent cells.
Caption-side It specifies the placement of a table caption.
Empty-cells It specifies the borders and background on empty cells in a table.
Table-layout It is used to set the layout algorithm to be used for a table.

Related Topics

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

CSS Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.

Cascading Style Sheet

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

7 minutes read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Inline

What is CSS? CSS is an acronym for Cascading Style Sheets. CSS is the language that we use to style an HTML document. It specifies how HTML components should appear. CSS...

4 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Word-spacing

Word-spacing The work-spacing property of CSS is applied to manage the space among the words. We can decrease or increase the space among the words by the use of this property. It...

3 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.