gpt4 book ai didi

html - 带有边距的 CSS flex child 装订线

转载 作者:太空狗 更新时间:2023-10-29 15:03:13 24 4
gpt4 key购买 nike

我有一个画廊布局,我想用边距分隔每个框。问题是每一行的最后一个框没有与网格对齐;右边还有空间,我怎么能不使用填充呢? (这将迫使我添加另一个包装 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% 并且您有五个框,则第二行将有两个框,一个在左侧,第二个在左侧右侧。

最佳答案

方法#01:

分别在parnet和child上添加如下css(此方法只在连续只有2个boxes时有效):

/* 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:

  1. 在子元素的两侧添加边距。
  2. 以相同数量的负边距从左/右扩展父项。

注意:如果出现水平滚动,请在 .gallery 的父级上添加 overflow: hidden

* {
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>

关于html - 带有边距的 CSS flex child 装订线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40735340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com