For working professionals
For fresh graduates
More
1. Introduction
6. PyTorch
9. AI Tutorial
10. Airflow Tutorial
11. Android Studio
12. Android Tutorial
13. Animation CSS
16. Apex Tutorial
17. App Tutorial
18. Appium Tutorial
21. Armstrong Number
22. ASP Full Form
23. AutoCAD Tutorial
27. Belady's Anomaly
30. Bipartite Graph
35. Button CSS
39. Cobol Tutorial
46. CSS Border
47. CSS Colors
48. CSS Flexbox
49. CSS Float
51. CSS Full Form
52. CSS Gradient
53. CSS Margin
54. CSS nth Child
55. CSS Syntax
56. CSS Tables
57. CSS Tricks
58. CSS Variables
61. Dart Tutorial
63. DCL
65. DES Algorithm
83. Dot Net Tutorial
86. ES6 Tutorial
91. Flutter Basics
92. Flutter Tutorial
95. Golang Tutorial
96. Graphql Tutorial
100. Hive Tutorial
103. Install Bootstrap
107. Install SASS
109. IPv 4 address
110. JCL Programming
111. JQ Tutorial
112. JSON Tutorial
113. JSP Tutorial
114. Junit Tutorial
115. Kadanes Algorithm
116. Kafka Tutorial
117. Knapsack Problem
118. Kth Smallest Element
119. Laravel Tutorial
122. Linear Gradient CSS
129. Memory Hierarchy
133. Mockito tutorial
134. Modem vs Router
135. Mulesoft Tutorial
136. Network Devices
138. Next JS Tutorial
139. Nginx Tutorial
141. Octal to Decimal
142. OLAP Operations
143. Opacity CSS
144. OSI Model
145. CSS Overflow
146. Padding in CSS
148. Perl scripting
149. Phases of Compiler
150. Placeholder CSS
153. Powershell Tutorial
158. Pyspark Tutorial
161. Quality of Service
162. R Language Tutorial
164. RabbitMQ Tutorial
165. Redis Tutorial
166. Redux in React
167. Regex Tutorial
170. Routing Protocols
171. Ruby On Rails
172. Ruby tutorial
173. Scala Tutorial
175. Shadow CSS
178. Snowflake Tutorial
179. Socket Programming
180. Solidity Tutorial
181. SonarQube in Java
182. Spark Tutorial
189. TCP 3 Way Handshake
190. TensorFlow Tutorial
191. Threaded Binary Tree
196. Types of Queue
197. TypeScript Tutorial
198. UDP Protocol
202. Verilog Tutorial
204. Void Pointer
205. Vue JS Tutorial
206. Weak Entity Set
207. What is Bandwidth?
208. What is Big Data
209. Checksum
211. What is Ethernet
214. What is ROM?
216. WPF Tutorial
217. Wireshark Tutorial
218. XML Tutorial
Understanding CSS tables is crucial for developing clean, responsive web designs. In this tutorial, we will delve into the specifics of table styling in CSS, demystifying its core properties, and when to use (or avoid) tables. Let's begin our exploration.
We will cover topics such as CSS tables properties, some quick styling tips, appropriate use cases for tables, and scenarios to avoid them. Starting from the basic understanding of CSS table styles, we'll explore the range of CSS table properties.
CSS tables styling involves utilizing a broad spectrum of properties to manage the appearance and structure of HTML tables. These properties allow you to exercise extensive control over how your tables appear in a web environment. They dictate everything, from the dimensions of the table and its cells to its color scheme and text alignment.
For instance, the border property in CSS sets the boundary around table cells, creating distinct separation. The padding property adjusts the space within the cells, contributing to the readability of the table's content. With text-align, you control the alignment of text within the cells, and the color property allows you to set the text color. Lastly, background-color enables you to set a specific background color for your cells. These properties collectively help in enhancing the visual presentation and accessibility of your table data.
CSS Property | Function |
border | Defines the border around table cells |
padding | Controls the space within the cells |
text-align | Manages text alignment in the cells |
color | Determines text color |
background-color | Sets the cell's background color |
In CSS, a variety of properties are dedicated specifically to styling tables. Understanding these properties allows developers to fine-tune table appearances and improve both aesthetics and user experience. Here are some essential CSS table properties:
These CSS table properties provide nuanced control over table styling, enabling the creation of visually appealing, user-friendly, and accessible tables.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid;
}
</style>
</head>
<body>
<h2>Add a border to a table upGrad:</h2>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
As a default, the spacing present between the borders of the two cells is 2px.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border: 1px solid;
}
</style>
</head>
<body>
<h2>Single Border Around The Table</h2>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
Property Values:
1)Length 2)initial 3)inherit
Code:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 1px solid black;
}
#t1 {
border-collapse: separate;
border-spacing: 15px;
}
#t2 {
border-collapse: separate;
border-spacing: 15px 50px;
}
</style>
</head>
<body>
<h2>border-spacing: 15px:</h2>
<p>When using "border-collapse: separate", the border-spacing property can be used to set the space between the cells:</p>
<table id="t1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>abc</td>
<td>xyz</td>
</tr>
<tr>
<td>mno</td>
<td>pqr</td>
</tr>
</table>
<h2>border-spacing: 15px 50px:</h2>
<p>Using two values (the first sets the horizontal spacing and the second sets the vertical spacing):</p>
<table id="t2">
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>abc</td>
<td>xyz</td>
</tr>
<tr>
<td>mno</td>
<td>pqr</td>
</tr>
</table>
</body>
</html>
The border-collapse property defines that the table borders can combine and become a single border (shared border) or it can be separated as in standard HTML.
Property Values:
Code:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 3px solid red;
}
#tab1 {
border-collapse: collapse;
border-color: blue;
}
#tab2 {
border-collapse: separate;
border-color: blue;
}
</style>
</head>
<body>
<h2>border-collapse: collapse:</h2>
<table id="tab1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>abc</td>
<td>xyz</td>
</tr>
<tr>
<td>mno</td>
<td>pqr</td>
</tr>
</table>
<h2>border-collapse: separate:</h2>
<table id="tab2">
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>abc</td>
<td>xyz</td>
</tr>
<tr>
<td>mno</td>
<td>pqr</td>
</tr>
</table>
</body>
</html>
It defines the space between the table borders and the table content. This is applied to the <td> and <th> elements. By default, the padding is set to 0.
Horizontal Alignment: The text-align property allows us to set the horizontal alignment of the content in <th> or <td> (like left, right, or center). By default, the content of <th> elements is center-aligned and the content of <td> elements is left-aligned.
Vertical Alignment: The vertical-align property helps us set the vertical alignment of the content in <th> or <td> (like top, bottom, or middle). By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).
This property allows us to manipulate the font color of table data.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
background-color: coral;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
</style>
</head>
<body>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
In CSS, there are multiple ways to control the layout of HTML tables. CSS provides a property called table-layout that determines how a table should be rendered regarding its width and cell layout.
Here are some common CSS properties and techniques used to manage the layout of tables:
Code:
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: red;
}
table.one {
width: 80px border-collapse: separate;
border-spacing: 10px;
/* Layout of table is auto. */
table-layout: auto;
}
table.two {
width: 80px border-collapse: separate;
border-spacing: 10px;
/* Layout of table is fixed. */
table-layout: fixed;
}
table,
td,
th {
border: 1.5px solid blue;
width: 80px;
}
</style>
</head>
<body>
<h1>upGradTutorial</h1>
<h2>auto table layout:</h2>
<table class="one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>fixed table layout:</h2>
<table class="two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
Code:
<!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}
th {
background-color: #04AA6D;
color: white;
}
</style>
</head>
<body>
<h2>upGradTutorial</h2>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
HTML table tags are used to create tabular data structures on a web page. They allow you to organize data into rows and columns, making it easy to present information in a structured and readable format. The basic HTML table structure involves three main tags:
<table>: This is the container element for the entire table. It encompasses all the rows and cells within the table.
<tr>: This tag represents a table row. It is used to group a set of table cells (td or th) that make up a single row of the table.
<td> and <th>: These tags represent table cells. <td> stands for "table data" and is used for regular data cells, while <th> stands for "table header" and is used for header cells. Header cells are typically bold and centered, and they provide labels for the data cells in that column or row.
Creating an appealing, easy-to-read CSS tables list involves various tactics.
Remember, the key to great table styling is balance - keeping elements consistent, using colors strategically, and managing whitespace effectively can make your tables much more engaging and user-friendly.
Stylish CSS tables are ideal for presenting data in a structured manner, especially when the data is comparative or interconnected. This could include displaying database content, financial data, schedules, or any form of tabular data.
Tables should not be used for web page layout as it makes the webpage heavy, negatively affecting load times and overall performance. CSS offers other layout techniques like flexbox and grid that are more efficient and flexible.
CSS tables are a powerful tool for presenting data, offering significant control over the style and presentation of tabular data. Understanding when and how to use them enhances your web development skills and helps to create more efficient, user-friendly webpages. With practice, these concepts will become second nature, allowing you to bring complex data presentations to life with ease.
As you continue your journey in web development with upGrad, remember the value of continuous learning. CSS is a powerful tool in your developer's toolkit, and mastering its use will open the doors to more advanced and interactive web design techniques. Happy coding!
CSS plays a crucial role in creating responsive tables that adjust according to the viewport size. By utilizing properties like overflow, or using media queries, developers can ensure tables display effectively on different screen sizes.
Yes, there are numerous online CSS table generators that can help you create table styles quickly. These tools generate the CSS and HTML code that you can then customize and incorporate into your project.
Absolutely! With CSS3, you can use the border-radius property to create tables with rounded borders, adding a unique aesthetic touch to your design.
Looking at CSS table style examples can provide inspiration and serve as practical learning tools. By studying various examples, you can learn different ways to use CSS properties and grasp how to create a specific look or functionality.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.