简体   繁体   English

显示表格的行宽不固定

[英]Display table row width doesn't fix

I'm trying to make the div have same height so i used display table , problem that i having is the width will grow. 我正在尝试使div具有相同的高度,所以我使用了display table,问题是宽度会增大。 As an example try adding content in the tableright , when it is exceeded the width , it will not break to the next line but it will expand horizontally to the tableleft. 作为示例,请尝试在右上方添加内容,当其超出宽度时,它不会中断到下一行,但会水平扩展到左上方。

Please Advice. 请指教。 Below is the sample code 下面是示例代码

 <div id="maintable">
    <div id="table-row">
    <div id="tableleft></div>
    <div id="tableright></div>
    </div>
    </div>

    <style>
    #maintable
    {display:table;
    width:100%;
    }
    #table-row
    {
    display:table-row
    width:100%;
    }
    #tableleft
    {
    width:60%;
    display:table-cell;
    }
    #tableright
    {
    width:40%;
    display:table-cell;
    }
    </style>

You made few syntax error there. 您在此处几乎没有语法错误。

Further to make sure if one big un spaced word is placed in table-cell i have used "table-layout: fixed;" 为了进一步确保是否在表格单元格中放置了一个大的未隔开的单词,我使用了“ table-layout:fixed;”。

check this fiddle: http://jsfiddle.net/5q9K8/17/ 检查这个小提琴: http : //jsfiddle.net/5q9K8/17/

The HTML: HTML:

<div id="maintable">
    <div id="table-row">
        <div id="tableleft">lorem</div>
        <div id="tableright">ipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsum</div>
    </div>
</div>

The Css: CSS:

#maintable
{
    display:table;
    width:100%;
    table-layout: fixed;
    word-wrap:break-word;    
}
#table-row
{
    display:table-row;
    width:100%;
}
#tableleft
{
    width:60%;
    display:table-cell;
    background: #eef;
}
#tableright
{
    width:40%;
    display:table-cell;
    background: #efe;
}

Add width as accordingly here just an example to show div height equal to table height. 相应地添加宽度,这里只是一个示例,以显示div高度等于表格高度。

css 的CSS

.maintable {
    width:100%;
    display: table;
    border: 1px solid blue;
}

.maintable .table-row {
    display: table-row;
    border: 2px solid black;
}

.maintable .table-row .tableleft, .maintable .table-row .tableright {
    display: table-cell;
    border: 4px solid red;
}

.tableright {
    width: 60%;
}
.tableleft {
    width: 40%;
}

DEMO 演示

Final DEMO 最终演示

在 a 中设置“width: auto”<div> 用“显示:弯曲; flex-direction:行”不起作用</div><div id="text_translate"><p>我正在尝试将一堆 div 并排放置,从左到右。 所以我使用 flexbox 方向设置为row 。</p><p> 我希望这些 div 中的每一个都从里面的内容中导出它们的高度和宽度,因此我将它们的高度和宽度保留为默认值。</p><p> 最后,我想使用 svg 作为背景。 为此,我使用了一个网格容器并将 svg 和 div 放置在同一列和同一行中,如此<a href="https://stackoverflow.com/a/51949049/9613633" rel="nofollow noreferrer">stackoverflow 帖子</a>中所建议的那样。 我将 svg 的高度和宽度设置为100% ,以确保它填充整个背景。</p><p> 但是,当我将所有内容组合在一起时,div 的宽度设置不正确。 div 的高度似乎是由里面的内容设置的,但宽度似乎总是默认为300px 。 对于我的一生,我无法弄清楚这个宽度是如何计算的。 如果内容比300px宽,那么一切都按预期工作。 但是如果内容小于300px ,则容器永远不会缩小以适应。</p><p> 如何确保 div 始终缩小以适应? 请参阅下面的代码示例: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .top { padding: 20px; display: flex; flex-direction: row; }.grid-container { background-color: red; display: grid; }.svg { grid-column: 1; grid-row: 1; width: 100%; height: 100%; }.content { grid-column: 1; grid-row: 1; font-size: 100px; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> This div has calculated width 300px for some reason &lt;div class="top"&gt; &lt;div class="grid-container" key="1"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; ab &lt;/div&gt; &lt;/div&gt; &lt;,-- more grid-containers --&gt; &lt;/div&gt; This works as expected &lt;div class="top"&gt; &lt;div class="grid-container"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; abcdefghijkl &lt;/div&gt; &lt;/div&gt; &lt;!-- more grid-containers --&gt; &lt;/div&gt;</pre></div></div><p></p></div> - Setting “width: auto” inside a <div> with “display: flex; flex-direction: row” doesn't work

暂无
暂无

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

相关问题 表格宽度不适用于多行 - table width doesn't work on multiple row 显示:表格不考虑宽度 - Display: table doesn't respect width 显示:块; 导致行宽发生变化……而我无法修复 - Display:Block; causes row width to change… and I can't fix it 子div的display:table不遵循父元素的宽度 - Child div doesn't follow the width of parent with display:table 在html表中设置列宽不显示滚动条 - Setting column width in html table doesn't display scrollbar display:table-cell在100%宽度下无法正常工作 - display:table-cell doesn't work properly with 100% width 将行中的列宽设置为“显示:表格” - Maintain column width in row set as “display: table” 在 a 中设置“width: auto”<div> 用“显示:弯曲; flex-direction:行”不起作用</div><div id="text_translate"><p>我正在尝试将一堆 div 并排放置,从左到右。 所以我使用 flexbox 方向设置为row 。</p><p> 我希望这些 div 中的每一个都从里面的内容中导出它们的高度和宽度,因此我将它们的高度和宽度保留为默认值。</p><p> 最后,我想使用 svg 作为背景。 为此,我使用了一个网格容器并将 svg 和 div 放置在同一列和同一行中,如此<a href="https://stackoverflow.com/a/51949049/9613633" rel="nofollow noreferrer">stackoverflow 帖子</a>中所建议的那样。 我将 svg 的高度和宽度设置为100% ,以确保它填充整个背景。</p><p> 但是,当我将所有内容组合在一起时,div 的宽度设置不正确。 div 的高度似乎是由里面的内容设置的,但宽度似乎总是默认为300px 。 对于我的一生,我无法弄清楚这个宽度是如何计算的。 如果内容比300px宽,那么一切都按预期工作。 但是如果内容小于300px ,则容器永远不会缩小以适应。</p><p> 如何确保 div 始终缩小以适应? 请参阅下面的代码示例: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .top { padding: 20px; display: flex; flex-direction: row; }.grid-container { background-color: red; display: grid; }.svg { grid-column: 1; grid-row: 1; width: 100%; height: 100%; }.content { grid-column: 1; grid-row: 1; font-size: 100px; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> This div has calculated width 300px for some reason &lt;div class="top"&gt; &lt;div class="grid-container" key="1"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; ab &lt;/div&gt; &lt;/div&gt; &lt;,-- more grid-containers --&gt; &lt;/div&gt; This works as expected &lt;div class="top"&gt; &lt;div class="grid-container"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; abcdefghijkl &lt;/div&gt; &lt;/div&gt; &lt;!-- more grid-containers --&gt; &lt;/div&gt;</pre></div></div><p></p></div> - Setting “width: auto” inside a <div> with “display: flex; flex-direction: row” doesn't work 行未完全填满容器宽度 - Row doesn't completely fill container width 为什么在 div 区域中使用 display:table 时,border-top 不给出相同的边框宽度? - Why border-top doesn't give same border width while using display:table in div area?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM