【问题标题】:Why are nested flexible boxes failing to contain their contents?为什么嵌套的弹性盒子不能包含它们的内容?
【发布时间】:2012-05-16 12:25:39
【问题描述】:

早上好,

当我将一个带有display: -webkit-box 的元素嵌套在另一个带有display: -webkit-box 的元素中时,它无法将其元素正确地包含为一个灵活的盒子。请参阅下图中删除按钮后面的文字。

LI HTML:

<li>
    <div class="reference">
        <div class="number">1.</div>
        Lawson, B. Sharp, R. (2011). <em>Introducing Html5.</em> New Riders Pub
    </div>
    <div class="edit">
        <button class="delete">Delete</button>
    </div>
</li>

LI CSS(美学省略):

li {
    display: -webkit-box;
    -webkit-box-align: center; /* Vertically center elements within LI */
}
.reference {
    -webkit-box-flex: 1; /* Expand to fill space */
    display: -webkit-box;
}
.number {
    width: 3ex;
    padding: 0 5px 0 0;
}
.edit {
    /* I have tried setting a width and -webkit-box-flex: 0,
    but this does not solve the problem. */
}

随着视口宽度的变化,.reference 保持灵活是至关重要的。

可以通过删除 .reference 的 display: -webkit-box; 来解决问题,但这意味着我不能在 .reference 中的元素上使用灵活的盒子模型,我需要保留项目编号(1.、2.等) ) 在他们自己的列中。

非常感谢任何帮助。亲切的问候。

【问题讨论】:

    标签: html css webkit flexbox


    【解决方案1】:

    您可以使用display: table 等来实现您想要的,而不是-webkit-box,您只需要一些小的标记更改:

    <li>
        <div class="reference">
            <div class="number">1.</div>
            <div class="title">Lawson, B. Sharp, R. (2011). <em>Introducing Html5.</em> New Riders Pub</div>
        </div>
        <div class="edit">
            <button class="delete">Delete</button>
        </div>
    </li>
    

    还有 CSS:

    ul {
        display: table;
    }
    
    li {
        display: table-row;
    }
    
    .number {
        width: 5%;
        display: table-cell;
    }
    
    .title {
        display: table-cell;
        width: 85%;
    }
    
    .edit {
        display: table-cell;
        width: 10%;
    }
    

    显然,您需要调整width 属性,但这应该可以工作并且具有更好的跨浏览器支持。

    【讨论】:

    • 这很有帮助,但并不能完全回答我的问题。我仅使用 PhoneGap 部署到 iOS Safari,因此浏览器支持并不重要。当单元格拉伸以填充窗口时,使用百分比会导致内容之间的间隙不一致,而使用box-flex: 1 使我能够控制它。我会再等一会得到答案,否则我会使用你的。
    猜你喜欢
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2020-02-13
    • 2011-05-26
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多