将表格标题列分成两列

时间:2017-08-14 06:47:31

标签: javascript html css

我想让下面的输出看起来像在图像中一样只将标题分成两行,就像蓝线一样 我需要两个标题为单td请帮助,谢谢。enter image description here



<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 90%;
}
td, th {
    border: 1px solid #dddddd;
    text-align: left; 
}
</style>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr> 
</table>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

这是带蓝色边框的分隔符:

th:last-child:after {
    content: 'Another';
    border-left: 1px solid blue;
    padding-left: 5px;
    margin-left: 10px;
    display: inline-block;
}

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 90%;
}
td, th {
    border: 1px solid #dddddd;
    text-align: left; 
}
th:last-child:after {
    content: 'Another';
    border-left: 1px solid blue;
    padding-left: 5px;
    margin-left: 10px;
    display: inline-block;
}
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr> 
</table>

答案 1 :(得分:1)

只需将colspan="2"添加到上一个td并添加th即可。就是这样。

详细了解w3schools colspan上的colspan

<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 90%;
}
td, th {
    border: 1px solid #dddddd;
    text-align: left; 
}
</style>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>Another</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td colspan="2">Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td colspan="2">Mexico</td>
  </tr> 
</table>

答案 2 :(得分:0)

<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 90%;
}
td, th {
    border: 1px solid #dddddd;
    text-align: left; 
}
</style>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>This??</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td colspan="2">Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td colspan="2">Mexico</td>
  </tr> 
</table>