[英]Space between flex items
我目前正在尝试学习flex box,但是我的知识不足以让我知道在Google或Stack上搜索什么。
我希望第一个元素“ stock”与flex容器对齐, 最后一个社交媒体图标向右对齐。
我需要在股票列表和第一个社交媒体图标之间留出空间,然后在每个社交图标之间留出一些空间。
.flex-container { width: 100%; padding-left: 0; padding-right: 0; } #flex-items { display: flex; flex-direction: row; flex-wrap: nowrap; justify-content: flex-end; background-color: orange; } .stock { margin-right: auto; background-color: #6dc993; display: flex; border: 2px solid blue; flex-grow: 1; } .stock > p { margin-left: 5%; display: flex; align-self: center; font-size: 20px; color: white; height: 20%; }
<div class="flex-container"> <div id="flex-items"> <div class="stock"><p>Stock List</p></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> </div> </div>
我摆脱了.stock上的flex-grow
,因为让它增长到适合您不想要的其余空间,然后定位到社交媒体div并增加了空间来隔开它们。 (或者,您可以在图标周围添加另一个容器,并在图标之间扩大空间)。 然后,我为股票添加宽度,例如40%。 可以像其他人所说的那样灵活地设置此宽度。
.flex-container { width: 100%; padding-left: 0; padding-right: 0; } #flex-items { display: flex; flex-direction: row; flex-wrap: nowrap; background-color: orange; } .stock { width: 40%; margin-right: auto; background-color: #6dc993; display: flex; border: 2px solid blue; } .stock>p { margin-left: 5%; display: flex; align-self: center; font-size: 20px; color: white; height: 20%; } #flex-items div:not(:first-of-type) { margin-left: 10px; }
<div class="flex-container"> <div id="flex-items"> <div class="stock"> <p>Stock List</p> </div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> </div> </div>
这是我为您解决的问题
.stock {
margin-right: auto;
background-color: #6dc993;
display: flex;
border-right: 5px solid blue;
flex-grow: 1; //removed
flex-basis: 30%; //added
}
我已将flex-basis
添加到静态30%,并删除了动态flex-grow
。 由于您的情况下flex-grow
设置为1
,因此.stock
的宽度将相对于屏幕的宽度增加。
最后为社交图标添加了间距。
#flex-items div:not(:first-of-type) {
margin-left: 10px;
}
像这样:
.flex-container { width: 100%; padding-left: 0; padding-right: 0; } #flex-items { display: flex; flex-direction: row; flex-wrap: nowrap; justify-content: space-between; background-color: orange; height: 75px; } .stock { margin-right: auto; background-color: #6dc993; display: flex; border: 2px solid blue; flex-grow: 1; width: 33%; } .links { width: 66%; display: flex; justify-content: flex-end; } .links > div > img { margin-left: 5px; } .stock > p { margin-left: 5%; display: flex; align-self: center; font-size: 20px; color: white; height: 20%; }
<div class="flex-container"> <div id="flex-items"> <div class="stock"><p>Stock List</p></div> <div class="links"> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> </div> </div> </div>
正如所有人都提到的,问题出在您的flex-grow上,我不建议删除该属性,而是为了更好地了解flexbox,请参阅此Flex-grow 。 添加伸缩增长将调整所有div项目的大小。
flex-grow属性指定弹性项目相对于其余弹性项目将增长多少。
您可以在此Codepen中找到解决方案
.flex-container { width: 100%; padding-left: 0; padding-right: 0; } #flex-items { display: flex; justify-content: flex-start; background-color: orange; } .socialWrapper { display: flex; } .stock { margin-right: auto; background-color: #6dc993; display: flex; width: 40%; border: 2px solid blue; } .stock > p { margin-left: 5%; display: flex; align-self: center; font-size: 20px; color: white; height: 20%; } .icon-wrap { margin-left:5px; } .icon-wrap img { height: 100%; }
<div class="flex-container"> <div id="flex-items"> <div class="stock"><p>Stock List</p></div> <div class="socialWrapper"> <div class="icon-wrap"><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div class="icon-wrap"><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div class="icon-wrap"><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div class="icon-wrap"><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> <div class="icon-wrap"><img title="Like us on Facebook" src="http://i.cubeupload.com/ULYeTe.jpg" alt="Like us on Facebook" width="68" height="76" /></div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.