Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a flexbox Layout in HTML?
To create a flexbox layout in HTML, we use CSS Flexbox, not CSS Float. Flexbox is a powerful layout method that provides an efficient way to arrange, distribute and align elements in a container, even when their size is unknown or dynamic.
The CSS Flexbox Layout Module was introduced in CSS3 to make it easier to design flexible responsive layout structures without using floats or positioning. Flexbox makes it simple to create layouts that adapt to different screen sizes and orientations, ensuring your web page looks great on all devices.
Syntax
To create a flexbox layout, apply the display: flex property to a container element −
.container {
display: flex;
}
This makes the container a flex container and its direct children become flex items. You can then use various flexbox properties to control the layout.
Key Flexbox Properties
The most commonly used flexbox properties are −
display: flex − Makes an element a flex container
flex-direction − Defines the direction of flex items (row, column, etc.)
flex-wrap − Controls whether items wrap to new lines
justify-content − Aligns items along the main axis
align-items − Aligns items along the cross axis
flex − Controls how flex items grow and shrink
Basic Flexbox Layout
Example − Simple Horizontal Layout
Following example shows a basic flexbox layout with three equal-width columns −
<!DOCTYPE html>
<html>
<head>
<title>Basic Flexbox Layout</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 10px;
}
.container {
display: flex;
background-color: #f0f0f0;
padding: 20px;
gap: 10px;
}
.item {
flex: 1;
background-color: #4CAF50;
color: white;
text-align: center;
padding: 20px;
border-radius: 5px;
}
</style>
</head>
<body>
<h2>Basic Flexbox Layout</h2>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
</body>
</html>
This creates three equal-width columns arranged horizontally −
Basic Flexbox Layout [Item 1] [Item 2] [Item 3] (three green boxes in a row)
Responsive Flexbox Layout
Example − Responsive Website Layout
Following example demonstrates a complete responsive website layout using flexbox −
<!DOCTYPE html>
<html>
<head>
<title>Responsive Flexbox Layout</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
header {
background-color: #333;
color: white;
padding: 20px;
text-align: center;
}
.main-content {
display: flex;
flex: 1;
}
nav {
background-color: #f1f1f1;
width: 200px;
padding: 20px;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
margin: 10px 0;
}
nav ul li a {
text-decoration: none;
color: #333;
}
article {
flex: 1;
padding: 20px;
background-color: white;
}
footer {
background-color: #333;
color: white;
text-align: center;
padding: 20px;
}
@media (max-width: 768px) {
.main-content {
flex-direction: column;
}
nav {
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>TutorialsPoint.com</h1>
<h3>Simply Easy Learning</h3>
</header>
<div class="main-content">
<nav>
<ul>
<li><a href="#">Tutorials Library</a></li>
<li><a href="#">Video Tutorials</a></li>
<li><a href="#">Coding Ground</a></li>
<li><a href="#">Tutor Connect</a></li>
<li><a href="#">Tools</a></li>
</ul>
</nav>
<article>
<h2>About Us</h2>
<p>Welcome to TutorialsPoint, your one-stop destination for learning programming and web development.</p>
<p>This flexbox layout automatically adjusts to different screen sizes, providing an optimal viewing experience on all devices.</p>
<p>On smaller screens, the navigation moves below the header, and on larger screens, it appears as a sidebar.</p>
</article>
</div>
<footer>
© 2024 TutorialsPoint. All rights reserved.
</footer>
</div>
</body>
</html>
This layout uses nested flexbox containers to create a responsive design that adapts to different screen sizes.
Flexbox Card Layout
Example − Flexible Card Grid
Following example shows how to create a flexible card layout that wraps to new lines −
<!DOCTYPE html>
<html>
<head>
<title>Flexbox Card Layout</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f5f5f5;
}
.card-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
.card {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
padding: 20px;
flex: 1 1 300px;
max-width: 400px;
min-width: 250px;
}
.card h3 {
color: #333;
margin-top: 0;
}
.card p {
color: #666;
line-height: 1.5;
}
</style>
</head>
<body>
<h1 style="text-align: center;">Our Courses</h1>
<div class="card-container">
<div class="card">
<h3>HTML5</h3>
<p>Learn the fundamentals of HTML5 and create modern web structures with semantic elements.</p>
</div>
<div class="card">
<h3>CSS3</h3>
<p>Master CSS3 styling techniques including flexbox, grid, animations, and responsive design.</p>
</div>
<div class="card">
<h3>JavaScript</h3>
<p>Discover JavaScript programming from basics to advanced concepts including ES6+ features.</p>
</div>
</div>
</body>
</html>
The cards automatically adjust their size and wrap to new lines based on available space −
Our Courses [HTML5 Card] [CSS3 Card] [JavaScript Card] (Three white cards with course information, arranged flexibly)
Common Use Cases
Flexbox is ideal for the following layout scenarios −
Navigation bars − Horizontal or vertical menu layouts
Card layouts − Product grids, blog post previews
Centering content − Both horizontally and vertically
Sidebar layouts − Main content with fixed or flexible sidebars
Form layouts − Input fields with consistent spacing
Media objects − Image with accompanying text
Browser Compatibility
CSS Flexbox is supported in all modern browsers including Chrome, Firefox, Safari, and Edge. For older browsers (Internet Explorer 10+), you may need to use vendor prefixes like -webkit-flex and -ms-flexbox.
Conclusion
CSS Flexbox is a powerful and modern approach to creating responsive layouts in HTML. It eliminates the need for floats and complex positioning, making your CSS cleaner and more maintainable. With flexbox, you can easily create layouts that adapt to different screen sizes and provide an optimal user experience across all devices.
