Keywords: Bootstrap 3 | box-shadow | negative margins | container shadow | CSS layout
Abstract: This article addresses the issue where box-shadow applied to a Bootstrap 3 container may be overlapped by grid rows due to the use of negative margins in the grid system. Based on the best answer, it proposes a solution of adding padding to ensure proper shadow display without compromising Bootstrap functionality. Detailed code examples are provided, rewritten for clarity, to help developers tackle common layout challenges.
Problem Description
When applying box-shadow to a Bootstrap 3 container element, developers often encounter issues where the shadow does not appear as expected or is overlapped by grid rows. For instance, adding CSS property box-shadow to <div class="container"> may result in the shadow being partially or fully hidden because Bootstrap's grid system uses negative margins to align columns, causing child elements like <div class="row"> to extend beyond the container's bounds and cover the shadow area.
Cause Analysis
Bootstrap 3's grid system employs negative margins to handle responsive layout by offsetting container padding for column alignment. Typically, the .container class has left and right padding of 15 pixels, while .row classes use negative margins. However, when box-shadow is applied to the container, the shadow is calculated based on the border-box, and negative margins can cause child elements to exceed the container's boundaries, clipping or hiding the shadow. Specifically, insufficient or unadjusted padding in the container allows grid rows to occupy extra space, obstructing the shadow rendering.
Solution
To resolve this, the optimal approach is to adjust the container's padding settings. By adding appropriate left and right padding to the .container class, the shadow area can be protected from overlap. It is recommended to use padding: 0 15px 0 15px;, which aligns with Bootstrap's default grid padding, ensuring consistent layout while enabling box-shadow to render correctly. This method is straightforward and effective, requiring no modifications to Bootstrap core files or additional HTML structures.
Code Example
Below is a rewritten code example demonstrating the proper implementation of box-shadow:
<style type="text/css">
.row {
height: 100px;
background-color: green;
}
.container {
margin-top: 50px;
box-shadow: 0 0 10px 10px black;
padding: 0 15px 0 15px; /* Key modification: add padding to prevent overlap */
}
</style>
<div class="container">
<div class="row">Row one</div>
<div class="row">Row two</div>
<div class="row">Row three</div>
</div>
Conclusion and Additional Notes
Adding padding effectively solves the problem of box-shadow being overlapped by grid rows in Bootstrap 3 containers, ensuring uniform shadow display on all sides while maintaining responsive grid functionality. As supplementary reference, alternative methods like adding extra divs and managing shadows with CSS classes are viable but may increase code complexity. In summary, understanding Bootstrap's layout mechanisms and adjusting CSS appropriately is key to optimizing visual effects. Note that this issue was resolved in the final release of Bootstrap 3, but the presented approach remains valuable for earlier versions or custom scenarios.