How to create a row of elements of equal width inside an inline container? Possibly using flexbox

StackOverflow https://stackoverflow.com/questions/22834781

  •  26-06-2023
  •  | 
  •  

문제

I'm trying to create a row of elements with the following features:

  1. Are inside an inline-block container (which needs to have content next to it)
  2. Are all the same width
  3. Are auto-sized to be wide enough to contain the widest element without it overflowing
  4. Are auto-sized to be narrow enough to just contain the widest element without it overflowing

My current efforts are using flexbox and haven't been able to fulfil all of these conditions. My latest attempt (tested in Chrome so far) is below but condition 3 is not met.

Is there something I'm missing? Or is there an alternative approach that might work?

live example

div {
  display: inline-block;
}

ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style-type: none;
}

li {
  flex: 1;
}

a {
  display: block;
  padding: 1em;
  border: solid black 1px;
}
<div>
  <ul>
    <li>
      <A href="/">foo</a>
    </li>
    <li>
      <A href="/">barrrrrrrrr</a>
    </li>
    <li>
      <A href="/">foooooooooooo</a>
    </li>
    <li>
      <A href="/">foo</a>
    </li>
    <li>
      <A href="/">hello&nbsp;world</a>
    </li>
  </ul>
</div>

(I'm not using browser compatibility vendor-prefixed versions for this test, so you might have to select a browser with good flexbox support).

도움이 되었습니까?

해결책

I was able to accomplish this by using display: inline-grid on the parent element and then adding the property grid-template-columns: 1fr 1fr 1fr to the parent as well (1fr per column needed).

div.inline {
    display: inline-grid;
    grid-template-columns: 1fr 1fr;
}

div.cell {
    border: 1px dotted grey;
    text-align: center;
}
content
<div class='inline'>
    <div class='cell'>lorem ipsum lorem ipsum</div>
    <div class='cell'>lorem</div>
</div>
content

다른 팁

An alternative would be to use display:table with table-layout:fixed, then setting the width of the underlying cells to 1% forces the browser into using an algorithm to use equal widths for all cells.

div.inline {
    display:inline-block;
    width:250px;
}
.table {
    display: table;
    table-layout: fixed;
    width:100%;
}
.cell {
    display: table-cell;
    border: 1px dotted red;
    width: 1%;
}
content
<div class='inline'>
    <div class='table'>
        <div class='cell'>lorem ipsum lorem ipsum</div>
        <div class='cell'>lorem</div>
    </div>
</div>
content

Would this be an acceptable solution?

If the area for the inline block is wide enough it ticks all your points, if it's not wide enough then it fails 2.

    <!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
<style>
  div {
    display: inline-block;
  }
  ul { 
    display: table; 
    margin: 0; 
    padding: 0; 
    list-style-type: none;
  }
  li {
    display: table-cell;
    width: 20%;
    text-align: center

  }
  a {
    display: block;
    padding: 1em;
    border: solid black 1px;
  }
  </style>
  </head>
<body>
  <div>
<ul>
  <li><A href="/">foo</a></li>
  <li><A href="/">barrrrrrrrr</a></li>
  <li><A href="/">foooooooooooo</a></li>
  <li><A href="/">foo</a></li>
  <li><A href="/">hello&nbsp;world</a></li>
  </ul>
  </div>
</body>
</html>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top