Resolving Gaps in Bootstrap's Stacked Rows
When constructing a Bootstrap grid for a portfolio page, you may encounter an issue where longer tiles cause a gap in the layout when stacking occurs. This issue arises due to the varying heights of elements within the grid. To resolve this, several solutions exist:
-
Set a Height for All Portfolio Elements: Establishing a fixed height for each portfolio element ensures consistent stacking, eliminating gaps.
-
Use Masonry for Dynamic Fitting: Masonry is a layout tool that dynamically arranges elements to fit the available space, eliminating gaps even with varying content lengths.
-
Employ Responsive Classes and Clearfix: As defined in the Bootstrap documentation under "Responsive Column Resets," this approach involves using responsive classes and clearfix to address grid alignment issues at different breakpoints.
-
Utilize jQuery to Adjust Column Heights Dynamically: jQuery can be used to adjust the heights of columns dynamically, ensuring that they align properly regardless of content length.
Here's a specific technique to resolve the issue:
Add a clearfix div after each grid element:
<div class="row portfolio">
<div class="col-...">
...
</div>
<div class="clear"></div>
</div>
Copy after login
Apply a media query to the clearfix div to clear the float:
@media (max-width: 767px) {
.portfolio>.clear:nth-child(6n)::before {
clear: both;
}
}
Copy after login
This approach provides:
-
Readable and maintainable markup: The HTML remains simple, allowing for easy management of portfolio elements.
-
Alignment across different screen sizes: The CSS ensures that elements are aligned at all common breakpoints, eliminating gaps.
The above is the detailed content of How to Eliminate Gaps in Bootstrap Stacked Rows?. For more information, please follow other related articles on the PHP Chinese website!