I have following html page (no external requirement)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*,
*::before,
*::after {
box-sizing: border-box;
}
html, body{margin:0;padding:0; }
html{ height:100%; background-color:#99cc33 }
body{ min-height:100%; background-color:#6633cc }
article{ margin: 30px 0; background-color:#3366cc }
.debug-border{ border:1px dashed #ff00ff !important; }
</style>
</head>
<body class="debug-border">
<article>
ARTICLE<br>
</article>
</body>
</html>
As you can see in above code, body occupies 100% of the height and everything is as expected. However as soon as I remove the debug-border
class from body, it shows vertical scroll. The only difference between above and below code is absence of debug-border
class on body of code below
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*,
*::before,
*::after {
box-sizing: border-box;
}
html, body{margin:0;padding:0; }
html{ height:100%; background-color:#99cc33 }
body{ min-height:100%; background-color:#6633cc }
article{ margin: 30px 0; background-color:#3366cc }
.debug-border{ border:1px dashed #ff00ff !important; }
</style>
</head>
<body>
<article>
ARTICLE<br>
</article>
</body>
</html>
Question:
Adding a transparent border-top
to body fixes my issue but I want to know why is this happening. Can someone help me explain this weird behavior?
Notes that:
- This question is not duplicate of Why does this CSS margin-top style not work? since issue is caused by removing border of body element which actually doesn't have a height initially and takes height of the content.
- Size of scroll area is exactly size of the margin on article tag.
- Tried this on Firefox 80, 84, 88, 90, 109(latest) and chrome 109.0.5414.119 (latest) and chromium (latest) and all of them show the same symptom.
- setting height of
body
to100vh
and removing 100% fromhtml
tag doesn't make any difference. article
tag doesn't occupy 100% of the height either and with margin it is still shorter thanbody
height.- Only
border-top
causes this behavior so instead of settingborder
onbody
,bodrder-top
can be used. box-sizing
is there since in original issue I was usingbootstrap 5