我试图编写一个函数,该函数需要用户输入才能创建网格(16x16、32x32等)。我遇到的问题是如何让附加的div正确地扩展到容器中。
如下所示:

function createGrid(input) {
const gridContainer = document.querySelector('.gridContainer');
for (let i = 0; i < input; i++) {
const row = document.createElement('div');
row.className = 'row';
for (let j = 1; j <= input; j++) {
const pixel = document.createElement('div');
pixel.className = 'pixel';
row.appendChild(pixel);
}
gridContainer.appendChild(row);
}
}body {
height: 100vh;
}
.pixel {
flex: 1;
min-width: 8px;
min-height: 8px;
border: 0.1px solid black;
}
.gridContainer {
display: flex;
flex-direction: row;
width: 600px;
height: 600px;
border: 2px solid red;
flex-wrap: wrap;
justify-content: center;
align-content: center;
align-items: stretch;
}<div class="gridContainer">
</div>
<input class="gridSize" type="number">
我以为在div中增加一个最小宽度和高度的flex 1就可以完成这个任务了,所以我肯定遗漏了一些东西。(我是个菜鸟,所以如果这是个超级容易解决的问题,我会提前道歉)。
发布于 2022-05-07 22:38:08
我相信这就是你要找的CSS:
body {
height: 100vh;
}
.row {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.pixel {
flex: 1;
min-width: 8px;
min-height: 8px;
border: 0.1px solid black;
}
.gridContainer {
display: flex;
flex-direction: row;
width: 600px;
height: 600px;
border: 2px solid red;
flex-wrap: nowrap;
justify-content: space-between;
align-content: stretch;
}简而言之,对于.gridContainer和.row类有一些调整。
https://stackoverflow.com/questions/72156763
复制相似问题