简体   繁体   English

为什么“垂直对齐中间”不能与此表格单元一起使用?

[英]Why won't “vertical-align middle” work with this table cell?

I am beginning a new project and I have vertically-aligned content in <td> cells before, so this error came as a bit unexpected. 我正在开始一个新项目,之前我在<td>单元格中有垂直对齐的内容,所以这个错误有点出乎意料。 Also, any research I do (that I have done before, also) yields the information that I would expect (can't apply vertical-align to a block element, apply display: table-cell; , etc.). 此外,我所做的任何研究(我之前也做过)都会产生我期望的信息(不能对块元素应用vertical-align ,应用display: table-cell;等等)。 However: 然而:

I have the following table in my simple html document: 我的简单html文档中有以下表格:

<table id="headingTable">
    <tr>
        <td id="dateHolder">2013-07-24 15:03:18</td>
        <td><h2>City of Okmulgee Authorized Leave Request</h2></td>
        <td><button type="button" class="printBtn">Print Form</button></td>
    </tr>
</table>

With this applicable CSS: 使用此适用的CSS:

table#headingTable
{
    width: 100%;
}

td
{
    vertical-align: middle;
}

h2
{
    font-family: Tahoma, Geneva, sans-serif;
    text-align: center;
}

.printBtn
{
    white-space: nowrap;
    font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
    font-size: 1em;
    border-width: 3px;
    border-color: #959595;
    cursor: pointer;
}

#dateHolder
{
    float: left;
}

The <td id="dateHolder"> cell should show the date and time in the middle, aligned with the next two table cells for that row, but you can see here that it does not: <td id="dateHolder">单元格显示中间的日期和时间,与该行的下两个表格单元格对齐,但您可以在此处看到它不是:

在此输入图像描述

I would like to think that this question is beneath me, but it is apparently not, because I was led to believe that as long as it was an inline element, or at least an element with display: table-cell; 我想这个问题在我身下,但显然不是,因为我被引导相信只要它是一个内联元素,或者至少是一个带有display: table-cell;的元素display: table-cell; , but especially if it IS a table-cell (ie, a <td> element), then vertical-align should be applied as specified in the CSS. ,但特别是如果它一个表格单元格(即<td>元素),则应该按照CSS中的指定应用vertical-align。

I'm sure I could get something working, (using positioning, line-height adjustments, padding, etc.) but the fact that I can't seem to grasp why it doesn't work with the current rules I have set, just begs an answer for my education's sake. 我确信我可以得到一些工作,(使用定位,行高调整,填充等)但事实上我似乎无法理解为什么它不适用于我设置的当前规则,只是为了我的教育而回答问题。

Things I have tried 我尝试过的事情

Using <span> inside the <td id="dateHolder"> element and targeting that with CSS, instead of just the text node. <td id="dateHolder">元素中使用<span>并使用CSS定位,而不仅仅是文本节点。

Setting a height (by px ) to all of the <td> elements in the <tr> row. 将高度(通过px )设置为<tr>行中的所有<td>元素。

Adjusting the line-height of all of the <td> elements in the <tr> row. 调整<tr>行中所有<td>元素的行高。

Things You May Want To Know: 你可能想知道的事情:

I have absolutely NO inline CSS. 我绝对没有内联CSS。 All of the CSS is in the same external document. 所有CSS都在同一个外部文档中。

In case you feel you need to view it, here is the entire CSS file (this site is still very new, so there's isn't much there yet): 如果你觉得你需要查看它,这里是整个CSS文件(这个网站仍然很新,所以还有很多):

body
{
    width: 100%;
    margin: auto;
    background-color: #464646;
}

div#mainWrapper
{
    background-color: #dfdfdf;
    width: 900px;
    border-style: ridge;
    border-width: 5px;
    border-color: #808080;
    margin: auto;
    padding: 10px 20px 0px;
    margin-top: 8px;
    text-align: center;
    padding-bottom: 20px;
}

table#headingTable
{
    width: 100%;
}

td
{
    vertical-align: middle;
}

h2
{
    font-family: Tahoma, Geneva, sans-serif;
    text-align: center;
}

table#mainTable
{
    width: 95%;
    margin: auto;
    margin-top: 40px;
    border-collapse: collapse;
    border: 2px solid #000;
}

td.leaveSection
{
    width: 50%;
    margin: auto;
    white-space: nowrap;
}

.printBtn
{
    white-space: nowrap;
    font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
    font-size: 1em;
    border-width: 3px;
    border-color: #959595;
    cursor: pointer;
}

#dateHolder
{
    float: left;
}

div.bigLblWrapper
{
    position: relative;
    left: -50px;
    display: inline-block;
    text-align: right;
    width: 80%;
}

label.bigLbl
{
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
    font-size: 15px;
    font-weight: bold;
}

input.leaveTypeRadio
{
    position: relative;
    left: -35px;
}

input[readonly]
{
    background-color: #c6ebff;
}

Lastly, I just want to stress that I am not as concerned with a solution as to how to do this (again, I'm sure I could make something work), as I am interested in an explanation as to what is disallowing me to establish the styling changes I want based on the rules that I have set. 最后,我只想强调,我并不关心如何做到这一点的解决方案(再次,我确信我可以做一些工作),因为我感兴趣的解释是什么让我无法做到根据我设置的规则建立我想要的样式更改。

You're floating the <td> in the last rule of your CSS - this will automatically make an element block-level, and vertical-align will not work as it only applies to elements in the inline-formatting context. 您将CSS中的<td>浮动 - 这将自动生成元素块级别,而vertical-align将不起作用,因为它仅适用于内联格式化上下文中的元素。 You should instead use text-align to align the <td> content instead of floating #dateHolder 您应该使用text-align来对齐<td>内容而不是浮动#dateHolder

http://jsfiddle.net/LRMrv/ http://jsfiddle.net/LRMrv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM