简体   繁体   English

100% 宽度,div 显示为表格单元格

[英]100% width with div displayed as table-cell

I have been trying to figure out how to format a list that has elements (in my case buttons) on the sides.我一直在试图弄清楚如何格式化一个侧面有元素(在我的例子中是按钮)的列表。 The major problem that I have had is that I do not want the list text to wrap around the buttons, I want it to behave more like a table;我遇到的主要问题是我不希望列表文本环绕按钮,我希望它表现得更像一个表格; text should not wrap into other columns.文本不应换行到其他列中。 To complicate things a little further, I do not necessarily know the sizes of the 'Side Elements' in advance.更复杂的是,我不一定事先知道“侧元素”的大小。

This is the code I came up with:这是我想出的代码:

<div style='display: table;'>
    <div style='display: table-row;'>
        <div style='display: table-cell'>
            <!--- Variable Width Left Side Element --->
        </div>
        <div style='display: table-cell; width: 100%;'>
            List Text
        </div>
        <div style='display: table-cell'>
            <!--- Variable Width Right Side Element --->
        </div>
    </div>
</div>

JSFiddle JSFiddle

I have tried this on Firefox, Chrome, Safari and Internet Explorer and it seems to work fine.我已经在 Firefox、Chrome、Safari 和 Internet Explorer 上尝试过这个,它似乎工作正常。 However, I am still concerned because I do not entirely understand why this works (and I do not like using code that I do not understand).但是,我仍然很担心,因为我不完全理解为什么会这样(而且我不喜欢使用我不理解的代码)。 My concern is with width: 100% .我关心的是width: 100% Shouldn't this specify that the cell should take up the entire parent, rather than all the space available in that row?这不应该指定单元格应该占据整个父级,而不是该行中的所有可用空间吗?

Ultimately, my question is this: Can I trust using width: 100% like this?最终,我的问题是:我可以信任使用width: 100%吗? Is this an appropriate way of solving my problem?这是解决我的问题的合适方法吗?

Ultimately, my question is this: Can I trust using width: 100% like this?最终,我的问题是:我可以信任使用 width: 100% 吗? Is this an appropriate way of solving my problem?这是解决我的问题的合适方法吗?

Yes, you can.是的你可以。

Tables are funny that way...when a cell is given a width of 100% it just means take up as much space as you can out of what is left .表格很有趣……当一个单元格的宽度为 100% 时,它只是意味着尽可能多地占用剩余空间

So you've already allocated some of the 100% of the parent so the middle div just grabs what's left.所以你已经分配了一些 100% 的父元素,所以中间的 div 只是抓住剩下的。


For what it's worth...a `flex-box alternative:对于它的价值......一个`flex-box替代方案:

 .row { display: flex; margin-bottom: .5em; } .row div { padding-left: 5px; padding-right: 5px; } .row div:first-child { flex-basis: 55px; } .row div:nth-child(2) { flex: 1; } .row div:last-child { flex-basis: 35px; }
 <div class="container"> <div class="row"> <div style="background-color: red"></div> <div>This text is not too long</div> <div style='background-color: blue'></div> </div> <div class="row"> <div style='background-color: red'></div> <div>This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long.</div> <div style='background-color: blue'></div> </div> </div>

JSfiddle Demo JSfiddle 演示

when you are mixing both absolute and relative measurements, its good to use calc() function.当您混合绝对和相对测量时,最好使用calc()函数。 So the width of the middle element will be,所以中间元素的宽度将是,

width: calc(100% - 90px) 100% being the full width of parent and 90px being the absolute measurements with you are excluding ( 55px + 35px ). width: calc(100% - 90px) 100% 是父的全宽,90px 是绝对测量值,你不包括( 55px + 35px )。

Browser will automatically compute the actual width of the element based on the parent width (which is the available width minus 90px).浏览器将根据父宽度(可用宽度减去 90px)自动计算元素的实际宽度。 Directly giving 100% is not the correct way, imho.直接给 100% 不是正确的方法,恕我直言。

The width of a table-cell depends upon its content.表格单元格的宽度取决于其内容。 The cells will expand based on their content unless table-layout: fixed is given on the table.单元格将根据其内容扩展,除非 table-layout: fixed 在表格中给出。

Instead of using tables you can use floats for example follow this link http://jsfiddle.net/osha90/e57dja5u/您可以使用浮点数代替使用表格,例如按照此链接http://jsfiddle.net/osha90/e57dja5u/

<div >
<div class="clearfix" style="margin:10px 0px">

    <div style='width: 55px; height: 35px; background-color: red;float:left'>
    </div>
     <div style='width: 35px; height: 25px; background-color: blue;float:right'>
        </div>
    <div style="overflow:hidden;">
        This text is not too long
    </div>

</div>
<div class="clearfix">

    <div style='width: 55px; height: 35px; background-color: red;float:left'>
    </div>
     <div style='width: 35px; height: 25px; background-color: blue;float:right'>
        </div>
    <div style="overflow:hidden;">
        This text is very long. This text is very long. This text is very       long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long.
    </div>

</div>

.clearfix:after,.clearfix:before{
   content: "";
   clear: both;
   display: block;
}

Because table rows can't wrap, table cell with 100% width will force the cell to occupy as much space as he can.因为表格行不能换行,100% 宽度的表格单元格将迫使单元格尽可能多地占据空间。 You other two cell have fixed width, because their content have a fixed width, and the a table cell minimum width is the one required to display the content.您其他两个单元格具有固定宽度,因为它们的内容具有固定宽度,而表格单元格的最小宽度是显示内容所需的宽度。 This means that the 100% width actual width is 100% - the two fixed cell.这意味着 100% 宽度实际宽度为 100% - 两个固定单元格。

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

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