如何将表格标题与表格内容对齐?

时间:2015-07-22 09:44:40

标签: html css

因为我正在使用锚标签,所以我使用div构建了一个表。显示为表格。不幸的是,标题与内容不一致。有什么问题?

jsfiddle

HTML

<div class="compare-table">
            <div class="row">
                <div class="tablehead">1 header</div>
                <div class="tablehead">2 header</div>
                <div class="tablehead">3 header</div>
                <div class="tablehead">4 header</div>
                <div class="tablehead">5 header</div>
            </div>
        </div>
   <div class="compare-table">                
            <a >
                <div class="cell">my value 1</div>
                <div class="cell">my value 2</div>
                <div class="cell">my value 3</div>
                <div class="cell">my value 4</div>
                <div class="cell">my value 5</div>
            </a>
</div>

2 个答案:

答案 0 :(得分:5)

您的行必须属于显示为表格的同一父级:http://jsfiddle.net/95rf60r8/2/

<div class="compare-table">
            <div class="row">
                <div class="tablehead">1 header</div>
                <div class="tablehead">2 header</div>
                <div class="tablehead">3 header</div>
                <div class="tablehead">4 header</div>
                <div class="tablehead">5 header</div>
            </div>

            <a class="row">
                <div class="cell">my value 1</div>
                <div class="cell">my value 2</div>
                <div class="cell">my value 3</div>
                <div class="cell">my value 4</div>
                <div class="cell">my value 5</div>
            </a>
</div>

答案 1 :(得分:4)

您正在使用table为您的内容打开新的compare-table

改为使用新的class="row"。请参阅代码段。

.compare-table {
  display: table;
  width: 100%;
  max-width: 1000px;
  padding: 0;
  margin: 0 auto;
  border: none;
  border-collapse: collapse;
  border-spacing: 0;
}
.tablehead {
  display: table-cell;
  padding: 26px 0 26px 38px;
  background-color: white;
  text-align: left;
  font-size: 13px;
  color: #9b9b9b;
  font-weight: 400;
  font-style: normal;
  text-transform: uppercase;
}
.cell {
  display: table-cell;
  padding: 32px 0 32px 38px;
  color: #9b9b9b;
  font-weight: 400;
  font-style: normal;
}
.compare-table .row .cell:first-child {
  color: #003668;
  font-weight: 900;
}
.row {
  display: table-row;
  border-bottom: solid 1px #d9d9d9;
  cursor: pointer;
}
<div class="compare-table">
  <div class="row">
    <div class="tablehead">1 header</div>
    <div class="tablehead">2 header</div>
    <div class="tablehead">3 header</div>
    <div class="tablehead">4 header</div>
    <div class="tablehead">5 header</div>
  </div>
  <div class="row">
    <div class="cell"><a>link goes here</a>
    </div>
    <div class="cell">my value 2</div>
    <div class="cell">my value 3</div>
    <div class="cell">my value 4</div>
    <div class="cell">my value 5</div>
  </div>
</div>

此外:如果您需要链接,则应使用<a>内的<div class="cell">