html - 如何合并具有不同格式的两列?

标签 html css

我正在尝试用 HTML 生成一个表格,在第一列中包含一些代码示例,行颜色交替,第二列包含注释。

这是我要实现的目标的粗略模型:

Aim

这就是我当前的代码产生的结果:

Current results

我使用的 HTML 和 CSS 是这些:

    table.codepack {
      margin-left: 250px;
      width: 80%;
    }
    
    col.vba {
      border-left: 1px solid #8bd5e6;
      font-family: Courier, monospace;
      font-size: small:
      padding: 0;
      width: 70%;
    }
    
    col.vba tr:nth-child(odd) {
      background-color: #b8eaef;
    }
    
    col.vba tr:nth-child(even) {
      background-color: #d0eef4;
    }
        
    col.notes {
      width: 30%;
    }
    
    div.comment {
      margin: 3px;
      border: 1px solid #e6b000;
      border-radius: 6px;
      background-color: #fff9e6;
      font-family: Calibri, Arial, sans-serif;
      font-size: small;
      font-style: italic;
    }
<table class=codepack>
  <col class="vba"><col class="notes">
  <tr>
    <td>  Sub Newcode</td>
    <td></td>
  </tr>
  <tr>
    <td>  IF RandomVar = True then</td>
    <td><div class="comment">This is a comment.</div></td>
  </tr>
  <tr>
    <td>  &nbsp;&nbsp;Random2 = Blue</td>
    <td><div class="comment">This is a comment.</div></td>
  </tr>
  <tr>
    <td>  ELSE</td>
    <td></td>
  </tr>
 </table>

有些位正在工作 - 评论 <div> ,例如 - 但我为第一列尝试的所有事情都没有给出好的结果。我试过声明 .vba既独立又特定于trtd ,但无济于事。我也尝试过各种版本的声明 nth-child样式,再次没有成功。

知道如何实现该结果吗?我并不拘泥于使用表格 - 事实上,我不想 - 但我想不出更好的方法。

最佳答案

我能想到的最简单的方法是不用担心特定列中的颜色,然后使用 :nth-of-type 确保每行中的第二个单元格的颜色相同。

tr:nth-child(odd) {
  background-color: #b8eaef;
}

tr:nth-child(even) {
  background-color: #d0eef4;
}

td:nth-of-type(even) {
  background-color: #fff;
}

CodePen Here.

关于html - 如何合并具有不同格式的两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768807/

相关文章:

jquery - 建表时使用 if 语句

python - 点击动态按钮不起作用(python)

jquery - 我该如何调用:parent for this tooltip?

CSS 后对齐

javascript - 如何在 CakePHP 2.6 中为同一用户在多个表中保存数据

html - 如何在CSS中对齐图像和文本?

html - 使用 CSS(响应式)裁剪和缩放图像的特定区域

javascript - 当浏览器最小化时引起用户的注意(跨浏览器桌面通知?)

html - 9 张图像在 HTML 和 CSS 中缩小到 3x3 网格中?

javascript - 如何在 Bootstrap 多选中添加标签?