纯CSS棋盘与div &没有类或ID,这可能吗?

发布于 2025-01-02 05:25:48 字数 1778 浏览 0 评论 0原文

我有以下布局

<div id="chess">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

是否可以仅使用 css 制作棋盘而不更改上面的 html?这意味着没有类或 ID。 我两天来一直在寻找这样的想法。我尝试使用 nth-child() 和一些变体,但没有成功。

我很好奇这是否可以做到。它是作为一项任务交给某人的。

那么请问,有什么想法吗?

I have the following layout

<div id="chess">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

Is it possible to make a chess board using only css and without changing the above html? That means no classes or ids.
I've been searching for ideas an such for 2 days now. I tried with nth-child() and some variations but no success.

I am awfully curious if this can be done. It was given as an assignment to someone.

So please, any ideas?

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

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

发布评论

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

评论(16

九局 2025-01-09 05:25:48

这是一个有趣的问题。我认为国际象棋棋盘最好用表格来表示,而不是用一系列 div 来表示,因为屏幕阅读器会指示图形所在的行和列。带有表格:

table tr:nth-child(odd) td:nth-child(even) {
  background: #000;
}
table tr:nth-child(even) td:nth-child(odd) {
  background: #000;
}

http://jsfiddle.net/9kWJZ/

This is an interesting problem. I think a chess board is better expressed as a table than as a series of divs, as a screen reader would dictate the rows and columns where the figures are located. With a table:

table tr:nth-child(odd) td:nth-child(even) {
  background: #000;
}
table tr:nth-child(even) td:nth-child(odd) {
  background: #000;
}

http://jsfiddle.net/9kWJZ/

驱逐舰岛风号 2025-01-09 05:25:48

您不必对每个 :nth-child() 进行硬编码。这是缩短它的一种方法。每个选择器对应棋盘上的一行:

#chess div:nth-child(-2n+8), 
#chess div:nth-child(8) ~ div:nth-child(-2n+15), 
#chess div:nth-child(16) ~ div:nth-child(-2n+24),
#chess div:nth-child(24) ~ div:nth-child(-2n+31),
#chess div:nth-child(32) ~ div:nth-child(-2n+40),
#chess div:nth-child(40) ~ div:nth-child(-2n+47),
#chess div:nth-child(48) ~ div:nth-child(-2n+56),
#chess div:nth-child(56) ~ div:nth-child(-2n+63) {
    background-color: #000;
}

jsFiddle 预览

You don't have to hardcode each :nth-child(). Here's one way to shorten it. Each selector corresponds to a row on the chessboard:

#chess div:nth-child(-2n+8), 
#chess div:nth-child(8) ~ div:nth-child(-2n+15), 
#chess div:nth-child(16) ~ div:nth-child(-2n+24),
#chess div:nth-child(24) ~ div:nth-child(-2n+31),
#chess div:nth-child(32) ~ div:nth-child(-2n+40),
#chess div:nth-child(40) ~ div:nth-child(-2n+47),
#chess div:nth-child(48) ~ div:nth-child(-2n+56),
#chess div:nth-child(56) ~ div:nth-child(-2n+63) {
    background-color: #000;
}

jsFiddle preview

深居我梦 2025-01-09 05:25:48

以下方法利用了着色图案每 16 个方格重复一次(从左上角到右下角)这一事实。因此,第一条规则 #chess div:nth-child(16n+1) 为方格 1、17、33 和 49(换句话说,“第一列”)着色。对从 3 到 16 的所有彩色方块重复此操作,每个方块代表一个单独的列。

对于给定的标记,使用 nth-of-typenth-child 并不重要,但是使用其他标记可能会如此,因此 nth- child 是更明显的选择。

for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess div{
     width:22px;height:22px;border:1px solid black;
     float:left; 
}

#chess div:nth-of-type(16n+16),
#chess div:nth-of-type(16n+14),
#chess div:nth-of-type(16n+12),
#chess div:nth-of-type(16n+10),
#chess div:nth-of-type(16n+7),
#chess div:nth-of-type(16n+5),
#chess div:nth-of-type(16n+3),
#chess div:nth-of-type(16n+1){   
    background-color:black;
}

#chess div:nth-of-type(8n+1){   
    clear:left;
}
<div id="chess"></div>

The following approach makes use of the fact that the coloring pattern repeats every 16 squares (counting from top left to bottom right). So, the first rule #chess div:nth-child(16n+1) colors the squares 1,17,33 and 49 (in other words, "the first column"). This is repeated with additional rules for all colored squares from 3 to 16 each representing a separate column.

With the given markup, it doesn't matter if you use nth-of-type or nth-child, however with additional markup it might, so nth-child is kind of the more obvious choice.

for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess div{
     width:22px;height:22px;border:1px solid black;
     float:left; 
}

#chess div:nth-of-type(16n+16),
#chess div:nth-of-type(16n+14),
#chess div:nth-of-type(16n+12),
#chess div:nth-of-type(16n+10),
#chess div:nth-of-type(16n+7),
#chess div:nth-of-type(16n+5),
#chess div:nth-of-type(16n+3),
#chess div:nth-of-type(16n+1){   
    background-color:black;
}

#chess div:nth-of-type(8n+1){   
    clear:left;
}
<div id="chess"></div>

望笑 2025-01-09 05:25:48

在纯 CSS 中,接受的答案看起来是正确的 - 但是,如果你想用 SCSS 缩短它,你可以做一些数学运算:

#chess {
  div {
    background: #fff;
    /* even children on odd rows, odd children on even rows */
    @each $offset in 2 4 6 8 9 11 13 15 {
      &:nth-child(16n + #{$offset}) {
        background: #000;
      }
    }
  }
}

In pure CSS, the accepted answer looks right - however, if you want to shorten this up with SCSS, you can do some maths:

#chess {
  div {
    background: #fff;
    /* even children on odd rows, odd children on even rows */
    @each $offset in 2 4 6 8 9 11 13 15 {
      &:nth-child(16n + #{$offset}) {
        background: #000;
      }
    }
  }
}
ぃ双果 2025-01-09 05:25:48

当然可以做到...

body {
    background-image:
    -moz-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%),
    -moz-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%);
    background-image:
    -webkit-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%),
    -webkit-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%);
    -moz-background-size:100px 100px;
    background-size:100px 100px;
    -webkit-background-size:101px 101px;
    background-position:0 0, 50px 50px;
}

of course it can be done...

body {
    background-image:
    -moz-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%),
    -moz-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%);
    background-image:
    -webkit-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%),
    -webkit-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%);
    -moz-background-size:100px 100px;
    background-size:100px 100px;
    -webkit-background-size:101px 101px;
    background-position:0 0, 50px 50px;
}
|煩躁 2025-01-09 05:25:48

您不能使用 nth-child(odd)nth-child(even) 为方块着色,因为并非所有“奇数”或“偶数”方块都是相同的颜色。从左上角位置“1”开始数,第一行的白色方块为 1, 3, 5, 7。继续进入第二行,白色方块为 10, 12, 14, 16。第三行为返回奇数 17、19、21 和 23。

因此,您可以手动为每个方块着色,如下所示:

#chess {
    /* 8 squares at 30x30px per square */
    width: 240px;
    height:240px;
    background:#000;
}

#chess div {
    width:30px;
    height:30px;
    float:left;
}

#chess div:nth-child(1), /* first row */
#chess div:nth-child(3),
#chess div:nth-child(5),
#chess div:nth-child(7),
#chess div:nth-child(10), /* second row */
#chess div:nth-child(12),
#chess div:nth-child(14),
#chess div:nth-child(16)
/* ... up to 64 ... */
{
    background:#fff;
}

You can't use nth-child(odd) or nth-child(even) to colour the squares, because not all the "odd" or "even" squares are the same colour. Counting from the top-left as position "1", the first row's white squares would be 1, 3, 5, 7. Continuing into the second row, the white squares would be 10, 12, 14, 16. The third row would be back to odd numbers, 17, 19, 21, and 23.

You could therefore manually colour each of the squares as follows:

#chess {
    /* 8 squares at 30x30px per square */
    width: 240px;
    height:240px;
    background:#000;
}

#chess div {
    width:30px;
    height:30px;
    float:left;
}

#chess div:nth-child(1), /* first row */
#chess div:nth-child(3),
#chess div:nth-child(5),
#chess div:nth-child(7),
#chess div:nth-child(10), /* second row */
#chess div:nth-child(12),
#chess div:nth-child(14),
#chess div:nth-child(16)
/* ... up to 64 ... */
{
    background:#fff;
}
旧瑾黎汐 2025-01-09 05:25:48

我意识到我已经迟到了,这个问题已经有几个很好的答案了。

我只想添加一个在处理高级 :nth-child 选择器时易于管理的解决方案。它有点冗长,不像其他一些建议那么优雅,但我发现它很容易阅读和处理。

通过链接 :nth-child 伪类,您可以将您的选择限制在特定范围内。在伪代码中,它可以布局为:

:nth-child( start of range ):nth-child( children to select ):nth-child( end of range )

这可以用于逐行为棋盘着色,如下所示:

/* Start at 1st square, color odd squares until the 8th */
#chess :nth-child(n+1):nth-child(odd):nth-child(-n+8),

/* Even squares from 9th to 16th */
#chess :nth-child(n+9):nth-child(even):nth-child(-n+16),

/* Odd squares from 17th to 24th */
#chess :nth-child(n+17):nth-child(odd):nth-child(-n+24),

/* Even squares from 25th to 32nd */
#chess :nth-child(n+25):nth-child(even):nth-child(-n+32),

/* Odd squares from 33rd to 40th */
#chess :nth-child(n+33):nth-child(odd):nth-child(-n+40),

/* Even squares from 41st to 48th */
#chess :nth-child(n+41):nth-child(even):nth-child(-n+48),

/* Odd squares from 49th to 56th */
#chess :nth-child(n+49):nth-child(odd):nth-child(-n+56),

/* Even squares from 57th to 64th */
#chess :nth-child(n+57):nth-child(even):nth-child(-n+64) {
    background: #000;
}

#chess {
    width: 320px;
    height: 320px;
    border: 1px solid #000;
}

#chess div {
    float: left;
    width: 40px;
    height: 40px;
    background: #fff;
}

#chess :nth-child(n+1):nth-child(odd):nth-child(-n+8),
#chess :nth-child(n+9):nth-child(even):nth-child(-n+16),
#chess :nth-child(n+17):nth-child(odd):nth-child(-n+24),
#chess :nth-child(n+25):nth-child(even):nth-child(-n+32),
#chess :nth-child(n+33):nth-child(odd):nth-child(-n+40),
#chess :nth-child(n+41):nth-child(even):nth-child(-n+48),
#chess :nth-child(n+49):nth-child(odd):nth-child(-n+56),
#chess :nth-child(n+57):nth-child(even):nth-child(-n+64) {
  background: #000;
}
<div id="chess">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

I realize I'm late to the game, and there are already several good answers to this question.

I'd just like to add a solution I find easy to manage when dealing with advanced :nth-child selectors. It's somewhat verbose and not as elegant as a few other suggestions, but I find it easy to read and deal with.

By chaining the :nth-child pseudo classes you can limit your selection to certain ranges only. In pseudo code it can be laid out as:

:nth-child( start of range ):nth-child( children to select ):nth-child( end of range )

This can be used to color the chess board row by row like this:

/* Start at 1st square, color odd squares until the 8th */
#chess :nth-child(n+1):nth-child(odd):nth-child(-n+8),

/* Even squares from 9th to 16th */
#chess :nth-child(n+9):nth-child(even):nth-child(-n+16),

/* Odd squares from 17th to 24th */
#chess :nth-child(n+17):nth-child(odd):nth-child(-n+24),

/* Even squares from 25th to 32nd */
#chess :nth-child(n+25):nth-child(even):nth-child(-n+32),

/* Odd squares from 33rd to 40th */
#chess :nth-child(n+33):nth-child(odd):nth-child(-n+40),

/* Even squares from 41st to 48th */
#chess :nth-child(n+41):nth-child(even):nth-child(-n+48),

/* Odd squares from 49th to 56th */
#chess :nth-child(n+49):nth-child(odd):nth-child(-n+56),

/* Even squares from 57th to 64th */
#chess :nth-child(n+57):nth-child(even):nth-child(-n+64) {
    background: #000;
}

#chess {
    width: 320px;
    height: 320px;
    border: 1px solid #000;
}

#chess div {
    float: left;
    width: 40px;
    height: 40px;
    background: #fff;
}

#chess :nth-child(n+1):nth-child(odd):nth-child(-n+8),
#chess :nth-child(n+9):nth-child(even):nth-child(-n+16),
#chess :nth-child(n+17):nth-child(odd):nth-child(-n+24),
#chess :nth-child(n+25):nth-child(even):nth-child(-n+32),
#chess :nth-child(n+33):nth-child(odd):nth-child(-n+40),
#chess :nth-child(n+41):nth-child(even):nth-child(-n+48),
#chess :nth-child(n+49):nth-child(odd):nth-child(-n+56),
#chess :nth-child(n+57):nth-child(even):nth-child(-n+64) {
  background: #000;
}
<div id="chess">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

雪化雨蝶 2025-01-09 05:25:48

试试这个:

table.CHESS {
    border-collapse: collapse;
}

table.CHESS td {
    width: 50px;
    height: 50px;
    border: solid gray 1px;
}

table tr:nth-child(odd) td:nth-child(odd) {
    background: #000;
}

table tr:nth-child(even) td:nth-child(even) {
    background: #000;
}

Try this :

table.CHESS {
    border-collapse: collapse;
}

table.CHESS td {
    width: 50px;
    height: 50px;
    border: solid gray 1px;
}

table tr:nth-child(odd) td:nth-child(odd) {
    background: #000;
}

table tr:nth-child(even) td:nth-child(even) {
    background: #000;
}
梨涡少年 2025-01-09 05:25:48

所有区域都具有相同的尺寸,因此您可以考虑在父元素上使用渐变着色

#chess {
  /* define the size */
  width: 300px;
  aspect-ratio: 1;
  /* define the grid */
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  /* define the coloration */
  background:
   repeating-conic-gradient(white 0 25%,black 0 50%)
   0 0/calc(100%/4) calc(100%/4);

  
  border: 1px solid;
}
<div id="chess">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

All the area have the same dimension so you can consider a gradient coloration on the parent element

#chess {
  /* define the size */
  width: 300px;
  aspect-ratio: 1;
  /* define the grid */
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  /* define the coloration */
  background:
   repeating-conic-gradient(white 0 25%,black 0 50%)
   0 0/calc(100%/4) calc(100%/4);

  
  border: 1px solid;
}
<div id="chess">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

辞别 2025-01-09 05:25:48

完毕。示例:http://jsfiddle.net/LFVQU/1/

<style type="text/css">
    #chess{
     width:800px;   
     height:800px;
     border:1px;
     border:1px solid #999;
    }
    #chess div{
     width:100px;
     height:100px;  
     float:left;  
    }
#chess div{background: #fff}
#chess div:nth-child(1), #chess div:nth-child(3), #chess div:nth-child(5), #chess div:nth-child(7),
#chess div:nth-child(10), #chess div:nth-child(12), #chess div:nth-child(14), #chess div:nth-child(16),
#chess div:nth-child(17), #chess div:nth-child(19), #chess div:nth-child(21), #chess div:nth-child(23),
#chess div:nth-child(26), #chess div:nth-child(28), #chess div:nth-child(30), #chess div:nth-child(32),
#chess div:nth-child(33), #chess div:nth-child(35), #chess div:nth-child(37), #chess div:nth-child(39),
#chess div:nth-child(42), #chess div:nth-child(44), #chess div:nth-child(46), #chess div:nth-child(48),
#chess div:nth-child(49), #chess div:nth-child(51), #chess div:nth-child(53), #chess div:nth-child(55),
#chess div:nth-child(58), #chess div:nth-child(60), #chess div:nth-child(62), #chess div:nth-child(64)
{
    background-color:#000;
} 
</style>

Done. Sample: http://jsfiddle.net/LFVQU/1/

<style type="text/css">
    #chess{
     width:800px;   
     height:800px;
     border:1px;
     border:1px solid #999;
    }
    #chess div{
     width:100px;
     height:100px;  
     float:left;  
    }
#chess div{background: #fff}
#chess div:nth-child(1), #chess div:nth-child(3), #chess div:nth-child(5), #chess div:nth-child(7),
#chess div:nth-child(10), #chess div:nth-child(12), #chess div:nth-child(14), #chess div:nth-child(16),
#chess div:nth-child(17), #chess div:nth-child(19), #chess div:nth-child(21), #chess div:nth-child(23),
#chess div:nth-child(26), #chess div:nth-child(28), #chess div:nth-child(30), #chess div:nth-child(32),
#chess div:nth-child(33), #chess div:nth-child(35), #chess div:nth-child(37), #chess div:nth-child(39),
#chess div:nth-child(42), #chess div:nth-child(44), #chess div:nth-child(46), #chess div:nth-child(48),
#chess div:nth-child(49), #chess div:nth-child(51), #chess div:nth-child(53), #chess div:nth-child(55),
#chess div:nth-child(58), #chess div:nth-child(60), #chess div:nth-child(62), #chess div:nth-child(64)
{
    background-color:#000;
} 
</style>
还如梦归 2025-01-09 05:25:48
#chess {width:256px; height:256px; border:1px solid;}  
#chess div {width:32px; height:32px; display:inline-block; }
#chess div:nth-child(16n+1), #chess div:nth-child(16n+3),
#chess div:nth-child(16n+5), #chess div:nth-child(16n+7),
#chess div:nth-child(16n+10),#chess div:nth-child(16n+12),
#chess div:nth-child(16n+14),#chess div:nth-child(16n+16) {
  background-color:black;
}

我认为使用 float/clear 的答案更好,这正是我想到的。

#chess {width:256px; height:256px; border:1px solid;}  
#chess div {width:32px; height:32px; display:inline-block; }
#chess div:nth-child(16n+1), #chess div:nth-child(16n+3),
#chess div:nth-child(16n+5), #chess div:nth-child(16n+7),
#chess div:nth-child(16n+10),#chess div:nth-child(16n+12),
#chess div:nth-child(16n+14),#chess div:nth-child(16n+16) {
  background-color:black;
}

I think answers using float/clear are better, just what I came up with.

禾厶谷欠 2025-01-09 05:25:48

对于那些需要 CSS3 棋盘且每个方格都有一个 id 以便您可以将其与 JavaScript 一起使用的人,我可以提出以下解决方案:

https://github.com/vpcom/CSS3-Chess-Board

此处提供演示:http://vincentperrin.com/cr/css3/css3-chess-board/

它是使用 Sass(SCSS 表示法)完成的,但您也可以使用经过处理的 CSS 文件。对于喜欢的人来说,这种事情也可以用玉来完成。

享受!

For those who need a CSS3 chess board with each square having an id so that you can use it with JavaScript, I can propose this solution:

https://github.com/vpcom/CSS3-Chess-Board

A demo is available here: http://vincentperrin.com/cr/css3/css3-chess-board/

It's done with Sass (SCSS notation) but you can use the processed CSS file also available. For those who like, this kind of things can also be done with Jade.

Enjoy!

甜心小果奶 2025-01-09 05:25:48

如果使用 2 个重叠的容器是可以接受的,我认为有一种更直接和“静态”的方法,无需使用花哨的 css 功能:

.chess {
  position: absolute;
  display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(4, 25%);
}

#up > div {
  height: 50px;
  width:  50px;
  margin: 0px 50px 50px 0px;
  background-color: #000;
}

#down > div {
  height: 50px;
  width:  50px;
  margin: 50px 0px 0px 50px;
  background-color: #000;
}
<html>
<div id = "cont">
  <div id = "up" class="chess">
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>

  </div>
  <div id = "down" class="chess">
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  </div>
</div>
</html>

If using 2 overlapped containers is acceptable, I think there is a more straightforward and "static" approach without using fancy css features:

.chess {
  position: absolute;
  display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(4, 25%);
}

#up > div {
  height: 50px;
  width:  50px;
  margin: 0px 50px 50px 0px;
  background-color: #000;
}

#down > div {
  height: 50px;
  width:  50px;
  margin: 50px 0px 0px 50px;
  background-color: #000;
}
<html>
<div id = "cont">
  <div id = "up" class="chess">
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>

  </div>
  <div id = "down" class="chess">
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
      <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  </div>
</div>
</html>

黯淡〆 2025-01-09 05:25:48

受到 BoltClocks 小提琴的启发

for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess {
    width: 8em;
    height: 8em;
    margin: 0.5em;
    border: 2px solid #808080;
}
#chess div {
    float: left;
    width: 1em;
    height: 1em;
    margin-left: 1em;
    background-color: #000;
}
#chess div:nth-child(8n+5){
  margin-left: 0;
}
#chess div:nth-child(32) ~ div{  /* we dont need those :D*/
  display: none;
}
<div id="chess"></div>

可能是我最喜欢的一种,使用网格:

for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess {
  width: 8em;
  height: 8em;
  margin: 0.5em;
  border: 2px solid #808080;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  place-items: stretch;
}

#chess div:nth-child(2n) {
  background-color: #000;
}

/* swapping these 2 rules greatly helps understanding */
#chess div:nth-child(9n+1) {
  /* background-color:red; */
  grid-row-start: 8;
}
<div id="chess"></div>

Inspired by BoltClocks fiddle

for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess {
    width: 8em;
    height: 8em;
    margin: 0.5em;
    border: 2px solid #808080;
}
#chess div {
    float: left;
    width: 1em;
    height: 1em;
    margin-left: 1em;
    background-color: #000;
}
#chess div:nth-child(8n+5){
  margin-left: 0;
}
#chess div:nth-child(32) ~ div{  /* we dont need those :D*/
  display: none;
}
<div id="chess"></div>

Probably my favorite one, using grid:

for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess {
  width: 8em;
  height: 8em;
  margin: 0.5em;
  border: 2px solid #808080;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  place-items: stretch;
}

#chess div:nth-child(2n) {
  background-color: #000;
}

/* swapping these 2 rules greatly helps understanding */
#chess div:nth-child(9n+1) {
  /* background-color:red; */
  grid-row-start: 8;
}
<div id="chess"></div>

记忆で 2025-01-09 05:25:48
<div class="chess" -data-rows="8" -data-cols="8">
  
</div>
<style>
.chess {
  display: flex;
  flex-flow: column;
  border:1px solid #000;
  width:max-content;
}
.row {
  display: flex;
  gap: 1px;
  margin-bottom: 1px;
  width: fit-content;
}
.row:nth-child(even) {
  flex-flow: row-reverse;
}
.column {
  width: 100px;
  height: 100px;
}
.column:nth-child(odd) {
  background-color: #fff;
}
.column:nth-child(even) {
  background-color: #000;
}
</style>
<script>
const chessContainer = document.querySelector(".chess");
const rows = +chessContainer.getAttribute("-data-rows");
const cols = +chessContainer.getAttribute("-data-cols");
for (let i = 0; i < rows; i++) {
  const row = document.createElement("div");
  row.setAttribute("class", "row");
  for (let j = 0; j < cols; j++) {
    const column = document.createElement("div");
    column.setAttribute("class", "column");
    row.appendChild(column);
  }
  chessContainer.appendChild(row);
}

</script>

https://codepen.io/yogi-dad/pen/mdzPRzp

<div class="chess" -data-rows="8" -data-cols="8">
  
</div>
<style>
.chess {
  display: flex;
  flex-flow: column;
  border:1px solid #000;
  width:max-content;
}
.row {
  display: flex;
  gap: 1px;
  margin-bottom: 1px;
  width: fit-content;
}
.row:nth-child(even) {
  flex-flow: row-reverse;
}
.column {
  width: 100px;
  height: 100px;
}
.column:nth-child(odd) {
  background-color: #fff;
}
.column:nth-child(even) {
  background-color: #000;
}
</style>
<script>
const chessContainer = document.querySelector(".chess");
const rows = +chessContainer.getAttribute("-data-rows");
const cols = +chessContainer.getAttribute("-data-cols");
for (let i = 0; i < rows; i++) {
  const row = document.createElement("div");
  row.setAttribute("class", "row");
  for (let j = 0; j < cols; j++) {
    const column = document.createElement("div");
    column.setAttribute("class", "column");
    row.appendChild(column);
  }
  chessContainer.appendChild(row);
}

</script>

https://codepen.io/yogi-dad/pen/mdzPRzp

赢得她心 2025-01-09 05:25:48

让我建议你更干净的CSS:

.divTableRow:nth-child(odd) .divTableCell:nth-child(even), .divTableRow:nth-child(even) .divTableCell:nth-child(odd) {
        background: #999;
    }

.divTableRow:nth-child(odd) .divTableCell:nth-child(even), .divTableRow:nth-child(even) .divTableCell:nth-child(odd) {
    background: #999;
}

.divTable {
    display: table;
    width: 60%;
    float: left;
}
.divTableRow {
    display: table-row;
}
.divTableHeading {
    background-color: #EEE;
    display: table-header-group;
}
.divTableCell, .divTableHead {
    display: table-cell;
    padding: 3px 10px;
    height: 12.5%;
    width: 12.5%;
    text-align: center;
}
.divTableHeading {
    background-color: #EEE;
    display: table-header-group;
    font-weight: bold;
}
.divTableFoot {
    background-color: #EEE;
    display: table-footer-group;
    font-weight: bold;
}
.divTableBody {
    background: white;
    display: table-row-group;
}
<div class="divTable">
   <div class="divTableBody">
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="black-rock1" class="draggable black">♜</div>
         </div>
         <div class="divTableCell">
            <div id="black-knight1" class="draggable black">♞</div>
         </div>
         <div class="divTableCell">
            <div id="black-bishop1" class="draggable black">♝</div>
         </div>
         <div class="divTableCell">
            <div id="black-queen" class="draggable black">♛</div>
         </div>
         <div class="divTableCell">
            <div id="black-king" class="draggable black">♚</div>
         </div>
         <div class="divTableCell">
            <div id="black-bishop2" class="draggable black">♝</div>
         </div>
         <div class="divTableCell">
            <div id="black-knight2" class="draggable black">♞</div>
         </div>
         <div class="divTableCell">
            <div id="black-rack2" class="draggable black">♜</div>
         </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="black-pawn1" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn2" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn3" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn4" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn5" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn6" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn7" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn8" class="draggable black">♟</div>
         </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="white-pawn1" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn2" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn3" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn4" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn5" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn6" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn7" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn8" class="draggable white">♙</div>
         </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="white-rock1" class="draggable white">♖</div>
         </div>
         <div class="divTableCell">
            <div id="white-knight1" class="draggable white">♘</div>
         </div>
         <div class="divTableCell">
            <div id="white-bishop1" class="draggable white">♗</div>
         </div>
         <div class="divTableCell">
            <div id="white-queen" class="draggable white">♕</div>
         </div>
         <div class="divTableCell">
            <div id="white-king" class="draggable white">♔</div>
         </div>
         <div class="divTableCell">
            <div id="white-bishop2" class="draggable white">♗</div>
         </div>
         <div class="divTableCell">
            <div id="white-knight2" class="draggable white">♘</div>
         </div>
         <div class="divTableCell">
            <div id="white-rack2" class="draggable white">♖</div>
         </div>
      </div>
   </div>
</div>

Let me suggest you more clean css:

.divTableRow:nth-child(odd) .divTableCell:nth-child(even), .divTableRow:nth-child(even) .divTableCell:nth-child(odd) {
        background: #999;
    }

.divTableRow:nth-child(odd) .divTableCell:nth-child(even), .divTableRow:nth-child(even) .divTableCell:nth-child(odd) {
    background: #999;
}

.divTable {
    display: table;
    width: 60%;
    float: left;
}
.divTableRow {
    display: table-row;
}
.divTableHeading {
    background-color: #EEE;
    display: table-header-group;
}
.divTableCell, .divTableHead {
    display: table-cell;
    padding: 3px 10px;
    height: 12.5%;
    width: 12.5%;
    text-align: center;
}
.divTableHeading {
    background-color: #EEE;
    display: table-header-group;
    font-weight: bold;
}
.divTableFoot {
    background-color: #EEE;
    display: table-footer-group;
    font-weight: bold;
}
.divTableBody {
    background: white;
    display: table-row-group;
}
<div class="divTable">
   <div class="divTableBody">
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="black-rock1" class="draggable black">♜</div>
         </div>
         <div class="divTableCell">
            <div id="black-knight1" class="draggable black">♞</div>
         </div>
         <div class="divTableCell">
            <div id="black-bishop1" class="draggable black">♝</div>
         </div>
         <div class="divTableCell">
            <div id="black-queen" class="draggable black">♛</div>
         </div>
         <div class="divTableCell">
            <div id="black-king" class="draggable black">♚</div>
         </div>
         <div class="divTableCell">
            <div id="black-bishop2" class="draggable black">♝</div>
         </div>
         <div class="divTableCell">
            <div id="black-knight2" class="draggable black">♞</div>
         </div>
         <div class="divTableCell">
            <div id="black-rack2" class="draggable black">♜</div>
         </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="black-pawn1" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn2" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn3" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn4" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn5" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn6" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn7" class="draggable black">♟</div>
         </div>
         <div class="divTableCell">
            <div id="black-pawn8" class="draggable black">♟</div>
         </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
         <div class="divTableCell"> </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="white-pawn1" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn2" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn3" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn4" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn5" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn6" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn7" class="draggable white">♙</div>
         </div>
         <div class="divTableCell">
            <div id="white-pawn8" class="draggable white">♙</div>
         </div>
      </div>
      <div class="divTableRow">
         <div class="divTableCell">
            <div id="white-rock1" class="draggable white">♖</div>
         </div>
         <div class="divTableCell">
            <div id="white-knight1" class="draggable white">♘</div>
         </div>
         <div class="divTableCell">
            <div id="white-bishop1" class="draggable white">♗</div>
         </div>
         <div class="divTableCell">
            <div id="white-queen" class="draggable white">♕</div>
         </div>
         <div class="divTableCell">
            <div id="white-king" class="draggable white">♔</div>
         </div>
         <div class="divTableCell">
            <div id="white-bishop2" class="draggable white">♗</div>
         </div>
         <div class="divTableCell">
            <div id="white-knight2" class="draggable white">♘</div>
         </div>
         <div class="divTableCell">
            <div id="white-rack2" class="draggable white">♖</div>
         </div>
      </div>
   </div>
</div>

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