html - CSS Background color is not full, it does not cover the whole page properly


Keywords:html 


Question: 

I am trying to add background color to my page, but it does not cover the whole page properly.

Screenshot 1:As it can be seen here, there are white space on the background after the cards

CSS CODE

#background  
{   
    background-color:#9B59B6;
    margin: -19px 0 0 0; /*-19px here to remove white space on the very top of the page*/
    width: 100%;  
}

If I add :

position:absolute; 
height:100%; 
width:100%;

It would solve the problem, However, it would cause another problem like this:

Screenshot 2:Here, when I added more cards, and scroll it down, there is a white space below the background

CSS CODE for screenshot 2

#background
{ 
    position:absolute; 
    height:100%; 
    width:100%;
    background-color:#9B59B6;
    margin: -19px 0 0 0;
    width: 100%;
}

How do I solve this? I tried using position:relative or overflow:hidden, But it does not help.

I am doing this on ASP.net MVC 6, so the format is cshtml instead of html.

Please help, Thank you!


7 Answers: 

Make use of viewheight and viewwidth.

html, body, #background { min-height: 100vh; min-width: 100vw; background-color:#9B59B6;}
 

Color the body

 body{
     background-color:black;
  }
 
body{background-color:#9B59B6;}

This should work. But what is the html element that #background id is set to?

I guess that is where the problem is.

 

Your body is not set to 100% height I think.

html, body {
    height: 100%;
}

and then

#background {
    height: 100%;
}

But why not set the background color property directly to the body ?

 

You have to make sure cover the whole body property

body  
{   
    background-color:#9B59B6;
    margin: -19px 0 0 0; /*-19px here to remove white space on the very top of the page*/
    width: 100%;  
}
<html>
  
  <body >
    </body>
  </html>
 

You should properly use body { background: #9B59B6 } or you well need to make your #background fixed instead of absolute

#background
{ 
    position:fixed; 
    height:100%; 
    width:100%;
    background-color:#9B59B6;
    margin: -19px 0 0 0;
    width: 100%;
}

Browser have default padding on a page, you can disable that with html, body { padding: 0 } instead of the margin: -17px

 
body{
margin:0px;
padding:0px;
}

#background
{ 
    position:absolute; 
    height:100%; 
    width:100%;
    background-color:#9B59B6;
    margin: 0px 0 0 0;

}