I am trying to have an image as a background. Background image should cover whole scrollable page (width+height). And background image should not be fixed, it should also scroll.
Here is example html:
<html>
<head></head>
<body>
<div class="content"></div>
</body>
</html>
Here is examle css:
html, body
{
height: 100% !important;
overflow: auto !important;
}
body:before {
content: "";
position: fixed;
display: block;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: -1;
background-color: blue;
background-repeat: no-repeat;
background-position: center top;
background-attachment: fixed;
background-size: cover;
background-image: url('https://static.pexels.com/photos/392018/pexels-photo-392018.jpeg');
}
.content {
height: 1800px;
width: 200px;
background-color: white;
margin: auto;
}
And here is JSFIDDLE
When you look at JSFIDDLE snippet, blue background color should not be visible at all - it should be covered by background image.
Is there a way how to achieve it?
html,
body {
height: 100% !important;
overflow: auto !important;
}
body:before {
content: "";
position: fixed;
display: block;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: -1;
background-color: blue;
background-repeat: no-repeat;
background-position: center top;
background-attachment: fixed;
background-size: cover;
background-image: url('https://static.pexels.com/photos/392018/pexels-photo-392018.jpeg');
}
.content {
height: 1800px;
width: 200px;
background-color: white;
margin: auto;
}
<html>
<head></head>
<body>
<div class="content"></div>
</body>
</html>
Thanks
Remove the height:100%
and make it min-height
instead. You can also simplify your code like this:
body {
min-height: 100%;
background-color: blue;
position:relative;
margin:0;
}
body:before {
content:"";
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
background-repeat: no-repeat;
background-position: center top;
background-size: cover;
background-image: url('https://static.pexels.com/photos/392018/pexels-photo-392018.jpeg');
}
.content {
height: 1800px;
width: 200px;
background-color: white;
margin:auto;
position:relative;
z-index:1;
}
<div class="content"></div>