Ce que je veux
deux div: un corps de page (1) et un bloc d'arrière - plan (2)
Contraintes
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...
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 simpleIl existe d'autres possibilités, mais je pense que la plupart d'entre elles auraient l'un des problèmes ci-dessus.
Par exemple, vous pouvez définir la #pageBody
largeur de 2560px, régler le fond sur elle, ajouter le remplissage d'avoir une taille de contenu 820px et translate
dans le but de le centrer sur la page (et empêcher les barres de défilement horizontales à l' aide overflow-x
sur body
). Cela serait possible car l'image d'arrière-plan et le corps de la page ont tous deux une largeur fixe.
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots