How to use the justify-content property. In CSS
The justify-content property in CSS Flexbox aligns flex items along the main axis. It can distribute space between items with values like flex-start, flex-end, center, space-between, space-around, and space-evenly, controlling the alignment and spacing within a flex container.
Syntax:
- The value space-between is used for displaying flex items with space between the lines.
justify-content: space-between;
- The value space-around is used for displaying flex items with space between, before and after the lines.
justify-content: space-around;
Example: In this example we example demonstrates using justify-content in CSS Flexbox to distribute space between items. space-around creates equal space around items, while space-between places equal space between items.
<!DOCTYPE html>
<html>
<head>
<title>Space between flexbox</title>
<style>
.flex2 {
display: flex;
justify-content: space-around;
background-color: green;
}
.flex3 {
display: flex;
justify-content: space-between;
background-color: green;
}
.flex-items {
background-color: #f4f4f4;
width: 100px;
height: 50px;
margin: 10px;
text-align: center;
font-size: 40px;
}
h3 {
text-align: center;
}
.geeks {
font-size: 40px;
text-align: center;
color: #009900;
font-weight: bold;
}
</style>
</head>
<body>
<div class="geeks">w3wiki</div>
<h3>Space between flexbox</h3>
<br>
<b>justify-content: space-around </b>
<div class="flex2">
<div class="flex-items">1</div>
<div class="flex-items">2</div>
<div class="flex-items">3</div>
</div>
<br>
<b>justify-content: space-between </b>
<div class="flex3">
<div class="flex-items">1</div>
<div class="flex-items">2</div>
<div class="flex-items">3</div>
</div>
<br>
</body>
</html>
Output:
How to Set Space Between the Flexbox ?
Setting space between Flexbox items involves using properties like justify-content with values like space-between or space-around, and gap, to evenly distribute space between items along the main axis, enhancing layout spacing and alignment in a flexible container.
There are some following approaches:
Table of Content
- Using the justify-content property.
- Using the gap property to Set Space