Reputation: 3025
What I want
two div : A page body (1) and a background block (2)
Constraints
What I tried
Page body CSS:
#pageBody {
width: 820px;
margin: 0 auto;
}
Background block CSS:
1. A full-page div which displays a CSS centered background
<div id="backgroundBlock"></div>
<div id="pageBody">
</div>
#backgroundBlock {
background: no-repeat center 0 url(bg.png);
width: 100%;
height: 100%;
position: absolute;
}
But when the window's size is odd:
2. A repositioned child div
<div id="pageBody">
<div id="backgroundBlock"></div>
</div>
#backgroundBlock {
background: no-repeat url(bg.png);
width: 2560px;
height: 780px;
position: absolute;
margin: 0 0 0 -870px;
overflow: hidden;
}
But problem: the scroll bar appears for the background block...
Upvotes: 4
Views: 2513
Reputation: 3408
Here are a few ideas I could think of, with their issues. However, I could not reproduce the "blurry in IE" issue, so I don't know which solution have it or not.
I did put "Extra markup" as an issue for solutions including a div
(#backgroundBlock
) only used to display the background image, as it is not semantic.
Description : Multiple-backgrounds on body. #backgroundBlock
div not needed.
body {
background: no-repeat center top url(bg.png), url(bodybg.png);
}
Issues :
Description : Use of translate
. No more pixel alignment errors.
#backgroundBlock
{
background: url(bg.png);
width: 2560px;
height: 780px;
position: absolute;
top: 0;
left: 50%;
transform: translate(-50%, 0);
}
Issues :
overflow-x: hidden
on body
to avoid horizontal scrollbar-webkit-
, -moz-
, ...). I did not add them to keep the example simpleDescription : Use of translate
and ::before
. Alternative version of solution 3. Pseudo-elements compatibility are not an issue here since every browser supporting 2D-tranforms supports ::before
(source).
#backgroundBlock
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
#backgroundBlock:before
{
content: '';
background: url(bg.png);
width: 2560px;
height: 780px;
position: absolute;
top: 0;
left: 50%;
transform: translate(-50%, 0);
}
Issues :
-webkit-
, -moz-
, ...). I did not add them to keep the example simpleThere are other possibilities but I think most of them would have one of the above issues.
For example, you could set the #pageBody
width to 2560px, set the background on it, add padding to have a content size of 820px and translate
it in order to have it centered on the page (and prevent horizontal scrollbars using overflow-x
on body
). This would be possible because the background image and page body both have fixed width.
Upvotes: 2