I have been trying to modify grid-template-areas
with javascript. I found a property in the css properties list (elem.style
) in the DOM called: gridTemplateAreas
. But I seem to be unable to change it with javascript.
How do i modify the grid-template-areas
-property with javascript?
Here is the code I tried:
window.addEventListener("click", function(){
let elem= document.getElementById("grid");
elem.style.gridTemplateAreas =
"z z z"
"a b c"
"d e f"
console.log("The grid-template area should have been redefined now. The blue block should have move to the top row.");
});
#grid{
width: 100px;
height: 100px;
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "x x x"
"y y z"
"y y z";
}
#div1{
background-color: blue;
grid-area: z;
}
<h3>Click to activate the code</h3>
<div id="grid">
<div id="div1"></div>
</div>
PS: I am using Google Chrome a.t.m.
The grid-template-areas property specifies areas within the grid layout. You can name grid items by using the grid-area property, and then reference to the name in the grid-template-areas property. Each area is defined by apostrophes. Use a period sign to refer to a grid item with no name.
CSS Grid Template Area Basics A CSS grid template area makes a visual representation of the grid using both columns and rows. This makes for a faster design process than when line-based placement or grid-column and grid-row values are used.
The reason your grid-template-areas declaration is invalid, was because it violated rule #2. You try to define each grid-area more than once and copy content into two places on the grid. You've duplicated the area o in the third, fourth and fifth rows. Once you fix that the property value becomes valid.
A grid area is one or more grid cells that make up a rectangular area on the grid. Grid areas are created when you place an item using line-based placement or when defining areas using named grid areas. Grid areas must be rectangular in nature; it is not possible to create, for example, a T- or L-shaped grid area.
Your elem.style.gridTemplateAreas =
"z z z"
"a b c"
"d e f"
Is not a valid statement or it does not do what you want to do.
You want to assign the value "z z z" "a b c" "d e f"
. Therefore, you need to surround it by quotes like this: elem.style.gridTemplateAreas = '"z z z" "a b c" "d e f"';
window.addEventListener("click", function(){
let elem= document.getElementById("grid");
elem.style.gridTemplateAreas = '"z z z" "a b c" "d e f"';
console.log("The grid-template area should have been redefined now. The blue block should have move to the top row.");
});
#grid{
width: 100px;
height: 100px;
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "x x x"
"y y z"
"y y z";
}
#div1{
background-color: blue;
grid-area: z;
}
<h3>Click to activate the code</h3>
<div id="grid">
<div id="div1"></div>
</div>
just change to elem.style.gridTemplateAreas ='"z z z" "a b c" "d e f"';
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