🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Adding borders to table cells helps improve the readability and structure of a table, making it easier to distinguish between rows and columns. In this tutorial, you'll learn how to apply borders to table cells using CSS.
Development Steps
- Create Basic HTML Table Structure: Set up a simple table with headers and data rows.
- Add Borders to Table Cells: Use CSS to add borders to each table cell (
thandtd). - Customize Borders with CSS: Customize the border style, color, and spacing.
Step 1: Create Basic HTML Table Structure
We’ll start by creating a simple HTML table structure that includes headers and data rows.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table with Borders</title>
</head>
<body>
<!-- Step 1: Basic table structure -->
<div class="table-container">
<table class="bordered-table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ananya</td>
<td>25</td>
<td>Delhi</td>
</tr>
<tr>
<td>Rohan</td>
<td>30</td>
<td>Mumbai</td>
</tr>
<tr>
<td>Meera</td>
<td>22</td>
<td>Bangalore</td>
</tr>
<tr>
<td>Vikram</td>
<td>35</td>
<td>Pune</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Explanation:
- The table contains a
theadwith headers (Name,Age,City) and atbodywith rows of data. - We will now use CSS to add borders to each table cell.
Step 2: Add Borders to Table Cells with CSS
In this step, we will add borders around each table cell (th and td) using CSS.
<head>
<style>
/* General body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.table-container {
width: 80%;
margin: 20px;
background-color: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
}
/* Step 2: Add borders to table cells */
table {
width: 100%;
border-collapse: collapse; /* Ensures the borders are collapsed into a single line */
}
th, td {
padding: 12px 15px;
text-align: left;
border: 1px solid #ddd; /* Add border around each cell */
}
th {
background-color: #3498db;
color: white;
text-transform: uppercase;
letter-spacing: 0.1em;
}
</style>
</head>
Explanation:
- Borders: We added a
border: 1px solid #ddd;to boththandtd, giving all table cells a light gray border. border-collapse: Theborder-collapse: collapseproperty ensures that the borders between cells collapse into a single border, avoiding the appearance of double borders.
Step 3: Customize Borders with CSS
Now we’ll customize the borders by changing the color, style, and thickness of the borders around the table cells.
<head>
<style>
/* General body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.table-container {
width: 80%;
margin: 20px;
background-color: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
}
/* Add borders to table cells */
table {
width: 100%;
border-collapse: collapse; /* Ensures the borders are collapsed into a single line */
}
th, td {
padding: 12px 15px;
text-align: left;
border: 2px solid #3498db; /* Custom thicker blue border */
}
th {
background-color: #3498db;
color: white;
text-transform: uppercase;
letter-spacing: 0.1em;
}
</style>
</head>
Explanation:
- Custom Borders: The border thickness has been increased to
2pxand the color changed to#3498db(blue) to make the table more visually appealing. - You can also customize the border style, such as changing it to
dashedordotted.
Complete Example
Here is the complete HTML and CSS code to add borders to a table with customized styles:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table with Borders</title>
<style>
/* General body styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.table-container {
width: 80%;
margin: 20px;
background-color: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
}
/* Add borders to table cells */
table {
width: 100%;
border-collapse: collapse; /* Ensures the borders are collapsed into a single line */
}
th, td {
padding: 12px 15px;
text-align: left;
border: 2px solid #3498db; /* Custom thicker blue border */
}
th {
background-color: #3498db;
color: white;
text-transform: uppercase;
letter-spacing: 0.1em;
}
</style>
</head>
<body>
<!-- Step 1: Basic table structure -->
<div class="table-container">
<table class="bordered-table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ananya</td>
<td>25</td>
<td>Delhi</td>
</tr>
<tr>
<td>Rohan</td>
<td>30</td>
<td>Mumbai</td>
</tr>
<tr>
<td>Meera</td>
<td>22</td>
<td>Bangalore</td>
</tr>
<tr>
<td>Vikram</td>
<td>35</td>
<td>Pune</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Output
Conclusion
In this tutorial, you learned how to add borders to table cells using CSS. By customizing the border properties such as color, thickness, and style, you can make your tables more visually appealing and structured. Adding borders enhances readability and makes the table easier to navigate, especially when working with large datasets.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment