3

I am getting output from a database. Sometimes I am getting 2 div and sometimes I am getting 3 div so I have to set the equal width of the div. I mean if there are 2 div then set 50% of each and if there are 3 div then set 33.33% of each. Would you help me in this?

 #container1
  {
    width: 100%;
    display: inline-flex;
  }
#container1 div{
      color: #fff;
    display: inline-block;
    height: 100%;
    width: 50%;
    background: #24252A;
    text-align: center;
    cursor: default;
    padding: 2em 0;
}

#container1 div:nth-of-type(2) {
    background: red;
    }

#container1 div:nth-of-type(3)
{
    width: 33.33% !important;
}
<div id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
  <div></div>
</div>
Naren Verma
  • 2,205
  • 5
  • 38
  • 95
  • You can define widths based on number of children using the answer on this question https://stackoverflow.com/questions/8720931/can-css-detect-the-number-of-children-an-element-has – kinggs Aug 30 '17 at 10:56
  • Not dynamically. Flexbox can set **initial** widths but content will change the size of those boxes to reflect their individiual requirements. – Paulie_D Aug 30 '17 at 10:59

4 Answers4

6

Use flex:1 in child and display:flex in parent

Note: don't need width nor display:inline-block in child

.ct {
  width: 100%;
  display: flex;
}

.ct div {
  color: #fff;
  height: 100%;
  background: #24252A;
  text-align: center;
  cursor: default;
  padding: 2em 0;
  flex: 1
}

.ct div:nth-of-type(2) {
  background: red;
}
<div class="ct" id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
  <div></div>
</div>
<hr />
<div class="ct" id="container2">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
</div>
dippas
  • 58,591
  • 15
  • 114
  • 126
5

Use flex: 1 100% and remove the width:

 #container1
  {
    width: 100%;
    display: inline-flex;
  }
#container1 div{
      color: #fff;
    display: inline-block;
    height: 100%;
    flex: 1 100%;
    background: #24252A;
    text-align: center;
    cursor: default;
    padding: 2em 0;
}

#container1 div:nth-of-type(2) {
    background: red;
    }
<div id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
  <div></div>
</div>

 #container1
  {
    width: 100%;
    display: inline-flex;
  }
#container1 div{
      color: #fff;
    display: inline-block;
    height: 100%;
    flex: 1 100%;
    background: #24252A;
    text-align: center;
    cursor: default;
    padding: 2em 0;
}

#container1 div:nth-of-type(2) {
    background: red;
    }
<div id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
</div>
Huelfe
  • 1,776
  • 16
  • 25
1

Just Use flex-grow: 1; in child element. check updated snippet below

#container1
  {
    width: 100%;
    display: flex;
  }
#container1 div{
      color: #fff;
    display: inline-block;
    height: 100%;
    background: #24252A;
    text-align: center;
    cursor: default;
    padding: 2em 0;
    flex-grow: 1;
}

#container1 div:nth-of-type(2) {
    background: red;
    }
<div id="container1">
  <!--div depanding upload database-->
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>
Super User
  • 9,448
  • 3
  • 31
  • 47
0

#container1, #container2
{
  width: 100%;
  height: 50px;
  display: flex;
  flex-direction: row;
  border:1px solid red;
  margin: 20px 0px;

}
#container1 div, #container2 div{
  flex: 1;
  height: 100%;
  border: 1px solid gray;

}
<div id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
  <div></div>
</div>
<div id="container2">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
</div>

Try to use display: flex.

kyun
  • 9,710
  • 9
  • 31
  • 66