Javatpoint Logo
Javatpoint Logo

CSS Table Style

The CSS table styles increase the readability of the table information and add appeal to the otherwise plain and unattractive HTML tables. An HTML table can be styled in a variety of ways. The table borders, row-column heights and widths, font colors, and many more movements, including hover effects, are some of them.

To appropriately organize the data into rows and columns or even a more complicated structure, a table in CSS is used to apply the different stylistic characteristics to the HTML Table components. Tables are frequently used in data analysis, research, and communication. To display the layout of the table, use the CSS table-layout attribute. This attribute controls the algorithm used to arrange the cells, rows, and columns of a table.

What does CSS's Table Style Mean?

Let's examine a straightforward HTML table that only contains data, i.e., without any CSS style.

CSS Table Style

As we can also see, the columns need to be better aligned; there are no borders to indicate the boundaries of the columns, and they are difficult to read. We want to see something else when we visit a website.

Let's now apply a small amount of CSS table styles and see the result.

How do We Style a Table in CSS?

We first need to develop a basic HTML table and comprehend what the various HTML components connected to an HTML table signify before we go on to decorating tables using CSS.

Output

CSS Table Style

A very simple HTML table without any CSS styling is displayed in the output:

HTML Table Tags

  1. <tr>: It denotes rows
  2. <td>: It is used to produce data cells
  3. <th>: It is used to add table headers
  4. <caption>: It is used for captioning the table
  5. <thead>: It adds a unique header to the table
  6. <tbody>: It displays the table's main body
  7. <tfoot>: It makes a unique footer only for the table.

Knowing what each of the many table parts means allows us to add our style, make them more aesthetically pleasing, and make them easier to read.

The majority of the time, we don't add any additional class or id when using CSS to layout tables. Instead, we use the CSS properties to apply the style to the HTML element directly. We may need to offer a class or an ID if we need to provide some custom styles, such as colouring only one cell or applying a special effect to a specific cell.

Properties in CSS

1. Border

It is utilized to specify table boundaries.

A CSS element's border can be styled using the border attribute. As a shorthand notation for these three characteristics, this property combines the three others border-width, border-style, and border-color. The border-width, border-style, and border-color Properties are set or returned.

Syntax:

Default Value: Initial is the default value for it.

Property Values

  • width: This number specifies the border's weight or breadth.
  • Style: This value indicates the border's style, such as whether it will be solid, dashed, or dotted.
  • Colour: This value describes the border's colour.

The CSS border property is demonstrated in the code below:

Output

CSS Table Style

Border Collapse

The border-collapse attribute indicates whether each cell's style should be preserved or if the browser should manage how neighbouring borders that touch one another look.

Syntax:

Default Value: It has the default value of separate.

Property Values

  • Separate: This attribute is used to define a cell's distinct boundary.
  • Collapse: Utilized to form a shared boundary by collapsing neighbouring cells.
  • Initial: The border-collapse property's default value is set using an initial.
  • Inherit: When the border-collapse attribute inherits from its parent elements, it is utilized.

Output

CSS Table Style

Border Spacing

This value describes how much space is available between adjacent cells' borders.

The border-spacing property specifies the distance between the borders of adjacent cells in the tables. This property is only active when the border-collapse property is set to no-collapse separate.

Default Value

2px

Syntax

Property values:

  1. Length-length: It is used to provide the distance between adjacent cell boundaries. Negative values are not permitted.
    • If two numbers are specified, the first specifies the horizontal gap, and the second the vertical spacing.
    • If just one number is specified, it determines both the horizontal and vertical space between cell boundaries.
  2. Initial: This function returns the property to its default value.

Syntax


Output

CSS Table Style

Caption Side

The caption side attribute is used to specify where the caption appears in the table. Captions are automatically added above the table.

This parameter is used to indicate the location of the table caption. It's found in HTML tables. This property can be applied to any element whose display property is set to caption-side.

Syntax

Properties:

  • Top: This indicates that the table caption should appear at the top of the table. It is the default setting.
  • Bottom: The table caption is positioned at the bottom of the table according to this attribute.
  • Initial: This function sets the property to its default value.

Output

CSS Table Style

Empty cells

This property determines whether or not empty cells in a table should have borders and backgrounds. This property specifies whether or not to display the borders in empty cells in a table.

Syntax

Default Value: show

Property values

  • Show property: This attribute is used to show the boundaries of empty cells.
  • Hide property: The hide property is used to conceal the border of an empty cell in a table.
  • Initial property: The default property is set using this property.
  • Inherit property: The inherit property is used to inherit a property from its parent.

Output

CSS Table Style





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA