css - Equal margin between "Dynamic" flex child elements -


i want equal margin between three div if 2 or 1 there should no margin in both side child , if it's 1 child there should no margin.

now code folowing this:

.parent {    display: flex;    height: 120px;    background: #000;    box-sizing: border-box;  }    .child {    height: 100px;    background: #ddd;    flex: 1;  }    .child:nth-child(2),  .child:last-child {    margin: 10px 0 10px 10px;  }    .child:first-child {    margin: 10px 0;  }
<div class="parent">    <div class="child"></div>    <div class="child"></div>    <div class="child"></div>  </div>

am getting result want using above code. wondering if there better way of attaining result.

what want margin should equal in centre no margin should there on sides of wrap

i not looking giving padding parent.

you can add margin-left adjacent child blocks using next sibling selector. demo:

.parent {    display: flex;    margin-bottom: 1em;    background: #000;  }    .child {    height: 50px;    background: #ddd;    flex: 1;    margin-top: 10px;    margin-bottom: 10px;  }    .child + .child {    margin-left: 10px;  }
<div class="parent">    <div class="child"></div>  </div>    <div class="parent">    <div class="child"></div>    <div class="child"></div>  </div>    <div class="parent">    <div class="child"></div>    <div class="child"></div>    <div class="child"></div>  </div>


Comments

Popular posts from this blog

Add new key value to json node in java -

javascript - Highcharts Synchronized charts with missing data points -

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -