我有一个画廊布局,我想用保证金分隔每个方框。问题是每行中的最后一个框与网格不对齐;右侧还有空间,如何在不使用填充的情况下执行此操作? (这将迫使我添加另一个包装div)
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
.flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.align-center {
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.space-between {
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.container {
max-width: 1000px;
margin: 0 auto;
height: 100%;
padding: 0 1em;
border: 1px solid;
}
nav {
background: #464646;
height: 70px;
}
.box {
background: lightblue;
padding: 1em;
-webkit-box-flex: 0;
-webkit-flex: 0 0 calc(50% - 1em);
-ms-flex: 0 0 calc(50% - 1em);
flex: 0 0 calc(50% - 1em);
text-align: center;
margin-right: 1em;
margin-bottom: 1em;
}
<nav>
<div class="container flex align-center space-between">
<ul>
<li>Logo</li>
</ul>
<ul>
<li>Home</li>
</ul>
</div>
</nav>
<div class="container">
<header>
<h1>Header</h1>
</header>
</div>
<div class="container">
<div class="gallery flex">
<div class="box">BOX 1</div>
<div class="box">BOX 2</div>
<div class="box">BOX 3</div>
</div>
</div>
注意:我不能使用flex grow:1因为我并不总是连续两个盒子。
我之间不能使用空格,因为网格是动态的,例如如果框的宽度是33.33%而你有五个框,第二行将有两个框,一个在左侧,第二个在右边。
答案 0 :(得分:5)
方法#01:
分别在parnet和child上添加以下css(此方法仅在连续只有2个框时才有效):
/* For Parent Element */
.flex {
justify-content: space-between;
}
/* For Child Element */
.flex-child {
flex: 0 0 calc(50% - 0.5em);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
.flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.align-center {
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.space-between {
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.container {
max-width: 1000px;
margin: 0 auto;
height: 100%;
padding: 0 1em;
border: 1px solid;
}
nav {
background: #464646;
height: 70px;
}
.box {
background: lightblue;
padding: 1em;
-webkit-box-flex: 0;
-webkit-flex: 0 0 calc(50% - 0.5em);
-ms-flex: 0 0 calc(50% - 0.5em);
flex: 0 0 calc(50% - 0.5em);
text-align: center;
margin-bottom: 1em;
}
<nav>
<div class="container flex align-center space-between">
<ul>
<li>Logo</li>
</ul>
<ul>
<li>Home</li>
</ul>
</div>
</nav>
<div class="container">
<header>
<h1>Header</h1>
</header>
</div>
<div class="container">
<div class="gallery flex space-between">
<div class="box">BOX 1</div>
<div class="box">BOX 2</div>
<div class="box">BOX 3</div>
<div class="box">BOX 4</div>
<div class="box">BOX 5</div>
<div class="box">BOX 6</div>
<div class="box">BOX 7</div>
<div class="box">BOX 8</div>
<div class="box">BOX 9</div>
</div>
</div>
方法#02:
注意 :如果出现水平滚动,请在overflow: hidden
的父级上添加.gallery
。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
.flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin: 0 -0.5em;
}
.align-center {
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.space-between {
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.container {
max-width: 1000px;
margin: 0 auto;
height: 100%;
padding: 0 1em;
border: 1px solid;
}
nav {
background: #464646;
height: 70px;
}
.box {
background: lightblue;
padding: 1em;
-webkit-box-flex: 0;
-webkit-flex: 0 0 calc(50% - 1em);
-ms-flex: 0 0 calc(50% - 1em);
flex: 0 0 calc(50% - 1em);
text-align: center;
margin: 0 0.5em 1em;
}
<nav>
<div class="container flex align-center space-between">
<ul>
<li>Logo</li>
</ul>
<ul>
<li>Home</li>
</ul>
</div>
</nav>
<div class="container">
<header>
<h1>Header</h1>
</header>
</div>
<div class="container">
<div class="gallery flex">
<div class="box">BOX 1</div>
<div class="box">BOX 2</div>
<div class="box">BOX 3</div>
</div>
</div>
答案 1 :(得分:0)
检查下面的代码段。已解决
使用CSS
框:第n个孩子(2N + 1){保证金左:0像素;}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
.flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.align-center {
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.space-between {
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.container {
max-width: 1000px;
margin: 0 auto;
height: 100%;
padding: 0 1em;
border: 1px solid;
}
nav {
background: #464646;
height: 70px;
}
.box {
background: lightblue;
padding: 1em;
-webkit-box-flex: 0;
-webkit-flex: 0 0 calc(50% - 1em);
-ms-flex: 0 0 calc(50% - 1em);
flex: 0 0 calc(50% - 1em);
text-align: center;
margin-left: 2em;
margin-bottom: 1em;
}
.box:nth-child(2n+1){
margin-left:0px;
}
<nav>
<div class="container flex align-center space-between">
<ul>
<li>Logo</li>
</ul>
<ul>
<li>Home</li>
</ul>
</div>
</nav>
<div class="container">
<header>
<h1>Header</h1>
</header>
</div>
<div class="container">
<div class="gallery flex">
<div class="box">BOX 1</div>
<div class="box">BOX 2</div>
<div class="box">BOX 3</div>
</div>
</div>
答案 2 :(得分:0)
改变两件事: -