Grid属性不起作用,因为代码不是以网格表格格式排列的。

huangapple go评论66阅读模式
英文:

Grid Property is not working as the code is not in the grid tabular format

问题

以下是您的代码中需要翻译的部分:

"我正在尝试创建一个用于我的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));
}

这是我的一些项目:

这是我希望输出的格式,但它没有以网格的形式显示:

"

英文:

I am trying to built a basic portfolio for my Freecodecamp project but I cannot understand by error as to why the grid property is not working.

Here is the code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#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));
}

<!-- language: lang-html -->

&lt;section id=&#39;projects&#39;&gt;
  &lt;h2&gt;Here are some of my Projects:&lt;/h2&gt;
  &lt;div id=&quot;projects-grid&quot;&gt;
    &lt;a href=&quot;&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;&lt;/image&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;&lt;/image&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;&lt;/image&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;&lt;/image&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;&lt;/image&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;&lt;/image&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
  &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

This is the format I want my output to be in, but it isn't going in a grid:

<a href="https://i.stack.imgur.com/kggzT.png"><img src="https://i.stack.imgur.com/kggzT.png"></a>

答案1

得分: 1

以下是您提供的内容的翻译:

#projects {
  padding: 3rem 2rem;
  /* 大多数元素(默认情况下)的宽度是
     声明的宽度加上边框宽度
     和填充; 这意味着此元素的宽度是:
       父元素宽度的100%,再加上4rem
       (每边2rem)
     由于它的100% + 4rem,所以在水平方向上出现滚动,通常不希望如此。
     因此 - 因为<section>默认情况下是块级元素,我注释掉了宽度,
     在这种情况下,浏览器将分配填充,然后使用剩余空间作为元素的大小。
  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;
  /* 我已更正以下一行中的拼写错误,“autofit”应为
     “auto-fit”,如下所示。如果您使用浏览器的开发工具
     (右键单击元素,选择“检查元素”并查看
     CSS属性,您会看到“无效值”,应该指向正确的解决方向:*/
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}
<section id='projects'>
  <h2>这是我的一些项目:</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>标签,原因有两个:
           1. <img>元素没有或不需要结束标签
           2. 如果需要结束标签,它需要与开始标签相同,但以
              '/'字符作为第一个字符,例如:<img>(但不要使用它,因为不需要结束标签
              该元素是空的,不包含子元素) -->
      <p class='project-title'>致敬页面</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'>致敬页面</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'>致敬页面</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'>致敬页面</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'>致敬页面</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'>致敬页面</p>
    </a>
  </div>
  <!-- 我注释掉了以下多余的</div>标签,未配对 -->
</section>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#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&#39;s width, plus 4rem
       (2rem for each side)
     as it&#39;s 100% + 4rem there&#39;s scrolling in
     the horizontal direction which is not usually
     desired. As such - because a &lt;section&gt; is a
     block element by default I&#39;ve commented out
     the width, in which case the browser will
     assign the padding and then use the remaining
     space as the element&#39;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 &quot;autofit&quot; should be
     &quot;auto-fit,&quot; as below. Had you used your browser&#39;s developer tools
     (right click the element, &quot;inspect element&quot; and looked in the
     CSS properties you&#39;d have seen &quot;invalid value&quot; which should have
     pointed you the correct direction to resolve the issue: */
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

<!-- language: lang-html -->

&lt;section id=&#39;projects&#39;&gt;
  &lt;h2&gt;Here are some of my Projects:&lt;/h2&gt;
  &lt;div id=&quot;projects-grid&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;
      &lt;!-- I removed the &lt;/image&gt; tags, for two reasons:
           1. the &lt;img&gt; element doesn&#39;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 &#39;/&#39; character as the first character, for example: `&lt;/img&gt;` (but don&#39;t use that, no closing tag
              is required as the element is void and contains no children) --&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;
      &lt;img src=&quot;https://www.aljazeera.com/wp-content/uploads/2022/12/SSS10784_1.jpg?resize=1920%2C1440&quot; class=&quot;img&quot;&gt;
      &lt;p class=&#39;project-title&#39;&gt;Tribute page&lt;/p&gt;
    &lt;/a&gt;
  &lt;/div&gt;
  &lt;!-- I commented out the following surplus &lt;/div&gt; tag that was unmatched --&gt;
&lt;/section&gt;

<!-- end snippet -->

References:

huangapple
  • 本文由 发表于 2023年5月7日 17:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定