Understanding Grid

Here is the playground: https://jsfiddle.net/4L7v8jhx/1/
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
22 Replies
MarkBoots
MarkBoots•3y ago
ah, it goes wrong with the pseudoselector. It is a bit tricky, put a pseudoselector works on the element itself, not childs. What you are doing with .container:first-child is looking for a .container that IS the first child of something else. what you need is .container > div:first-child so, the div that IS the first-child within a parent
son arg
son argOP•3y ago
Ohhh okay Thanks a bunch! So now, I just repeat with the rest of the div's? So the next one would look like:
.container > div:nth-of-type(2) {
grid-column: 7 / 13;
}
.container > div:nth-of-type(2) {
grid-column: 7 / 13;
}
And so on...
MarkBoots
MarkBoots•3y ago
yes that is an option. but instead of defining the start and end, you could also just use span {number of cells} the start position is the first available spot, and it spans over that number example:
.container > div:nth-of-type(1),
.container > div:nth-of-type(2) {
grid-column: span 6
}
.container > div:nth-of-type(1),
.container > div:nth-of-type(2) {
grid-column: span 6
}
son arg
son argOP•3y ago
Aha, I see Yes, that seems more efficient since it's much less code
MarkBoots
MarkBoots•3y ago
and if you want it even more reduced, you could even use css variables
<div class="container">
<div style="--span: 6">This is column 1 of 2</div>
<div style="--span: 6">This is another column 1 of 2</div>

<div style="--span: 4">This is column 1 of 3</div>
<div style="--span: 4">This is another column 1 of 3</div>
<div style="--span: 4">This is yet another column 1 of 3</div>

<div style="--span: 4">This is column 1 of 3</div>
<div style="--span: 8">This is column 2 of 3</div>

<div style="--span: 3">This is column 1 of 4</div>
<div style="--span: 3">This is another column 1 of 4</div>
<div style="--span: 3">This is yet another column 1 of 4</div>
<div style="--span: 3">This is one more column 1 of 4</div>

<div style="--span: 3">This is column 1 of 4</div>
<div style="--span: 3">This is another column 1 of 4</div>
<div style="--span: 6">This is column 2 of 4</div>
</div>
<div class="container">
<div style="--span: 6">This is column 1 of 2</div>
<div style="--span: 6">This is another column 1 of 2</div>

<div style="--span: 4">This is column 1 of 3</div>
<div style="--span: 4">This is another column 1 of 3</div>
<div style="--span: 4">This is yet another column 1 of 3</div>

<div style="--span: 4">This is column 1 of 3</div>
<div style="--span: 8">This is column 2 of 3</div>

<div style="--span: 3">This is column 1 of 4</div>
<div style="--span: 3">This is another column 1 of 4</div>
<div style="--span: 3">This is yet another column 1 of 4</div>
<div style="--span: 3">This is one more column 1 of 4</div>

<div style="--span: 3">This is column 1 of 4</div>
<div style="--span: 3">This is another column 1 of 4</div>
<div style="--span: 6">This is column 2 of 4</div>
</div>
.container {
display: grid;
gap: 2rem;
grid-template-columns: repeat(12, 1fr);
}

.container div {
grid-column: span var(--span)
}
.container {
display: grid;
gap: 2rem;
grid-template-columns: repeat(12, 1fr);
}

.container div {
grid-column: span var(--span)
}
son arg
son argOP•3y ago
Oh okay, great! Will attempt that Thanks! @MarkBoots 🙂
MarkBoots
MarkBoots•3y ago
so here the span value is set inline as a variable, the div will get that value for the grid-column you're welcome. Good luck Grid is fun!
son arg
son argOP•3y ago
Thanks again! I agree! @MarkBoots I appreciate your help, I just got 1 more question. If I didn't want to have any inline styles and have everything in my CSS file instead, how would I manage that?
MarkBoots
MarkBoots•3y ago
then your option with nth-child(n) and span {number} should work
son arg
son argOP•3y ago
I use SASS, so perhaps still use variables? Ah okay, thanks!
CodeNascher
CodeNascher•3y ago
instead of nth-child i'd give my elements a class named after the amount of spans and then style those classes with the span custom prop from earlier no inline styles that way but same flexibility
son arg
son argOP•3y ago
That's smart too, I'll try that out, thanks!
MarkBoots
MarkBoots•3y ago
yea, thats is an option too. (but if then, why not use custom props. then you don't have to define those classes)
son arg
son argOP•3y ago
I hope it's not a dumb q, but what's a custom prop?
CodeNascher
CodeNascher•3y ago
css variable
MarkBoots
MarkBoots•3y ago
a css variable
son arg
son argOP•3y ago
Ah okay
MarkBoots
MarkBoots•3y ago
hopefully soon we can use attributes to calculate with (they are working on it). that would make it really great. you could do something like <div cell-span="3">
div{
grid-column: span attr(cell-span)
}
div{
grid-column: span attr(cell-span)
}
son arg
son argOP•3y ago
That's cool yeah
CodeNascher
CodeNascher•3y ago
sounds interesting
MarkBoots
MarkBoots•3y ago
now it reads attribute as a string. so its not possible. but we'll be able to define the type of the value (in this case a number) ohw, btw. you said scss you can use this loop. it will generate classes span-1 to span-12
.container {
$columns: 12;

display: grid;
gap: 2rem;
grid-template-columns: repeat( #{$columns}, 1fr);

@for $i from 1 through $columns {
.span-#{$i} {
grid-column: span #{$i}
}
}
}
.container {
$columns: 12;

display: grid;
gap: 2rem;
grid-template-columns: repeat( #{$columns}, 1fr);

@for $i from 1 through $columns {
.span-#{$i} {
grid-column: span #{$i}
}
}
}
CodeNascher
CodeNascher•3y ago
ye, seems like the best solution, but i was too lazy to type all of that on my phone lol 😬
Want results from more Discord servers?
Add your server