• Home
  • HTML
    • HTML Introduction
    • HTML Basic
    • HTML Elements
    • HTML Attributes
    • HTML Headings
    • HTML Paragraphs
    • HTML Text Formatting
    • HTML Styles
    • HTML Comments
    • HTML Styles - CSS
    • HTML Links
    • HTML Images
    • HTML Tables
    • HTML Lists
    • HTML Block and Inline Elements
    • HTML class Attribute
    • HTML Forms
    • HTML Media
  • CSS
    • Introduction of CSS
    • CSS Syntax
    • CSS Selectors
    • How To Add CSS
    • CSS Comments
    • CSS Colors
    • CSS Backgrounds
    • CSS Borders
    • CSS Margins
    • CSS Text
    • CSS Lists
    • CSS Tables
    • CSS Box Model
    • CSS Dimension
    • CSS Padding
    • CSS Border
    • CSS Margin
    • CSS Outline
    • CSS Cursors
    • CSS Overflow
    • CSS Units
    • CSS Visual Formatting
    • CSS Display
    • CSS Visibility
    • CSS Position
    • CSS Layers
    • CSS Float
    • CSS Alignment
    • CSS Pseudo-classes
    • CSS Pseudo-elements
    • CSS Media Types
    • CSS Sprites
    • CSS Opacity
    • CSS Attribute Selectors
    • CSS Validation
    • CSS3 Border
    • CSS3 Gradients
    • CSS3 Text Overflow
  • JavaScript
    • JS Introduction
    • JS Getting Started
    • JS Syntax
    • JS Variables
    • JS Generating Output
    • JS Data Types
    • JS Operators
    • JS Events
    • JS Strings
    • JS Numbers
    • JS If Else
    • JS Switch Case
    • JS Arrays
    • JS Sorting Arrays
    • JS Loops
    • JS Functions
    • JS Objects
    • JS DOM Nodes
    • JS DOM Selectors
    • JS DOM Styling
    • JS DOM Get Set Attributes
    • JS DOM Manipulation
    • JS DOM Navigation
    • JS Window
    • JS Screen
    • JS Location
    • JS History
    • JS Navigator
    • JS Dialog Boxes
    • JS Timers
    • JS Date and Time
    • JS Math Operations
    • JS Type Conversions
    • JS Event Listeners
    • JS Event Propagation
    • JS Borrowing Methods
    • JS Hoisting Behavior
    • JS Closures
    • JS Strict Mode
    • JS JSON Parsing
    • JS Error Handling
    • JS Regular Expressions
    • JS Form Validation
    • JS Cookies
    • JS AJAX Requests
    • JS ES6 Features
  • jQuery
    • jQuery Introduction
    • jQuery Syntax
    • jQuery Selectors
    • jQuery Events
    • jQuery Show/Hide
    • jQuery Fade
    • jQuery Slide
    • jQuery Animation
    • jQuery Stop
    • jQuery Chaining
    • jQuery Callback
    • jQuery Get/Set
    • jQuery Insert
    • jQuery Remove
    • jQuery CSS Classes
    • jQuery Style Properties
    • jQuery Dimensions
    • jQuery Traversing
    • jQuery Ancestors
    • jQuery Descendants
    • jQuery Siblings
    • jQuery Filtering
    • jQuery Ajax
    • jQuery Load
    • jQuery Get/Post
    • jQuery No-Conflict
  • PHP
    • PHP Introduction
    • PHP Install
    • PHP Syntax
    • PHP Comments
    • PHP Variables
    • PHP Echo / Print
    • PHP Data Types
    • PHP Strings
    • PHP Constants
    • PHP Operators
    • PHP If...Else...Elseif
    • PHP Switch
    • PHP Loops
    • PHP Functions
    • PHP Arrays
    • PHP Superglobals
    • PHP Date and Time
    • PHP Include
    • PHP File Handling
    • PHP File Upload
    • PHP Cookies
    • PHP Sessions
    • PHP Filters
    • PHP Callback Functions
    • PHP JSON
    • PHP Exceptions
    • PHP What is OOP
    • PHP Classes/Objects
    • PHP Constructor
    • PHP Destructor
    • PHP Access Modifiers
    • PHP Inheritance
    • PHP Abstract Classes
    • PHP Interfaces
    • PHP Traits
    • PHP Static Methods
    • PHP Namespaces
  • SQL
    • Introduction to SQL
    • SQL Create Command
    • SQL ALTER Command
    • SQL Truncate Drop Rename
    • INSERT SQL command
    • UPDATE SQL command
    • DELETE SQL command
    • SQL COMMIT command
    • SQL ROLLBACK command
    • SQL GRANT and REVOKE Command
    • SQL WHERE clause
    • SQL LIKE clause
    • SQL ORDER BY Clause
    • SQL Group By Clause
    • SQL HAVING Clause
    • SQL DISTINCT keyword
    • SQL AND OR operator
    • SQL Constraints
    • SQL Functions
    • SQL JOIN
  • Python
    • Getting started with Python
    • Introduction to IDLE
    • Python 2.x vs. Python 3.x
    • Syntax Rules and First Program
    • Numbers and Math Functions
    • Python Operators
    • Python Variables
    • Python Modules and Functions
    • Python Input and Output
    • Data Types in Python
    • String in Python
    • String Functions in python
    • Lists in Python
    • Utilizing List Elements by Iterating
    • Deleting List Elements & other Functions
    • Dictionaries in Python
    • Functions for Dictionary
    • Tuples in Python
    • Relational and Logical Operators
    • Conditional Statements in Python
    • Looping in Python
    • Define Functions in Python
    • Python-Introduction to OOP
    • Object Oriented Programming in Python
    • Classes in Python
    • The concept of Constructor
    • Destructors - Destroying the Object in Python
    • Inheritance in Python
    • Access Modifers in Python
    • Types of Inheritance
    • Method Overriding in Python
    • Polymorphism
    • static Keyword
    • Operator Overloading Python
    • Introduction to Error Handling
    • Exception Handling: try and except
    • Exeption Handling: finally
    • Exception Handling: raise
    • File Handling
    • Reading and Writing File
    • Introduction to Multithreading
    • Threading Module in Python
    • Thread Object
    • Lock Object
    • RLock Object
    • Event Object
    • Timer Object
    • Condition Object
    • Barrier Object
    • __name__ Variable in Python
    • Iterable and Iterator
    • yield Keyword
    • Python Generators
    • Python Closures
    • Python Decorators
    • @property Decorator in Python
    • Assert Statement
    • Garbage Collection
    • Shallow and Deep Copy
    • Introduction to Logging
    • Configure Log LEVEL, Format etc
    • Python Logging in a file
    • Python Logging Variable Data
    • Python Logging Classes and Functions
    • Python MySQL Introduction
    • Create Database - Python MySQL
    • Create Table - Python MySQL
    • Insert Data in Table
    • Select Data from Table
    • Update data in Table
    • Delete data from Table
    • Drop Table from Database
    • WHERE clause - Python MySQL
    • Order By clause - Python MySQL
    • Limit clause - Python MySQL
    • Table Joins - Python MySQL
  • MongoDB
    • MongoDB Introduction
    • Overview of MongoDB
    • MongoDB vs SQL Databases
    • Advantages of MongoDB
    • When to go for MongoDB
    • Data Modelling in MongoDB
    • Is MongoDB really Schemaless?
    • Installing MongoDB on Windows and Linux
    • Datatypes in MongoDB
    • Create and Drop Database in MongoDB
    • MongoDB: Creating a Collection
    • CRUD Operations in MongoDB
    • Data Relationships in MongoDB
    • Indexing in MongoDB
    • Sorting in MongoDB
    • Aggregation in MongoDB
    • Data Backup and Restoration in MongoDB
    • Sharding in MongoDB
    • Java Integration with MongoDB
  • Elixir
    • Elixir Overview
    • Elixir Environment
    • Elixir Basic Syntax
    • Elixir Data Types
    • Elixir Variables
    • Elixir Operators
    • Elixir Pattern Matching
    • Elixir Decision Making
    • Elixir Strings
    • Elixir Char Lists
    • Elixir Lists and Tuples
    • Elixir Keyword Lists
    • Elixir Maps
    • Elixir Modules
    • Elixir Aliases
    • Elixir Functions
    • Elixir Recursion
    • Elixir Loops
    • Elixir Enumerables
    • Elixir Streams
    • Elixir Structs
    • Elixir Protocols
    • Elixir File I/O
    • Elixir Processes
    • Elixir Sigils
    • Elixir Comprehensions
    • Elixir Typespecs
    • Elixir Behaviours
    • Elixir Errors Handling
    • Elixir Macros
    • Elixir Libraries
  • TypeScript
    • TypeScript Overview
    • Install TypeScript
    • First TypeScript Program
    • Type Annotation
    • TypeScript Variable
    • TypeScript Data Type Number
    • TypeScript Data Type String
    • TypeScript Data Type Boolean
    • TypeScript Arrays
    • TypeScript Tuples
    • TypeScript Enum
    • TypeScript Union
    • TypeScript Any Data Type
    • TypeScript Void Data Type
    • TypeScript Never Data Type
  • Home
  • Introduction of CSS
  • CSS Syntax
  • CSS Selectors
  • How To Add CSS
  • CSS Comments
  • CSS Colors
  • CSS Backgrounds
  • CSS Borders
  • CSS Margins
  • CSS Text
  • CSS Lists
  • CSS Tables
  • CSS Box Model
  • CSS Dimension
  • CSS Padding
  • CSS Border
  • CSS Margin
  • CSS Outline
  • CSS Cursors
  • CSS Overflow
  • CSS Units
  • CSS Visual Formatting
  • CSS Display
  • CSS Visibility
  • CSS Position
  • CSS Layers
  • CSS Float
  • CSS Alignment
  • CSS Pseudo-classes
  • CSS Pseudo-elements
  • CSS Media Types
  • CSS Sprites
  • CSS Opacity
  • CSS Attribute Selectors
  • CSS Validation
  • CSS3 Border
  • CSS3 Gradients
  • CSS3 Text Overflow
Home >> css >> CSS Tables

CSS Tables

This tutorial helps you learn how to style HTML tables with CSS.

 

Styling Tables with CSS

Tables are used for displaying tabular data, like financial reports. When an HTML table is created without any styles or attributes, then browsers display them without any border. Therefore, the appearance of your tables can be significantly improved using CSS.

CSS offers many properties that let you control the table element's layout and presentation. The following section helps you learn how to use CSS for creating elegant and consistent tables.

 

Adding Borders to Tables

The CSS border property is an excellent way to define the table borders.

The below example tells how to set a black border for the table <table>, <th>, and <td> elements.

Example :

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<h2>Adding Borders to Tables</h2>

<table>
<tr>
      <th>Name</th>
      <th>Email</th>
      <th>Country</th>
</tr>
<tr>
      <td>Ada</td>
      <td>[email protected]</td>
      <td>UAE</td>
</tr>
<tr>
      <td>Sidney Bermudez</td>
      <td>[email protected]</td>
      <td>USA</td>
</tr>
<tr>
      <td>Tristan Sumacote</td>
      <td>[email protected]</td>
      <td>USA</td>
</tr>
</table>

</body>
</html>

Try With Example

 

By default, the browser draws a border around the table, and all the cells, with spacing in-between, resulting in the double border. However, you can collapse the adjoining table cell borders to eliminate this double border problem and create clean single-line borders.

Let's look at the example given here to understand how a border is applied to a table.

 

 

Collapsing Table Borders

In CSS, you can set the borders on a table in two different ways: separate and collapse. The separate border model is the default model. In it, each table cell has its distinct borders. The other border model is the collapsed border model. In it, adjacent table cells share a common border. The border model can be set for an HTML table using the CSS border-collapse property.

The below-mentioned style rules will collapse the table cell borders and apply a one-pixel black border.

<!DOCTYPE html>
<html>
<head>
<style>
table {
    border-collapse: collapse;
}
th, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<h2>Adding Borders to Tables</h2>

<table>
<tr>
      <th>Name</th>
      <th>Email</th>
      <th>Country</th>
</tr>
<tr>
      <td>Ada</td>
      <td>[email protected]</td>
      <td>UAE</td>
</tr>
<tr>
      <td>Sidney Bermudez</td>
      <td>[email protected]</td>
      <td>USA</td>
</tr>
<tr>
      <td>Tristan Sumacote</td>
      <td>[email protected]</td>
      <td>USA</td>
</tr>

</table>

</body>
</html>

Try With Example

Note: The space between the table cell borders can be removed by setting the CSS border-spacing property's value to 0. Although, this will only remove the space and will not merge the borders like in case when the border is to-collapse to collapse.

 

Adjusting Space inside Tables

By default, the table cells created by the browser are large enough to contain the data in the cells.

You can add more space between the table cell contents and the cell borders using the CSS padding property. Let's use the following example to see how it works:

Example :

th, td {
    padding: 15px;
}

Try With Example

You can also use the CSS border padding to adjust the spacing between the borders of the cells if the borders of your table are separated (which is default).

The style rules mentioned below apply the spacing of 10 pixels between all borders within a table:

Example :

table {
    border-spacing: 10px;
}

Try With Example

 

Setting Table Width and Height

By default, a table is wide and tall enough to contain all its contents.

However, the width and height of the table and its cells can be explicitly set using the width and height CSS property. In the example given here, the style rules will set the table's width to 100% and the table header cell's height to 40px.

Example :

table {
    width: 100%;
}
th {
    height: 40px;
}

Try With Example

 

Controlling the Table Layout

As per the default behavior of a table, it can expand and contract to accommodate the data contained inside the table. As data fill inside, the table continues to expand till there is space. However, sometimes, it is required to set a fixed width for the table to manage its layout, and this can be done with the help of the CSS table-layout property. This property specifies the algorithm to be used to lay out the table cells, rows, and columns. This property takes one of two values:

  • Auto — Here, an automatic table layout algorithm is used. The table and its cell's width are adjusted to fit the content with an auto algorithm. This is the default value.
  •  Fixed — Here, the fixed table layout algorithm is used. With a fixed algorithm, the table's horizontal layout does not depend on the cell's contents; it only depends on the table's width, the columns' width, and borders or cell spacing. It usually is faster than auto.

The style rules in the example mentioned here specify that the HTML table is laid out using the fixed layout algorithm with a fixed width of 300 pixels. Let's see how it works:

Example :

table {
    width: 400px;
    table-layout: fixed;
}

Try With Example

 

Tip: The table rendering performance can be optimised by specifying the value fixed for the table-layout property. The fixed value of this property causes the table to be rendered one row at a time, thereby providing users with information at a faster rate.

Note: Without a fixed value of the table-layout property on large tables, users will not be able to see any part of the table until the browser has rendered the whole table.

 

Aligning the Text Inside Table Cells

The text content inside the table cells can be aligned either horizontally or vertically.

 

Horizontal Alignment of Cell Contents

To align the text inside the table cells horizontally, you can use the text-align property the same way you use it with other elements. You can align text to the left, right, center, or justify.

The style rules given here will left-align the text inside the <th> elements.

  • Prev
  • Next


-Advertisement-


DeveloperTutorial
[email protected] © 2022-2023 Developers Tutorial All rights reserved.

Follow Us

Facebook Twitter LinkedIn Printerest Reddit

Announcement

Its a big achivement for us, We make a partnership with TutorialWithExample.com for the better content of our users.

Still Need Help ?

Let us now about your issue and a Professional will reach you out.