如何确定display:table-cell的填充 [英] How is the padding of display:table-cell determined

查看:101
本文介绍了如何确定display:table-cell的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试采用类似于表格的布局,药物名称显示在左侧,而数据占据了表格的其余部分.简而言之:

I'm trying to come out with a table-like layout, with medicine names appearing on the left and the data occupy the rest of the table. Simply put:

           +-------------+------------+
medicine 1 |  some data  | some data  |
           +-------------+------------+
medicine 2 |  some data  | some data  |
           +-------------+------------+

由于我想保持数据网格的动态性,因此我将两个<div>的样式为display:table-cell用作两个容器,左边的是所有药品名称,右边的​​是数据网格.这两个表格单元格<div>中有几个内部<div>,但是我不确定为什么当我使用Chrome inspect接口进行调查时,左侧的顶部为何有较大的填充区域(请参见下图):

Since I want to keep the data grid dynamic, I use two <div>'s with the style display:table-cell as two containers, the left one for all the medicine names, and the right one for the data grid. There are several inner <div>'s inside these two table-cell <div>'s, but I'm not sure why the left one has a big padding area on the top when I use Chrome inspect interface to investigate it (please see the image below):

我不太确定哪个部分出了问题,并且inspect接口没有给我看似相关的信息.我想学习如何处理这种情况.这是供您参考的html代码:

I'm not quite sure which part went wrong, and the inspect interface didn't give me information that seems relevant. I want to learn how to approach this situation. Here is the html code for your reference:

<div style="display:table">
  <div style="display:table-row">
    <div style="display:table-cell">
      <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; 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>

推荐答案

这是关于垂直对齐的.默认值设置为基线,并产生此输出.只需将对齐方式更改为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.

这篇关于如何确定display:table-cell的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆