0

I have a very simple website I'm working on for practice and having a problem with the background-color for my container div. I'm sure it's a pretty simple fix but I want to understand why it didn't work. Here is my CSS:

body {
  margin: 0px;
  padding:0px; 
  height: 100%; 
  background-color: black;
}

#Container {
  width: 98%;
  height: 100%;
  background-color: grey;
}

For some reason the only way I've managed to get it working is when I include html in my body styling, is there a reason for this?

body, html {
  margin: 0px;
  padding:0px; 
  height: 100%; 
  background-color: black;
}

Thanks

andreas
  • 16,357
  • 12
  • 72
  • 76
Sai
  • 801
  • 3
  • 10
  • 27
  • 3
    That is because 100% is a relative unit. Without declaring the height of the `html` element, the `body` does not know what 100% should be based on. Also, it would be recommended to set `body` to `min-height: 100%`, so that it can "grow" if the total content exceeds viewport height. – Terry Nov 14 '14 at 14:12
  • possible duplicate of [Make div 100% height of browser window](http://stackoverflow.com/questions/1575141/make-div-100-height-of-browser-window) – Vitorino fernandes Nov 14 '14 at 14:21
  • So is it right in saying you have to give html a width or height 100% if you want other divs to use % as a unit? – Sai Nov 14 '14 at 14:22
  • @Sai That is **partially** correct. Give HTML a height of 100% will allow other **immediate children** to use percentages based on viewport height. However, other `
    `s that are not the immediate child of body will not have their height relative to that... it depends on the height of the parent.
    – Terry Nov 14 '14 at 14:26

2 Answers2

2

While the problem is that an element, other than the <html> root element, needs a parent with a specified width (in order to calculate its own relative height), you can avoid the problem using units relative to the viewport, such as vh (1vh is equal to one-percent of the height of the view-port, and so is pretty much a direct drop-in replacement for a %-based height), such as:

body {
    margin: 0px;
    padding:0px;
    height: 100vh;
    background-color: black;
}
#Container {
    width: 98%;
    height: 100%;
    background-color: grey;
}

JS Fiddle demo.

The problem with that approach, of course, is that it restricts the content of the #Container from growing and allowing the <body> to scroll (this may be by-design, of course), but you could instead use min-height to obviate the problem, and allow the elements to grow:

html {
    padding: 0;
    margin: 0;
}
body {
    margin: 0px;
    padding:0px;
    min-height: 100vh;
    background-color: black;
}
#Container {
    width: 98%;
    min-height: 100vh;
    background-color: grey;
}
#expansion {
    height: 3000px;
    width: 2em;
    background-color: #f00;
}

JS Fiddle demo.

(Note that in the above demo I'm using another element to force the #Container to expand, that's purely for demonstration purposes, and is not required by this approach.)

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

Make sure that you are calling your selector correctly. This is case sensitive.

Container Vs container (Small c and Capital C) and also ( # Vs .) Class for dot(.) And ID for Hash (#)

Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49