我已经看到很多尝试这样做,但没有一个能满足我的强迫症需求。尽管这比使用 JavaScript 更好。
已知的缺点:
- 在具有动态宽度的容器的情况下不支持多个元素行。
- 在 IE6 中不起作用。
基地:
-
red 是(辅助)容器,用于设置内容的边距。
-
green 是
position: relative; overflow: hidden 和(可选,如果您希望列居中)text-align: center; font-size: 0; line-height: 0;
-
蓝色
display: block; float: left; 或(可选,如果您希望列居中)display: inline-block; vertical-align: top;
到目前为止,没有什么不寻常的。无论 blue 元素有什么内容,都需要添加一个绝对定位的元素(yellow;注意这个元素的z-index必须低于实际的蓝框的内容)用这个元素设置top: 0; bottom: 0;(不要设置左右位置)。
您的所有元素现在都具有相同的高度。对于大多数布局,这已经足够了。我的场景需要动态内容后跟静态内容,其中静态内容必须在同一行。
为此,您需要将padding-bottom (dark green) eq 添加到 blue 元素的固定高度内容中。
然后在 yellow 元素内创建另一个绝对定位 (left: 0; bottom: 0;) 元素(深蓝色)。
假设,如果这些框(黄色)必须是活动超链接,并且您有任何想要应用于原始蓝色框的样式,则可以使用相邻兄弟选择器:
yellow:hover + blue {}
这是代码和demo:
HTML:
<div id="products">
<ul>
<li class="product a">
<a href="">
<p class="name">Ordinary product description.</p>
<div class="icon-product"></div>
</a>
<p class="name">Ordinary product description.</p>
</li>
<li class="product b">
<a href="">
<p class="name">That lenghty product description or whatever else that does not allow you have fixed height for these elements.</p>
<div class="icon-product"></div>
</a>
<p class="name">That lenghty product description or whatever else that does not allow you have fixed height for these elements.</p>
</li>
<li class="product c">
<a href="">
<p class="name">Another ordinary product description.</p>
<div class="icon-product"></div>
</a>
<p class="name">Another ordinary product description.</p>
</li>
</ul>
</div>
SCSS/LESS:
#products {
ul { position: relative; overflow: hidden; text-align: center; font-size: 0; line-height: 0; padding: 0; margin: 0;
li { display: inline-block; vertical-align: top; width: 130px; padding: 0 0 130px 0; margin: 0; }
}
li {
a { display: block; position: absolute; width: 130px; background: rgba(255,0,0,.5); z-index: 3; top: 0; bottom: 0;
.icon-product { background: #ccc; width: 90px; height: 90px; position: absolute; left: 20px; bottom: 20px; }
.name { opacity: 1; }
}
.name { position: relative; margin: 20px 10px 0; font-size: 14px; line-height: 18px; opacity: 0; }
a:hover {
background: #ddd; text-decoration: none;
.icon-product { background: #333; }
}
}
}
请注意,该演示使用涉及数据复制的解决方法来修复z-index。或者,您可以使用pointer-events: none 以及任何适用于 IE 的解决方案。