gpt4 book ai didi

html - 如何创建响应式棋盘

转载 作者:搜寻专家 更新时间:2023-10-31 22:55:50 24 4
gpt4 key购买 nike

我正在尝试创建一个移动友好的响应式棋盘,并在窗口最小化时更改大小,但我似乎无法摆脱内部 div 颜色和外部 div 方形之间的空间。不知道我做错了什么。

HTML:

<body>
<p></p>
<div class="square">

<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->

<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<!--2nd -->

<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->

<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<!--2nd -->

<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->

<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<!--2nd -->

<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->

<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>

</div>

</body>

CSS:

.square {
border: 10px solid gray;
text-align: center;
width: 60vw;
height: 60vw;
margin: auto;
}
.red {
background-color: red;
height: 11.5%;
width: 11.5%;
max-height: 100vw;
max-width: 100vw;
float: left;
padding:0;
margin:auto;

}

.black {
background-color: black;
height: 11.5%;
width: 11.5%;
max-height: 100vw;
max-width: 100vw;
float: left;
padding:0;
margin:auto;
}

最佳答案

你的数学有问题。您为每个正方形指定的宽度为 11.5%,加起来为 92%。应该是 12.5% 加起来达到 100%(即容器的整个宽度)。

.square {
border: 10px solid gray;
text-align: center;
width: 60vw;
height: 60vw;
margin: auto;
}

.red {
background-color: red;
height: 12.5%;
width: 12.5%;
max-height: 100vw;
max-width: 100vw;
float: left;
padding: 0;
margin: auto;
}

.black {
background-color: black;
height: 12.5%;
width: 12.5%;
max-height: 100vw;
max-width: 100vw;
float: left;
padding: 0;
margin: auto;
}
<body>
<p></p>
<div class="square">

<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->

<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<!--2nd -->

<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->

<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<!--2nd -->

<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<!--2nd -->
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<!--2nd -->
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>
<div class="black"></div>
<div class="red"></div>

</div>

</body>

JSFiddle

关于html - 如何创建响应式棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42461537/

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