Unable to keep the gridlines behind grid items, they're overlapping! how to prevent this?

So I've got 9 tiles and its a grid layout. I want to add 4 gridlines for each tile, top/bot/left/right . But i want the gridlines to be behind the tiles, I even tried negative z-index for each gridline and z-100 for the tile but it doesnt work https://play.tailwindcss.com/fKF7sIPjzR
No description
33 Replies
Chris Bolson
Chris Bolson•2w ago
This is a bit tricky. As you have seen, no amount of z-indexing works. Basically the child elements, your lines, can't be placed behind their parent element, your blocks. I can think of 2 possible solutions to this though no doubt there are others: 1. Create an exact copy of the all of the blocks, placing them in the same place in the grid (elements can share cells) and use these clones just for the lines. You either place these "empty" blocks before the main ones in the HTML or you use z-index. z-index will work here as they would be siblings, not children. 2. Alter your layout slightly, placing the "contents" (text, bg color ect.) in another sibling rahter than directly within the cell itslef. As with the previous example, siblings can be placed above/behind each other so in this case you just need to adjust the z-index of the "content" element. The 2nd option is probably preferable as it avoids adding a load of empty boxes Based on your code, I have moved the "tile" element to a new sibling, removed some styling on the parent element and adjusted the tileclass a bit
<div class="col-start-2 row-span-2 row-start-2 relative">
<!-- content -->
<div class="tile">Color</div>
<!-- lines -->
<div class="absolute top-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute -bottom-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute top-[-150vw] left-0 h-[300vw] w-1 bg-black"></div>
<div class="absolute top-[-150vw] right-0 h-[300vw] w-1 bg-black"></div>
</div>
<div class=" col-start-3 row-start-2 relative">
<!-- content -->
<div class="tile">M</div>
<!-- lines -->
<div class="absolute top-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute -bottom-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute top-[-150vw] left-0 h-[300vw] w-1 bg-black"></div>
<div class="absolute top-[-150vw] right-0 h-[300vw] w-1 bg-black"></div>
</div>
<div class="col-start-2 row-span-2 row-start-2 relative">
<!-- content -->
<div class="tile">Color</div>
<!-- lines -->
<div class="absolute top-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute -bottom-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute top-[-150vw] left-0 h-[300vw] w-1 bg-black"></div>
<div class="absolute top-[-150vw] right-0 h-[300vw] w-1 bg-black"></div>
</div>
<div class=" col-start-3 row-start-2 relative">
<!-- content -->
<div class="tile">M</div>
<!-- lines -->
<div class="absolute top-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute -bottom-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute top-[-150vw] left-0 h-[300vw] w-1 bg-black"></div>
<div class="absolute top-[-150vw] right-0 h-[300vw] w-1 bg-black"></div>
</div>
.tile {
@apply bg-gray-400 p-5 rounded text-white relative h-full z-10;
@apply flex items-center justify-center;
}
.tile {
@apply bg-gray-400 p-5 rounded text-white relative h-full z-10;
@apply flex items-center justify-center;
}
Notes: Personally I would use psuedo elements rather than actual div elements for the lines. I don't really use Tailwind but I believe that the use of @apply for custom styles is not encouraged but things may have changed or I may be missunderstanding your use case Let me know if you would like to see how it could be done with psuedo elements. You would basically use one psuedo element for the horizontal lines and the other for the vertical lines.
siduck
siduckOP•2w ago
will test the 2nd option as for psuedo elements, arent they just before after how could we add 4 lines
Chris Bolson
Chris Bolson•2w ago
yes On the before pseudo element define inline (left and right) borders for the vertical lines On the after pseudo element define block (top and bottom) borders for the horizontal lines
siduck
siduckOP•2w ago
No description
siduck
siduckOP•2w ago
i tried 2nd option, it doesnt work @Chris Bolson as u can see the lines of "color" tile overlaps the framework 🤕 https://play.tailwindcss.com/9mg7nLdUl4
Chris Bolson
Chris Bolson•2w ago
it does work, I have a working demo. You haven't implemented it as I suggested. The "content", eg "Framework" needs to be contained within a div, making it a sibling of the line elements
div class="relative col-start-1 row-span-2">
<div class="tile">Framework </div>
<div class="absolute top-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute -bottom-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute top-[-150vw] left-0 h-[300vw] w-1 bg-black"></div>
<div class="absolute top-[-150vw] right-0 h-[300vw] w-1 bg-black"></div>

</div>
div class="relative col-start-1 row-span-2">
<div class="tile">Framework </div>
<div class="absolute top-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute -bottom-0 left-[-150vw] h-1 w-[300vw] bg-black"></div>
<div class="absolute top-[-150vw] left-0 h-[300vw] w-1 bg-black"></div>
<div class="absolute top-[-150vw] right-0 h-[300vw] w-1 bg-black"></div>

</div>
siduck
siduckOP•2w ago
oh mb, lemme try
siduck
siduckOP•2w ago
phew @Chris Bolson 🥳
No description
Chris Bolson
Chris Bolson•2w ago
Do you want to see how to do it with the psuedo elements or might that just confuse things?
siduck
siduckOP•2w ago
thats way above my lvl 😭 i doubt if thats gonna scale cuz onscroll am scaling the tiles too
Chris Bolson
Chris Bolson•2w ago
of course it would scale
siduck
siduckOP•2w ago
siduck
siduckOP•2w ago
the one which just worked messes up on scale 😭 like its overlappin
siduck
siduckOP•2w ago
No description
Chris Bolson
Chris Bolson•2w ago
Unfortunately what I suspect that is happening is that by defining scale (transform) it creates a new stacking context on each element, bringing it's "borders" above the previous element.
siduck
siduckOP•2w ago
is there not a fix for this else i give up on this feature 😔
Chris Bolson
Chris Bolson•2w ago
which makes me think that the option 1 might be better. I have been testing this on my version and have had similar issues when scaling using the borders within the tiles. On a separate test, using the duplicated "empty" boxes, there were no issues when scaling as they are all behind the the main boxes
Chris Bolson
Chris Bolson•2w ago
This is my original code which I have just adapted using the "duplicate" boxes method (option 1).
MarkBoots
MarkBoots•2w ago
i see you and chris are getting there. just wanted to show another possible approach without using the positioning? layers of subgrid. https://codepen.io/MarkBoots/pen/zxvPORE that way you only have to control the grid-template-cols/rows sizes
Muhammad Zakariyyah
Muhammad Zakariyyah•2w ago
Does this scale?
MarkBoots
MarkBoots•2w ago
i mean you have to adjust the grid-template and declare the grid-areas if you want more content i didnt see the part about animating the thing.. was only working on the original question
Chris Bolson
Chris Bolson•2w ago
The issue with using separate elements for the borders is that, when scaling (with whatever method your are using) you will need to scale both the "content" elements and the "line" elements. This shouoldn't be an issue but just something to bear in mind.
siduck
siduckOP•2w ago
i dont understand this solution 🤔 cant we just add pseudo elements for original tile? instead of making empty boxes
MarkBoots
MarkBoots•2w ago
pseudo elements are stuck to the element, so they wont be able to go behind the tiles that come before it unless you make them relative to the grid, but then you need to position them absolute with set positions
siduck
siduckOP•2w ago
No description
Chris Bolson
Chris Bolson•2w ago
I have adapted your Tailwind example to use these "empty" boxes. I have used named grid areas to hopefully help you understand it better. To clean up the code (Tailwind...) I have moved the "Lines" styles into an @apply. As I mentioned before this isn't recommended but seeing as you are already doing it for the tiles.... Of course this doesn't have any scaling as I don't know how you are doing that part. https://play.tailwindcss.com/L7DdVYPRSj (updated to include hover scale to demonstrate that the lines should also scale with their corresponding boxes) Note - this is very similar to what Mark did for you and to what I previously did in my codepen demo
Tailwind Play
Tailwind Play
An advanced online playground for Tailwind CSS that lets you use all of Tailwind's build-time features directly in the browser.
siduck
siduckOP•2w ago
No description
siduck
siduckOP•2w ago
this works
No description
siduck
siduckOP•2w ago
Although i have to scale the empty boxes too i wish this worked lol, sounded so simple 😭 is it not possible to keep pseudo elements behind of original ones 🤔 i'll just adapt to the above empty boxes* method solution thanks everyone 🙌
MarkBoots
MarkBoots•2w ago
hm, i think it works with outline and using the correct order https://codepen.io/MarkBoots/pen/zxvPORE
siduck
siduckOP•2w ago
i tried this one, messes up on scale
No description
MarkBoots
MarkBoots•2w ago
ah yea, scale/transform create new stacking contexts... then there is no way around it
siduck
siduckOP•2w ago
i'll just stick to the above solution http://play.tailwindcss.com/L7DdVYPRSj
Tailwind Play
Tailwind Play
An advanced online playground for Tailwind CSS that lets you use all of Tailwind's build-time features directly in the browser.

Did you find this page helpful?