当您的sub_header 的高度被添加到content 的高度时,会发生什么情况,因此它们加起来超过其父级的 100%。
一种解决方案是给sub_header 一个固定的高度并从content 中减去它,所以如果你用这个更新这两条规则
.content {
height: calc(100% - 70px);
}
.sub_header {
height: 70px;
}
样本
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
/* general */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
}
html, body {
height: 100%;
font-family: 'Open Sans', sans-serif;
}
#header {
width: 100%;
height: 75px;
background-color: #34495e;
margin: 0 auto;
}
.logo {
float: left;
margin-top: 5px;
margin-left: 15px;
padding-top: 6px;
color: #ffffff;
}
.logo a {
font-size: 1.9em;
color: #ffffff;
}
#container {
width: 100%;
height: 100%;
margin: 0 auto;
}
.sidebar {
width: 220px;
height: 100%;
background-color: #262626;
float: left;
padding-left: 0px;
padding-top: 0px;
color: #ffffff;
}
ul {
}
#sidebar li {
list-style: none;
}
#sidebar li a {
color: #cccccc; /* grey */
display: block; /* default for most elements, but overrides in this case */
padding: 15px;
font-size: 1em;
border-bottom: 1px solid #000000;
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
transition: 0.4s; /* however effects delay */
}
#sidebar li a:hover {
color: #ffffff;
background-color: #030303;
padding-left: 25px; /* shifts sidebar elements on hover */
}
.content {
margin-left: 220px; /* same as side bar to bring content to the right place */
height: calc(100% - 70px);
backgrouond-color: #030303;
/* then add padding */
padding: 15px;
/* padding-top: 15px; */
font-size: 1.1em;
background-color: #bdc3c7;
}
/* sub pages */
.sub_header {
font-size: 1.7em;
margin-left: 220px;
padding: 15px;
background-color: #7f8c8d;
height: 70px;
}
#contact_info {
margin-left: 220px;
font-size: 1em;
}
<div id="header">
<div class="logo"> <!-- Logo of Website -->
<a href="#">Everyshine Impex Limited<span> <!-- Source --></span></a> <!-- Span for endline element -->
</div>
</div>
<div id="container">
<!-- 245px for menu bar -->
<div class="sidebar">
<ul id="sidebar">
<li><a class="selected" href="index.html">Home</a></li>
<li><a href="about us.html">About Us</a></li>
<li><a href="catalog items.html">Catalog Items</a></li>
<li><a href="contact.html">Contact Information</a></li>
</ul>
</div>
<div class="sub_header">
Sample Catalog Items
</div>
<div class="content">
Catalog Items
</div>
</div>
上面的缺点是在较小的屏幕上,sub_header 文本可能不适合并且看起来很奇怪。为了解决这个问题,您可以使用媒体查询,但更聪明的方法是使用flexbox,因此在sub_header/content 周围的标记中添加wrapper,如下所示,
<div class="wrapper">
<div class="sub_header">
Sample Catalog Items
</div>
<div class="content">
Catalog Items
</div>
</div>
并像这样添加/更新这些规则
.wrapper {
margin-left: 220px; /* same as side bar to bring content to the right place */
height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
background-color: #030303;
padding: 15px;
font-size: 1.1em;
background-color: #bdc3c7;
}
/* sub pages */
.sub_header {
font-size: 1.7em;
padding: 15px;
background-color: #7f8c8d;
}
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
/* general */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
}
html, body {
height: 100%;
font-family: 'Open Sans', sans-serif;
}
#header {
width: 100%;
height: 75px;
background-color: #34495e;
margin: 0 auto;
}
.logo {
float: left;
margin-top: 5px;
margin-left: 15px;
padding-top: 6px;
color: #ffffff;
}
.logo a {
font-size: 1.9em;
color: #ffffff;
}
#container {
width: 100%;
height: 100%;
margin: 0 auto;
}
.sidebar {
width: 220px;
height: 100%;
background-color: #262626;
float: left;
padding-left: 0px;
padding-top: 0px;
color: #ffffff;
}
ul {
}
#sidebar li {
list-style: none;
}
#sidebar li a {
color: #cccccc; /* grey */
display: block; /* default for most elements, but overrides in this case */
padding: 15px;
font-size: 1em;
border-bottom: 1px solid #000000;
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
transition: 0.4s; /* however effects delay */
}
#sidebar li a:hover {
color: #ffffff;
background-color: #030303;
padding-left: 25px; /* shifts sidebar elements on hover */
}
.wrapper {
margin-left: 220px; /* same as side bar to bring content to the right place */
height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
background-color: #030303;
padding: 15px;
font-size: 1.1em;
background-color: #bdc3c7;
}
/* sub pages */
.sub_header {
font-size: 1.7em;
padding: 15px;
background-color: #7f8c8d;
}
#contact_info {
margin-left: 220px;
font-size: 1em;
}
<div id="header">
<div class="logo"> <!-- Logo of Website -->
<a href="#">Everyshine Impex Limited<span> <!-- Source --></span></a> <!-- Span for endline element -->
</div>
</div>
<div id="container">
<!-- 245px for menu bar -->
<div class="sidebar">
<ul id="sidebar">
<li><a class="selected" href="index.html">Home</a></li>
<li><a href="about us.html">About Us</a></li>
<li><a href="catalog items.html">Catalog Items</a></li>
<li><a href="contact.html">Contact Information</a></li>
</ul>
</div>
<div class="wrapper">
<div class="sub_header">
Sample Catalog Items
</div>
<div class="content">
Catalog Items
</div>
</div>
</div>
注意,对于基于元素的百分比高度,所有父级,一直到html/body 都需要一个高度,在你的情况下,这个更新就足够了
html, body {
height: 100%;
font-family: 'Open Sans', sans-serif;
}
另一个观察结果是您在logo 和sidebar 上使用float。使用浮点数时,有时需要清除它们,否则它们可能会在以后/如果您向其父级添加更多元素时引起问题。
如果发生这种情况,您可以根据现有标记清除它们
#header::after,
#container::after {
content: '';
display: block;
clear: both;
}