search

Home  >  Q&A  >  body text

CSS Grid Layout: Align the baseline of elements that span multiple rows to the bottom row of other columns.

<p>I want to align the baselines of a and c. </p> <p><br /></p> <pre class="brush:css;toolbar:false;">#grid{ display:grid; grid-template-columns: 1fr 1fr; align-items: baseline; } #a{ grid-row: 1 / span 2; grid-column: 1; padding: 8px; background: red; } #b{ grid-row: 1; grid-column: 2; background: yellow; } #c{ grid-row: 2; grid-column: 2; background: blue; }</pre> <pre class="brush:html;toolbar:false;"><div id="grid"> <div id="a">aaaaa</div> <div id="b">bbbbb</div> <div id="c">ccccc</div> </div></pre> <p><br /></p> <p>I tried setting the outer grid to a single line and wrapping b and c in a div with the inline-whatever attribute, but a still always aligns with b instead of c. </p> <p><br /></p> <pre class="brush:css;toolbar:false;">#grid{ display:grid; grid-template-columns: 1fr 1fr; align-items: baseline; } #a{ grid-row: 1; grid-column: 1; padding:8px; background:red; } #d{ display:inline-block; grid-row: 1; grid-column: 2; } #b{ background:yellow; } #c{ background: blue }</pre> <pre class="brush:html;toolbar:false;"><div id="grid"> <div id="a">aaaaa</div> <div id="d"> <div id="b">bbbbb</div> <div id="c">ccccc</div> </div> </div></pre> <p><br /></p> <p>How to align a and c with the baseline? </p>
P粉032649413P粉032649413560 days ago459

reply all(2)I'll reply

  • P粉645569197

    P粉6455691972023-08-03 00:56:55

    Since last baseline is relatively new (I'm using Electron 19 which doesn't support it), I'm looking for alternatives. Referring to this SO post about flex, it turns out I need to wrap the inline-block in another container.


    #grid{
      display:grid;
      grid-template-columns: 1fr 1fr;
      align-items: baseline;
    }
    #a{
      grid-row: 1;
      grid-column: 1;
      padding:8px;
      background:red;
    }
    #d{
      grid-row: 1;
      grid-column: 2;
    }
    #b{
      background:yellow;
    }
    #c{
      background:blue
    }
    .inline-block{
      display:inline-block;
    }
    <div id="grid">
      <div id="a">aaaaa</div>
      <div id="d">
        <div class="inline-block">
          <div id="b">bbbbb</div>
          <div id="c">ccccc</div>
        <div>
      </div>
    </div>


    reply
    0
  • P粉555696738

    P粉5556967382023-08-03 00:54:09

    I believe you want to declare align-items: last baseline.

    "Can I Use align-items: last baseline?" shows a global support rate of 85%.

    For specification terminology, see Flex Container Baseline.


    #grid{
      display: grid;
      grid-template-columns: 1fr 1fr;
      align-items: last baseline;
    }
    
    #a {
      grid-row: span 2;
      padding: 8px;
      background: red;
    }
    #b {
      background: yellow;
    }
    
    #c {
      background: blue;
    }
    <div id="grid">
      <div id="a">aaaaa</div>
      <div id="b">bbbbb</div>
      <div id="c">ccccc</div>
    </div>

    reply
    0
  • Cancelreply