Home > Mobile >  I want to have 3 items on the right and 1 item on the left using flexbox,
I want to have 3 items on the right and 1 item on the left using flexbox,

Time:12-25

I have a parent element with 4 items inside them how can I make 3 items on the left and one item on the right. using flexbox

In flex direction row, with the help of align self, I can move the items vertically, I thought I can move them along the horizontal axis with justify self, but when I used it, I didn't get an answer, please help me, thank you

this is my code :

body{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
.container{
    border: 2px solid gray;
    display: flex ;
    flex-direction: row;
    align-items: flex-start;
    width: 600px;
    height: 600px;
}
.item{
    width: 100px;
    background-color: lightgray;
    margin: 2px 0;
    font-size: 40px;
    line-height: 100px;
    text-align: center;
}
.item:nth-child(4){
    background-color: orange;
    align-self: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div >
        <div >1</div>
        <div >2</div>
        <div >3</div>
        <div >4</div>
    </div>
</body>
</html>

CodePudding user response:

You can give the last item margin-left: auto to make it align to the right.

body{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
.container{
    border: 2px solid gray;
    display: flex ;
    flex-direction: row;
    align-items: flex-start;
    width: 600px;
    height: 600px;
}
.item{
    width: 100px;
    background-color: lightgray;
    margin: 2px 0;
    font-size: 40px;
    line-height: 100px;
    text-align: center;
}
.item:nth-child(4){
    background-color: orange;
    margin-left: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div >
        <div >1</div>
        <div >2</div>
        <div >3</div>
        <div >4</div>
    </div>
</body>
</html>

  • Related