CSS
Elevate Your Layouts with Flex-Box and flex-direction: Row, Column
¶Flex-Box:
CSS Flexbox (Flexible Box Layout) is a layout model that allows you to design complex layouts more efficiently and easily than traditional models. Flexbox is particularly useful for distributing space along a single axis (row or column) and aligning items within a container. Here’s a basic example to get you started:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
display: flex;
/* Flex container properties */
justify-content: space-between; /* Distribute items along the main axis */
align-items: center; /* Align items along the cross axis */
height: 200px; /* Set a fixed height for demonstration */
background-color: #f0f0f0;
padding: 20px;
}
.item {
flex: 1; /* Flex-grow, flex-shrink, flex-basis in one shorthand */
padding: 10px;
border: 1px solid #ccc;
margin: 5px;
}
</style>
<title>Flexbox Example</title>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
</body>
</html>
¶Output:
In this example:
- The container has
display: flex;
, making it a flex container. -
justify-content: space-between;
distributes the items along the main axis with equal space between them. -
align-items: center;
centers the items along the cross axis. - Each item has
flex: 1;
, making them flexible and distribute remaining space equally. - The
padding
,border
, andmargin
properties are used for styling. Feel free to modify the code and experiment with different Flexbox properties to see how they affect the layout. Flexbox is powerful and can handle more complex layouts with additional properties likeflex-direction
,flex-wrap
, and more.
The flex-direction
property in CSS is used within a flex container to define the direction of the main axis. The main axis is the primary axis along which flex items are laid out. There are two main values for flex-direction:
row
and column
.
¶flex-direction: row;
This value sets the main axis to be horizontal. Items are placed along the horizontal axis, and the cross axis is vertical.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
flex-direction: row;
justify-content: space-between;
border: 1px solid #ccc;
padding: 10px;
}
.flex-item {
width: 100px;
height: 100px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
</body>
</html>
¶Output :
In this example, three flex items are arranged in a row with `flex-direction: row;`. The `justify-content: space-between;` property ensures that there is space between the items.¶flex-direction: column;
This value sets the main axis to be vertical. Items are placed along the vertical axis, and the cross axis is horizontal.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
flex-direction: column;
align-items: center;
border: 1px solid #ccc;
padding: 10px;
}
.flex-item {
width: 100px;
height: 100px;
background-color: lightblue;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
</body>
</html>
¶Output :
In this example, three flex items are arranged in a column with `flex-direction: column;`. The `align-items: center;` property centers the items along the cross axis.Crafting Responsive Designs: Images, Fonts, Width, and Height
Mastering Flex-wrap, No-wrap, and Flex-flow for Dynamic CSS Layouts
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development