CSS网格不符合

时间:2018-10-09 11:26:16

标签: css grid-layout css-grid

红色和蓝色部分彼此相邻,但黄色部分不在同一行上,并且在蓝色部分之下,它们的高度也与红色部分相同,因此我无法弄清楚哪里出了问题

.contact-information {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
  grid-auto-rows: minmax(90px, auto);
  width: 100%;
  overflow: hidden;
}

.contact-left-section {
  grid-column: 1/3;
  grid-row: 1/2;
  background-color: red;
}

.contact-middle-section {
  grid-column: 3/6;
  grid-row: 1/2;
  background-color: blue;
}

.contact-right-section {
  grid-column: 6/9;
  grid-row: 1/2;
  background-color: yellow;
}
<section class="about light contact-information">
  <div class="contact-left-section">
    <div class="section-title">Phone</div>
    <div class="section-content">
      <p>Lorem ipsum</p>
    </div>
  </div>

  <div class="contact-middle-section">
    <div class="section-title">Services</div>
    <div class="section-content">
      <p>Lorem ipsum</p>
    </div>

    <div class="contact-right-section">
      <div class="section-title">Email</div>
      <div class="section-content">
        <p>Lorem ipsum</p>
      </div>
    </div>
</section>

2 个答案:

答案 0 :(得分:1)

首先,您仅定义了八个列,而不是 9 列,但是更重要的是,您没有关闭.contact-middle-section div从而使布局不可用。

.contact-information {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-auto-rows: minmax(90px, auto);
  width: 100%;
  overflow: hidden;
}

.contact-left-section {
  grid-column: 1/3;
  grid-row: 1/2;
  background-color: red;
}

.contact-middle-section {
  grid-column: 3/6;
  grid-row: 1/2;
  background-color: blue;
}

.contact-right-section {
  grid-column: 6/9;
  grid-row: 1/2;
  background-color: yellow;
}
<section class="about light contact-information">
  <div class="contact-left-section">
    <div class="section-title">Phone</div>
    <div class="section-content">
      <p>Lorem ipsum</p>
    </div>
  </div>

  <div class="contact-middle-section">
    <div class="section-title">Services</div>
    <div class="section-content">
      <p>Lorem ipsum</p>
    </div>
  </div>

  <div class="contact-right-section">
    <div class="section-title">Email</div>
    <div class="section-content">
      <p>Lorem ipsum</p>
    </div>
  </div>
</section>

答案 1 :(得分:0)

您可以尝试替换: grid-template-columns:1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;

用于: grid-template-columns: repeat(9,1fr);

我希望该解决方案可以帮助...