🎓 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
A three-column layout is a common web design pattern used for arranging content in a neat, side-by-side structure. This layout is typically used for dividing content into sections such as navigation, main content, and additional information. Flexbox is an easy and efficient way to create this layout, providing flexibility and responsiveness.
In this tutorial, you will learn how to create a three-column layout using Flexbox.
Development Steps
- Set up Flexbox for the Three Columns: Use Flexbox to align three
divelements side by side. - Ensure Responsiveness: Use media queries to stack the columns vertically on smaller screens.
HTML Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three-Column Layout with Flexbox</title>
<style>
/* Step 1: Set up the Flexbox container */
.flex-container {
display: flex; /* Aligns items side by side */
gap: 20px; /* Adds space between columns */
}
/* Step 2: Style the columns */
.column {
flex: 1; /* Makes each column take equal space */
padding: 20px;
background-color: #3498db;
color: white;
text-align: center;
}
/* Step 3: Responsive design for smaller screens */
@media (max-width: 768px) {
.flex-container {
flex-direction: column; /* Stacks the columns vertically */
}
}
</style>
</head>
<body>
<div class="flex-container">
<div class="column">
<h2>Column 1</h2>
<p>This is the first column with some content.</p>
</div>
<div class="column">
<h2>Column 2</h2>
<p>This is the second column with some content.</p>
</div>
<div class="column">
<h2>Column 3</h2>
<p>This is the third column with some content.</p>
</div>
</div>
</body>
</html>
Output
Explanation
Step 1: Set Up the Flexbox Container
- Use
display: flexto align the columns side by side:.flex-container { display: flex; /* Aligns items side by side */ gap: 20px; /* Adds space between columns */ }display: flex;aligns the child elements horizontally, allowing them to sit side by side.gap: 20px;adds space between the columns to keep them separated.
Step 2: Style the Columns
- Use
flex: 1to ensure the columns take equal width:.column { flex: 1; /* Ensures all columns take equal space */ padding: 20px; background-color: #3498db; color: white; text-align: center; }flex: 1;ensures that each column takes an equal portion of the container width.padding: 20px;provides space inside each column to prevent the content from being cramped.background-color: #3498db;gives the columns a blue background.
Step 3: Responsive Design for Smaller Screens
- Use a media query to stack the columns vertically on smaller screens:
@media (max-width: 768px) { .flex-container { flex-direction: column; /* Stacks the columns vertically */ } }flex-direction: column;changes the layout from side-by-side to top-down, making the columns stack vertically on devices with a screen width of 768px or less.
Conclusion
Creating a three-column layout using Flexbox is simple and efficient. Flexbox allows you to align items horizontally, distribute space evenly, and make the layout responsive with minimal effort. By using media queries, the layout can easily adapt to smaller screens, ensuring a clean and user-friendly design across all devices.
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