网格布局的 CSS 大小调整问题

时间:2021-07-17 16:01:19

标签: javascript html css resize css-grid

我的页面上有一个网格,但它似乎没有按照我的要求执行(像往常一样哈哈)。有两个主要问题我似乎无法解决。

第 1 点:网格的右侧(最近的文章)似乎占用了太多空间。理想情况下,我希望网格居中,这样两半的大小相同 enter image description here

第 2 点:调整大小时,网格的左侧部分会被压缩,而右侧则不会。如果可能,我希望左侧保持不变,并压缩“最近文章”列表的右侧 enter image description here

使用的代码在这里:

HTML:

 <div class="homepage">
            <div class="topics">
                <div class="homepage-topics-title">
                    Explore some topics:
                </div>
                <div class="individual-topics">
                    <a href="/productivity" class="topic1">Productivity</a>
                    <a href="/orginisation" class="topic2">Orginisation</a>
                    <a href="/time-management" class="topic3">Time-Management</a>
                </div>
            </div>
            <div class="recent">
                <div class="homepage-recent-title">
                    Recent articles
                </div>
                <div class="hompage-recent-articles">
                    {{{body}}}
                </div>
            </div>
        </div>

CSS:

.site {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.site-content {
    flex-grow: 1;
    padding: 6rem 0;
}

@media (max-width: 767px) {
    .site-content {
        padding: 3rem 0;
    }
}


/* Homepage code */
.homepage{
    display: grid;
    grid-template-columns: 1fr 1fr;
    margin-top: 3rem;
}

.homepage-recent-title{
    font-size: 3rem;
    position: relative;
    align-self: center;
    overflow: hidden;
    padding-left: 3rem;
    line-height: 1;
}

.homepage-topics-title{
    font-size: 3rem;
    position: relative;
    align-self: center;
    overflow: hidden;
    margin-left: 3rem;
    line-height: 1;
}

.individual-topics{
    position: relative;
    display: grid;
    grid-template-rows: 1fr 1fr 1fr;
    padding-top: 1.5rem;
    padding-bottom: 1.5rem;
    padding-left: 5rem;
    padding-right: 50%;
    font-size: 1.6rem;
    font-weight: 400;
    align-items: center;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 3;
}

1 个答案:

答案 0 :(得分:0)

如果你调整你grid-template-columns,我想你会得到你所需要的。

/* Homepage code */
.homepage{
    display: grid;
    grid-template-columns: min-content 1fr;
    margin-top: 3rem;
}

.site {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.site-content {
  flex-grow: 1;
  padding: 6rem 0;
}

@media (max-width: 767px) {
  .site-content {
    padding: 3rem 0;
  }
}


/* Homepage code */

.homepage {
  display: grid;
  grid-template-columns: min-content 1fr;
  margin-top: 3rem;
}

.homepage-recent-title {
  font-size: 3rem;
  position: relative;
  align-self: center;
  overflow: hidden;
  padding-left: 3rem;
  line-height: 1;
}

.homepage-topics-title {
  font-size: 3rem;
  position: relative;
  align-self: center;
  overflow: hidden;
  margin-left: 3rem;
  line-height: 1;
}

.individual-topics {
  position: relative;
  display: grid;
  grid-template-rows: 1fr 1fr 1fr;
  padding-top: 1.5rem;
  padding-bottom: 1.5rem;
  padding-left: 5rem;
  padding-right: 50%;
  font-size: 1.6rem;
  font-weight: 400;
  align-items: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 3;
}
<div class="homepage">
  <div class="topics">
    <div class="homepage-topics-title">
      Explore some topics:
    </div>
    <div class="individual-topics">
      <a href="/productivity" class="topic1">Productivity</a>
      <a href="/orginisation" class="topic2">Orginisation</a>
      <a href="/time-management" class="topic3">Time-Management</a>
    </div>
  </div>
  <div class="recent">
    <div class="homepage-recent-title">
      Recent articles
    </div>
    <div class="hompage-recent-articles">
      {{{body}}}
    </div>
  </div>
</div>