I put min-height: 100vh;
on the body, then height: 100%
on the main. For some reason, the main height does not stretch full to the parent.
Can somebody explain? Thanks.
I also tried adding other, but anything does not seem to work under the
body {
border: 2px solid red;
min-height: 100vh;
}
main {
border: 2px solid green;
height: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<main>hihihihasidfiojdi</main>
</body>
</html>
3
Answers
The issue arises because setting
height: 100%;
on an element only works if the parent element also has a defined height. In your case, the main hasheight: 100%;
, but its parent, body, has amin-height
set to100vh
, not a fixed height. This means that the height of body is not explicitly defined, and thus main cannot inherit100%
of it.You can use
flexbox
on the body to ensure that main stretches to fill the remaining space.Change
min-height: 100vh;
toheight: 100vh;
in the body selector. If you specify a height in percent, there has to be an explicit height in the parent element (min-height will not set the height explicitly).Add main height as 100vh