Home >Web Front-end >JS Tutorial >How to Extend Wrapped Elements to the Full Browser Width Using CSS
This article discusses extending CSS elements beyond a centered page to fill the browser window, a common layout challenge. The problem arises when a centered element (e.g., an article
with width: 70%; margin: 0 auto;
) needs to have a full-width header or footer.
While using a body
background for headers is simple, footers pose a problem due to their content-dependent position within the centered article
. A common, albeit semantically questionable, solution is using wrapper divs:
<article> …content… <div class="content"> <p>Footer content.</p> </div> </article>
With CSS:
footer { width: 100%; background: url(footer.png) 0 0 repeat-x; } .content { width: 70%; margin: 0 auto; }
This adds unnecessary divs. A cleaner cross-browser solution uses padding and negative margins:
body { overflow-x: hidden; } .extendfull, .extendleft { padding-left: 3000px; margin-left: -3000px; } .extendfull, .extendright { padding-right: 3000px; margin-right: -3000px; }
Classes extendleft
, extendright
, and extendfull
control the extension. overflow-x: hidden
prevents horizontal scrolling. This works across major browsers, but IE6 and IE7 require a fix:
/* IE6/7 fix */ .extendfull, .extendleft, .extendright { position: relative; display: inline; float: left; width: 100%; }
This might affect modern browser layouts; adjustments may be needed.
Frequently Asked Questions (FAQs):
The article then provides a FAQ section addressing common questions about creating responsive, centered, and styled full-width bars using CSS, including:
vw
units for viewport-based width.body
or html
.position: sticky;
for fixed positioning on scroll.The article concludes by reiterating the solutions provided and highlighting their advantages and potential drawbacks.
The above is the detailed content of How to Extend Wrapped Elements to the Full Browser Width Using CSS. For more information, please follow other related articles on the PHP Chinese website!