POV
POV

Reputation: 12005

How to set equal width for flex elements?

Why blocks .b have different width? How to set it equal?

.parent {
  display: flex;
}

.parent>div {
  flex: 1 1 auto;
}

.parent .b {
  display: flex;
}
<div class="parent">
  <div class="cell">
    <div class="b"></div>
  </div>
  <div class="cell">>
    <div class="b"></div>
  </div>
  <div class="cell">>
    <div class="b"></div>
  </div>
  <div class="cell">>
    <div class="b"></div>
  </div>
  <div>

Why blocks <div class="cell"> have different width?

Upvotes: 0

Views: 87

Answers (3)

ShrewdStyle
ShrewdStyle

Reputation: 520

Second re-edit**

First choice you can do is just set a flex on the parent element as this will only effect the first element below that, which in this case is the cell class, i will add a border on the cell class so you can see this in effect

  <div class="parent">
      <div class="cell">
        <div class="b"></div>
      </div>
      <div class="cell">
        <div class="b"></div>
      </div>
      <div class="cell">
        <div class="b"></div>
      </div>
      <div class="cell">
        <div class="b"></div>
      </div>
  <div>
.parent {
  display: flex;
  width: 70%;
}

.cell {
  width: 20%;
  height: 100px;
  border: 1px solid orange;
}

here you can set the size of your parent width which will be the size across your screen, you can then set the width of the .cell childs and they will all then be the same, but only at a maximum of the parent

** second option you can do

Here is a simpler version, and i have added 3 different classes to show how you can choose the sizing you want

<div class="parent">
      <div class="a"></div>
      <div class="b"></div>
      <div class="c"></div>
<div>
.parent {
  display: flex;
  width: 80%;
  height: 100px;
}

.a {
  flex: 40%;
  border: 1px solid greenyellow;
}
.b {
  flex: 20%;
  border: 1px solid orange;
}
.c {
  flex: 20%;
  border: 1px solid blue;
}

Of course you can change them back and have them all be called the same class, and just assign one width and again they will all be the same... i hope this helps

Upvotes: 1

symlink
symlink

Reputation: 12209

Edit: use CSS grid and auto-fit:

.parent {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}

.parent {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}

.parent>div {
  background-color: lightblue;
  margin-left: 5px;
}
<div class="parent">
  <div class="cell">
    <div class="b"></div>
  </div>
  <div class="cell">>
    <div class="b"></div>
  </div>
  <div class="cell">>
    <div class="b"></div>
  </div>
  <div class="cell">>
    <div class="b"></div>
  </div>
</div>

Upvotes: 1

Sifat Haque
Sifat Haque

Reputation: 6057

I think they all are in same width. You need to use this css instead of the .parent>div selector

.cell {
  flex: 1 1 auto;
}

.parent {
  display: flex;
}

.cell {
  flex: 1 1 auto;
}

.parent .b {
  display: flex;
  align-items: center;
  justify-content: center;
}

.b {
  height: 50px;
}
.cell:nth-child(1) {
  background: red;
}
.cell:nth-child(2) {
  background: yellow;
}
.cell:nth-child(3) {
  background: green;
}
.cell:nth-child(4) {
  background: teal;
}
<div class="parent">
  <div class="cell">
    <div class="b">hi</div>
  </div>
  <div class="cell">
    <div class="b">hi</div>
  </div>
  <div class="cell">
    <div class="b">hi</div>
  </div>
  <div class="cell">
    <div class="b">hi</div>
  </div>
<div>

Upvotes: 0

Related Questions