我正在建设奥赛罗游戏板在HTML和CSS.我使用<table>元素的董事会,有8行,包含8个单元格.非常标准的元素 struct .

为了在表格单元格元素中绘制黑白的奥赛罗游戏棋子(以及最近移动指示器的红点),我希望能够向<td>个元素添加特定的类,并使用:before伪元素应用特定的背景样式.大概是这样的:

<td class="circle white"></td>块用于显示白色棋子的位置

<td class="circle black"></td>表示应在何处显示黑色块.

和 something like

<td class="circle black most-recent-move"></td>分,其中一块黑色的是最近采取的行动.

So far on that, I'm good using the following HTML 和 CSS:

#board-table tbody tr td.circle:before {
    content: "";
    display:inline-block;
    width:60px;
    height:60px;
    line-height: 60px;
    text-align: center;
    vertical-align: middle;
    border-radius: 50%;
    position:relative;
    left:3px;
}

#board-table tbody tr td{
    background-color:#2EAE52;
    padding:4px;
    min-height:70px;
    min-width:70px;
    height:70px;
    width:70px;
    border:solid 2px #000000;
}

#board-table tbody tr td.white:before {
    border:solid 2px white;
    background: linear-gradient(225deg, #959696 10%, #FFFFFF);
    background-repeat: no-repeat;
}

#board-table tbody tr td.black:before {
    border:solid 2px black;
    background: linear-gradient(225deg, #6c6c6c 10%, #000000);
    background-repeat: no-repeat;
}

#board-table tbody tr td.white.most-recent-move:before {
    border:solid 2px white;
    background-repeat: no-repeat;
    background: radial-gradient(circle, red 15%,rgba(255,0,0,0) 10%) 100%,
                linear-gradient(225deg, #959696 10%, #FFFFFF);
}

#board-table tbody tr td.black.most-recent-move:before {
    content: "";
    border:solid 2px black;
    background-repeat: no-repeat;
    background: radial-gradient(circle, red 15%,rgba(255,0,0,0) 10%) 100%,
                linear-gradient(225deg, #6c6c6c 10%, #000000);
}
<div>
<table id="board-table" cellpadding="0" cellspacing="0">
                        <thead>
                            
                        </thead>
                        <tbody>
                            <tr>
                                <td id="a1" class="circle"></td>
                                <td id="b1" class="circle"></td>
                                <td id="c1" class="circle"></td>
                                <td id="d1" class="circle"></td>
                                <td id="e1" class="circle"></td>
                                <td id="f1" class="circle"></td>
                                <td id="g1" class="circle"></td>
                                <td id="h1" class="circle"></td>
                            </tr>
                            <tr>
                                <td id="a2" class="circle"></td>
                                <td id="b2" class="circle black most-recent-move"></td>
                                <td id="c2" class="circle"></td>
                                <td id="d2" class="circle"></td>
                                <td id="e2" class="circle"></td>
                                <td id="f2" class="circle"></td>
                                <td id="g2" class="circle"></td>
                                <td id="h2" class="circle"></td>
                            </tr>
                            <tr>
                                <td id="a3" class="circle"></td>
                                <td id="b3" class="circle"></td>
                                <td id="c3" class="circle"></td>
                                <td id="d3" class="circle"></td>
                                <td id="e3" class="circle"></td>
                                <td id="f3" class="circle"></td>
                                <td id="g3" class="circle"></td>
                                <td id="h3" class="circle"></td>
                            </tr>
                            <tr>
                                <td id="a4" class="circle"></td>
                                <td id="b4" class="circle"></td>
                                <td id="c4" class="circle"></td>
                                <td id="d4" class="circle white"></td>
                                <td id="e4" class="circle black"></td>
                                <td id="f4" class="circle"></td>
                                <td id="g4" class="circle"></td>
                                <td id="h4" class="circle"></td>
                            </tr>
                            <tr>
                                <td id="a5" class="circle"></td>
                                <td id="b5" class="circle"></td>
                                <td id="c5" class="circle"></td>
                                <td id="d5" class="circle black"></td>
                                <td id="e5" class="circle white"></td>
                                <td id="f5" class="circle"></td>
                                <td id="g5" class="circle"></td>
                                <td id="h5" class="circle"></td>
                            </tr>
                            <tr>
                                <td id="a6" class="circle"></td>
                                <td id="b6" class="circle"></td>
                                <td id="c6" class="circle"></td>
                                <td id="d6" class="circle"></td>
                                <td id="e6" class="circle"></td>
                                <td id="f6" class="circle"></td>
                                <td id="g6" class="circle"></td>
                                <td id="h6" class="circle"></td>
                            </tr>
                            <tr>
                                <td id="a7" class="circle"></td>
                                <td id="b7" class="circle"></td>
                                <td id="c7" class="circle"></td>
                                <td id="d7" class="circle"></td>
                                <td id="e7" class="circle"></td>
                                <td id="f7" class="circle"></td>
                                <td id="g7" class="circle"></td>
                                <td id="h7" class="circle"></td>
                            </tr>
                            <tr>
                                <td id="a8" class="circle"></td>
                                <td id="b8" class="circle"></td>
                                <td id="c8" class="circle"></td>
                                <td id="d8" class="circle"></td>
                                <td id="e8" class="circle"></td>
                                <td id="f8" class="circle"></td>
                                <td id="g8" class="circle"></td>
                                <td id="h8" class="circle"></td>
                            </tr>            
                        </tbody>
                    </table>
                  </div>

The problem I'm having is with adding "star points" to the board. There are 4 star points that I want to put on the board - little black dots at the corners of the inner 16 (4 x 4) squares. These star points need to be shown at all times, whether there are game pieces around them or not. You can see them in this image

I've tried adding a third radial gradient to some of the background gradients I already have but then that third gradient seems to be taking over the entire table cell 和 nothing else gets displayed. I can't seem to figure out a way to position the little black circles where I want them without messing up what I already have.

我也有点不确定我是应该继续使用<td>:before伪元素,还是应该把所有的点都加到<table>:before上,或者其他什么.我通常不会做很多的css工作,所以这对我来说是一个很大的挑战.

推荐答案

如果你对不同的 idea 持开放态度,我会改变标记,使用网格布局,然后使用渐变创建板(带点).

为了演示,我go 掉了所有的ID,但你可以很容易地把它们放回go .

.board {
  --t: 3px; /* line thickness */
  
  display: grid;
  max-width: 800px;
  margin: auto; 
  grid-template-columns: repeat(8,1fr); /* 1fr to keep things responsive, changed with a pixel for fixed size */
  aspect-ratio: 1;
  border: calc(var(--t)/2) solid #000;
  background:
    /* the 4 dots above the grid */
    radial-gradient(calc(3*var(--t)),#000 90%,#0000) 0 0/50% 50%,
    /* the grid lines */
    conic-gradient(from 90deg at var(--t) var(--t),#0000 25%,#000 0)
    calc(var(--t)/-2) calc(var(--t)/-2)/calc(100%/8) calc(100%/8)
    /* the main color */
    #2EAE52;
}
.board > div {
  width: 80%;
  aspect-ratio: 1;
  border-radius: 50%;
  margin: auto;
  box-sizing: border-box;
}
.white {
  border: solid 2px #fff;
  background: var(--b, ) linear-gradient(225deg, #959696 10%, #fff);
}
.black{
  border: solid 2px #000;
  background: var(--b, ) linear-gradient(225deg, #6c6c6c 10%, #000);
}
.most-recent-move {
  --b: radial-gradient(red 15%,#0000 16%),;
}
<div class="board">
  <!-- 1st row -->
  <div class="white"></div><div class="black"></div><div></div><div></div><div></div><div></div><div></div><div></div>
  <!-- 2nd row and so on -->
  <div></div><div class="white most-recent-move"></div><div class="black most-recent-move"></div><div></div><div></div><div></div><div></div><div></div>
  
  <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  
  <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  
  <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  
  <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  
  <div></div><div></div><div></div><div></div><div></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相关问答推荐

D3.js—分组条形图—更新输入数据时出错

Tailwincss:自动设置网格高度

react :事件和转发器在特定代码段中不起作用

如何设置弹性盒子容器的具体大小?

为什么前端框架可以创建自定义属性,而我却被期望在挖掘之前添加数据

有没有什么方法可以很容易地给一个SVG一个插框阴影?

浮动Div在CSS中未按预期工作

如何在css中旋转块时调整内容宽度、高度

HTML 邮箱在 Gmail 中无法正确显示

复制两个

会产生一个空行;复制

元素不会

如何使用html css将图像显示为附加形状

在DIV上应用梯度模糊未如预期工作

将现有内容拆分为三列或部分

如何将两个平行 div 的页面内的表格居中?

如何让一个 div 始终贴在容器的边缘?

高度大于页面高度和页面大于覆盖的 CSS 覆盖

表格中每 4 行条纹一次

如何将某些 div 的高度限制为具有 display flex 行的同一父 div 中的其他 div?

Tailwind CSS 复选框样式不起作用

每次按下按钮时减小字体大小的 jquery 函数