使用`column-count`和`box-shadow`,CSS Masonry UI无法正常工作 [英] CSS Masonry UI not working properly using `column-count` and `box-shadow`
本文介绍了使用`column-count`和`box-shadow`,CSS Masonry UI无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的 Masonry UI 的代码,我使用纯 CSS
如果有超过 4张卡,但是如果我在 4张卡下使用列数:3 ;
效果不佳。
This works well if there are more than 4 cards but if I use it with below 4 cards the column-count: 3;
does not work well.
body{
height:1000px;
}
ul {
list-style: none;
-moz-column-count: 3;
-webkit-column-count: 3;
column-count: 3;
-moz-column-gap: 1em;
-webkit-column-gap: 1em;
column-gap: 1em;
padding: 0px 4px 4px 4px;
margin-top: -10px;
display: inline-block;
width: 100%;
margin: 0px;
}
li {
width: 100%;
display: inline-block;
box-shadow: none;
background: transparent;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
margin: 2%;
}
li div {
border-radius: 3px;
background-color: #f4faff;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.84);
margin-top: 1em;
cursor: pointer;
}
li div img{
height: auto;
width: 100%;
vertical-align: middle;
border: 0;
}
<ul>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
</ul>
<br><br><br>
BUT THIS WORKS IF THERE IS MORE THAN 4 IMAGES
<ul>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
</ul>
尝试过的解决方案:
Tried Soluition:
body{
height:500px;
}
ul {
list-style: none;
-moz-column-count: 3;
-webkit-column-count: 3;
column-count: 3;
-moz-column-gap: 1em;
-webkit-column-gap: 1em;
column-gap: 1em;
padding: 0px 4px 4px 4px;
margin-top: -10px;
display: inline-block;
width: 100%;
overflow:visible;
}
li {
width: 100%;
display: inline-block;
float:left;
background: transparent;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
margin: 2%;
}
li div {
border-radius: 3px;
background-color: #f4faff;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.84);
margin-top: 1em;
cursor: pointer;
}
li div img{
height: auto;
width: 100%;
vertical-align: middle;
border: 0;
}
<ul>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
<li><div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt=""></div></li>
</ul>
我用过 float:left;
到上面的解决方案< li>
并且它适用于这种情况但是在我的真实视图中它会减少最后 img的
box-shadow
请查看以下屏幕截图。
I have used float: left;
to the above solutions <li>
and it works in this case but in my real view it crops down the last img's
box-shadow
check the below screenshot.
任何解决方案都非常有帮助。
推荐答案
我希望这样的事情可能会有所帮助。
I hope something like this could be helpful.
.dw {
box-sizing: border-box;
-webkit-column-gap: 0;
column-gap: 0;
position: relative;
}
.dw * {
box-sizing: border-box;
}
@media (min-width: 768px) {
.dw {
-webkit-column-count: 2;
column-count: 2;
}
}
@media (min-width: 992px) {
.dw {
-webkit-column-count: 3;
column-count: 3;
}
}
@media (min-width: 1500px) {
.dw {
-webkit-column-count: 4;
column-count: 3;
}
}
.dw-pnl {
margin: 0;
padding: 5px;
}
.dw-pnl__cntnt {
border-radius: 3px;
overflow: hidden;
padding: 20px;
width: 100%;
}
@media (min-width: 768px) {
.dw-pnl {
-webkit-column-break-inside: avoid;
break-inside: avoid;
}
}
img {
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.84);
}
img.dw-pnl__cntnt {
padding: 0;
}
<body>
<div class="dw">
<div class="dw-pnl "> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt="img1" class="dw-pnl__cntnt"/> </div>
<div class="dw-pnl "> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt="img2" class="dw-pnl__cntnt"/> </div>
<div class="dw-pnl "> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt="img3" class="dw-pnl__cntnt"/> </div>
<div class="dw-pnl "> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt="img3" class="dw-pnl__cntnt"/> </div>
<div class="dw-pnl "> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt="img3" class="dw-pnl__cntnt"/> </div>
<div class="dw-pnl "> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs7OT7p9xBXn090AjKYgX4eV6vr-kHsmbIfcSFh__PoXgdNtb5gg" alt="img3" class="dw-pnl__cntnt"/> </div>
</div>
</body>
这篇关于使用`column-count`和`box-shadow`,CSS Masonry UI无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文