CSS Grid 用于在桌面上创建浮动侧边栏,而在移动设备上堆叠。

huangapple go评论65阅读模式
英文:

CSS Grid to make a floating sidebar on desktop that stacks for mobile

问题

我正在尝试使用CSS Grid创建一个右侧边栏,而在移动设备上,它会转换到顶部并堆叠在顶部。但是,出于某种原因,我的代码在页面加载时运行得很好,但在屏幕折叠时它不会缩小1fr部分。我原本以为1fr会根据可用空间来缩小,因为我将侧边栏设置为固定的350px。有什么想法吗?另外,我应该如何在CSS上设置一个堆叠的网格,其中固定的侧边栏位于顶部,然后在全宽度时出现。

.grid-container {
  display: grid;
  grid-template-columns: auto 350px;
  grid-template-rows: 1fr;
  grid-template-areas: "table-section filter-section";
}
.table-section {
  grid-area: table-section;
}
.filter-section {
  grid-area: filter-section;
}

我的HTML如下所示:

<div class="full-height" :class="$q.screen.lt.md ? 'grid-container-mobile' : 'grid-container'">
  <div class="col table-section q-pa-md">
  </div>
  <div class="col-auto filter-section q-pa-md">
    <TableFilters />
  </div>
</div>

由于某种原因,它不会折叠,因此窗口大小缩小导致溢出:
CSS Grid 用于在桌面上创建浮动侧边栏,而在移动设备上堆叠。

CSS Grid 用于在桌面上创建浮动侧边栏,而在移动设备上堆叠。

正如您所看到的,1fr部分在初始加载时不会缩小。

英文:

I am trying to use css grid to create a right sidebar that on mobile will convert to the top and be stacked up top. However, my code for some reason it works great on the page load but it does not shrink the 1fr section when the screen collapses. I would have thought 1fr would shrink to the available space since i set the side bar at a fixed 350px. Any ideas? Also how would I set this up css wise for a stacked grid where the fixed sidebar goes on top then at full width.

.grid-container{
  display: grid;
  grid-template-columns: auto 350px;
  grid-template-rows: 1fr;
  grid-template-areas: &quot;table-section filter-section&quot;;
}
.table-section{
  grid-area: table-section;
}
.filter-section{
  grid-area: filter-section;
}

My html is as follows:

  &lt;div class=&quot;full-height&quot; :class=&quot;$q.screen.lt.md ? &#39;grid-container-mobile&#39; : &#39;grid-container&#39;&quot;&gt;
    &lt;div class=&quot;col table-section q-pa-md&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-auto filter-section q-pa-md&quot;&gt;
      &lt;TableFilters /&gt;
    &lt;/div&gt;
  &lt;/div&gt;

For some reason it does not collapse so the shrinking window size results in a overflow:
CSS Grid 用于在桌面上创建浮动侧边栏,而在移动设备上堆叠。

CSS Grid 用于在桌面上创建浮动侧边栏,而在移动设备上堆叠。

As you can see the 1fr section is not shrinking as if the width on initial load is fixed.

答案1

得分: 1

你需要使用@media查询,链接在这里,这允许您根据媒体查询中的条件使用不同的规则。

例如,您可以使用:

@media screen and (orientation: portrait) {

}

这不是检测移动客户端的最佳方法,但在移动客户端上始终有效,也适用于任何纵向显示。

有了这个,这是我如何构建您的元素以及相应的CSS结构:

<div class='container'>
   <div class='calendar'>

   </div>
   <div class='filter'>

   </div>
</div>
.container {
    display: flex;
    width: 100%;
    box-sizing: border-box;
}

.calendar {
    width: 100%;
    max-width: 100%;
}

.filter {
    width: 350px;
    box-sizing: border-box;
}

@media screen and (orientation: landscape) {
    /* 媒体查询条件可以更改,这只是一个示例 */

    .container {
        flex-flow: column-reverse;
        height: 100%;
    }

    .calendar {
        height: 100%;
        max-height: 100%;
    }

    .filter {
        height: 250px; /* 您希望的任何高度 */
    }
}
英文:

You need to use the @media query, link here, this allows you to use different rules depending on what condition you put inside the media query.

For example you could use

@media screen and (orientation: portrait) {

}

This isn't the best method to detect a mobile client but will always work when it is a mobile client but will also work on any portrait display.

With that, here is how I would structure your elements and the CSS to go with it.

&lt;div class=&#39;container&#39;&gt;
   &lt;div class=&#39;calendar&#39;&gt;

   &lt;/div&gt;
   &lt;div class=&#39;filter&#39;&gt;

   &lt;/div&gt;
&lt;/div&gt;
.container {
    display: flex;
    width: 100%;
    box-sizing: border-box;
}

.calendar {
    width: 100%;
    max-width: 100%;
}

.filter {
    width: 350px;
    box-sizing: border-box;
}

@media screen and (orientation: landscape) {
    /* media condition could be changed, just for example */
    
    .container {
        flex-flow: column-reverse;
        height: 100%;
    }

    .calendar {
        height: 100%;
        max-height: 100%;
    }

    .filter {
        height: 250px; /* whatever height you want it to be */
    }

}

huangapple
  • 本文由 发表于 2023年6月26日 03:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定