css 网格属性不起作用,因为代码不是网格表格格式

7rfyedvj  于 2023-05-08  发布在  其他
关注(0)|答案(2)|浏览(158)

我试图为我的Freecodecamp项目建立一个基本的投资组合,但我不能理解错误,为什么网格属性不起作用。
代码如下:

#projects {
  padding: 3rem 2rem;
  width: 100%;
  height: 120%;
  background-color: #3c6382;
}

#projects h2 {
  text-align: Center;
  text-decoration: underline;
  color: #c8d6e5;
  font-size: 2rem;
}

.img {
  margin-top: 4rem;
  width: 40%;
  border: 0.5rem solid white;
}

.project-title {
  color: white;
  font-size: 1.5rem;
  margin-left: 4rem;
}

#projects-grid {
  display: grid;
  grid-template-columns: repeat(autofit, minmax(320px, 1fr));
}
<section id='projects'>
  <h2>Here are some of my Projects:</h2>
  <div id="projects-grid">
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></image>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></image>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></image>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></image>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></image>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></image>
      <p class='project-title'>Tribute page</p>
    </a>
  </div>
  </div>
</section>

这是我希望输出的格式,但它不是网格格式:

dy2hfwbg

dy2hfwbg1#

#projects {
  padding: 3rem 2rem;
  /* the width of most elements (by default) is
     the declared width plus the border-widths
     and the padding; this means the width of
     this element is calculated to be:
       100% of the parent's width, plus 4rem
       (2rem for each side)
     as it's 100% + 4rem there's scrolling in
     the horizontal direction which is not usually
     desired. As such - because a <section> is a
     block element by default I've commented out
     the width, in which case the browser will
     assign the padding and then use the remaining
     space as the element's size.
  width: 100%;
  */
  height: 120%;
  background-color: #3c6382;
}

#projects h2 {
  text-align: center;
  text-decoration: underline;
  color: #c8d6e5;
  font-size: 2rem;
}

.img {
  margin-top: 4rem;
  width: 40%;
  border: 0.5rem solid white;
}

.project-title {
  color: white;
  font-size: 1.5rem;
  margin-left: 4rem;
}

#projects-grid {
  display: grid;
  /* I corrected the typo in the following line "autofit" should be
     "auto-fit," as below. Had you used your browser's developer tools
     (right click the element, "inspect element" and looked in the
     CSS properties you'd have seen "invalid value" which should have
     pointed you the correct direction to resolve the issue: */
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}
<section id='projects'>
  <h2>Here are some of my Projects:</h2>
  <div id="projects-grid">
    <a href="#">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img">
      <!-- I removed the </image> tags, for two reasons:
           1. the <img> element doesn't have, or require, a closing tag, and
           2. if it did it would require that closing tag to be the same as the openening tag, but with
              a '/' character as the first character, for example: `</img>` (but don't use that, no closing tag
              is required as the element is void and contains no children) -->
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="#">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img">
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="#">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img">
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="#">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img">
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="#">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img">
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="#">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img">
      <p class='project-title'>Tribute page</p>
    </a>
  </div>
  <!-- I commented out the following surplus </div> tag that was unmatched -->
</section>

参考文献:

toiithl6

toiithl62#

CSS代码看起来是正确的,但HTML代码中的img标记中存在语法错误。不要使用</image>关闭标记,而使用</img>。这是核心代码HTML

<section id='projects'>
  <h2>Here are some of my Projects:</h2>
  <div id="projects-grid">
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></img>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></img>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></img>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></img>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></img>
      <p class='project-title'>Tribute page</p>
    </a>
    <a href="">
      <img src="https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440" class="img"></img>
      <p class='project-title'>Tribute page</p>
    </a>
  </div>
</section>

相关问题