Css Flexbox Part1
Css Flexbox Part1
The display: flex property in CSS is used to create a flex container, which
allows you to use the powerful flexbox layout system. Flexbox provides a
more efficient way to lay out, align, and distribute space among items in a
container, even when their size is unknown or dynamic. It helps solve
common layout problems such as aligning items vertically, distributing
space among items, and making the layout responsive.
Basic Usage
To use flexbox, you need to set the display property of a container
element to flex:
This makes the container a flex container and its direct children flex items.
HTML CODE
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
CSS CODE
.parent{
disply: flex:
.child{
height: 90px;
width: 90px;
margin: 4px;
justify-content
property is part of the CSS Flexible Box Layout Module,
commonly known as Flexbox. This property is used to
align flex items along the main axis of the flex
containerKey Points of justify-content
Main Axis Alignment: The justify-content property aligns
flex items along the main axis (which can be horizontal
or vertical based on flex-direction).
Distribution of Extra Space: It helps distribute any extra
free space that remains after all flex items have been
sized and placed. This can occur when the items are
inflexible (fixed size) or flexible but have reached their
maximum size.
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Document</title>
<style>
.parent{
border: 2px solid black;
display: flex;
justify-content: center;
}
.child{
height: 90px;
width: 90px;
background-color: rgb(255,
191, 0);
border:2px solid black ;
margin: 4px;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</body>
</html>
o/p
Try yourself
After Applying Align item center o/p
Code
.parent{
border: 2px solid black;
display: flex;
justify-content: center;
align-items: center;
height: 90vh;
width: 90vw;
}
.child{
height: 90px;
width: 90px;
background-color: rgb(255,
191, 0);
border:2px solid black ;
margin: 4px;
}
Understanding flex-direction
The flex-direction property specifies the direction in
which flex items are placed inside the flex container. It
can have one of four values: