我正在尝试创建一个包含2行的3列网格,以在我的投资组合中显示我的项目,但它们似乎只填充第一列。我已经尝试了很多次,但是我只是不知道该怎么做才能使其正常工作。
HTML / PHP
<div id="projects">
<div class="project-item-container">
<div class="project-item">
<?php foreach ($projects as $projectItem) {?>
<div class="project-item-img">
<img class="absolute-center" src="assets/img/<?php echo $projectItem["img"]; ?>.png" alt="<?php echo $projectItem["name"]; ?>">
</div><!-- project item img end -->
<div class="project-item-info">
<h2><?php echo $projectItem["name"]; ?></h2>
<h4><?php echo $projectItem["category"]; ?></h4>
</div><!-- project item info end -->
<?php } ?>
</div><!-- project item end -->
</div><!-- project-item container end -->
CSS
.project-item-container {
display: grid;
grid-template-columns: 400px 400px 400px;
grid-gap: 40px;
}
.project-item img {
max-width: 100%;
display: block;
}
.project-item-info {
text-align: center;
position: relative;
top: -50px;
background-color: #ffffff;
height: 100px;
padding: 10px 0;
}
答案 0 :(得分:1)
您的CSS需要看起来像这样...
#projects {
width: 92%;
padding: 4rem 0;
margin: 0 auto;
}
.project-item-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
justify-content: center;
grid-gap: 4rem;
}
.project-item img {
width: 400px;
max-width: 100%;
display: block;
}
.project-item-info {
padding: 1rem 0;
text-align: center;
}
答案 1 :(得分:0)
我认为您应该将.project-item放入forEach循环中。 该网站提供了很多示例:https://css-tricks.com/snippets/css/complete-guide-grid/