Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make one flex item fixed width and its siblings all equal size

Tags:

html

css

flexbox

I have four list items and I want three of them to stay the same size and the other one to be at with of 300px.

My HTML:

   <ul class="test">
      <li class="desc">Test description</li>
      <li class="test">Test</li>
      <li class="test">Test</li>
      <li class="test">Test</li>
    </ul>

My SASS:

      ul.test {
        flex: 1 0 auto;
        flex-direction: row;
        display: flex;
      }

      li.desc{
        width: 300px;
      }

      li.test{
        background: green;
        flex: 1 0 auto
      }

I want the first li to be 300px in width and then the other three to be the same size.

like image 706
AngularM Avatar asked Mar 08 '23 19:03

AngularM


1 Answers

ul.test {
  display: flex;
  padding: 0;
  list-style: none;
}

li.desc {
  width: 300px;
  flex-shrink: 0;           /* disable default shrinking behavior */
  background-color: orange;
}

li.test {
  flex: 1;                  /* distribute free space evenly among items */
  background: lightgreen;
}
li{ border-right: 1px solid black; }
<ul class="test">
  <li class="desc">Test description</li>
  <li class="test">Test</li>
  <li class="test">Test</li>
  <li class="test">Test</li>
</ul>
like image 154
Michael Benjamin Avatar answered Mar 10 '23 12:03

Michael Benjamin