CSS网格被覆盖,我不知道为什么

发布于 2025-02-08 20:56:23 字数 807 浏览 1 评论 0原文

我正在尝试创建一个4 x 4个正方形(Divs)的4 x 4网格。我已经制作了带有网格显示的容器div。 16个正方形是行的,但是当我尝试制作4列时,它在开发工具的“样式”选项卡中具有罢工。

这是我在CSS中拥有的:

html,body{
    height: 100%;
    margin: none;
}

#heading{
    height: 250px;
}


#wrapper{
    width: 600px;
    height: 600px;
    margin-left: auto;
    margin-right: auto;
}

#container{
    border: solid 1px;
    display: grid;
    grid-template-columns: 25%, 25%, 25%, 25%;
    height: 600px;
    width: auto;
}

.squares{
    border: solid 1px rgba(0, 0, 0, 0.3);
}

他们由JS创建了16个孩子。这会影响为什么网格无法正常工作吗?

let x = 0;

do{
    const square = document.createElement("div");
square.className = "squares";
square.setAttribute("id","block");
    document.getElementById("container").appendChild(square);
    x++;
}
while(x < 16);

I am trying to create a 4 x 4 grid of 16 squares(divs). I have made the container div with a grid display. The 16 squares are in rows but when I try to make 4 columns it has a strikethrough in the styles tab of the dev tools.

Here is what I have in my CSS:

html,body{
    height: 100%;
    margin: none;
}

#heading{
    height: 250px;
}


#wrapper{
    width: 600px;
    height: 600px;
    margin-left: auto;
    margin-right: auto;
}

#container{
    border: solid 1px;
    display: grid;
    grid-template-columns: 25%, 25%, 25%, 25%;
    height: 600px;
    width: auto;
}

.squares{
    border: solid 1px rgba(0, 0, 0, 0.3);
}

They 16 child divs are created with JS. Would that have an impact on why grid is not working as expected?

let x = 0;

do{
    const square = document.createElement("div");
square.className = "squares";
square.setAttribute("id","block");
    document.getElementById("container").appendChild(square);
    x++;
}
while(x < 16);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疑心病 2025-02-15 20:56:23

无需在之间,网格 - 板块列值。
您也可以将其更改为自动,而不是自己进行计算,如果它们都相同并指定25%
现在看看

let x = 0;

do{
    const square = document.createElement("div");
square.className = "squares";
square.setAttribute("id","block");
    document.getElementById("container").appendChild(square);
    x++;
}
while(x < 16);
html,body{
    height: 100%;
    margin: none;
}

#heading{
    height: 250px;
}


#wrapper{
    width: 600px;
    height: 600px;
    margin-left: auto;
    margin-right: auto;
}

#container{
    border: solid 1px;
    display: grid;
    grid-template-columns: auto auto auto auto;
    height: 600px;
    width: auto;
}

.squares{
    border: solid 1px rgba(0, 0, 0, 0.3);
}
<div id="container">

</div>

No need for the , between the grid-template-columns values.
You can also change it to auto instead of doing the calculation yourself if they are all the same and specify 25%.
take a look now

let x = 0;

do{
    const square = document.createElement("div");
square.className = "squares";
square.setAttribute("id","block");
    document.getElementById("container").appendChild(square);
    x++;
}
while(x < 16);
html,body{
    height: 100%;
    margin: none;
}

#heading{
    height: 250px;
}


#wrapper{
    width: 600px;
    height: 600px;
    margin-left: auto;
    margin-right: auto;
}

#container{
    border: solid 1px;
    display: grid;
    grid-template-columns: auto auto auto auto;
    height: 600px;
    width: auto;
}

.squares{
    border: solid 1px rgba(0, 0, 0, 0.3);
}
<div id="container">

</div>

关于从前 2025-02-15 20:56:23

逗号正在干扰网格 - 板块柱,如果您这样做:

 grid-template-columns: 25% 25% 25% 25%;

应该解决上述问题

The commas are interfering with the grid-template-columns, if you do:

 grid-template-columns: 25% 25% 25% 25%;

should fix the mentioned issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文