【问题标题】:CSS grid-template-columns not behaving the way I it toCSS grid-template-columns 的行为方式与我不同
【发布时间】:2018-02-06 04:56:53
【问题描述】:

我是 CSS 网格的新手,这是我的第一次尝试。

在下面的代码中有一个带有网格包装器类的 div 容器。它包含两个子 div,类名为 grid-item。在 CSS 中,它们被称为 grid-item:nth-child(1) 和 grid-item:nth-child(2)。在 grid-wrapper 中,grid-template-areas 正确放置了 grid-item:nth-child(1) 和 grid-item:nth-child(2)。

grid-item:nth-child(2) 有自己的子项,但我无法让它们正确排列。

我确实有一个 FSFiddle 项目。

有人可以启发我吗?提前致谢。

body {
    font-family: "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif;
}

.grid-wrapper {
    display: grid;
    grid-template-columns: repeat(8, {1fr});
    grid-template-areas:
      ". header header header header header header ."
      ". tabs tabs tabs tabs tabs tabs .";
}

.grid-item:nth-child(1) {
  grid-area: header;
  background-color: #003566;
  padding-top: 30px;
  padding-bottom: 30px;
  text-align: center;
  border-bottom: 2px solid #ff6a00;
}

.grid-item:nth-child(2) {
  grid-area: tabs;
  background-color: #b3cccc;
  padding-top: 10px;
  padding-bottom: 10px;
  /*grid-template-areas: "tabs tabs";
  grid-template-columns: 1fr 1fr;*/
}

.logo {
    grid-area: logo;
    font-size: 1.3em;
    color:#fff;
}

.logo span {
    font-weight: 100;
    color: #ff6a00;
}

.logo i {
    font-size: 5px;
    vertical-align: middle;
    color: #fff;
}

.grid-item:nth-child(2)>div {
  /*grid-area: tabs;*/
  color: #003566;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<html>
<head>
  <meta name="viewport" content="width=device-width" />
  <title>SurEyeDentity Host Services Manager</title>
</head>
<body>
  <div class="grid-wrapper">
    <div class="grid-item">
      <div class="logo">
        SurEyeDentity<span> Host<i class="fa fa-circle" aria-hidden="true"></i>Services<i class="fa fa-circle" aria-hidden="true"></i>Manager</span>
      </div>
    </div>
    <div class="grid-item">
      <div>
        <i class="fa fa-camera"></i> Cameras
      </div>
      <div>
        <i class="fa fa-user" aria-hidden="true"></i> Users
      </div>
    </div>
  </div>
</body>
</html>

【问题讨论】:

    标签: css html


    【解决方案1】:

    在内部网格项目中使用display:grid...并使用grid-auto-flow: column 将它们按列对齐...使用justify-content: center 将项目水平居中对齐..

    您还为 grid-template-columns. 使用了错误的值。我看到您不需要这个,所以删除它

    堆栈片段

    body {
      font-family: "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif;
    }
    
    .grid-wrapper {
      display: grid;
      grid-template-columns: repeat(8, {
        1fr
      }
      );
      grid-template-areas : ". header header header header header header ." ". tabs tabs tabs tabs tabs tabs .";
    }
    
    .grid-item:nth-child(1) {
      grid-area: header;
      background-color: #003566;
      padding-top: 30px;
      padding-bottom: 30px;
      text-align: center;
      border-bottom: 2px solid #ff6a00;
    }
    
    .grid-item:nth-child(2) {
      grid-area: tabs;
      background-color: #b3cccc;
      padding-top: 10px;
      padding-bottom: 10px;
      display: grid;
      justify-content: center;
      grid-auto-flow: column;
    }
    
    .logo {
      grid-area: logo;
      font-size: 1.3em;
      color: #fff;
    }
    
    .logo span {
      font-weight: 100;
      color: #ff6a00;
    }
    
    .logo i {
      font-size: 5px;
      vertical-align: middle;
      color: #fff;
    }
    
    .grid-item:nth-child(2)>div {
      color: #003566;
      margin: 0 10px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
    <html>
    
    <head>
      <meta name="viewport" content="width=device-width" />
      <title>SurEyeDentity Host Services Manager</title>
    </head>
    
    <body>
      <div class="grid-wrapper">
        <div class="grid-item">
          <div class="logo">
            SurEyeDentity<span> Host<i class="fa fa-circle" aria-hidden="true"></i>Services<i class="fa fa-circle" aria-hidden="true"></i>Manager</span>
          </div>
        </div>
        <div class="grid-item">
          <div>
            <i class="fa fa-camera"></i> Cameras
          </div>
          <div>
            <i class="fa fa-user" aria-hidden="true"></i> Users
          </div>
        </div>
      </div>
    </body>
    
    </html>

    【讨论】:

    • 行得通!非常感谢!我会研究它并获得完整的理解。
    【解决方案2】:

    将 .grid-item:nth-child(2)>div 更改为 .grid-item:nth-child(2) div

    body {
        font-family: "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif;
    }
    
    .grid-wrapper {
        display: grid;
        grid-template-columns: repeat(8, {1fr});
        grid-template-areas:
          ". header header header header header header ."
          ". tabs tabs tabs tabs tabs tabs .";
    }
    
    .grid-item:nth-child(1) {
      grid-area: header;
      background-color: #003566;
      padding-top: 30px;
      padding-bottom: 30px;
      text-align: center;
      border-bottom: 2px solid #ff6a00;
    }
    
    .grid-item:nth-child(2) {
      grid-area: tabs;
      background-color: #b3cccc;
      padding-top: 10px;
      padding-bottom: 10px;
      /*grid-template-areas: "tabs tabs";
      grid-template-columns: 1fr 1fr;*/
    }
    
    .logo {
        grid-area: logo;
        font-size: 1.3em;
        color:#fff;
    }
    
    .logo span {
        font-weight: 100;
        color: #ff6a00;
    }
    
    .logo i {
        font-size: 5px;
        vertical-align: middle;
        color: #fff;
    }
    
    .grid-item:nth-child(2) div {
      /*grid-area: tabs;*/
      color: #003566;
      margin: 10px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
    <html>
    <head>
      <meta name="viewport" content="width=device-width" />
      <title>SurEyeDentity Host Services Manager</title>
    </head>
    <body>
      <div class="grid-wrapper">
        <div class="grid-item">
          <div class="logo">
            SurEyeDentity<span> Host<i class="fa fa-circle" aria-hidden="true"></i>Services<i class="fa fa-circle" aria-hidden="true"></i>Manager</span>
          </div>
        </div>
        <div class="grid-item">
          <div>
            <i class="fa fa-camera"></i> Cameras
          </div>
          <div>
            <i class="fa fa-user" aria-hidden="true"></i> Users
          </div>
        </div>
      </div>
    </body>
    </html>

    【讨论】:

    • 将 .grid-item:nth-child(2)>div 更改为 .grid-item:nth-child(2) div 不起作用,但下面的下一个答案可以。非常感谢您的快速回答。
    猜你喜欢
    • 2020-10-15
    • 2017-08-18
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2022-12-28
    • 1970-01-01
    • 2021-01-03
    相关资源
    最近更新 更多