I'm taking a course where there is a navbar at top in fixed position. The background is set to a height of 100vh and a div with text content is centered on the background. using flexbox it's almost centered but in order to actually get it to center of the background the height of this div has to be set to height of 100%. I have a grasp on flexbox and viewport height however I'm foggy on why setting the height of the div to 100% is necessary to actually get the dive centered. I think you could probably put any image as background here to replicate what i'm asking. Hopefully I made sense here.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background: #fff;
color: #333;
line-height: 1.5;
}
ul {
list-style: none;
}
a {
color: #333;
text-decoration: none;
}
h1,
h2 {
font-weight: 300px;
line-height: 1.5:
}
p {
margin: 10px 0;
}
img {
width: 100%;
/*makes image width of the container */
}
/* navbar */
.navbar {
display: flex;
align-items: center;
justify-content: space-between;
background-color: #333;
color: #fff;
width: 100%;
height: 70px;
/* apparently a common height for navbar */
position: fixed;
top: 0px;
padding: 0 30px;
}
.navbar a {
color: #fff;
padding: 10px 20px;
margin: 0 5px;
}
.navbar a:hover {
border-bottom: 1px solid #28a745;
}
.navbar ul {
display: flex;
}
/* Header */
.hero {
background: url("/img/home/showcase.jpg") no-repeat center center/cover;
height: 100vh;
position: relative;
color: #fff;
}
.hero .content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
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>Welcome to EdgeLedger</title>
<link rel="stylesheet" href="/css/utilities.css">
<!-- style.css is last in case utilities.css needs to be overwritten -->
<link rel="stylesheet" href="/css/style.css">
<script src="https://kit.fontawesome.com/6eab1538de.js" crossorigin="anonymous"></script>
</head>
<body id="home">
<header class="hero">
<div id="navbar" class="navbar">
<h1 class="logo">
<span class="text-primary"><i class="fas fa-book-open"></i>Edge</span>Ledger
</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#cases">Cases</a></li>
<li><a href="#blog">Blog</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</div>
<div class="content">
<h1>The sky is the limit</h1>
<p>We provide world class financial assistance</p>
<a href="#about" class="btn">Read More</a>
</div>
</header>
</body>
</html>
If I understand correctly, you are saying that the content is centered horizontally by default, but that the div needs a height: 100%
to center the content vertically.
Divs are block elements, which means that, by default:
auto
).If your div is a flexbox with the content centered, even if the content is centered vertically, the div will still only expand downwards as far as it needs in order to fit the tallest element inside of it. Since the div is still at the top of the screen, even if its content is centered vertically inside the div, the content will appear at the top of the screen because the div is only as tall as the content and because the div is at the top of the screen.
However, the height: auto
default property of divs can be overridden. If you set the height to 100%
, you force the div to be 100% of the height of its parent element, the page. The div would then have a bunch of extra space for the content, and due to the flex rule, it would position the content in the vertical center of that extra space.
To understand this further, you can try adding border: 5px dashed black
to the div so you can see its size and position, and then using different values for height, like unset
, 100%
, 50%
, etc. Experiment and play around!