0

function init() {
        let x = 12;
        let y = 8;
        let content = document.getElementById("content");
        let grid = content.appendChild(document.createElement("div"));
        grid.id = "grid";
        grid.classList.add("grid");
        grid.style.display = "grid";
        grid.style.gridTemplateColumns = `repeat(${x} 4fr 1fr 4fr 1fr 4fr)`;
        grid.style.gridTemplateRows = `repeat(${y} 4fr 1fr 4fr 1fr 4fr)`;

        for (let i = 0; i < x; ++i) {
            for (let j = 0; j < y; ++j) {
                let cell = grid.appendChild(document.createElement("div"));
                cell.classList.add("cell");
                cell.style.gridColumn = `${1 + i * 5} / ${6 + i * 5}`;
                cell.style.gridRow = `${1 + j * 5} / ${6 + j * 5}`;
            }
        }
        
        for (let i = 0; i < (x - 1); ++i) {
            for (let j = 0; j < y; ++j) {
                let cell = grid.appendChild(document.createElement("div"));
                cell.classList.add("h-path");
                cell.style.gridColumn = `${5 + i * 5} / ${7 + i * 5}`;
                cell.style.gridRow = `${3 + j * 5} / ${4 + j * 5}`;
            }
        }

        for (let i = 0; i < x; ++i) {
            for (let j = 0; j < (y - 1); ++j) {
                let cell = grid.appendChild(document.createElement("div"));
                cell.classList.add("v-path");
                cell.style.gridColumn = `${3 + i * 5} / ${4 + i * 5}`;
                cell.style.gridRow = `${5 + j * 5} / ${7 + j * 5}`;
            }
        }
}

document.addEventListener("DOMContentLoaded", init);
.content {
    padding: 0.5rem;
    background-color: grey;
    flex-grow: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.grid {
    display: grid;
    background-color: white;
    /*grid-template-columns: repeat(12, 4fr 1fr 4fr 1fr 4fr);
    grid-template-rows: repeat(8, 4fr 1fr 4fr 1fr 4fr);*/
    padding: 2px;
    gap: 2px;
    width: 50svw;
}

.grid > div {
    box-sizing: border-box;
    border: 1px solid rgb(24 186 96 / 70%);
    border-radius: 5px;
    background-color: rgb(24 186 96 / 50%);
}

.grid > .cell {
    aspect-ratio: 1;
}
<!DOCTYPE html>
<html>
<body>
  <div class="container">
    <div id="content" class="content">
    </div>
  </div>
</body>
</html>

If I uncomment the "grid-template-columns" and "grid-template-rows" properties in the css it works as expected. Why aren't those properties getting set by the script?

I need to set those properties in the script so I can change the grid size. I threw in the grid.style.display = "grid"; line just to make sure I can set style properties on the grid element in the script, and it works. So why aren't the "grid-template-*" properties getting set?

If it matters, I'm using Firefox, I haven't tried a different browser.

Developer Tools - Final HTML Developer Tools - Final Styles

2
  • I checked Crome and it has the same behavior. Commented 13 hours ago
  • Using your broswer devtools inspect facility would show you that the value for the grid-templates-... you are using is not valid. Look up the correct syntax. MDN is a reliable source. developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/… Commented 3 hours ago

1 Answer 1

0

The properties are set correctly, but the syntax you provided for repeat() is incorrect. The problem is that you forgot a comma. Here are corrected lines of code:

grid.style.gridTemplateColumns = `repeat(${x}, 4fr 1fr 4fr 1fr 4fr)`;
grid.style.gridTemplateRows = `repeat(${y}, 4fr 1fr 4fr 1fr 4fr)`;
New contributor
oulol is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.