CSS网格上没有边距/填充底部?

时间:2019-05-16 00:28:38

标签: html css reactjs margin

出现溢出时,css网格上的边距底部似乎消失了。

无论我以哪种方式处理页面布局(flexbox,css网格,固定),使用css网格时我似乎都松开了边距/填充底部。

问题的示例:

HTML

<div class="header">&nbsp;</div>
<div class="nav">&nbsp;</div>
<div class="article">
       <div class="settings__grid">
        <div class="settings__grid-item">&nbsp;</div>
        <div class="settings__grid-item">&nbsp;</div>
        <div class="settings__grid-item">&nbsp;</div>
        <div class="settings__grid-item">&nbsp;</div>
        <div class="settings__grid-item">&nbsp;</div>
        <div class="settings__grid-item">&nbsp;</div>
      </div>
</div>

CSS

html,
body {
  margin: 0;
  height: 100%;
}
.header {
  position: fixed;
  z-index: 2;
  top: 0;
  left: 0;
  width: 100%;
  height: 20px;
  background: white;
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.24);
}
.nav {
  position: fixed;
  z-index: 1;
  top: 20px;
  left: 0;
  width: 200px;
  height: calc(100% - 20px);
  background: #FAF9F8;
  border-right: 1px solid #d9d9d9;
}

.article {
  background: #F3F3F5;
  position: relative;
  top: 20px;
  left: 200px;
  width: calc(100% - 200px);
  height: calc(100% - 20px);
  padding: 10px;
}

.settings__grid {
  display: grid;
  height: 100%;

  grid-template-columns: repeat(auto-fit, minmax(32rem, 1fr));
  grid-auto-rows: 14rem;
  grid-gap: 3rem;
}

.settings__grid-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  text-align: center;
  background-color: rgba(0, 0, 0, 0.4);
  border-radius: 6px;
}

@media (max-width: 768px) {
  .nav {
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    height: 60px;
  }
  .article {
    height: calc(100% - 80px);
    left: 0;
    width: 100%;
  }
}

https://codepen.io/anon/pen/EzWmeg

我希望页边距/填充与我在父级上定义的一致,但不会,并且消失/折叠

2 个答案:

答案 0 :(得分:0)

问题仅在于在height上设置.article;这会将高度设置为初始视口(不考虑滚动)。

删除此height可解决问题:

html,
body {
  margin: 0;
  height: 100%;
}

.header {
  position: fixed;
  z-index: 2;
  top: 0;
  left: 0;
  width: 100%;
  height: 20px;
  background: white;
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.24);
}

.nav {
  position: fixed;
  z-index: 1;
  top: 20px;
  left: 0;
  width: 200px;
  height: calc(100% - 20px);
  background: #FAF9F8;
  border-right: 1px solid #d9d9d9;
}

.article {
  background: #F3F3F5;
  position: relative;
  top: 20px;
  left: 200px;
  width: calc(100% - 200px);
  /*height: calc(100% - 20px);*/
  padding: 10px;
}

.settings__grid {
  display: grid;
  height: 100%;
  grid-template-columns: repeat(auto-fit, minmax(32rem, 1fr));
  grid-auto-rows: 14rem;
  grid-gap: 3rem;
}

.settings__grid-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  text-align: center;
  background-color: rgba(0, 0, 0, 0.4);
  border-radius: 6px;
}

@media (max-width: 768px) {
  .nav {
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    height: 60px;
  }
  .article {
    /*height: calc(100% - 80px);
    left: 0;
    width: 100%;
  }
}
<div class="header">&nbsp;</div>
<div class="nav">&nbsp;</div>
<div class="article">
  <div class="settings__grid">
    <div class="settings__grid-item">&nbsp;</div>
    <div class="settings__grid-item">&nbsp;</div>
    <div class="settings__grid-item">&nbsp;</div>
    <div class="settings__grid-item">&nbsp;</div>
    <div class="settings__grid-item">&nbsp;</div>
    <div class="settings__grid-item">&nbsp;</div>
  </div>
</div>

答案 1 :(得分:0)

你可以设置 after 来做我的伎俩:

&::after {
  content: '';
  position: relative;
  height: 16rem;
  width: 100%;
}