gpt4 book ai didi

css - 如何使 CSS Grid 调整内容的大小

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:00 26 4
gpt4 key购买 nike

我想要一个包含 4 个框的列,它们是响应式的并且也位于页面的中心,其余的在代码的注释中解释

我找到了一个解决方案,但它阻止了它位于中心,我将所有内容都包含在我尝试的代码中。

  • 这会调整它的大小,但由于某种原因它会阻止它位于中心。

/* THIS RESIZES IT, BUT PREVENTS IT FROM BEING IN THE CENTER FOR SOME REASON */

.wrapper {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr;
grid-template-areas: "1" "2" "3" "4";
position: relative;
top: 25rem;
left: 50%;
transform: translate(-50%, -50%);
}

.wrap {
min-width: 30rem;
max-width: 60rem;
padding: 2rem;
margin: 1rem;
border: 3px solid #fff;
background-color: #c8e6c9;
}
<div class="wrapper">
<div class="wrap">1</div>
<div class="wrap">2</div>
<div class="wrap">3</div>
<div class="wrap">4</div>
</div>

  • 这是完美的,但没有调整它的大小

/*  THIS IS PERFECT, EXCEPT IT DOESN'T RESIZE IT */

.wrapper {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr;
grid-template-areas: "1" "2" "3" "4";
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.wrap {
min-width: 30rem;
max-width: 60rem;
padding: 2rem;
margin: 1rem;
border: 3px solid #fff;
background-color: #c8e6c9;
}
<div class="wrapper">
<div class="wrap">1</div>
<div class="wrap">2</div>
<div class="wrap">3</div>
<div class="wrap">4</div>
</div>

非常感谢。

How I would like to have it

最佳答案

您可以使用 html 和 body 通过 flex 将您的内容居中:

body {
min-height: 100vh;
display: flex;
margin: 0;
}

.wrapper {
margin: auto;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr;
grid-template-areas: "1" "2" "3" "4";
}

.wrap {
min-width: 30rem;
max-width: 60rem;
padding: 2rem;
margin: 1rem;
border: 3px solid #fff;
background-color: #c8e6c9;
}
<div class="wrapper">
<div class="wrap">1</div>
<div class="wrap">2</div>
<div class="wrap">3</div>
<div class="wrap">4</div>
</div>

关于css - 如何使 CSS Grid 调整内容的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57296017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com