具有完整浏览器宽度背景颜色的CSS Grid Item?

时间:2018-02-01 12:26:22

标签: css css3 layout grid css-grid

我现在尝试跳转介绍CSS网格!但有一件事对我来说并不清楚。你将如何建立一个主网格/布局,你的标题,英雄和页脚背景颜色可能有完整的浏览器宽度(如stackoverflow页眉/页脚)?没有嵌套div,就像你今天使用flexbox,floats&合。这可能是干净的吗?我希望你明白我努力工作。我添加了一个图像,也许这使得它更清晰;)

enter image description here

好的,这是一个小提琴:



<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Grid Layout Test</title>
</head>

<body>

  <div class="container">
    <div class="topbar">
      <div class="logo">LOGO</div>
      <ul class="menu">
        <li class="menu-item">Menu Item</li>
        <li class="menu-item">Menu Item #2</li>
        <li class="menu-item">Menu Item #3</li>
      </ul>
    </div>

    <div class="hero">
      <p class="hero-text">Hero Banner</p>
    </div>

    <div class="content">
      <h1 class="content-headline">Content</h1>
      <p class="content-text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis
        natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque
        eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget,
        arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.
        Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula,
        porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.
        Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue.
        Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus,
        sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit
        id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis
        ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales
        sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nun</p>
    </div>

    <div class="sidebar">
      <h2 class="sidebar-headline">Sidebar</h2>
    </div>

    <div class="footer">
      <p class="footer-text">Footer</p>
    </div>
  </div>

  <style>
    * {
      box-sizing: border-box
    }

    html,
    body {
      margin: 0;
      padding: 0;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
    }

    .container {
      display: grid;
      margin: 0 auto;
      max-width: 1000px;
      grid-template-areas: "topbar topbar topbar" "hero hero hero" "content content sidebar" "footer footer footer";
    }

    .topbar {
      grid-area: topbar;
      display: grid;
      grid-template-columns: auto 1fr;
      background-color: darkolivegreen;
      color: white;
      padding: 20px 0;
    }

    .topbar ul {
      display: flex;
      list-style: none;
      margin: 0;
      padding: 0;
      justify-content: flex-end;
    }

    .hero {
      grid-area: hero;
      background-color: lightcoral;
      min-height: 300px;
      display: grid;
      place-items: center center;
    }

    .sidebar {
      grid-area: sidebar;
      width: 200px;
      background-color: lightgray;
    }

    .footer {
      grid-area: footer;
      background-color: darkgoldenrod;
    }
  </style>

</body>

</html>
&#13;
&#13;
&#13;

但是现在我希望topbar,hero和footer的背景颜色是完整的浏览器大小,而不需要使用我的区域周围的经典嵌套div。如果我必须这样做,我看不到css网格对整体布局的任何好处。

1 个答案:

答案 0 :(得分:0)

您可以做的是使用容器创建某个HTML布局,该容器会强制某些元素保持在设定的宽度范围内。然后容器外的任何东西都将显示为全宽。

这是一个实时的JSFiddle example

<强> HTML

<div class="wrapper">
    <div class="section">
        <div class="container">
            <!-- Set width content -->
        </div>
    </div>
    <div class="section">
        <div class="container">
            <!-- Set width content -->
        </div>
    </div>
    <div class="section">
        <!-- Full width content -->
    </div>
</div>

<强> CSS

.wrapper {
  width: 100%;
  height: 100%;
}

.container {
  width: 80%;
  margin: 0 auto;
}