本文使用响应式图像库探讨了掌握响应网格布局中列之间间距的技术。
>>要进一步了解响应式布局,请查看我们的屏幕截图:在Flexbox中创建多列布局。
钥匙要点:
display: inline-block
方法创建响应式图像库;将父字体的大小设置为零,删除了默认的内联窗口间距。构建一个响应式布局:>
在较大的屏幕上,画廊类似于以下内容:>
<div> <a href="https://www.php.cn/link/14d2bc475177e1dde633b4ca1972d53c"> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/" class="lazy" alt="Using Modern CSS to Build a Responsive Image Grid " /> </a> <!-- ...more images... --> </div>
使用:inline-block
方法构建画廊。 考虑一下此CSS:display: inline-block
div { font-size: 0; } a { font-size: 16px; display: inline-block; margin-bottom: 8px; width: calc(50% - 4px); margin-right: 8px; } a:nth-of-type(2n) { margin-right: 0; } @media screen and (min-width: 50em) { a { width: calc(25% - 6px); } a:nth-of-type(2n) { margin-right: 8px; } a:nth-of-type(4n) { margin-right: 0; } }
通过将父字体大小设置为零来覆盖
默认的内联块间距。 子元素字体大小可能需要重置(不在此)。>
小屏幕具有两个列布局,带有8px间距。 列宽度计算:
calc(50% - 4px)
calc(25% - 6px)
请参阅
>
使用flexbox:
inline-block
解决方案具有缺点。 添加标题演示一个:
更新的标记:
<div> <a href="https://www.php.cn/link/14d2bc475177e1dde633b4ca1972d53c"> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/" class="lazy" alt="Using Modern CSS to Build a Responsive Image Grid " /> </a> <!-- ...more images... --> </div>
>带有标题的大屏幕库:
不相等的高度。 更新父元素的CSS:
div { font-size: 0; } a { font-size: 16px; display: inline-block; margin-bottom: 8px; width: calc(50% - 4px); margin-right: 8px; } a:nth-of-type(2n) { margin-right: 0; } @media screen and (min-width: 50em) { a { width: calc(25% - 6px); } a:nth-of-type(2n) { margin-right: 8px; } a:nth-of-type(4n) { margin-right: 0; } }
结果是在所有屏幕上相等的高度列。大屏幕示例:
>使用带有改进字幕的Flexbox请参阅Codepen演示。
> Flexbox的属性并未直接创建此布局。 justify-content
>和space-between
在最后一行导致尴尬的分布。 CSS:space-around
<div> <a href="https://www.php.cn/link/14d2bc475177e1dde633b4ca1972d53c"> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/" class="lazy" alt="Using Modern CSS to Build a Responsive Image Grid " /> <figcaption>Some text here</figcaption> </a> <!-- ...more images... --> </div>
需要; margin-right
处理项目分布。justify-content
属性参见Codepen演示。justify-content
结论:
>。
inline-block
calc()
在我们的屏幕截图中了解有关Flexbox布局的更多信息:在Flexbox中创建多列布局。
> >(为简洁而省略了FAQ部分,因为这是对常见响应式设计问题的重复。)
以上是使用现代CSS构建响应式图像网格的详细内容。更多信息请关注PHP中文网其他相关文章!