CSS Tables
CSS tables can be used to create layouts and display data in a tabular format, similar to traditional HTML <table> elements, but with more flexibility and control over styling. Here's an overview with examples:
Basic Concept
CSS allows you to style elements to behave like tables, rows, and cells without using actual <table> HTML elements. This can be useful for creating layouts or aligning content.
Key CSS Properties
display: table;- Makes an element behave like a<table>.display: table-row;- Makes an element behave like a<tr>.display: table-cell;- Makes an element behave like a<td>.display: table-header-group;- Makes an element behave like a<thead>.display: table-footer-group;- Makes an element behave like a<tfoot>.display: table-column;- Makes an element behave like a<col>.display: table-column-group;- Makes an element behave like a<colgroup>.
Example 1: Simple Table Layout
Here's a simple example of using CSS to create a table-like layout.
HTML
<div class="table">
<div class="table-row">
<div class="table-cell">Header 1</div>
<div class="table-cell">Header 2</div>
</div>
<div class="table-row">
<div class="table-cell">Row 1, Cell 1</div>
<div class="table-cell">Row 1, Cell 2</div>
</div>
<div class="table-row">
<div class="table-cell">Row 2, Cell 1</div>
<div class="table-cell">Row 2, Cell 2</div>
</div>
</div>
CSS
.table {
display: table;
width: 100%;
border-collapse: collapse;
}
.table-row {
display: table-row;
}
.table-cell {
display: table-cell;
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
Example 2: Responsive Table Layout
For a more complex example with responsive design:
HTML
<div class="responsive-table">
<div class="responsive-table-row">
<div class="responsive-table-cell header">Name</div>
<div class="responsive-table-cell header">Age</div>
<div class="responsive-table-cell header">City</div>
</div>
<div class="responsive-table-row">
<div class="responsive-table-cell">John Doe</div>
<div class="responsive-table-cell">30</div>
<div class="responsive-table-cell">New York</div>
</div>
<div class="responsive-table-row">
<div class="responsive-table-cell">Jane Smith</div>
<div class="responsive-table-cell">25</div>
<div class="responsive-table-cell">Los Angeles</div>
</div>
</div>
CSS
.responsive-table {
display: table;
width: 100%;
border-collapse: collapse;
}
.responsive-table-row {
display: table-row;
}
.responsive-table-cell {
display: table-cell;
border: 1px solid #ccc;
padding: 10px;
text-align: left;
}
.responsive-table-cell.header {
background-color: #f4f4f4;
font-weight: bold;
}
/* Responsive adjustments */
@media (max-width: 600px) {
.responsive-table, .responsive-table-row {
display: block;
}
.responsive-table-cell {
display: block;
width: 100%;
box-sizing: border-box;
border: none;
padding: 10px;
}
.responsive-table-cell.header {
background-color: transparent;
font-weight: normal;
}
.responsive-table-cell::before {
content: attr(data-label);
font-weight: bold;
display: block;
}
}
Explanation
- Basic Table Layout: Creates a simple table-like structure using CSS
displayproperties. This is ideal for static layouts. - Responsive Table Layout: Adjusts the table layout for small screens by converting rows into block elements and using
::beforeto add labels to cells.
Feel free to adjust these examples to fit your needs or ask if you have specific requirements or questions!
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
