【问题标题】:Align table header text with table body text?将表格标题文本与表格正文文本对齐?
【发布时间】:2018-07-07 21:19:39
【问题描述】:

我只是不知道如何将左侧标题中的文本与正文标题中的文本对齐。该表是一个可滚动的表。

.container {
    padding-left:260px;
    table-layout: fixed;
    text-align: left;
}

.tableheader{
    background-color: rgba(255, 255, 255, 0.3);
    width: 1000px;
}

.tablecontent{
    height: 100px;
    width: 1000px;
    overflow-x: auto;
    margin-top: 0px;
    border: 1px solid rgba(255,255,255,0.3);
}

th{
    font-weight: 500;
    font-size: 12px;
    color: rgb(68, 68, 68);
    text-transform: uppercase;
    padding: 20px;
    width: 100px;

}

td{
    padding: 15px;
    width: 100px; 
    vertical-align:middle;
    font-weight: 300;
    font-size: 12px;
    color: rgb(59, 59, 59);
    border-bottom: solid 1px rgba(255,255,255,0.1);
}

::-webkit-scrollbar {
    width: 6px;
} 
::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
} 
::-webkit-scrollbar-thumb {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
}
<section  class="container">
    <div class="tableheader" >
        <table cellpadding="0" cellspacing="0" border="0">
            <thead>
                <tr>
                    <th>Username</th>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Status</th>
                    <th>Time</th>
                    <th>Date</th>
                </tr>
            </thead>
        </table>
    </div>
    <div class="tablecontent">
        <table cellpadding="0" cellspacing="0" border="0">
            <tbody class="tablecontent">
                <?php echo $stafftable?>
            </tbody>        
        </table>
    </div>
</section>

如您所见,表格标题文本对齐方式与表格主体对齐方式不同。我似乎无法找到问题所在。我尝试编辑 th 和 tr 的填充和宽度。

【问题讨论】:

  • fyi.. 我删除了 PHP 标签,因为这与 PHP 无关

标签: html css html-table


【解决方案1】:

调整您的td 填充属性以匹配th 的此属性。在您的示例中,td 具有填充 15px 和 th 具有 20px。它负责左缩进,因此将其调整为相同。在下面的示例中,它设置为20px。考虑在一个地方做,比如

td, th { padding: 20px; }

工作示例:

 .container {
    padding-left:260px;
    table-layout: fixed;
    text-align: left;
}

.tableheader{
    background-color: rgba(255, 255, 255, 0.3);
    width: 1000px;
}

.tablecontent{
    height: 100px;
    width: 1000px;
    overflow-x: auto;
    margin-top: 0px;
    border: 1px solid rgba(255,255,255,0.3);
}

th{
    font-weight: 500;
    font-size: 12px;
    color: rgb(68, 68, 68);
    text-transform: uppercase;
    padding: 20px;
    width: 100px;

}

td{
    padding: 20px; /* CHANGED FROM 15PX TO 20PX */
    width: 100px; 
    vertical-align:middle;
    font-weight: 300;
    font-size: 12px;
    color: rgb(59, 59, 59);
    border-bottom: solid 1px rgba(255,255,255,0.1);
}

::-webkit-scrollbar {
    width: 6px;
} 
::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
} 
::-webkit-scrollbar-thumb {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
}
<section  class="container">
    <div class="tableheader" >
        <table cellpadding="0" cellspacing="0" border="0">
            <thead>
                <tr>
                    <th>Username</th>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Status</th>
                    <th>Time</th>
                    <th>Date</th>
                </tr>
            </thead>
        </table>
    </div>
    <div class="tablecontent">
        <table cellpadding="0" cellspacing="0" border="0">
            <tbody class="tablecontent">
                <tr>
                    <td>Username</td>
                    <td>Name</td>
                    <td>Position</td>
                    <td>Status</td>
                    <td>Time</td>
                    <td>Date</td>
                </tr>
                <tr>
                    <td>Username</td>
                    <td>Name</td>
                    <td>Position</td>
                    <td>Status</td>
                    <td>Time</td>
                    <td>Date</td>
                </tr>
                <tr>
                    <td>Username</td>
                    <td>Name</td>
                    <td>Position</td>
                    <td>Status</td>
                    <td>Time</td>
                    <td>Date</td>
                </tr>
                <tr>
                    <td>Username</td>
                    <td>Name</td>
                    <td>Position</td>
                    <td>Status</td>
                    <td>Time</td>
                    <td>Date</td>
                </tr>
            </tbody>        
        </table>
    </div>
</section>

【讨论】:

  • 哦……哎呀,真是个小错误。谢谢
【解决方案2】:

这是因为你设置了 th 和 td 的不同填充。

试试这个:

.container {
    padding-left:260px;
    table-layout: fixed;
    text-align: left;
}

.tableheader{
    background-color: rgba(255, 255, 255, 0.3);
    width: 1000px;
}

.tablecontent{
    height: 100px;
    width: 1000px;
    overflow-x: auto;
    margin-top: 0px;
    border: 1px solid rgba(255,255,255,0.3);
}

th{
    font-weight: 500;
    font-size: 12px;
    color: rgb(68, 68, 68);
    text-transform: uppercase;
    padding: 15px;
    width: 100px;

}

td{
    padding: 15px;
    width: 100px; 
    vertical-align:middle;
    font-weight: 300;
    font-size: 12px;
    color: rgb(59, 59, 59);
    border-bottom: solid 1px rgba(255,255,255,0.1);
}

::-webkit-scrollbar {
    width: 6px;
} 
::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
} 
::-webkit-scrollbar-thumb {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
<section  class="container">
            <div class="tableheader" >
                <table cellpadding="0" cellspacing="0" border="0">
                    <thead>
                        <tr>
                            <th align="left">Username</th>
                            <th>Name</th>
                            <th>Position</th>
                            <th>Status</th>
                            <th>Time</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="tablecontent">
                <table cellpadding="0" cellspacing="0" border="0">
                    <tbody class="tablecontent">
                         <tr>
                            <td>Username</td>
                            <td>Name</td>
                            <td>Position</td>
                            <td>Status</td>
                            <td>Time</td>
                            <td>Date</td>
                        </tr>
                    </tbody>        
                </table>
            </div>
 </section>

【讨论】:

  • 哦……哎呀,真是个小错误。谢谢
猜你喜欢
  • 2012-12-09
  • 1970-01-01
  • 2021-11-17
  • 2016-07-29
  • 1970-01-01
  • 2012-12-21
  • 2019-04-24
  • 2021-09-08
  • 2011-05-21
相关资源
最近更新 更多