Search code examples
csscss-gridgrid-layout

How to stretch middle column in a 3 column grid setup


I am trying to setup a header with a single row and three columns 'grid' system. Such as :

* {box-sizing: border-box;}

.wrapper {
    border: 2px solid #f76707;
    border-radius: 5px;
    background-color: #fff4e6;
}

.wrapper > div {
    border: 2px solid #ffa94d;
    border-radius: 5px;
    background-color: #ffd8a8;
    padding: 1em;
    color: #d9480f;
}
.wrapper {
  display: grid;
  grid-template-columns: 100px,1fr,100px;
  grid-template-rows: 1fr;

  grid-template-areas: "a b c";

}
.item1 {
  grid-area: a;
  justify-self:start;
}
.item2 {
  grid-area: b;
  justify-self:stretch;
}
.item3 {
  grid-area: c;
  justify-self:end;
}
<div class="wrapper">
  <div class="item1">Item 1</div>
  <div class="item2">Item 2</div>
  <div class="item3">Item 3</div>
</div>

However the middle column will only center rather than stretch. Here's the fiddle https://jsfiddle.net/mmo7534/pnxtys79/2/

How can I make it stretch ?


Solution

  • Remove the commas from your grid-template-columns value, then you don't have to worry about styling the individual items:

    * {
      box-sizing: border-box;
    }
    
    .wrapper {
      border: 2px solid #f76707;
      border-radius: 5px;
      background-color: #fff4e6;
      display: grid;
      grid-template-columns: 100px 1fr 100px;
    }
    
    .wrapper>div {
      border: 2px solid #ffa94d;
      border-radius: 5px;
      background-color: #ffd8a8;
      padding: 1em;
      color: #d9480f;
    }
    <div class="wrapper">
      <div class="item1">Item 1</div>
      <div class="item2">Item 2</div>
      <div class="item3">Item 3</div>
    
    </div>