css - 如何绘制不同边框宽度的偶数表格边框?

标签 css

我实现了一个填字游戏生成器。它生成一个填字游戏,每个单词一行,每个单元格一个字母,关键字用更宽的边框和灰色背景标记,如图所示: Crossword image

为此,我生成的代码大致如下:

#crossword {
   border-spacing: 0px;
   border-collapse:collapse;
}

.word {
  vertical-align: middle;
  align-items: center;
  text-align: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

.cell {
  width: 30px;
  height: 30px;
  margin: 0;
  padding: 0;
}

.letter {
  border: 1px solid black;
}

.keyword {
  border-width: 2px;
  background-color: gray;
}
<table id="crossword">
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
</table>

我的问题在图像中用红色椭圆标记 - 关键字的边框比其余部分宽 1 像素,打破了纵横字谜的顶部和底部线。我尝试删除 border-collapse 属性,但这导致双边框出现在每行连接上。

目前我使用tabletrtd来生成,但是如果有使用div的解决方案s,我很乐意转换。

所以我的问题是:假设我能够唯一地定位每个单元格(包括向它们添加特殊类等),如何使顶部和底部边框出现在关键字单元格内部,而不是外部?

我找到了一个使用 box-shadow 的解决方案,但它并不是真正的跨浏览器,并且涉及很多魔法,所以我宁愿尽可能避免使用它。

最佳答案

通过使用保持表格的最外边框与表格的默认边框相匹配

table#crossword tr:first-child td {
  border-top: 1px solid black;
}
table#crossword tr:last-child td {
  border-bottom: 1px solid black;
}
table#crossword tr td:first-child {
  border-left: 1px solid black;
}
table#crossword tr td:last-child {
  border-right: 1px solid black;
}

它看起来有点低,但除了这个你别无选择才能使最外层边框均匀对齐

演示

#crossword {
   border-spacing: 0px;
   border-collapse:collapse;
}

.word {
  vertical-align: middle;
  align-items: center;
  text-align: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

.cell {
  width: 30px;
  height: 30px;
  margin: 0;
  padding: 0;
}

.letter {
  border: 1px solid black;
}

.keyword {
  border-width: 2px;
  background-color: gray;
}

table#crossword tr:first-child td {
  border-top: 1px solid black;
}
table#crossword tr:last-child td {
  border-bottom: 1px solid black;
}
table#crossword tr td:first-child {
  border-left: 1px solid black;
}
<table id="crossword">
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
    <tr class="word">
        <td class="cell letter">W</td>
        <td class="cell letter keyword">C</td>
        <td class="cell letter">A</td>
    </tr>
</table>

关于css - 如何绘制不同边框宽度的偶数表格边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704405/

相关文章:

css - Snap SVG/CSS 悬停是从左到右而不是从下到上?

css - Win8 上的字体端IE 版本兼容性调试工具(从IE 8 9 10 11 轻松转换)是什么?

html - 蓝图 CSS3,3 列布局

html - 自动调整大小的 div,分别保持与其父级的高度比

php - 为什么我的img不显示?

javascript - 使用 HTML 或 JS/jQuery 时光标移动失真

css - 过渡 : width Change direction

html - 导航栏按具有线性渐变的颜色/图像水平分割

html - Canvas 菜单动画

java - 用 css 替换复杂的嵌套表格布局