Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

CSS Grid Layout with 3 even squares and 1 odd square

I am trying to get the following layout using grid: enter image description here

This is my code so far. Row gap and column gap are not quite right in the output. I'm not sure if there is any other property that needs to be set.

div.grid-container {
        display: grid;
        grid-template-columns: 50% 50%;
        column-gap: 1px;
        row-gap: 1px;
    }
    
    div.box-small {
    width: 30px;
    height: 30px;
    background: gray;
    }
    
        div.box-big {
    width: 50px;
    height: 50px;
    background: gray;
    }
<div class="grid-container">
                <div class="box-small"></div>
                <div class="box-big"></div>
                <div class="box-small"></div>
                <div class="box-small"></div>
            </div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You just need to align the small divs to the end of their cells with align-self: end;

Example:

div.grid-container {
  display: inline-grid;
  grid-template-columns: 3fr 5fr;
  column-gap: 10px;
  row-gap: 10px;
  margin: 1em;
  border: 1px solid grey;
}

div.box-small {
  width: 30px;
  height: 30px;
  background: gray;
  align-self: end;
}

div.box-big {
  width: 50px;
  height: 50px;
  background: gray;
}
<div class="grid-container">
  <div class="box-small"></div>
  <div class="box-big"></div>
  <div class="box-small"></div>
  <div class="box-small"></div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...