CSS网格隐藏元素不适合

发布于 2025-01-31 17:33:48 字数 2007 浏览 1 评论 0原文

我正在尝试创建纯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-fillauto-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 with auto-fill or auto-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2025-02-07 17:33:48

经过多年来的玩法,我无法改进您的网格解决方案。

我认为我在项目中要做的是使用带有固定高度容器/包装器的Flexbox。这可能不适合您的用例。

.container {
  border: 1px solid black;
  height: 2em;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  flex-wrap: wrap;
  column-gap: 1em;
  overflow: hidden;
}
.box {
  box-sizing: border-box;
  height: 100%;
  background: cyan;
  padding-left: 5px;
}
<div class="container">
  <div class="box">the quick</div>
  <div class="box">the quick brown</div>
  <div class="box">the quick brown fox</div>
  <div class="box">the quick brown fox jumps</div>
  <div class="box">the quick brown fox jumps over</div>
  <div class="box">the quick brown fox jumps over the lazy dog</div>
</div>

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.

.container {
  border: 1px solid black;
  height: 2em;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  flex-wrap: wrap;
  column-gap: 1em;
  overflow: hidden;
}
.box {
  box-sizing: border-box;
  height: 100%;
  background: cyan;
  padding-left: 5px;
}
<div class="container">
  <div class="box">the quick</div>
  <div class="box">the quick brown</div>
  <div class="box">the quick brown fox</div>
  <div class="box">the quick brown fox jumps</div>
  <div class="box">the quick brown fox jumps over</div>
  <div class="box">the quick brown fox jumps over the lazy dog</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文