【发布时间】:2020-08-03 04:36:09
【问题描述】:
我正在学习使用 flexbox,但我无法在 .headercontent 类内垂直对齐内容。它似乎尊重justify-content 等其他所有内容,但忽略了align-content。我搜索并找到this 线程,这似乎表明父级应该明确设置高度。我通过设置flex-basis 和flex-grow 和min-height 来设置高度。但仍然由包含h1 的div 粘在header 的顶部。我希望那个绿色 div 位于header 的垂直中心。我做错了什么?
html {
box-sizing: border-box;
margin: 0;
padding: 0;
font-size: 100%;
line-height: 1.2em;
}
body {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: stretch;
height: 100vh;
background-color: #cdc9c1;
}
header {
background-color: #a99879;
flex-basis: 10%;
flex-grow: 1;
min-height: 20px;
}
main {
background-color: #5b431a;
flex-basis: 80%;
flex-grow: 8;
}
footer {
background-color: #77613c;
flex-basis: 10%;
flex-grow: 1;
}
.headercontent {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
background-color: green;
min-height: 20px;
}
.navstyle {
list-style-type: none;
}
<html>
<head>
</head>
<body>
<header>
<div class="headercontent">
<h1>This is the header</h1>
</div>
</header>
<nav>
<ul>
<li>section1</li>
<li>section2</li>
<li>section3</li>
</ul>
</nav>
<main>
<p> this is the main body</p>
</main>
<footer>
<p> this is the footer </p>
</footer>
</body>
</html>
【问题讨论】: