简体   繁体   English

样式不会通过CSS网格中的装订线显示

[英]body styles not showing through gutters in CSS Grid

I have a layout I've created using CSS Grid. 我有一个使用CSS Grid创建的布局。 My body element has a different background color than the actual sections of the grid. 我的body元素的背景颜色与网格的实际部分不同。 I would like this color to show through the "gutters" I have created in my grid using margin . 我希望这种颜色能通过使用margin在网格中创建的“装订线”显示出来。 I've linked a fiddle of this problem: 我已经链接了这个问题的小提琴:

body {
  background: #f00;
  margin: 0;
}

#page {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-areas: "header header" "navigation content" "footer footer";
  grid-template-columns: 200px auto;
  grid-template-rows: 15% 75% 10%;
  background: #fff;
}

#page>* {
  border: 1px solid #000;
  padding: 10px;
  margin: 5px;
}

#page>header {
  grid-area: header;
}

#page>nav {
  grid-area: navigation;
}

#page>main {
  grid-area: content;
}

#page>footer {
  grid-area: footer;
}
<section id="page">
  <header>
    <h1>Header</h1>
  </header>
  <nav>
    <h1>Navigation</h1>
  </nav>
  <main>
    <h1>Content</h1>
  </main>
  <footer>
    <h1>Footer</h1>
  </footer>
</section>

Fiddle 小提琴

You could also achieve this using the Outline property: 您还可以使用Outline属性实现此目的:

#page > * {
    border: 1px solid #000;
    padding: 10px;
    outline: 5px solid #f00;
    margin: 5px;
}

You're making the entire background of the #page section white. 您正在将#page部分的整个背景#page白色。 To have the red show, the white needs to apply only to the interior of the children of #page . 要显示红色,白色只需要应用于#page的内部。 Move the background style from #page to #page > * , like so: background样式从#page移到#page > * ,如下所示:

#page {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-areas: "header header"
                       "navigation content"
                       "footer footer";
  grid-template-columns: 200px auto;
  grid-template-rows: 15% 75% 10%;
}

#page > * {
  border: 1px solid #000;
  padding: 10px;
  margin: 5px;
  background: #fff;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM