Can't reason how to span grid item?
Hello everyone.
I am new to CSS grid.
Given this code
I want the grid item which contains
D
to expand for 2 rows and position at column 2.
I can get it work but can't reason why setting grid-row: 2 / span 2
needs grid-column-start: 2
unless it will be moved to column 1.
Setting only grid-row-start: span 2
, grid-row-end: span 2
or grid-row: span 2
works without setting grid-column-start: 2
,
It would be glad if someone help me figure it out or if there is the reference/document for this behavior.
Here is the codepen :
https://codepen.io/aaronamm/pen/bNNzXqM?editors=1100
Thank you so much.3 Replies
I don't fully understand the reason but I think that it has to do with the way that the algorithm attempts to place elements that have a explicit grid position defined. These take priority over items that don't have an explicit position.
As item D is the only one to which you have assigned a grid position, it is being priotorized over the rest of the items.
Items A and B can take their natural positions as you have explicitly defined row 2 for item D so they are unaffected. As item C does not have an explicit grid position defined, it has lower "priority" than item D so it gets moved to the next available space once the explicit positioning of D has been fulfilled.
I am not sure if that is what is exactly what is happening but I am fairly sure that it is along those lines.
You might get a better understand if you wade through the auto placement algorithm documentation.
Thank you so much. I am so impressed for your prompt reply.
I think what you explained about priority makes sense a lot.
If we convert
to
.
The item D will be moved to the column 1.
Then we change CSS rule to
or
which causes item D to not have higher priority than item C.
Item D stays on column 2.
@Chris Bolson thank you so much, you have maded my day.
👍 Glad to have been able to help.