【问题标题】:No margin / padding bottom on css grid?css网格上没有边距/填充底部?
【发布时间】:2019-05-16 00:28:37
【问题描述】:

出现溢出时,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

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

【问题讨论】:

    标签: html css reactjs margin


    【解决方案1】:

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

    删除此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>

    【讨论】:

    • 很好,它有效。记住这一点,通过我的项目,我似乎仍然遇到同样的问题,尽管父级没有设置为 height: 100% 而是 flex: 1。我认为这又导致了问题。我还需要将网格保持在 100% 的高度,这样孩子们就可以在桌面设备上达到 1fr。你有机会看看吗? codepen.io/anon/pen/XwMLoz
    • 我忘记了我在网格布局容器中设置的高度。删除高度就像为某些列设置 margin-top 一样。
    【解决方案2】:

    你可以设置后做我的伎俩:

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

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2015-12-02
      • 2016-12-29
      • 1970-01-01
      • 2015-11-03
      • 2011-11-24
      相关资源
      最近更新 更多