5

How is it possible to display two div containers on the start of a row flex parent and the third one at the end? child-3 should be on the right side in the example below.

Here is a quick sample with all cildren on the start of the parent: jsfiddle

.parent {
  height: 50px;
  width: 100%;
  border: 1px solid red;
  display: flex;
  flex-direction: row;
}

.child-1,
.child-2,
.child-3 {
  margin: 2px 1px;
  padding: 2px;
  border: 1px solid blue;
}

.child-1 {}

.child-2 {}

.child-3 {}
<div class="parent">
  <div class="child-1">
    child-1
  </div>
  <div class="child-2">
    child-2
  </div>
  <div class="child-3">
    child-3
  </div>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
JohnDizzle
  • 1,268
  • 3
  • 24
  • 51
  • If you are comfortable with child #3 being taken out of the document flow, you can simply just position it absolutely with `right: 0` in the same parent. – Terry Aug 17 '17 at 08:45

2 Answers2

9

just add margin-left: auto;

.parent {
    height: 50px;
    width: 100%;
    border: 1px solid red;
    display: flex;
    flex-direction: row;
}

.child-1,
.child-2,
.child-3 {
    margin: 2px 1px;
    padding: 2px;
    border: 1px solid blue;
}

.child-1 {}

.child-2 {}

.child-3 {
    margin-left: auto;    /* new line added */
}
<div class="parent">
  <div class="child-1">
   child-1
  </div>
  <div class="child-2">
   child-2
  </div>
  <div class="child-3">
   child-3
  </div>
 </div>
bhv
  • 5,188
  • 3
  • 35
  • 44
0

Wrap the first two elements in a div and the third one in a different div.

Give parent div justify-content: space-between.

Do the same with the new wrapping divs. (Make sure they are also display: flex;)

Check fiddle https://jsfiddle.net/jt6p4a30/2/

Your html should be

<div class="parent">
  <div class="subparent">
  <div class="child-1">
    child-1
  </div>
  <div class="child-2">
    child-2
  </div>
  </div>
  <div class="subparent">
  <div class="child-3">
    child-3
  </div>
  </div>
</div>

and CSS:

.parent {
  height: 50px;
  width: 100%;
  border: 1px solid red;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.subparent {
  display: flex;
  justify-content: space-between;
}
Roysh
  • 1,542
  • 3
  • 16
  • 26