英文:
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>
正如您所看到的,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: "table-section filter-section";
}
.table-section{
grid-area: table-section;
}
.filter-section{
grid-area: filter-section;
}
My html is as follows:
<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>
For some reason it does not collapse so the shrinking window size results in a overflow:
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.
<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) {
/* 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 */
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论