[英]Remove space on the left of list items
ul { display: flex; flex-wrap: wrap; width: 45%; height: 40%; border: 1px solid red; list-style: none; margin: 0 auto; } ul li { flex: 1; }
<ul> <li><img src="images/yellow.gif" alt="Yellow"></li> <li><img src="images/orange.gif" alt="Orange"></li> <li><img src="images/purple.gif" alt="Purple"></li> <li><img src="images/blue.gif" alt="Blue"> </li> <li><img src="images/pink.gif" alt="Pink"> </li> <li><img src="images/green.gif" alt="Green"> </li> <li><img src="images/black.gif" alt="Black"> </li> <li><img src="images/gray.gif" alt="Gray"> </li> <li><img src="images/red.gif" alt="Red"> </li> </ul>
As per the actual output, there is left-margin on inspecting ul
element, as shown below, 根据实际输出,检查
ul
元素的余量如下所示,
there is also right-margin on inspecting li
element, as shown below 检查
li
元素也有保证金,如下所示
1) Why these margin space exist? 1)为什么这些边距空间存在?
2) How to avoid these margin space? 2)如何避免这些边距空间?
The whitespace on the left is caused by the default padding on the ul
. 左边的空白是由
ul
上的默认填充引起的。 Remove it with: 删除它:
ul { padding-left: 0; }
Note that some browsers may use margin
instead of padding
for this indentation. 请注意,某些浏览器可能会使用
margin
而不是padding
来缩进。
The whitespace on the right is caused by the flex-wrap
property. 右边的空格是由
flex-wrap
属性引起的。 When flex items wrap, the container does not recalculate its width to shrink-wrap the new layout. 当flex项目包装时,容器不会重新计算其宽度以收缩包装新的布局。
By re-sizing the window horizontally, you'll see the right spacing come and go in this demo . 通过水平调整窗口的大小,您将在此演示中看到正确的间距。
Here are some more details and a possible workaround: 以下是更多详细信息和可能的解决方法:
ul
by default has padding
, so remove it: ul
默认具有padding
,因此将其删除:
img { display: block /*fix inline gap */ } ul { display: flex; flex-wrap: wrap; width: 50%; /*changed */ height: 40%; border: 1px solid red; list-style: none; margin: 0 auto; padding: 0 } ul li { flex:1; }
<ul> <li> <img src="http://placehold.it/100/ff0" alt="Yellow"> </li> <li> <img src="http://placehold.it/100/f90" alt="Orange"> </li> <li> <img src="http://placehold.it/100/c6f" alt="Purple"> </li> <li> <img src="http://placehold.it/100/06c" alt="Blue"> </li> <li> <img src="http://placehold.it/100/fcf" alt="Pink"> </li> <li> <img src="http://placehold.it/100/0f0" alt="Green"> </li> <li> <img src="http://placehold.it/100/000" alt="Black"> </li> <li> <img src="http://placehold.it/100/666" alt="Gray"> </li> <li> <img src="http://placehold.it/100/f00" alt="Red"> </li> </ul>
NOTE : In this snippet above you will have, at same point, the desired output, but as pointed out by Michael_B : 注意 :在上面的这段代码中,您将同时具有所需的输出,但正如Michael_B指出的那样:
When the flex items wrap, the container does not recalculate its
width
to perfectly shrink-wrap the new layout.当flex项目包装时,容器不会重新计算其
width
以完美收缩包装新的布局。
So If I was you I would try another approach. 因此,如果我是您,我将尝试另一种方法。
Browsers put a certain amount of padding on ul
's (Chrome adds 40px to the left). 浏览器在
ul
添加了一定的填充量(Chrome在左侧添加了40px)。 You can override this by adding padding: 0
to the ul
: https://jsfiddle.net/2ocbyhdc/ 您可以通过在
ul
添加padding: 0
来覆盖它: https : //jsfiddle.net/2ocbyhdc/
ul{
display: flex;
flex-wrap: wrap;
width: 45%;
height: 40%;
border: 1px solid red;
list-style: none;
margin: 0 auto;
padding: 0;
}
Setting padding: 0;
设置
padding: 0;
on the list helps: 该列表上的内容有助于:
ul { display: flex; flex-wrap: wrap; width: 300px; border: 1px solid red; list-style: none; margin: 0 auto; padding: 0; } ul li { display: flex; flex-grow: 1; } ul li img { flex-grow: 1; }
<ul> <li><img src="https://dummyimage.com/100x100/ffff00/000" alt="Yellow"></li> <li><img src="https://dummyimage.com/100x100/FFA500/000" alt="Orange"></li> <li><img src="https://dummyimage.com/100x100/800080/fff" alt="Purple"></li> <li><img src="https://dummyimage.com/100x100/0000FF/fff" alt="Blue"></li> <li><img src="https://dummyimage.com/100x100/FF69B4/fff" alt="Pink"></li> <li><img src="https://dummyimage.com/100x100/006600/fff" alt="Green"></li> <li><img src="https://dummyimage.com/100x100/000000/fff" alt="Black"></li> <li><img src="https://dummyimage.com/100x100/cccccc/000" alt="Gray"></li> <li><img src="https://dummyimage.com/100x100/ff0000/fff" alt="Red"></li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.