Make the div children resize able and fit parent

Viewed 25

https://jsfiddle.net/98oqr3jb/

I'm trying to make the green elements always fit the container. I don't want min-width: 25%; to be static, I want it to be dynamic. So if I decide I want to add another green div, it will fit the container correctly.

html,body {
  background-color: rgba(40,40,40)
}

.container {
  width: 100%;
  height: 200px;
  background-color: rgba(0,0,40);
}

.green {
  /* Make min width dynamic. I.E if I have 5 items it'd be 20%, if I had 6, 16.6666667. */
  min-width: 25%;
  background-color: rgba(0,40,0);
  height: 180px;
  display: inline-block;
}


<div class="container">
<div class="green">test</div><div class="green">test</div><div class="green">test</div><div class="green">test</div>
</div>
1 Answers

Assign display: flex; to parent(container) and assign flex:1; to child(green)

html,body {
  background-color: rgba(40,40,40)
}

.container {
  width: 100%;
  height: 200px;
  background-color: rgba(0,0,40);
  display:flex;
}

.green {
  /* Make min width dynamic. I.E if I have 5 items it'd be 20%, if I had 6, 16.6666667. */
  background-color: green;
  height: 180px;
  flex: 1;
}
<div class="container">
<div class="green">test</div><div class="green">test</div><div class="green">test</div><div class="green">test</div>
</div>

Related