How to Get a Table Like Row/Column Alignment in a Flexbox ?
Last Updated :
14 Oct, 2024
Getting a table-like row/column alignment in Flexbox refers to structuring content similarly to traditional tables, but with more flexibility. Flexbox allows items to align in rows and columns dynamically, offering responsive control over spacing, alignment, and positioning, ideal for modern layouts.
We can set a container element to be a Flexbox by setting its display property to Flex. We can then use various Flexbox properties to control the layout of its child elements.
1. Using Flexbox and Grid
The Using Flexbox and Grid approach involves setting the container as a Flexbox for centering, while child elements use CSS Grid to create a table-like structure. Grid handles the columns and rows, offering flexible, responsive alignment and spacing.
Example: In this example the table-like alignment using Flexbox and Grid. It creates a structured layout with rows and cells for displaying data, such as names, ages, and genders.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Table-like Row/Column Alignment
using Flexbox and Grid
</title>
<style>
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.row {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-column-gap: 10px;
margin: 10px 0;
}
.cell {
padding: 10px;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="cell">Name</div>
<div class="cell">Age</div>
<div class="cell">Gender</div>
</div>
<div class="row">
<div class="cell">John Doe</div>
<div class="cell">30</div>
<div class="cell">Male</div>
</div>
<div class="row">
<div class="cell">Jane Doe</div>
<div class="cell">25</div>
<div class="cell">Female</div>
</div>
</div>
</body>
</html>
Output:
Using Flexbox and Grid Example Output2. Using Flexbox and Pseudo-elements
The Using Flexbox and Pseudo-elements approach creates a table-like layout by using Flexbox for row alignment and ::before/::after pseudo-elements to simulate column spacing. The flex-grow property ensures child elements fill available space, mimicking table cells with flexible, responsive design.
Example: In this example we creates a table-like layout using Flexbox. It displays data in rows and cells, including headers for name, age, and gender, with flexible spacing.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Table-like Row/Column Alignment
using Flexbox and Grid
</title>
<style>
.table {
display: flex;
flex-direction: column;
align-items: stretch;
}
.row {
display: flex;
flex-direction: row;
align-items: center;
}
.cell {
flex-grow: 1;
padding: 10px;
border: 1px solid black;
}
.header {
font-weight: bold;
}
.table::before,
.table::after,
.row::before,
.row::after {
content: "";
flex-basis: 10px;
flex-shrink: 0;
}
</style>
</head>
<body>
<div class="table">
<div class="row header">
<div class="cell">Name</div>
<div class="cell">Age</div>
<div class="cell">Gender</div>
</div>
<div class="row">
<div class="cell">John Doe</div>
<div class="cell">30</div>
<div class="cell">Male</div>
</div>
<div class="row">
<div class="cell">Jane Doe</div>
<div class="cell">25</div>
<div class="cell">Female</div>
</div>
</div>
</body>
</html>
Output:
Using Flexbox and Pseudo-elements Example Output
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis
8 min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read