英文:
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 -->
<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>
<!-- 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'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));
}
<!-- language: lang-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">
<!-- 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>
<!-- end snippet -->
References:
- CSS:
- HTML:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论