This should get you a single image showing at the full size of the browser, even if you have a <!DOCTYPE>
(which I recommend you do include, as it's better practice):
html, body {
width: 100%; height: 100%;
margin: 0; padding: 0;
}
body {
image-rendering: pixelated;
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Square_definition.svg/178px-Square_definition.svg.png");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
Note it's important to make sure that html
and body
elements have 100% size (and to remove any default margin + padding added by browser).
Note I used a random example image.
EDIT: D.Pardal rightfully pointed out that this answer does not explain the difference observed.
Most of the explanation comes from the fact that:
- Adding
<!DOCTYPE html>
puts the document into "standards mode"
- "Standards mode" dictates that the
body
element's height defaults to the minimum size needed to contain its content
The original version without <!DOCTYPE html>
has a <body>
which fills the page. Applying a background-image to this <body>
, with background-size: 100% 100%
fills the entire page. Adding the DOCTYPE
shrinks the body
's height.
"Why is the image visible at all if its height is equal to the <body>
's height of 0?"
Because the body has default margins, applied by the browser, and these count towards the height of the background-image.
"Why wasn't the image repeating in the first place?"
It was repeating! The first repetition, however, was big enough to fill the entire parent. This can be verified by removing the DOCTYPE
and shrinking the image's size to background-size: 20% 20%;
.