【问题标题】:CSS Masonry UI not working properly using `column-count` and `box-shadow`CSS Masonry UI 使用 `column-count` 和 `box-shadow` 无法正常工作
【发布时间】:2018-11-28 05:18:10
【问题描述】:

下面是我的 Masonry UI 代码,我使用的是纯 CSS

如果有超过 4 张卡,这很有效,但如果我将它与 4 张卡一起使用,column-count: 3; 就不能正常工作。

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>

尝试过的解决方案:

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; 用于上述解决方案&lt;li&gt;,它在这种情况下有效,但在我的真实视图中,它会缩小最后一个img's box-shadow 检查下面的屏幕截图。

任何解决方案都会非常有帮助。

【问题讨论】:

  • 如果它与您的“真实视图”隔离开来,您需要告诉我们您在其他地方使用/做什么。不过,作为一种猜测,您是否确定 overflow:visible 已应用于包含元素?
  • @dom_ahdigital 我已经重新创建了这个错误,请看看。
  • @webBer 这可能会有所帮助Pure css example
  • @Matt 您能否将该示例代码发布为我的 UI 的答案,如果它有效,我将很乐意将其标记为答案并给您赏金。
  • @webBer 我尝试修改示例代码,使用您的图像并应用框阴影。我希望它会有所帮助

标签: html css


【解决方案1】:

我知道这不是一种合适的解决方案,但是当我使用砖石时它对我很有效 事情是删除column-count 属性,只给li 宽度,例如width:25%;width:300px; 并使用flex-box 来维护它的响应能力。 (使用 flex-box 由你决定)

body{
  height:1000px;
}

ul {
    list-style: none;
   
 
    padding: 0px 4px 4px 4px;
    margin-top: -10px;
    display: flex;
    justify-content:center;
    flex-wrap:wrap;/*suggested to use if you set width in px*/

    width: 100%;
    margin: 0px;
    
}

li {
    width: 25%;
    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>

没有flex-box

body{
  height:1000px;
}

ul {
    list-style: none;
   
 
    padding: 0px 4px 4px 4px;
    margin-top: -10px;
 

    width: 100%;
    margin: 0px;
    
}

li {
    width: 25%;
    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>

【讨论】:

  • 问题是兄弟,我有不同大小的图像,Masonry UI 是在这种情况下将它们堆叠起来的最佳选择..
  • 嗯,你是对的,那么当我使用它时,这个解决方案可能不起作用我有固定宽度的图像
  • img 的高度也会变化很大。所以 Masonry UI 将是最好的。
【解决方案2】:

我希望这样的事情会有所帮助。

.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>

【讨论】:

  • 你的答案是肯定的,但如果你使用错误图像,它是 box-shadow 作物。
  • 对不起@webBer,你是什么意思?我会尝试解决这个问题:) p.s.我尝试添加更多图像,但看起来仍然不错。
  • 请检查此链接 - jsfiddle.net/phrj3evk/2,我希望您看到图像之间的错位问题。
【解决方案3】:

这里的根本原因似乎是li 元素上的display: inline-block。将此更改为 block 应该将它们包装成列。

li {
    width: 25%;
    display: block;
    box-shadow: none;
    background: transparent;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    margin: 2%;
}

希望对您有所帮助。

【讨论】:

  • 你的答案是我认为我想要的最接近的答案,但是仍然存在问题但可以解决,所以如果你粘贴我的示例代码并证明你的答案,那么我很乐意将其标记为一个和你的赏金。
  • 请注意,删除 inline-block 可能会导致元素在 2 列之间拆分(一列中的部分元素和另一列中的其余元素跨度>
  • @vals 你能提出任何解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
相关资源
最近更新 更多