【问题标题】:How do I remove the white bar under the content on 1440p monitors?如何去除 1440p 显示器内容下方的白条?
【发布时间】:2019-02-01 14:50:35
【问题描述】:

我正在探索 CSS Grid,但我遇到了麻烦,因为尺寸没有使用全高,而且我的 HTML 和正文是 [宽度,高度 100%]。我有一个大的白色部分,如何使它与顶部的任何部分对齐? 任何建议将不胜感激。 这是我的 CSS。

*,** {
    margin: 0 !important;
    padding: 0 !important;
    box-sizing: border-box;
}
html{
    margin: 0;
    padding:0;
    width: 100%;
    height: auto;
}
.grid {
margin: 0;
display: grid;
grid-template-columns: 100%;
grid-template-rows: repeat(auto, 4);
/* grid-template-rows: 13% auto 20%; */
grid-template-areas:
"header" 
"main";

@media screen and (min-width: 500px) {
    grid-template-columns: auto;
    grid-template-rows:   repeat(auto, 3);

    grid-template-areas: 
    "header header"
    "main main"
    "main main";
}

@media screen and (min-width: 800px) {
    grid-template-columns: 30% auto;
    grid-template-rows:   repeat(auto, 3);

    grid-template-areas: 
    "header header"
    "sidebar main"
    "sidebar2 main";
}

一个问题是,如果我弄乱了位置,它会让事情向左飞......

【问题讨论】:

  • 如果你将 body height: body{height:100vh} 和 container 设置为 100vh 或 100% 会怎样?
  • @JakubMuda 如果我做 body 100vh,标题和 main 之间有空格

标签: css html sass grid


【解决方案1】:

您可以使用 vh 单位、计算和最小高度,例如:

header { grid-area: header; height: 80px; }
nav    { grid-area: sidebar;  }
main   { grid-area: main; min-height: calc(100vh - 80px); } /* subtract header height */
aside  { grid-area: sidebar2; }

这是一个示例(我已经稍微更改了大小以在 stackoverflow 上工作):

*, ** {
  margin: 0 !important;
  padding: 0 !important;
  box-sizing: border-box;
}

html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
}

.grid {
  margin: 0;
  display: grid;
  min-height: 100vh;
  grid-template-columns: 100%;
  grid-template-rows: repeat(auto, 4);
  /* grid-template-rows: 13% auto 20%; */
  grid-template-areas: "header"  "main";
}
@media screen and (min-width: 200px) {
  .grid {
    grid-template-columns: auto;
    grid-template-rows: repeat(auto, 3);
    grid-template-areas: "header header" "main main" "main main";
  }
}
@media screen and (min-width: 400px) {
  .grid {
    grid-template-columns: 30% auto;
    grid-template-rows: repeat(auto, 3);
    grid-template-areas: "header header" "sidebar main" "sidebar2 main";
  }
}

header {
  background: tomato;
  grid-area: header;
  height: 40px;
}

nav {
  background: gold;
  grid-area: sidebar;
}

main {
  background: olive;
  grid-area: main;
  min-height: calc(100vh - 40px);
}

aside {
  background: orange;
  grid-area: sidebar2;
}
<div class="grid">
  <header>Header</header>
  <nav>Sidebar</nav>
  <main>Main</main>
  <aside>Sidebar 2</aside>
</div>

【讨论】:

    猜你喜欢
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2015-03-18
    • 2017-08-05
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多