html - css:使内部容器高度为 100%,并带有顶部和底部边距

标签 html css

<分区>


关闭 7 年前

我有一个奇怪的案例:

我尝试制作一个内部容器来填充父容器(高度为 100%),但结果我在底部得到了溢出的内容:

enter image description here

但一定是这样(100% margin top and bottom除外):

enter image description here

代码:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <div class="main-wrapper">
      <aside class="full-nav" action-bar>
        </aside>
      <section class="wrapper">
            <header>
            </header>
        <div class="content">
          <div class="innner-wrapper">
            <div class="main-partial">
                <div class="content-wrapper">Content</div>
            </div>
          </div>
        </div>
      </section>
    </div>
  </body>

</html>

和 plunker(CSSit 在那里):

http://plnkr.co/edit/ku7ZXK6uezfZ86cMFhds?p=preview

(当我设置绝对位置时,我得到奇怪的宽度...)

最佳答案

您可以计算.content-wrapper 的高度并进行调整。

.content-wrapper {
   height: calc(100% - 70px);
}

输出:

Adjusted Wrapper

/* Styles go here */

html,
body {
  height: 100%;
  min-height: 100%;
}
body {
  min-width: 1024px;
  font-family: 'Open Sans', sans-serif;
  margin: 0;
  padding: 0;
  border: 0;
}
.pull-right {
  float: left;
}
.pull-left {
  float: left;
}
.main-wrapper {
  width: 100%;
  height: 100%;
}
aside {
  width: 48px;
  height: 100%;
  float: left;
  background: #dedede;
  position: absolute;
}
aside.full-nav {
  width: 168px;
}
section.wrapper {
  width: 100%;
  height: 100%;
  float: left;
  padding-left: 168px;
  background: #eeeeee;
}
section.wrapper.full-size {
  padding-left: 48px;
}
aside ul.full-width {
  width: 100%;
  list-style-type: none;
}
aside nav ul li {
  height: 34px;
}
aside nav ul li:hover {
  opacity: 0.9;
}
aside.full-nav nav ul li.company-name {
  width: 100%;
  height: 80px;
  background: #717cba;
  position: relative;
}
aside.full-nav nav ul li.company-name span {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
aside nav ul li a {
  height: 34px;
  line-height: 1;
  max-height: 34px;
  font-size: 14px;
  font-weight: 400;
  color: #ffffff;
  margin: 5px 0 0 12px;
  text-decoration: none;
  display: block;
}
aside.full-nav nav ul li a.first {
  margin: 20px 0 0 12px;
  text-decoration: none;
}
aside nav ul li a:hover {
  color: #ffffff;
}
aside nav ul li.company-name .nav-company-overflow {
  display: none;
}
aside nav ul li.company-name .nav-company-logo {
  display: none;
}
aside.full-nav nav ul li.company-name a {
  height: 16px;
  color: #ffffff;
  margin: 10px 0 0 12px;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
  position: absolute;
  z-index: 20;
}
aside.full-nav nav ul li.company-name .nav-company-overflow {
  width: 168px;
  height: 80px;
  position: absolute;
  top: 0;
  background: rgba(78, 91, 169, 0.8);
  z-index: 15;
  display: block;
}
aside.full-nav nav ul li.company-name .nav-company-logo {
  width: 168px;
  height: 80px;
  position: absolute;
  top: 0;
  z-index: 10;
  display: block;
}
aside nav ul li a em {
  line-height: 100%;
  display: inline-block;
  vertical-align: middle;
  margin: 0 18px 0 0;
}
aside nav ul li a span {
  width: 110px;
  display: inline-block;
  line-height: 100%;
  vertical-align: middle;
  max-width: 110px;
}
aside nav ul li a.profile em {
  width: 18px;
  height: 18px;
  background: url(../images/png/profile_spritesheet.png);
  background-position: -10px -676px;
  margin: 6px 8px 0 0;
}
aside.full-nav nav ul li a.profile em {
  margin: 0 8px 0 0;
}
aside nav ul li a.contacts em {
  width: 20px;
  height: 20px;
  background: url(../images/png/profile_spritesheet.png);
  background-position: -10px -224px;
}
aside nav ul li a.events em {
  width: 20px;
  height: 22px;
  background: url(../images/png/profile_spritesheet.png);
  background-position: -10px -268px;
}
aside nav ul li a.policy em {
  width: 20px;
  height: 23px;
  background: url(../images/png/profile_spritesheet.png);
  background-position: -10px -310px;
}
aside nav ul li a.admins em {
  width: 18px;
  height: 18px;
  background: url(../images/png/profile_spritesheet.png);
  background-position: -10px -676px;
}
aside.full-nav nav ul li a span {
  display: inline-block;
}
aside nav ul li a span {
  display: none;
}
aside .hide-sidebar {
  width: 100%;
  height: 34px;
  background: #455095;
  position: absolute;
  bottom: 0;
}
#hide-sidebar-btn {
  width: 30px;
  height: 34px;
  line-height: 40px;
  background: #394485;
  float: right;
  text-align: center;
}
#hide-sidebar-btn:hover {
  cursor: pointer;
}
aside .collapse-btn-icon {
  width: 8px;
  height: 15px;
  display: inline-block;
  background: url(../images/png/profile_spritesheet.png);
  background-position: -10px -353px;
  -webkit-transform: rotate(180deg);
  transform: rotate(180deg);
}
aside.full-nav .collapse-btn-icon {
  -webkit-transform: rotate(360deg);
  transform: rotate(360deg);
}
.innner-wrapper {
  height: 100%;
  margin: 0 12px 0 12px;
}
.main-partial {
  height: 100%;
}
header {
  height: 40px;
  background: #ffffff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
header .buttons-header-area {
  float: right;
}
header .company-header-avatar {
  width: 28px;
  height: 28px;
  -webkit-border-radius: 28px;
  -webkit-background-clip: padding-box;
  -moz-border-radius: 28px;
  -moz-background-clip: padding;
  border-radius: 28px;
  background-clip: padding-box;
  margin: 7px 0 0 5px;
  float: left;
}
header .info {
  height: 40px;
  line-height: 40px;
  margin: 0 5px 0 0;
}
header .info em {
  width: 28px;
  height: 28px;
  display: inline-block;
  vertical-align: middle;
  background: url(../images/png/profile_spritesheet.png);
  background-position: -10px -53px;
}
header .dropdown-toggle {
  width: 170px;
  height: 40px;
  border: none;
  padding: 0;
  background: transparent;
  font-size: 12px;
  color: #444444;
}
header .btn-group {
  height: 40px;
  vertical-align: top;
}
header .btn-group.open {
  background: #eeeeee;
}
header .open > .dropdown-menu {
  width: 170px;
  font-size: 12px;
  border-color: #d9d9d9;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.11);
  margin: 2px 0 0 0;
}
header .dropdown-toggle:hover {
  background: #eeeeee;
}
header .profile-name {
  width: 100px;
  height: 40px;
  line-height: 40px;
  display: inline-block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
header .caret {
  height: 40px;
  border-top: 6px solid #bfbfbf;
  border-right: 6px solid transparent;
  border-left: 6px solid transparent;
}
.content {
  height: 100%;
  margin: 12px 0;
  overflow: hidden;
}
.content-wrapper {
  background: #ffffff none repeat scroll 0 0;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  height: calc(100% - 70px);
  width: 100%;
}
.content-row.company {
  height: 300px;
}
.content-row-wrapper {
  margin: 0 18px;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
  <div class="main-wrapper">
    <aside class="full-nav" action-bar>
    </aside>
    <section class="wrapper">
      <header>
      </header>
      <div class="content">
        <div class="innner-wrapper">
          <div class="main-partial">
            <div class="content-wrapper">Content</div>
          </div>
        </div>
      </div>
    </section>
  </div>
</body>

</html>

关于html - css:使内部容器高度为 100%,并带有顶部和底部边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233811/

上一篇:html - Bootstrap 3 : Can 'row' and 'col-xx-xx' classes be assigned to non-block leven elements?

下一篇:javascript - 如何从文本形式获取html脚本并在新窗口中执行?

相关文章:

jquery - 如何删除/添加事件类到 Jquery 中的元素?

javascript - 来自不同位置的过渡元素

html - 我在 ul、li 元素中嵌套 CSS/HTML 复选框时遇到问题

html - 元素之间的意外间隙

html - 如何 float 一个div垂直居中对齐另一个div

html - 在网格中创建填充并且高度仍然相等

html - 使用图像作为菜单 : animation inside div

javascript - 选择图像区域并触发JS事件

html - 将 "Widgets"样式设置为内联并在较小的屏幕下彼此下方

css - IE8 是否有像 6 或 7 这样的渲染错误?