How to position items within a container using Flex Box ?

1. Create a Container with items

Html

<div class=”container”>
<div class=”item”>A</div>
<div class=”item”>B</div>
<div class=”item”>C</div>
</div>

CSS


This content originally appeared on DEV Community and was authored by Danyson

1. Create a Container with items

Html

<div class="container">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
</div>

CSS

.container{
    width: 720px;
    height: 480px;
    background-color: blue;
}

.item{
    font-size: 18px;
    text-align: center;
    padding: 5px 10px;
    border: 1px solid green;
    background-color: orange;
}

2. Apply Flex Box to the container

.container{
    display: flex; /* flex box applied here */
    width: 720px;
    height: 480px;
    background-color: blue;
}

3. Add Flex-Direction

We can now add a ‘flex-direction’ property to the container and assign one of the following values:

flex-direction: row; - Aligns items horizontally, left to right.

image

flex-direction: column; - Aligns items vertically, top to bottom.

image

flex-direction: row-reverse;- Aligns items horizontally, right to left.;`
image

flex-direction: column-reverse;- Aligns items vertically, bottom to top.
image

4. Add Justify-Content

We can place the items at different positions along the container’s main axis by using the ‘justify-content’ property coupled with one of the following values:

justify-content: flex-start;
image

justify-content: flex-end;
image

justify-content: center;
image

justify-content: space-between;
image

justify-content: space-around;
image

justify-content: space-evenly;
image

5. Add Align-Items

We can place the items at different along the container’s cross axis by using the ‘align-items’ property coupled with one of the following values:

align-items: flex-start;
image

align-items: flex-end;
image

align-items: center;
image

align-items: strech;
image

Our Tech works @ Matrix Automata


This content originally appeared on DEV Community and was authored by Danyson


Print Share Comment Cite Upload Translate Updates
APA

Danyson | Sciencx (2021-07-17T21:22:20+00:00) How to position items within a container using Flex Box ?. Retrieved from https://www.scien.cx/2021/07/17/how-to-position-items-within-a-container-using-flex-box/

MLA
" » How to position items within a container using Flex Box ?." Danyson | Sciencx - Saturday July 17, 2021, https://www.scien.cx/2021/07/17/how-to-position-items-within-a-container-using-flex-box/
HARVARD
Danyson | Sciencx Saturday July 17, 2021 » How to position items within a container using Flex Box ?., viewed ,<https://www.scien.cx/2021/07/17/how-to-position-items-within-a-container-using-flex-box/>
VANCOUVER
Danyson | Sciencx - » How to position items within a container using Flex Box ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/17/how-to-position-items-within-a-container-using-flex-box/
CHICAGO
" » How to position items within a container using Flex Box ?." Danyson | Sciencx - Accessed . https://www.scien.cx/2021/07/17/how-to-position-items-within-a-container-using-flex-box/
IEEE
" » How to position items within a container using Flex Box ?." Danyson | Sciencx [Online]. Available: https://www.scien.cx/2021/07/17/how-to-position-items-within-a-container-using-flex-box/. [Accessed: ]
rf:citation
» How to position items within a container using Flex Box ? | Danyson | Sciencx | https://www.scien.cx/2021/07/17/how-to-position-items-within-a-container-using-flex-box/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.