CSS网格隐藏元素不适合
我正在尝试创建纯CSS响应式导航。这意味着没有媒体查询,没有像素值,也没有JS。
第一个挑战是如果没有足够的水平空间,则隐藏导航。我能够实现这一目标,但以牺牲它为像素价值为代价。
.wrapper {
background: rgba(0, 255, 0, 0.1);
width: 100%;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min-content, 250px));
justify-content: space-between;
grid-template-rows: auto;
grid-auto-rows: 0;
/* Automatically created rows will have a height of 0. */
overflow: hidden;
border: 1px solid red;
}
.logo {
background: rgba(255, 0, 0, 0.1);
}
.another-element {
background: rgba(0, 0, 255, 0.1);
display: flex;
gap: 10px;
flex-wrap: nowrap;
overflow: hidden;
}
a {
white-space: nowrap;
background: orange;
}
<div class="wrapper">
<div class="logo">Logo</div>
<div class="another-element">
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
<a>Link 4</a>
</div>
</div>
我已经研究了 repot()function 。一些注释:
- 只能通过使用
repot()
函数来创建自动行,auto-fill
或auto-fit
。 - 自动行可以用
grid-auto行折叠:0;
,从而隐藏了不适合第一行的元素。 - 将重复()与自动拟合作为第一个参数时,可以将minmax()用作第二个参数。 minmax()函数允许我们为列设置最小值和最大值,但是它需要一对长度/百分比和最小符号,最大含义或自动,但不接受这两个参数的最小内容:
minmax(min-content,min-content,, Min-content)
似乎不是有效的CSS。
因此,一切都按预期工作,除了包装发生在固定的250px*2值时,而不是在两个项目没有足够的空间时。知道如何使包裹只有在没有足够的空间时才能实现?
这是 codepen 。
I'm trying to create a pure CSS responsive navigation. This means no media queries, no pixel values and no JS.
The first challenge is to hide the navigation if there's not enough horizontal space. I was able to accomplish this, but at the expense of giving it a pixel value.
.wrapper {
background: rgba(0, 255, 0, 0.1);
width: 100%;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min-content, 250px));
justify-content: space-between;
grid-template-rows: auto;
grid-auto-rows: 0;
/* Automatically created rows will have a height of 0. */
overflow: hidden;
border: 1px solid red;
}
.logo {
background: rgba(255, 0, 0, 0.1);
}
.another-element {
background: rgba(0, 0, 255, 0.1);
display: flex;
gap: 10px;
flex-wrap: nowrap;
overflow: hidden;
}
a {
white-space: nowrap;
background: orange;
}
<div class="wrapper">
<div class="logo">Logo</div>
<div class="another-element">
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
<a>Link 4</a>
</div>
</div>
I've studied the repeat() function. A few notes:
- Auto rows can only be created by using the
repeat()
function withauto-fill
orauto-fit
. - Auto rows can be collapsed with
grid-auto-rows: 0;
thus hiding the elements that do not fit the first line. - When using repeat() with auto-fit as the first argument, one can use minmax() as the second. The minmax() function allows us to set a minimum and maximum values for the columns, but it requires a pair of length/percentage and min-content, max-content or auto, but does not accept min-content for both arguments:
minmax(min-content, min-content)
doesn't seem to be valid CSS.
So everything works as expected, except the fact that the wrapping happens at a fixed 250px*2 value instead of when there's not enough space for both items. Any idea how to make the wrap only happen when there's not enough space?
Here's a codepen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过多年来的玩法,我无法改进您的网格解决方案。
我认为我在项目中要做的是使用带有固定高度容器/包装器的Flexbox。这可能不适合您的用例。
Having played around with this for ages, I can’t improve on your grid solution.
I think what I will do in my project is to use a flexbox with a fixed height container/wrapper. This may or may not suit your use case.