【问题标题】:Html drawing horizontal and vertical lines connecting circle nodesHtml绘制连接圆形节点的水平和垂直线
【发布时间】:2021-05-27 13:14:50
【问题描述】:

我想在我的网页中绘制以下内容:

其中三个按钮是提交按钮。

我设法完成了大部分工作,但在绘制垂直线时遇到了问题。 这是我的代码:

th,
td {
  padding: 10px;
}

table.center {
  margin-left: auto;
  margin-right: auto;
  border-collapse: separate;
  border-spacing: 50px 0;
}

span {
  border-bottom: 2px solid gray;
  border-right: 2px solid gray;
  padding: 2px;
  display: inline-block;
  width: 100%;
}

.button {
  width: 100%;
  padding: 10px;
  font: bold;
  font-size: 20px;
  border: none;
}

.button_blue {
  background-color: LightSkyBlue;
}

.button_red {
  background-color: OrangeRed;
}

.button_yellow {
  background-color: yellow;
}

.boxed {
  width: 600px;
  border: 3px solid green;
  padding: 5px;
  margin: 5px;
  margin-left: auto;
  margin-right: auto;
}

.circle {
  width: 40px;
  height: 40px;
  line-height: 40px;
  border-radius: 50%;
  border: none;
  font-size: 30px;
  font: bold;
  text-align: center;
  display: inline-block;
  ;
}
<div style="text-align:center">
  <u style="color:Red;">
            <h1 style="color:DodgerBlue;font-size:3em;">
                Hashi
            </h1>
        </u>
  <form action="" method="post" novalidate>
    {{ form.hidden_tag() }}
    <p></p>
    <div style="text-align:center">
      <p></p>
      <table class="center">
        <tr>
          <td></td>
          <td style="border-bottom:4px solid black"></td>
        </tr>
        <tr>
          <td style="text-align:center"><text style="color:DodgerBlue;font-size: 2em;"> Build bridges </text></td>
          <td>
            <span> {{ form.create(class_="button button_blue") }} </span>
          </td>
        </tr>
        <tr>
          <td style="text-decoration: line-through; text-decoration-thickness:5px;">
            <div class="circle" style="background-color:yellow;">
              3
            </div>
            &nbsp; &nbsp; &nbsp; &nbsp;
            <div class="circle" style="background-color:OrangeRed;">
              2
            </div>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <div class="circle" style="background-color:lightgreen;">
              5
            </div>
          </td>
          <td>
            <span>{{ form.load(class_="button button_red") }}</span>
          </td>
        </tr>
        <tr>
          <td>
            <div class="circle" style="background-color:blue;color:yellow;">
              4
            </div>
          </td>
          <td>
            <span>{{ form.solve(class_="button button_yellow") }}</span>
          </td>
        </tr>
      </table>
    </div>
  </form>
</div>

【问题讨论】:

  • 如果您不使用表格数据,请不要使用表格。 SVG 或伪元素在这里会有所帮助。
  • 嗨,我可以建议您以稍微不同的方式重新编码,以便例如屏幕阅读器更容易理解结构吗?重要的部分显然是 3 个按钮,它们看起来有点像菜单/导航或列表,大文本可能是标题,圆圈/线条是装饰,可能部分仅由 CSS 和(如@tacoshy 建议)使用伪元素。
  • @A Haworth,我已经尝试过使用装饰,但没有成功。你能告诉我怎么做吗?是的,这 3 个按钮是一个菜单,其余的是装饰。
  • @tacoshy,举个例子会有所帮助

标签: html css


【解决方案1】:

你不应该使用表格进行设计,但如果这是你想要的,那么我会想出一个解决方案。

  1. 不要使用内联样式。将它们重构为类,以便 HTML 可读。

  2. 在所有 TD 上设置 position: relative

  3. 将每个元素放在自己的 TD 中。

  4. 使 圆形 按钮 absolute 位于右下角的顶部。

  5. 使用colspan 将元素相对放置。

  6. 使用border 绘制线条。

/** UNCOMMENT TO REVEAL THE ENTIRE STRUCTURE OF THE TABLE **/
/*td { 
  border: 2px solid rgba(0, 0, 0, 0.3) !important;
}*/

body {
  font-family: Helvetica;
}

h1 {
  color: DodgerBlue;
  font-size: 4em;
  text-decoration: underline;
  text-decoration-color: red;
  text-decoration-thickness: 1px;
  margin: 2rem auto 1rem;
}

table {
  border-collapse: collapse;
  margin-left: auto;
  margin-right: 1rem;
}

td.subtitle {
  color: DodgerBlue;
  font-size: 3em;
  padding: 1rem;
}

td {
  position: relative;
  text-align: center;
  border: 4px none black;
}

td.right-line {
  border-right-style: solid;
}

td.bottom-line {
  border-bottom-style: solid;
}

td.double-stroke {
  border-width: 4px;
  border-style: solid;
}

button {
  display: flex;
  justify-content: center;
  align-items: center;
}

button.big {
  margin: 1rem;
  padding: 1rem 2rem;
  font-size: 1.3rem;
  width: calc(100% - 2rem);
  box-sizing: border-box;
}

button.circle {
  position: absolute;
  right: 0px;
  bottom: 0px;
  transform: translate(50%, 50%);
  
  width: 40px;
  height: 40px;
  line-height: 40px;
  border-radius: 50%;
  border: none;
  font-size: 1.1rem;
  font-weight: bold;
}

.blue.big {
  background-color: LightSkyBlue;
}

.red {
  background-color: OrangeRed;
}

.yellow {
  background-color: yellow;
}

.lightgreen.circle {
  background-color: lightgreen;
}

.blue.circle {
  background-color: blue;
  color:yellow;
}
<form action="" method="post" novalidate>
  <table>
    <tr>
      <td colspan="4"></td>
      <td class="bottom-line"><h1>Hashi</h1></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="3" class="subtitle right-line">Build bridges</td>
      <td><button class="blue big">Create a Puzzle</button></td>
    </tr>
    <tr>
      <td><button class="yellow circle">3</button></td>
      <td class="bottom-line"><button class="red circle">2</button></td>
      <td class="bottom-line right-line" colspan="2"><button class="lightgreen circle">5</button></td>
      <td><button class="red big">Upload External</button></td>
    </tr>
    <tr>
      <td class="right-line double-stroke"><button class="blue circle">4</button></td>
      <td colspan="3">&nbsp;</td>
      <td><button class="yellow big">Run Internal</button></td>
    </tr>
  </table>
</form>

【讨论】:

  • 美丽。我将在我的代码中实现它。顺便说一句,我不必使用 table,我想看看如何在没有 table 的情况下实现它。谢谢
  • 很抱歉再次打扰您,但您的解决方案将圆圈放在单元格的底部,但设计将它们放在单元格的中心。所以我想我不能使用边界。任何帮助将不胜感激。
  • 不用担心。我做了这张桌子,因为我只需要 20 分钟左右就可以拿出一份专注的证明。你可以使用伪类和相对距离,但对于不遵循响应式设计的东西来说,这是很多工作(大约 2 小时?)。图片上的按钮会产生不平衡的印象,但您可以使用transform: translateY 将它们向下移动。但是,这会在水平右上角的笔划和按钮之间创建一个空间。无论如何,我希望你设法解决这个问题。
  • 你可能可以看看我写的一篇旧文章,以获得如何使用伪元素将圆圈与线条“连接”的指针:stackoverflow.com/a/59413479/5526624