且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确定display:table-cell的填充

更新时间:2022-06-20 10:06:45

这是关于垂直对齐的.默认值设置为基线,并产生此输出.只需将对齐方式更改为table-cell上的top,就不会出现此问题:

This is about vertical alignment. The default one is set to baseline and produce this output. Simply change the alignment to top on the table-cell and you won't have this issue:

<div style="display:table">
  <div style="display:table-row">
    <div style="display:table-cell;
    vertical-align: top;">
      <div style="height:85px; width:170px; text-align:right; font-size:13px; margin-right:5px">
        Dexedrine Spansules (Dextroamphetamine, ER) <br/><span style="font-style:italic">(20mg)</span>
      </div>
      <div style="height:85px; width:170px; text-align:right; font-size:13px; margin-right:5px">
        Methamphetamine (Desoxyn, IR) <br/><span style="font-style:italic">(15mg)</span>
      </div>
    </div>
    <div style="display:table-cell;
    vertical-align: top; overflow:hidden; max-width:800px">
      <div id="medicine_table_container_2" class="medicine-table-container" style="position:relative; left:0">
        <div style="white-space:nowrap; font-size:0px">
          <div style="display:inline-block; background-color:yellow; width:130px; height:85px; border:1px solid #999; font-size: 12px; white-space:normal">
            <div>
              <div style="display:inline-block; width:70px; height:45px">
                Morning<br/>-
              </div>
              <div style="display:inline-block; width:50px; height:45px">
                Noon<br/>5mg
              </div>
            </div>
            <div>
              <div style="display:inline-block; width:70px; height:35px">
                Afternoon<br/>12mg
              </div>
              <div style="display:inline-block; width:50px; height:35px">
                Evening<br/>-
              </div>
            </div>
          </div>
        </div>
        <div style="white-space:nowrap; font-size:0px">
          <div style="display:inline-block; background-color:yellow; width:130px; height:85px; border:1px solid #999; font-size: 12px; white-space:normal">
            <div>
              <div style="display:inline-block; width:70px; height:45px">
                Morning<br/>-
              </div>
              <div style="display:inline-block; width:50px; height:45px">
                Noon<br/>5mg
              </div>
            </div>
            <div>
              <div style="display:inline-block; width:70px; height:35px">
                Afternoon<br/>12mg
              </div>
              <div style="display:inline-block; width:50px; height:35px">
                Evening<br/>-
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

由于您的代码有点复杂,因此这里有一个基本的代码可以重现问题并更好地了解正在发生的事情:

Since your code is a bit complex, here is a basic one to reproduce the issue and better understand what's happening:

.table {
  display: table;
  border: 1px solid;
  margin: 5px;
}

.table>div {
  display: table-row;
}

.table>div>span {
  display: table-cell;
  padding: 0 5px;
}

.table>div>span span {
  display: inline-block;
}

baseline
<div class="table">
  <div>
    <span>one line</span>
    <span><span>two <br> line (inline-block)</span></span>
  </div>
</div>
baseline
<div class="table">
  <div>
    <span>two<br> line</span>
    <span><span>two <br> line (inline-block)</span></span>
  </div>
</div>
baseline (with overflow:hidden)
<div class="table">
  <div>
    <span>one line</span>
    <span><span style="overflow:hidden;">two <br> line</span></span>
  </div>
</div>
baseline (with overflow:hidden)
<div class="table">
  <div>
    <span>one line</span>
    <span><span style="overflow:hidden;">another line</span></span>
  </div>
</div>
top will fix all the cases
<div class="table">
  <div>
    <span style="vertical-align:top;">one line</span>
    <span><span>two <br> line</span></span>
  </div>
</div>
<div class="table">
  <div>
    <span style="vertical-align:top;">one line</span>
    <span><span style="overflow:hidden;">two <br> line</span></span>
  </div>
</div>
<div class="table">
  <div>
    <span style="vertical-align:top;">one line</span>
    <span><span style="overflow:hidden;">another line</span></span>
  </div>
</div>
<div class="table">
  <div>
    <span style="vertical-align:top;">two<br> line</span>
    <span><span>two <br> line (inline-block)</span></span>
  </div>
</div>

在这里您可以清楚地看到inline-block(和overflow:hidden)的使用是罪魁祸首,因为它使基准计数器的计算变得直观而出乎意料.

You can clearly see how the use of inline-block (and overflow:hidden) is the culprit here as it make the calculation of the baseline counter intuitive and unexpected.