Position grid behind other items

Hey, so this might be a little more difficult, that is why I came here to ask this, because I have no clue how I would achieve it properly. Following demo: https://jsfiddle.net/3d15u4hp/35/ As you can see, I have a container which is a grid and is set to make twelve columns. Now, I have over twelve items in that container. First, there are the grid columns. These are just cells that should be displayed with blue outline. And second, you can see that there are some items (with text). What I now want to achieve is that these blue grid cells are visibly behind the text items, and the grid columns should still work properly for both of them. So it should look something like the attached image. You might wonder why I would want to do this, and the reason that I cannot do it with pure CSS here is that each of these grid columns there marks a drop zone. (I will use the JS drag and drop api to create some nice builder)
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
No description
21 Replies
Bowser
BowserOP8mo ago
So the requirement is that <div class="grid-column"></div> and <div class="grid-item"> are definitely siblings.
lko
lko8mo ago
.columns,
.actual-content,
.container {
display: grid;
grid-template-columns: repeat(12, 1fr);
position: relative;
}

.grid-column {
outline: 0.125rem solid blue;
height: 100%;
}
.columns,
.actual-content {
grid-column: -1/1;
grid-row-start: 1;
}
.columns,
.actual-content,
.container {
display: grid;
grid-template-columns: repeat(12, 1fr);
position: relative;
}

.grid-column {
outline: 0.125rem solid blue;
height: 100%;
}
.columns,
.actual-content {
grid-column: -1/1;
grid-row-start: 1;
}
<div class="container">
<div class="columns">
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
</div>

<div class="actual-content">
<div class="grid-item">
<p>
Some item
</p>
</div>
<div class="grid-item">
<p>
Some item
</p>
</div>
<div class="grid-item">
<p>
Some item
</p>
</div>
<div class="grid-item">
<p>
Some item
</p>
</div>

</div>
</div>
<div class="container">
<div class="columns">
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
<div class="grid-column"></div>
</div>

<div class="actual-content">
<div class="grid-item">
<p>
Some item
</p>
</div>
<div class="grid-item">
<p>
Some item
</p>
</div>
<div class="grid-item">
<p>
Some item
</p>
</div>
<div class="grid-item">
<p>
Some item
</p>
</div>

</div>
</div>
Chris Bolson
Chris Bolson8mo ago
One method might be to define the corresponding elements (eg 1st and 13th) so that they occupy the same column position:
.container > *:nth-child(12n + 1){
grid-column: 1 / 1;
grid-row: 1/1;
}
.container > *:nth-child(12n + 2){
grid-column: 2 / 2;
grid-row: 1/1;
}
...
.container > *:nth-child(12n + 1){
grid-column: 1 / 1;
grid-row: 1/1;
}
.container > *:nth-child(12n + 2){
grid-column: 2 / 2;
grid-row: 1/1;
}
...
Mannix
Mannix8mo ago
@lko he want them to be siblings 😉 if understand correctly you need to put them on the same row with grid-row and then manipulate grid-column to place them where you want.
Bowser
BowserOP8mo ago
hello, thank you, but they need to be siblings
Chris Bolson
Chris Bolson8mo ago
quick demo using the method I mentioned. https://codepen.io/cbolson/pen/VwNVJLN You could also define the grid areas but that seems like overkill for something like this.
Bowser
BowserOP8mo ago
Damn thank you very much, I thought I needed position absolute to achieve the having behind thank you as well, I will play around a bit and tell you how it turned out 🙂
Mannix
Mannix8mo ago
good luck :thumbup:
Bowser
BowserOP8mo ago
When I have more than 12 columns and want them to automatically wrap to the next line I need minmax() right?
Mannix
Mannix8mo ago
don't think you can do the wrap in this situation
Bowser
BowserOP8mo ago
oof then I cannot use this solution I basically need this for an infinite amount of rows and more than 12 items
Chris Bolson
Chris Bolson8mo ago
you could use JS (which you are going to be using anyway) to do the nth-child calculations
Bowser
BowserOP8mo ago
Let me shortly explain my basic problem so you have better understanding: In the image you can see the data-zone div, and this is basically a drop zone to allow people to drag and drop elements onto a specific grid cell in this case. (using javascript drag and drop api) Only problem I have is that I want people to be able to resize items. But here this is only possible by adding more column span to the zone, but the zone should not be made larger. Just the child item inside the zone should take up multiple columns
No description
Bowser
BowserOP8mo ago
So the only solution I could think of is to have the indiviual items and zones to be siblings but maybe there is a better way
Chris Bolson
Chris Bolson8mo ago
Continuing the idea of 12 columns (max) and your original code of having the 12 drop zones first and then the items, I have updated my codepen to have multiple rows of 12 using this CSS:
.container > *{
grid-row: 1;
}
.container > *:nth-child(24n + 24) ~ * {
grid-row: 2;
}
.container > *:nth-child(24n + 48) ~ * {
grid-row: 3;
}
etc.
.container > *{
grid-row: 1;
}
.container > *:nth-child(24n + 24) ~ * {
grid-row: 2;
}
.container > *:nth-child(24n + 48) ~ * {
grid-row: 3;
}
etc.
However, in reality, it might be easier to have the drop-zone and item next to each other in the html rather than in blocks of 12. (note - I was doing this before reading your last comment but I wanted to mention what I had done anyway)
Bowser
BowserOP8mo ago
However, in reality, it might be easier to have the drop-zone and item next to each other in the html rather than in blocks of 12.
Because it simplifies CSS?
Chris Bolson
Chris Bolson8mo ago
Just because this method requires you to always have 12 drop zones for each row of the grid before adding the “draggable” elements.
Bowser
BowserOP8mo ago
I would have made it that all drop zones come first and then all draggable elements, no blocks of one and then the other Squarespace for example does this
Chris Bolson
Chris Bolson8mo ago
Just reading through your previous message again - are there only 12 drop zones and the an undefined number of droppable items?
Bowser
BowserOP8mo ago
Both are an n amount of items. My current logic is that the amount of droppable items is multiplied by 24 to dynamically get more dropzones when you create new items. And each row has 12 columns (12 drop zones) All that I currently have would work fine if I did not need to resize everything, but this sibling logic is required so I can make use of column span of css grid for the items
Want results from more Discord servers?
Add your server