Bootstrap 3中能否将两张背景图在一行并排?附布局问题求助
Hey Jeff, let's fix that stubborn footer layout issue you're dealing with in Bootstrap 3. First, let's address why your green div keeps wrapping—chances are it's a misstep with Bootstrap's grid system, which is designed exactly for this kind of side-by-side layout. Let's start with the proper grid-based solution, then I'll share the absolute positioning fallback you asked for.
1. Correct Bootstrap 3 Grid Implementation (No Wrapping)
Bootstrap 3 uses a 12-column grid, so dividing your footer into two equal blocks means using col-md-6 (adjust the breakpoint to col-sm-6 or col-xs-6 if you want them to stay side-by-side on smaller screens). The key is nesting your columns inside a .row and ensuring you don't add extra margins/padding that breaks the grid's width constraints.
Here's a complete working example:
<footer class="footer-container"> <div class="row"> <!-- First Footer Block --> <div class="col-md-6 footer-block footer-block-left"> <h3>About Us</h3> <p>Your text content here. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <div class="social-icons"> <a href="#"><i class="fa fa-facebook"></i></a> <a href="#"><i class="fa fa-twitter"></i></a> </div> </div> <!-- Second Footer Block --> <div class="col-md-6 footer-block footer-block-right"> <h3>Contact Us</h3> <p>Another text block here. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <div class="social-icons"> <a href="#"><i class="fa fa-instagram"></i></a> <a href="#"><i class="fa fa-linkedin"></i></a> </div> </div> </div> </footer>
.footer-container { width: 100%; margin: 0; padding: 0; } .footer-block { min-height: 250px; /* Set a minimum height for your footer blocks */ padding: 2rem; color: white; /* Adjust text color for contrast with background */ box-sizing: border-box; /* Critical to prevent padding from breaking width */ } .footer-block-left { background: url('your-left-background-image.jpg') no-repeat center center; background-size: cover; } .footer-block-right { background: url('your-right-background-image.jpg') no-repeat center center; background-size: cover; } .social-icons a { color: white; margin-right: 1rem; font-size: 1.5rem; }
If your green div was still wrapping, check for these common issues:
- Did you forget to wrap the columns in a
.row? The.rowadds negative margins to offset column padding, which keeps everything aligned. - Are you adding custom
width: 100%or extramargin-left/margin-rightto the footer blocks? That will push the column beyond the grid's 50% width. - Are you using a
.containeraround the.row? If you want full-width footer, use.container-fluidinstead, or skip the container entirely.
2. Absolute Positioning Fallback
If for some reason the grid approach isn't working for your setup, here's the absolute positioning solution you requested. We'll make the footer a relative container, then position each block absolutely to take half the width:
<footer class="footer-absolute-container"> <div class="footer-absolute-block footer-absolute-left"> <h3>About Us</h3> <p>Your text content here. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <div class="social-icons"> <a href="#"><i class="fa fa-facebook"></i></a> <a href="#"><i class="fa fa-twitter"></i></a> </div> </div> <div class="footer-absolute-block footer-absolute-right"> <h3>Contact Us</h3> <p>Another text block here. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <div class="social-icons"> <a href="#"><i class="fa fa-instagram"></i></a> <a href="#"><i class="fa fa-linkedin"></i></a> </div> </div> </footer>
.footer-absolute-container { position: relative; width: 100%; min-height: 250px; /* Match the block height */ overflow: hidden; /* Prevent content from spilling outside */ } .footer-absolute-block { position: absolute; top: 0; width: 50%; min-height: 100%; padding: 2rem; color: white; box-sizing: border-box; } .footer-absolute-left { left: 0; background: url('your-left-background-image.jpg') no-repeat center center; background-size: cover; } .footer-absolute-right { right: 0; background: url('your-right-background-image.jpg') no-repeat center center; background-size: cover; } .social-icons a { color: white; margin-right: 1rem; font-size: 1.5rem; } /* Optional: Adjust for extra small screens if needed */ @media (max-width: 767px) { .footer-absolute-block { width: 100%; min-height: 200px; position: relative; } }
This absolute positioning setup ensures both blocks stay side-by-side regardless of screen size (unless you add the media query to stack them on mobile, which is optional). Just make sure your footer has a defined height so the absolute blocks have a reference point.
内容的提问来源于stack exchange,提问作者Jeff

