English 中文(简体)
How to have a background image wider than the content area of a website (without scrollbars)
原标题:

I ve been given a design for a website, and am trying to implement it.

One problem I ve run into is that the design has some background images (one for the header, one for the body, and one for the footer of the site) that are wider than the main content area of the site.

Simply putting them in as background images doesn t work, since expanding the header, body and footer divs enough to accommodate the backgrounds causes horizontal scrollbars to appear if the browser window is not big enough to fully show the backgrounds.

This is undesirable since the backgrounds are not really important for viewing the website, and I don t want scrollbars to appear for them (scrollbars should only appear once the browser is too small to completely show the content of the website).

The second technique is to have a separate, absolutely positioned div to show the header background image (and put it under an element with the browser window s size), and set its width to 100% so that it never exceeds the size of the browser window (and hence create scrollbars).

This works up to a point - however, when the window is too small, the background starts shifting around relative to the content since the "top center" position of the background is relative to the browser window, not the content area. At large sizes, these are effectively the same since the content area is centered, but at small sizes, only part of the content is shown, so the center of the content and the center of the browser window are different.

A good illustration of this problem that I ve found is the Quicken website: http://quicken.intuit.com/. At large sizes, its cloud background looks fine, but if you make your window s width small enough, the clouds start shifting relative to the content (bad!).

Any ideas on how to fix this so that backgrounds images

  1. don t create scrollbars since they are not part of the content of the site
  2. are fixed relative to the content of the site (and don t shift around at small browser window sizes)

?

An ideal solution would be something like turning overflow to hidden on the body, but only for specified divs. Unfortunately I believe this is impossible.

I d prefer a pure html/css solution, but I accept that I may need js to get the effect I want.

Thanks! (this is a complex issue, so if any clarification is needed, let me know)

UPDATE: Fixed this by setting min-width on the background div to the width of the content.

最佳回答

Set the min-width on the div containing the background image to the width of the content.

问题回答

You need to have your header, content & footer have a width of 100%. And put the image in as a background image in these divs ... center it horizontally.

Inside the specific divs have a wrapper that is centered. and is the width of the content of them divs.

Like so.

HTML

<div id="header">
    <div class="wrapper">
        ...
    </div>
</div>
<div id="content">
    <div class="wrapper">
        ...
    </div>
</div>
<div id="footer">
    <div class="wrapper">
        ...
    </div>
</div>

CSS

div#header {
    background: url(...) 50% 0; /* to center your background image horizontally */
    }

div#content {
    background: url(...) 50% 0; /* to center your background image horizontally */
    }

div#footer {
    background: url(...) 50% 0; /* to center your background image horizontally */
    }

div.wrapper {
    margin: 0 auto; /* to center the div horizontally */
    width: 960px; /* or however wide it should be */
    }

Hope this helps.

Am I missing something, or should you be using the CSS background-image property?

I had a look at the Quicken site, and to be honest the cloud background image shifting when the browser is resized shouldn t be worried about unless your background-image is most distinctive than a bunch of clouds.

See what I mean?

You could use the overflow property and set it to hidden on the div that cause a scrollbars to appear.

I had the same issue on a site that I worked on, and come up with the following solution, which works well if all your background images are the same width.

/* 
A container div that is set to the 100% width, with the overflow set to hidden. 
This hides the overflowing images if the window sizes is too small 
*/

#bg_container {
    position:absolute;
    z-index:0;
    top:0px;
    width:100%;
    overflow:hidden;
}

/* 
A div that sets the size of the content and centers itself on the page. 
*/

.bg {
    margin:0 auto;
    width:1000px;  /* content size */
    overflow:visible;
}

/* 
Here I set the image away from the left edge of the div to center it to the content. The actual size of the image is 1500px. 
*/

.bg img {
    margin-left:-250px;
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签