gpt4 book ai didi

html - 网格元素未使用 minmax() 函数正确包装

转载 作者:行者123 更新时间:2023-11-28 01:54:06 25 4
gpt4 key购买 nike

我正在尝试创建一个响应式页面,当浏览器窗口变小时,“框”会换行到另一行。我用过:

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

在下面的笔中完成此操作。有关所需行为的示例,请参见链接。

https://codepen.io/DukeJellington/pen/BrMQga

html {
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
color: var(--dark)
}

body {
background: #ccc;
margin: 30px 50px;
line-height: 1.4;
}

.boxes {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.box {
background: var(--primary);
text-align: center;
padding: 1.5rem 2rem;
box-shadow: var(--shadow);
}
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>

<!-- Boxes -->
<section class="boxes">
<div class="box">
<i class="fas fa-chart-pie fa-4x"></i>
<h3>Analytics</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
<div class="box">
<i class="fas fa-globe fa-4x"></i>
<h3>Marketing</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
<div class="box">
<i class="fas fa-cog fa-4x"></i>
<h3>Development</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
<div class="box">
<i class="fas fa-users fa-4x"></i>
<h3>Support</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
</section>

请注意,当窗口变小时,图标/文本将如何换行到下一行,直到一个网格元素位于另一个网格元素之上。

但是,在下面的链接中创建的完整页面上有禁止此行为的内容;而且我无法找到禁止这种行为的原因。

https://codepen.io/DukeJellington/pen/vRbyqb

/* CSS Variables */
:root {
--primary: #ddd;
--dark: #333;
--light: #fff;
--shadow: 0 1px 5px rgba(104, 104, 104, 0.8)
}

html {
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
color: var(--dark)
}

body {
background: #ccc;
margin: 30px 50px;
line-height: 1.4;
}

.btn {
background-color: var(--dark);
color: var(--light);
padding: 0.6rem 1.3rem;
text-decoration: none;
border: 0;
}

img {
max-width: 100%;
}

.wrapper {
display: grid;
grid-gap: 20px;
}

/* Navigation */
.main-nav ul {
display: grid;
grid-gap: 20px;
padding: 0;
list-style: none;
grid-template-columns: repeat(4, 1fr);
}

.main-nav a {
background: var(--primary);
display: block;
text-decoration: none;
padding: 0.8rem;
text-align: center;
color: var(--dark);
text-transform: uppercase;
font-size: 1.1rem;
box-shadow: var(--shadow);
}

.main-nav a:hover {
background: var(--dark);
color: var(--light);
}


/* Top Container */
.top-container {
display: grid;
grid-gap: 20px;
grid-template-areas: 'showcase showcase top-box-a' 'showcase showcase top-box-b'
}

.showcase {
grid-area: showcase;
min-height: 400px;
background: url(img/showcase.jpg);
background-size: cover;
background-position: center;
padding: 3rem;
display: flex;
flex-direction: column;
align-items: start;
justify-content: center;
box-shadow: var(--shadow);
}

.showcase h1 {
font-size: 4rem;
margin-bottom: 0;
color: var(--light);
}

.showcase p {
font-size: 1.3rem;
margin-top: 0;
color: var(--light);
}


/* Top Box */
.top-box {
background: var(--primary);
display: grid;
align-items: center;
justify-items: center;
box-shadow: var(--shadow);
padding: 1.5rem;
}

.top-box .price {
font-size: 2.5rem;
}

.top-box-a {
grid-area: top-box-a;
}

.top-box-b {
grid-area: top-box-b;
}

/* Boxes */
.boxes {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.box {
background: var(--primary);
text-align: center;
padding: 1.5rem 2rem;
box-shadow: var(--shadow);
}
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>

<div class="wrapper">
<!-- Navigation -->
<nav class="main-nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<!-- Top Container -->
<section class="top-container">
<header class="showcase">
<h1>Your Web Presence</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos, omnis magnam? Tempore ut vero ex.</p>
<a href="#" class="btn">Read More</a>
</header>
<div class="top-box top-box-a">
<h4>Membership</h4>
<p class="price">$199/month</p>
<a href="#" class="btn">Buy Now</a>
</div>
<div class="top-box top-box-b">
<h4>Pro Membership</h4>
<p class="price">$299/month</p>
<a href="#" class="btn">Buy Now</a>
</div>
</section>
<!-- Boxes Section -->
<section class="boxes">
<div class="box">
<i class="fas fa-chart-pie fa-4x"></i>
<h3>Analytics</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
<div class="box">
<i class="fas fa-globe fa-4x"></i>
<h3>Marketing</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
<div class="box">
<i class="fas fa-cog fa-4x"></i>
<h3>Development</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
<div class="box">
<i class="fas fa-users fa-4x"></i>
<h3>Support</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, sapiente.</p>
</div>
</section>
<!-- Information Section -->

<section class="info">
<img src="img/pic1.jpg" alt="">
<div>
<h2>Your Business on the Web</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi, minima praesentium accusamus maxime repellat, amet non necessitatibus enim officia ipsam saepe nemo deleniti incidunt vero autem earum accusantium sed corporis.
<a href="#" class="btn">Learn More</a>
</p>
</div>
</section>

<!-- Portfolio Section -->
<section class="portfolio">
<img src="https://source.unsplash.com/random/200x200" alt="">
<img src="https://source.unsplash.com/random/200x201" alt="">
<img src="https://source.unsplash.com/random/200x202" alt="">
<img src="https://source.unsplash.com/random/200x203" alt="">
<img src="https://source.unsplash.com/random/200x204" alt="">
<img src="https://source.unsplash.com/random/200x205" alt="">
<img src="https://source.unsplash.com/random/200x206" alt="">
<img src="https://source.unsplash.com/random/200x207" alt="">
<img src="https://source.unsplash.com/random/200x208" alt="">
</section>
<!-- Footer -->
<footer>
<p>GridBiz &copy; 2018</p>
</footer>

</div>
<!-- Wrapper Ends -->

在上面的链接中,当窗口变小时,最终的 div 会换行到下方,但当窗口进一步最小化时,其他网格元素仍保留在同一行。

我稍微研究了一下代码,注意到当我注释掉包装类的以下样式时,网格项的行为符合预期。

.wrapper {
display: grid;
grid-gap: 20px;

有人能告诉我让包装器类显示网格会阻止我下面的网格项正确包装吗?

最佳答案

由于主 .wrapper block 中的其他较宽的兄弟 block ,您的 .boxes 网格不会进一步换行。

默认情况下,当您为 block 添加 display: grid 时,它会变成网格容器。您的 .wrapper 网格容器只有一列,该列的最小宽度由具有最大最小尺寸的子项定义。在您的案例中,最大的 child 是 .main-nav ul 因为它有 4 列,无论分辨率如何都不会缩小。下一个大容器是 .top-container,它也有大量不可收缩的内容。

因此,为了解决您的问题,只需使 .main-nav ul.top-container block 以任何方式收缩即可。

另请注意,对于一维布局,您可以使用 Flexbox。

关于html - 网格元素未使用 minmax() 函数正确包装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745424/

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