【发布时间】:2018-02-24 01:56:36
【问题描述】:
我正在尝试使用此代码在纯 HTML/CSS(基本上,不使用引导程序)响应式网站上实现全宽 4 列布局:
HTML:
<div class="row-fourcol">
<div class="column-fourcol">
<div class="stockitembox">
<img src="../resources/graphics/images/product_hexagonal.png" title="Search our hexagonal steel stock range ..." alt="Hexagonal steel bar" onclick="hexagonalFunction()">
<p>HEXAGONAL</p>
</div>
</div>
<div class="column-fourcol">
<div class="stockitembox">
<img src="../resources/graphics/images/product_round.png" title="Search our round steel stock range ..." alt="Round steel bar" onclick="roundFunction()">
<p>ROUND</p>
</div>
</div>
<div class="column-fourcol">
<div class="stockitembox">
<img src="../resources/graphics/images/product_rectangular.png" title="Search our rectangular steel stock range ..." alt="Rectangular steel bar" onclick="rectangularFunction()">
<p>RECTANGULAR</p>
</div>
</div>
<div class="column-fourcol">
<div class="stockitembox">
<img src="../resources/graphics/images/product_square.png" title="Search our square steel stock range ..." alt="Square steel bar" onclick="squareFunction()">
<p>SQUARE</p>
</div>
</div>
</div>
CSS:
.column-fourcol {
float: left;
width:24.3%;
padding: 10px 10px 10px 0px;
}
.row-fourcol::after {
content: "";
display: table;
clear: both;
}
@media (max-width: 1903px) {
.column-fourcol {
width: 45%;
float: left;
}
}
但是,我的问题是,在添加间距(使用填充)时,我必须在右侧添加填充,这意味着我的列在左侧齐平,但在右侧不齐平,因为它们没有填充整个空间或由于右侧填充而下降到下一行。我在下图中显示了这一点,左侧与左边框齐平,但右侧不能齐平,因为填充会阻止它。
如何制作一个 4 列布局,其中列是间隔开的,但在左侧和右侧都与容器的侧面齐平?我已经搜索了与我自己类似的问题(例如:CSS Full-Width Grid - Columns with Even Margins)但是,这个示例似乎与我想要的间隔列效果不匹配(或者我可能只是不明白答案)。
Example of four column padding (红线表示容器的侧面。)
感谢您的帮助,非常感谢所有帮助。
【问题讨论】:
-
首先,要成为 css world 构建响应式网站的摇滚明星,不要使用
float或display: table,使用css grid或flexbox -
不要在最后一个元素上放置正确的内边距,或者使用border-box box-sizing。