gpt4 book ai didi

html - 使用 CSS Grid 创建棋盘

转载 作者:行者123 更新时间:2023-12-05 06:21:00 28 4
gpt4 key购买 nike

我必须使用 htmlcss(使用 grid)创建一个棋盘。我创建了电路板以及顶部和底部。现在我卡在中间部分了。我创建了每个字段的大小 (80px x 80px) 我的问题是单个 field 类在我的网格底部溢出 (棋盘)。我该如何解决所有 div“字段”都留在我创建的网格中的问题?

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
box-sizing: border-box;
margin: 5%;
background-color: lightgreen;
display: flex;
justify-content: center;
}

.chess-board {
width: 640px;
height: 720px;
background-color: white;
display: grid;
grid-template-columns: repeat(8, 80px);
grid-template-rows: 40px repeat(8, 80px) 40px;
grid-gap: 0;
}

.upper-numbers {
display: grid;
grid-column: 1/9;
grid-row: 1/1;
grid-auto-flow: column;
background-color: moccasin;
}

.lower-numbers {
display: grid;
grid-column: 1/9;
grid-row: 10/10;
grid-auto-flow: column;
background-color: moccasin;
}

.numbers {
display: grid;
grid-auto-flow: column;
text-align: center;
align-self: center;
}


/*.main-field {
display: grid;
grid-column: 1/9;
grid-row: 2/10;
background-color: white ;
}*/

.field {
height: 80px;
width: 80px;
}

.field:nth-child(-2n+8) {
background-color: black;
}
<main class="chess-board">
<section class="upper-numbers">
<div class="numbers">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</section>

<section class="main-field">

<div class="field">a</div>
<div class="field">b</div>
<div class="field">c</div>
<div class="field">d</div>
<div class="field">e</div>
<div class="field">f</div>
<div class="field">g</div>
<div class="field">h</div>


<div class="field">i</div>
<div class="field">j</div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>

<div class="field">q</div>
<div class="field">r</div>
<div class="field">s</div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>

<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>

<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>

<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>

<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>

<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
</section>

<section class="lower-numbers">
<div class="numbers">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</section>
</main>

最佳答案

您遇到的主要问题是网格属性不会继承到容器的子项之外。您正在尝试将网格属性应用于曾孙,但这是行不通的。

在您的代码中,您有创建网格容器的规则:

.chess-board {
display: grid;
grid-template-columns: repeat(8,80px);
grid-template-rows: 40px repeat(8,80px) 40px;
}

但这是您的 HTML:

<main class="chess-board">

<section class="upper-numbers">
<div class="numbers">
<div>1</div>
...
</div>
</section>

<section class="main-field">
<div class="field">a</div>
...
</section>

<section class="lower-numbers">
<div class="numbers">
<div>1</div>
...
</div>
</section>

</main>

.chess-board为容器,只有.upper-numbers.main-field.lower-数字是网格项,可以接受网格命令。

嵌套在网格项中的所有元素不参与网格布局。它们是标准的 block 布局元素。因此他们会忽略您的 grid-template-columnsgrid-template-rows 命令。

我的建议是,如果您想使用单个网格容器,并且在浏览器支持网格“子网格”功​​能之前,我的建议是“展平”HTML 结构。

这是我结合使用网格和 flex 布局构建的一个简单示例。

.chess-board {
margin: auto;
width: 640px;
height: 720px;
background-color: white;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 40px 1fr 40px;
grid-gap: 0;
}

.upper-numbers,
.lower-numbers {
display: flex;
background-color: moccasin;
}

.upper-numbers>div,
.lower-numbers>div {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}

.main-field {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 640px;
}

.field {
height: 80px;
width: 80px;
display: flex;
align-items: center;
justify-content: center;
}

.field:nth-child(-2n+8) {
background-color: black;
}

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
margin: 5%;
background-color: lightgreen;
}
<main class="chess-board">

<section class="upper-numbers">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</section>

<section class="main-field">
<div class="field">a</div>
<div class="field">b</div>
<div class="field">c</div>
<div class="field">d</div>
<div class="field">e</div>
<div class="field">f</div>
<div class="field">g</div>
<div class="field">h</div>
<div class="field">i</div>
<div class="field">j</div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field">q</div>
<div class="field">r</div>
<div class="field">s</div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
<div class="field"></div>
</section>

<section class="lower-numbers">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</section>

</main>

更多详情:

关于html - 使用 CSS Grid 创建棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60126276/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com