user6224087
user6224087

Reputation:

How to make inline-flex not affect one of the child

I have an HTML here.

<style>
    .parent {
        display: inline-flex;
    }
</style>

<div class="parent">
    <div class="child1"></div>
    <div class="child2"></div>
    <div class="image"><div>
</div>

Here I wanted child1 and image div's to come in one line so I put it inside parent div with property inline flex. But at the same time I don't want child1 and child2 divs to to come in the same line. Any solution available for that?

Upvotes: 0

Views: 1726

Answers (3)

paceaux
paceaux

Reputation: 1890

Using flex box, the most effective way I can see doing this is with this:

.parent {
  display: inline-flex;
  flex-wrap: wrap;
}

.child1,
.child2,
.image {
  flex-grow: 1;
  flex-basis: 50%;
}

.child2 {
  order: 1;
}


/*DEMO STYLES ONLY*/

.parent {
  height: 6em;
  width: 18em;
  background: rgba(90, 90, 90, .2)
}

.parent>div {
  height: 2.5em;
  outline: 1px solid red;
}

[class] {
  position: relative;
}

[class]::after {
  color: rgba(65, 65, 65, .8);
  content: attr(class);
  position: absolute;
  bottom: 0;
  right: 0;
}
<div class="parent">
  <div class="child1"></div>
  <div class="child2"></div>
  <div class="image"><div>
 </div>

The noteworthy properties are

  • flex-wrap which allows you to have more than one row
  • order which allows you to put an item at the start of what will be flex ordered (none of the other items get order, so they stay where they are)
  • flex-basis which tells the items at what point they should start growing
  • flex-grow which is optional, but if you choose it, will have the item on the second row fill up all available space

Upvotes: 1

Boschko
Boschko

Reputation: 374

Put your children element in rows

.row { height:100px; overflow hidden;} // assuiming 100px is height of a child
.row > .children { height:100px; float:left;}

or assuming all div's are the same size oyu could enclose each pair of div [1][2][3][4] and float them to wherever you want.

Upvotes: 0

Michael Benjamin
Michael Benjamin

Reputation: 371033

One method would be to enable wrap on the flex container.

Then make div.child2 appear as the last flex item (order: 1) and force it to wrap by setting a big enough width.

You can adjust the widths as you see fit.

.parent {
  display: flex;
  flex-wrap: wrap;
}

.child1, .image {
   flex: 0 0 50%;
}

.child2 {
  flex: 0 0 100%;
  order: 1;
}

* { box-sizing: border-box; }
<div class="parent">
  <div class="child1"></div>
  <div class="child2"></div>
  <div class="image"></div>
</div>

Upvotes: 2

Related Questions