To achieve the desired outcome of having the <body> element occupy the full height of the screen, follow these guidelines:
If your content is not expected to exceed the height of the screen or if you intend to have a scrollable inner element, use
body {
height: 100vh;
}
However, if you anticipate that the content will exceed the screen height and require scrolling, then set
body {
min-height: 100vh;
}
This basic setup accomplishes the goal but can be further enhanced for better presentation.
One important step is to remove the margin applied to the <body> element.
body {
margin: 0;
}
There are two primary reasons for eliminating the margin:
- The <body> reaches the window's edge.
- The <body> does not have an initial scrollbar.
P.S. For a radial gradient background centered on the screen rather than in the bottom right corner as shown in the example, consider using this CSS snippet:
body {
min-height: 100vh;
margin: 0;
background: radial-gradient(circle, rgba(255,255,255,1) 0%, rgba(0,0,0,1) 100%);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=">
<title>test</title>
</head>
<body>
</body>
</html>