Is there any way to combine CSS Grid with transforms to move divs around a grid layout?
For example, if the user clicks Box B (it would expand to occupy the space currently held by itself and boxes C and F) , how could I use transforms to slide C and F out of the newly occupied space and into space currently unoccupied within the grid?
Code as follows:
.grid-wrapper {
display: grid;
grid-template-columns: repeat(5, 18% 20px);
grid-template-rows: repeat(3, 30% 20px);
height: 95vh;
width: 95vw;
}
<div class="grid-wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
<div class="box e">E</div>
<div class="box f">F</div>
<div class="box g">G</div>
<div class="box h">H</div>
</div>
CSS Grid isn’t just for creating flat, 2D layouts — we can make the most of the CSS grid and combine it with other CSS properties to do a lot of amazing things, like 3D layouts (and also some great shapes too). If you just want to play with the Codepen, feel free, otherwise read on for some explanations/ step by step process.
In the header block, grid-area: header; needs to be added. The HTML structure is the same as it is in the Flexbox example, but the CSS is quite different to create the grid layout. To begin working with a CSS Grid Layout, it is very important to have display: grid; set on the container.
CSS transforms allow you to move, rotate, scale, and skew elements. In this chapter you will learn about the following CSS property: The numbers in the table specify the first browser version that fully supports the property.
The 'transform' property can accept as many functions as needed, so you can use and combine them any way you want. It's common to use multiple transforms at once. Define only one transform declaration in a rule, otherwise the bottom-most declaration will override the transforms above.
The CSS Grid spec provides a multitude of properties and methods for adjusting your layout.
To adjust the size and placement of any grid item, you can use defined placement (as opposed to auto placement).
Here are some examples:
.grid-wrapper {
display: inline-grid;
grid-template-columns: repeat(3, 75px);
grid-template-rows: repeat(3, 75px);
grid-auto-rows: 75px;
grid-auto-columns: 75px;
grid-gap: 10px;
padding: 10px;
border: 1px solid black;
}
.a {
grid-row: 1 / 2;
grid-column: 1 / 2;
}
.a:hover {
grid-column: 1 / 4;
background-color: orange;
}
.b:hover {
grid-row: 1 / 4;
grid-column: 1 / 3;
background-color: aqua;
}
.c:hover~.box {
grid-column: 1 / 4;
background-color: pink;
}
.h:hover {
grid-column-end: span 2;
background-color: green;
}
.box {
background-color: lightgreen;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
<div class="grid-wrapper">
<div class="box a">A<br>hover</div>
<div class="box b">B<br>hover</div>
<div class="box c">C<br>hover</div>
<div class="box d">D</div>
<div class="box e">E</div>
<div class="box f">F</div>
<div class="box g">G</div>
<div class="box h">H<br>hover</div>
</div>
With regard to this part of your question:
How could I use transforms to slide C and F out of the newly occupied space and into space currently unoccupied within the grid?
The Grid spec actually provides a method for accomplishing this exact behavior.
With grid-auto-flow: dense
, the Grid auto-placement algorithm will look to fill unoccupied cells with items that fit.
7.7. Automatic Placement: the
grid-auto-flow
propertyGrid items that aren’t explicitly placed are automatically placed into an unoccupied space in the grid container by the auto-placement algorithm.
grid-auto-flow
controls how the auto-placement algorithm works, specifying exactly how auto-placed items get flowed into the grid.
dense
If specified, the auto-placement algorithm uses a “dense” packing algorithm, which attempts to fill in holes earlier in the grid if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items.
In the example below, grid-auto-flow: dense
is activated on hover.
.grid-wrapper {
display: inline-grid;
grid-template-columns: repeat(5, 50px);
grid-template-rows: repeat(3, 50px);
grid-auto-rows: 50px;
grid-auto-columns: 50px;
grid-gap: 10px;
padding: 10px;
border: 1px solid black;
}
.grid-wrapper:hover {
grid-auto-flow: dense;
}
.a, .h {
grid-column-end: span 2;
}
.b, .e {
grid-row-end: span 2;
}
.f {
grid-row-end: span 2;
grid-column-end: span 2;
}
.box {
background-color: lightgreen;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.grid-wrapper:hover .g,
.grid-wrapper:hover .h {
background-color: orange;
}
<div class="grid-wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
<div class="box e">E</div>
<div class="box f">F</div>
<div class="box g">G</div>
<div class="box h">H</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With