i am making a website to practice my web developer skills, (i am still a beginner so be easy on me please). Anyway, i added a background image and i am making it responsive. i am using twitter bootstrap and using the media queries, Everything is working fine, except that when i reduce the web browser’s window starting at 595 height x 591 width, the background image starts to not cover the entire window. i want to know why because i am using the media queries, as twitter bootstrap says and i thought the extra small device media query cover this.
I’m really sorry for my bad English, it isn’t my first language.
If i am not wrong, you can see my HTML and CSS source code my right clicking on the page and clicking on the inspect option, but i am going to add the code here just in case:
body {
/* background-color: #000000 */
background-image: url(../images/city_landscape.png);
background-repeat: no-repeat;
}
/********** Large devices only **********/
@media (min-width: 1200px) {
body {
background-size: cover;
}
}
/********** Medium devices only **********/
@media (min-width: 992px) and (max-width: 1199px) {
body {
background-size: cover;
}
}
/********** Small devices only **********/
@media (min-width: 768px) and (max-width: 991px) {
body {
background-size: cover;
}
}
/********** Extra small devices only **********/
@media (max-width: 767px) {
body {
background-size: cover;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cyberangel's site</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<!-- jQuery (Bootstrap JS plugins depend on it) -->
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/ajax-utils.js"></script>
</body>
</html>
5
Answers
That happens because the body doesn’t stretch the whole screen, since its content is not big enough.
To solve this, you can try adding
height: 100vh
to your body:To apply an image to the entire page, you need to also give
body
andhtml
a height of 100:From: W3Schools
Edit* If you want to dislike, can you also elaborate why?
I would also like to add (and if anyone would like to elaborate) that you can use CSS flexbox and CSS grid which are much easier to understand, that makes Bootstrap obsolete in my opinion, but some say it’s still better than grid, still, I’m also just a beginner.
I learn from this as it is really easy to understand:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://css-tricks.com/snippets/css/complete-guide-grid/
Instead of
background-size: cover
you can useobject-fit: fill;
which will fill entire screen:Bear in mind that this property is not very well supported by Internet Explorer/Edge browsers.
Adding
height: 100vh;
to thebody
tag will do the trick.