简体   繁体   English

为什么这两个代码段使用display:table / table-cell / table-row呈现不同的显示?

[英]Why do these two snippets render differently using display:table/table-cell/table-row?

In writing some CSS using display: table/table-row/table-cell, I ran into some behaviour I don't understand, but is consistent across Chrome and Firefox. 在使用display编写一些CSS时:table / table-row / table-cell,我遇到了一些我不了解的行为,但是在Chrome和Firefox中是一致的。 I reduced it to the following two snippets highlighting the difference: 我将其简化为以下两个片段,以突出说明差异:

The styling rules are very simple: 样式规则非常简单:

* { box-sizing: border-box; border: none; margin: 0; }
.stack, .flow {
    width: 100%;
    display: table;
}
.stack > * {
    display: table-row;
}
.flow > * {
    display: table-cell;
}
.w-100 { width: 100%; }

The html is rougly: 的HTML是难看的:

<div class="flow">
  <div>L</div>
  <input type="text"class="w-100" placeholder="M" />
  <div>R</div>
</div>

The only difference in the second link is that the last div with the R is replaced by <button> (input also shows the same behaviour). 第二个链接中的唯一区别是带有R的最后一个div被<button>替换(输入也显示相同的行为)。 Both should render as a table-cell wrapped in an anonymous table-row, and I think it should look like the first snippet regardless. 两者都应呈现为包裹在匿名表行中的表单元格,并且我认为无论如何它都应该看起来像第一个代码段。 I must be missing something obvious to explain this difference, so can anyone explain why these two snippets render differently? 我必须缺少一些明显的东西来解释这种差异,所以谁能解释为什么这两个片段呈现的方式不同?

Interestingly enough, Edge renders them exactly the same as I initially expected. 有趣的是,Edge使它们与我最初期望的完全相同。

Edit: If you constrain the L and R elements to be fixed size, and the row as a whole is set to 100%, the M/middle cell should expand to fit the whole width, but it doesn't work as expected either in Chrome. 编辑:如果将L和R元素限制为固定大小,并且将行整体设置为100%,则M /中间单元格应该扩展为适合整个宽度,但是在任何情况下都不能正常铬。 These two snippets are also rendered differently for some inexplicable reason: 由于某些无法解释的原因,这两个摘要的呈现方式也有所不同:

It seems the rendering only works consistently if all of the elements used as table-cell are divs, like so: https://codepen.io/anon/pen/mxKreZ 似乎仅当用作表单元的所有元素都是div时,渲染才能始终如一地工作,例如: https : //codepen.io/anon/pen/mxKreZ

This behavior with buttons is a known issue affecting both browsers ( issue for Chrome and for Firefox ). 带有按钮的行为是影响两个浏览器的已知问题( ChromeFirefox的问题 )。 The explanation given by both vendors is the special rendering of buttons owing to their nature as form elements. 两家供应商给出的解释是按钮的特殊渲染,因为它们具有表单元素的性质。 There are no plans by either vendor to fix this, at least not until the css-display-3 and/or css-tables-3 specs have matured. 两家供应商都没有计划解决此问题,至少直到css-display-3和/或css-tables-3规范成熟为止。

As you've observed, Edge renders your snippets as expected, because Edge (and in fact IE11) does support rendering buttons as table-cells. 如您所见,Edge可以按预期方式渲染您的代码片段,因为Edge(实际上是IE11)确实支持将渲染按钮作为表格单元格显示。

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

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