Flexbox, also known as Flexible Box Layout, is a CSS module that provides an efficient way to organize, align and distribute elements within a container. Its main purpose is to provide a flexible and responsive layout solution, especially useful when dealing with one-dimensional layouts, such as rows or columns.
Here is a description of what it does Flexbox:
Flexbox sets a "flex context" in the parent container by means of the property
display: flex;
. This turns the parent container into a flex container that affects its direct child elements.Elements within the parent container, known as flex items, can adjust their size, order and alignment to suit the desired design.
The parent container has two axes: the main axis (main axis) and the cross axis (cross axis). The main axis is defined through the property
flex-direction
The secondary axis is automatically adjusted according to the main direction.The flexible elements can be distributed along the main axis by using the property
justify-content
which controls its horizontal alignment, or by usingalign-items
which controls its vertical alignment.In addition, you can adjust the individual size of the flexible elements using the property
flex
which defines the stretching and shrinking capacity of the flexible elements in relation to each other.Flexbox also provides properties such as
flex-wrap
to control whether flexible elements should be wrapped in several lines when there is not enough space available, andalign-content
to distribute the additional space along the secondary shaft when there are several lines of flexible elements.
In summary, Flexbox is a powerful CSS tool for creating flexible and responsive layouts, allowing you to easily control the alignment, order and layout of elements within a container.
Here are some examples:
Adding styles to a navigation menu for a bakery website.
body {
font-family: sans-serif;
}
#welcome {
margin-top: 20px;
padding: 0px 20px;
}
img {
margin-left: 20px;
float: right;
max-width: 40%;
min-width: 20%;
}
h1 {
text-align: center;
}
.container {
display: flex;
width: 100%;
background-color: #DE9C51;
color: #ffffffff;
}
.item {
margin: 10px;
}
Nonna's Italian Bakery
Home
Menu
Pasteries
Contact
Welcome!
Nonna's Italian Bakery has something for everyone, including breads,
desserts, gelato, and more.
Stop in today to see what Nonna's cooking up!
.item {
padding: 15px;
margin: 2px 0;
background-color: #F9F9F9F9;
border-radius: 5px;
width: 90%;
align-self: center;
border: 2px solid #DDDDDDDDDDD;
color: #FFA600;
}
h1,
.item {
text-align: center;
font-weight: bold;
}
h1 {
font-family: Verdana;
text-decoration: underline;
}
button {
border-radius: 5px;
margin: 10px 5px;
font-weight: bold;
border: 1px solid darkgray;
background-color: #F9F9F9F9;
}
.buttons {
display: flex;
justify-content: flex-end;
}
body {
background-color: #F1F1F1F1;
}
.movie-list {
width: 80%;
margin: auto;
}
.container {
display: flex;
flex-direction: column-reverse;
align-content: center;
justify-content: center;
}
Best Movies
2017 - Lady Bird
2018 - Black Panther
2019 - Parasite
2020 - One Night of Miami
2021 - Nomadland
document.getElementById("latest").addEventListener("click", updateLatest);
document.getElementById("oldest").addEventListener("click", updateOldest);
function updateLatest() {
document.getElementById("movies").style["flex-direction"] = "column-reverse";
}
function updateOldest() {
document.getElementById("movies").style["flex-direction"] = "column";
}
Navigation links could overflow on smaller phones. A quick fix here would be to allow the content that would overflow to shrink to a second row. The flex-wrap property does this.
.container {
display: flex;
}
.navigation {
padding: 0px;
margin: 0px;
flex-direction: row;
flex-wrap: wrap;
}
.todayPromo {
flex-direction: column;
}
body {
font-family: Arial, sans-serif;
background-color: #FFF9F5;
color: #36454F;
text-align: center;
margin: 0;
}
h1 {
font-size: 50px;
}
h2 {
font-size: 30px;
}
a {
margin: 15px;
text-decoration: none;
color: #36454F;
}
img {
width: 150px;
border: solid 3px #36454F;
border-radius: 5px;
}
img:hover {
cursor: pointer;
}
.item {
margin: 10px;
font-size: 15px;
}
.partners {
text-decoration: underline;
flex-direction: row-reverse;
justify-content: center;
}
.otherPromo {
height: 540px;
flex-direction: column-reverse;
}
Milla's Motos
Milla's Motorcycles
Click Today's Promotions!
Check out our partner sites!
Other Promotions
body {
font-family: Garamond;
background-color: black;
color: white;
}
.accountNav {
display: flex;
flex-direction: row-reverse;
}
button {
background-color: red;
margin: 5px;
padding: 5px;
border: solid white 1px;
color: white;
}
nav {
margin: 5px;
padding: 5px;
}
.container {
display: flex;
}
.list {
flex-direction: column;
}
.playlistImg {
width: 10%;
margin: 10px;
}
.playlist {
display: flex;
flex-direction: row;
}
#selected {
background-color: gray;
}
.comments {
display: flex;
flex-direction: column;
}
.name {
font-weight: bold;
color: gray;
}
Color theory 101
Colors are what make the world beautiful. It is a way to express our emotions and feelings.
emotions and feelings, it is also used to communicate with others.
Take a dive into the theory of colors with Allen Green, painter and
art professor.
Color theory 101
Moon Landing - Documentary
My cat's first vlog
Best Cameras 2022
html,
body {
font-family: 'Arial', sans-serif;
background: lightblue;
}
.contact-page {
margin: 5%;
display: flex;
justify-content: center;
align-items: center;
}
.contact-form {
border-radius: 10px;
background-color: lightgray;
border: 1px solid dimgray;
margin-top: auto;
margin-bottom: auto;
}
.header {
padding: 15px;
}
h2 {
text-align: center;
color: slategray;
margin: 0;
}
.form-body {
background: white;
padding: 10px;
}
.row {
display: flex;
justify-content: center;
}
.input-group {
display: flex;
flex-direction: column;
margin: 10px;
}
.form-footer {
display: flex;
justify-content: flex-end;
padding: 10px;
}
.btn {
padding: 15px;
background-color: turquoise;
font-size: 17px;
border: none;
border-radius: 5px;
color: white;
}
label {
color: darkturquoise;
font-size: 17px;
font-weight: 500;
}
.form-input,
textarea {
font-size: 18px;
height: 34px;
padding-left: 10px;
padding-right: 10px;
color: grey;
border: 1px solid lightgray;
border-radius: 4px;
background: white;
}
textarea {
width: 480px;
height: 100px;
}
Contact Form
Comments(3)
JoshTV
Fantastic!
PrankGuru
Very informative! Please make more of such videos
Dani Talks Tech
Good job!