Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Decimal numbers not working for the span keyword used in grid layout line placement of grid items

I’m trying to achieve a layout like in the image below using CSS Grid Layout:

The desired layout

I’ve tried to do this myself, but was at this layout below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My attempted layout

The Grid container has the red background, and the items has the green background.

This is my code that I used to try:


<div class="wrapper">
            <div class="box1">One</div>
            <div class="box2">Two</div>
            <div class="box3">Three</div>
            <div class="box4">Four</div>
        </div>


html,
body {
    width: 100%;
    height: 100%;
}

body {
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgb(3, 3, 41);
}

.wrapper {
    background-color: red;
    width: 50%;
    height: 50%;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 100px);
}

.wrapper div {
    background-color: greenyellow;
    border: 0.2em solid green;
}

.box1 {
    grid-column: 1 / span 1;
    grid-row: 2 / span 1;
}

.box2 {
    grid-column: 2 / span 1;
    grid-row: 1 / span 1;
}

.box3 {
    grid-column: 2 / span 1;
    grid-row: 3 / span 1;
}

.box4 {
    grid-column: 3 / span 1;
    grid-row: 2 / span 1;
}

I tried using fractional values for the tracks to span span: 2.5, but still won’t work.

I really want to know if it’s possible to achieve such a layout with CSS Grid. Or if you’ve got a better solution, I’d still appreciate.

>Solution :

You don’t want 3 rows, you want (I think) 6, then span the div as 2 rows each.

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgb(3, 3, 41);
}

.wrapper {
  background-color: red;
  padding: 10px;
  width: 100%;
  gap: 4px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(6, 50px);
}

.wrapper div {
  background-color: greenyellow;
  border: 0.2em solid green;
}

.box1 {
  grid-column: 1 / span 1;
  grid-row: 2 / span 2;
}

.box2 {
  grid-column: 2 / span 1;
  grid-row: 1 / span 2;
}

.box3 {
  grid-column: 2 / span 1;
  grid-row: 3 / span 2;
}

.box4 {
  grid-column: 3 / span 1;
  grid-row: 2 / span 2;
}
<div class="wrapper">
  <div class="box1">One</div>
  <div class="box2">Two</div>
  <div class="box3">Three</div>
  <div class="box4">Four</div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading