0

I'm wondering why contain works like this. This is my HTML code.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Zero to Mastery | Landing Page</title>
  <link rel="stylesheet" type="text/css" href="landingPageStyle.css">    
</head>

<body>

</body>

</html>

Now, this is my CSS code:

body {
/* min-height: 100%;   */
width: 600px;
height: 600px;
border: 4px solid red;
background-image: url('Nature500x400.jpg');
background-size: contain;   
background-repeat: no-repeat;
}

My question is this: why is not the whole background picture inside the body? Why the picture exceed the border like in this picture of browser window:

https://i.stack.imgur.com/uDuzD.png

Darko
  • 13
  • 6

3 Answers3

0

Try changing to background-size:cover. You will need to add this property to a full-width div inside your body. You can not add it to the body

div {
/* min-height: 100%;   */
width: 100%;
height: 600px;
border: 4px solid red;
background-image: url('Nature500x400.jpg');
background-size: cover;   
background-repeat: no-repeat;
}
Claire
  • 3,146
  • 6
  • 22
  • 37
0

background-size: contain not work in body tag

Try this

<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Zero to Mastery | Landing Page</title>
  <link rel="stylesheet" type="text/css" href="landingPageStyle.css">    
</head>

<body>
<div>

</div>
</body>

</html>

and css

    div {
width: 600px;
height: 600px;
border: 4px solid red;
background-image: url('img Url');
background-size: contain;   
background-repeat: no-repeat;
}
Salar
  • 1
  • 1
0

Applying the styles to html instead of body will fix this problem

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Zero to Mastery | Landing Page</title>
  <link rel="stylesheet" type="text/css" href="landingPageStyle.css">    
</head>

<body>

</body>

</html>

Use html instead:

html {
/* min-height: 100%;   */
width: 600px;
height: 600px;
border: 4px solid red;
background-image: url('Nature500x400.jpg');
background-size: contain;   
background-repeat: no-repeat;
}
Claire
  • 3,146
  • 6
  • 22
  • 37
Friday Ameh
  • 1,664
  • 1
  • 10
  • 15