html - Flexbox:如何创建我的缩略图以跨越 100% 的容器宽度?

标签 html css flexbox

我正在尝试创建跨越其包含元素 100% 的相等 div。

您可以查看 Fiddle here .必须如何编写代码才能从容器的一侧跨越 div?

html

<div class="container">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

CSS

.container {
    display: flex;
    flex-flow: row wrap;
    width: 100%;
    background: blue;
}
.container div {
    width: 24%;
    height: 2.5em;
    background-color: red;
    margin-right: 1%;
    margin-bottom: 1em;
    box-sizing: border-box;
}

本质上,我正在尝试复制与此类似的内容:

enter image description here

最佳答案

对于每行 n 个元素,有 n-1 个间隙。 如果您想要 1% 的宽度间距,这意味着对于每行 4 个元素,div 可以占据宽度的 97%。

所以需要设置宽度为97/4 = 24.25% 并取消每第 4 个 child 的 margin 。

.container {
    display: flex;
    flex-flow: row wrap;
    width: 100%;
    background: blue;
}
.container div {
    width: 24.25%;
    height: 2.5em;
    background-color: red;
    margin-right: 1%;
    margin-bottom: 1em;
    box-sizing: border-box;
}

.container div:nth-child(4n) {
    margin-right:0;
}

http://jsfiddle.net/rq1vkdcg/10/

关于html - Flexbox:如何创建我的缩略图以跨越 100% 的容器宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32130006/

相关文章:

javascript - 滚动时淡出div并淡入其他div

css - [attribute~=value] 和 [attribute*=value] 的区别

javascript - 具有多种宽度的元素

html - 如何删除最后一个 child 的边框?

css - Flexbox 和溢出 : hidden not working properly

python - 如何使用 BeautifulSoup 寻找值(value)

javascript - Blogger.com : How to embed formatted code snippet in a blog post?

html - 在 IE 中滚动 HTML 5 Canvas

html - 使用 css3 flex 的砌体布局

html - Flex 以第一个 child 为中心,第二个 child 在最后