1

我试图了解 HTML 结构如何影响CSS 网格布局模块规范。我正在使用启用了实验功能的 Chrome Canary。

附加代码按预期工作,但将导航和搜索放在标题标签内只是为了语义会抛出 CSS 网格放置。

我缺少什么能够使用 CSS 网格模块并仍然将我的语义标头与导航、品牌和搜索包装在我的标头标签中?

html {
  box-sizing: border-box;
  min-height: 2000px;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
.wrapper {
  width: 100%;
  display: grid;
  grid-template-columns: 20% auto 20%;
  grid-template-rows: 50px auto 50px;
  background-color: #fff;
  color: #444;
}
.mainheader {
  grid-column: 1 / span 3;
  grid-row: 1;
  background: #39444C;
}
.brand {
  height: 100%;
  margin: auto;
  color: #FFFFFF;
  background: #2E3840;
}
.search {
  grid-column: 3;
  grid-row: 1;
}
.navbar {
  grid-column: 2;
  grid-row: 1;
  background: #39444C;
}
ul {
  padding: 12px;
  margin: 0;
}
.navbar li {
  display: inline;
  padding: 10px;
}
.navbar li a {
  color: #9CA19A;
  text-decoration: none;
  font-size: 1.1rem;
}
.navbar a:hover {
  color: #00A5D5;
}
.sidenav {
  grid-column: 1;
  grid-row: 2;
  background: #F2F2F2;
}
.content {
  grid-column: 2;
  grid-row: 2;
  background: yellow;
}
.mainfooter {
  background: gray;
  grid-column: 1 / span 2;
  grid-row: 3;
}
.mainheader h1 {
  font-size: 2.8vw;
}
<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="styles.css" />
</head>

<body>
  <div class="wrapper">

    <header class="mainheader">
      <h1 class="brand">Simply Expense</h1>
    </header>
    <nav class="navbar">
      <ul>
        <li><a href="#">Expenses</a>
        </li>
        <li><a href="#">Receipts</a>
        </li>
        <li><a href="#">Reports</a>
        </li>
      </ul>
    </nav>
    <div class="search">
      <button>test</button>
      <input type="text">
    </div>
    <aside class="sidenav">
      Side nav
    </aside>

    <div class="content">
      <button>test</button>
      <button>test</button>
      <button>test</button>
    </div>

    <footer class="mainfooter">
      Footer
    </footer>
  </div>

</body>

</html>

4

1 回答 1

1

想通了,如果您将品牌、导航和搜索放在标题中,它们不再是网格的直接后代,因此要正确放置它们,您需要在subgrid关键字可用时创建另一个网格/子网格。现在,您可以只使用具有相同尺寸的 display:grid

鉴于此 HTML:

<div class="wrapper">
<div class="header">
    <h1 class="logo">Simply Expense</h1>
    <nav class="navbar">
        <ul>
            <li><a href="#">Expenses</a></li>
            <li><a href="#">Receipts</a></li>
            <li><a href="#">Reports</a></li>
        </ul>
    </nav>
    <div class="search">
        <button>test</button>
        <input type="text">
    </div>
</div>
</div>

CSS 将是:

.wrapper {
    display: grid;
    grid-template-columns: 20% 60% 20%;
    grid-template-rows: 50px auto 50px;
} 
.header {
    grid-column: 1 /span 3;
    grid-row: 1 ;
    display: grid;
    grid-template-columns: 20% 60% 20%;
    grid-template-rows: 50px;
}
.logo {
    grid-column: 1;
    grid-row: 1;
    margin: 0;
    color: #FFFFFF;
    background: #2E3840;
}
.search{
    grid-column: 3;
    grid-row: 1;
    background: #2E3840;
}

演示:

html {
  box-sizing: border-box;
  min-height: 2000px;
}

*, *:before, *:after {
  box-sizing: inherit;
}
body{
  margin: 10px;
  padding: 0;
}
.header{
  grid-column: 1 /span 3;
  grid-row: 1 ;
  display: grid;
  grid-template-columns: 20% 60% 20%;
  grid-template-rows: 50px;
}
.wrapper {
  display: grid;
  grid-template-columns: 20% 60% 20%;
  grid-template-rows: 50px auto 50px;
}


.logo {
  grid-column: 1;
  grid-row: 1 ;
  margin: 0;
  color: #FFFFFF;
  background: #2E3840;
}
.search{
  grid-column: 3;
  grid-row: 1;
  background: #2E3840;
}
.navbar {
  grid-column: 2;
  grid-row: 1;
  background: #39444C;
}

.sidenav {
  grid-column: 1;
  grid-row: 2;
  background: #F2F2F2;
}

.content {
  grid-column: 2/span 2;
  grid-row: 2;
  background: yellow;
}

.mainfooter {
  background: gray;
  grid-column: 1 / span 3 ;
  grid-row: 3;
}

.mainheader h1 {
  font-size: 2.8vw;
}

ul {
  padding: 12px;
  margin: 0; }

.navbar li {
  display: inline;
  padding: 10px; }

.navbar li a {
  color: #9CA19A;
  text-decoration: none;
  font-size: 1.1rem; }

.navbar a:hover {
  color: #00A5D5; }
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="styles.css"/>
</head>
<body>
<div class="wrapper">
    <div class="header">
        <h1 class="logo">Simply Expense</h1>
        <nav class="navbar">
            <ul>
                <li><a href="#">Expenses</a></li>
                <li><a href="#">Receipts</a></li>
                <li><a href="#">Reports</a></li>
            </ul>
        </nav>
        <div class="search">
            <button>test</button>
            <input type="text">
        </div>
    </div>
</div>

</body>
</html>

于 2015-06-01T00:56:34.040 回答