CSS flexbox
Created By: chatGPT
CSS Flexbox (Flexible Box Layout) is a layout model that allows you to design complex layouts with a simpler and cleaner approach. It enables a responsive design without the need for float or positioning hacks. A flex container expands items to fill available space and distributes space between them efficiently.
.container {
display: flex;
flex-direction: row; /* or column */
justify-content: center; /* Options: flex-start, flex-end, space-between, space-around */
align-items: stretch; /* Options: flex-start, flex-end, center, baseline */
}
To create a flex container, simply set the
display
property to flex
. By default, the flex direction is set to row, meaning child elements will be laid out in a horizontal line..flex-container {
display: flex;
/* You can change the direction */
flex-direction: column; /* Aligns children vertically */
}
The justify-content property controls how space is distributed along the main axis. You can use several values, such as space-between to place equal space between items and space-around to give equal space around each item.
.flex-container {
display: flex;
justify-content: space-between;
}
The align-items property adjusts the alignments of flex items on the cross axis. For example, setting it to center makes all flex items centered vertically (if the direction is row).
.flex-container {
display: flex;
align-items: center;
}
You can also control the flex-grow, flex-shrink and flex-basis of individual flex items. This allows you to specify whether an item should grow to fill available space or shrink if necessary.
.flex-item {
flex: 1; /* Grow to fill space, shrink if needed. */
min-width: 100px; /* Sets a minimum width */
max-width: 300px; /* Sets a maximum width */
}
To wrap flex items, use the flex-wrap property. This can be useful if you want items to move to the next line when they can't fit in one line.
.flex-container {
display: flex;
flex-wrap: wrap;
}
Lastly, if you want to control how items are aligned in a specific space, use align-content which is particularly useful when there's extra space in the cross axis.
.flex-container {
display: flex;
align-content: space-between;
}