【问题标题】:Can I set css grid row height based on if anything targets it with grid-area?我可以根据是否有任何东西以网格区域为目标来设置 CSS 网格行高吗?
【发布时间】:2021-03-27 09:06:24
【问题描述】:

是否可以根据是否使用网格区域来设置css网格行高?

.container {

  height: 100vh;

  grid-template-areas: 
    "mobile-head mobile-head mobile-head" 
    "nav app app"
    "footer footer footer";
}

.nav {
  grid-area: nav
}
 <div class="container">
   <div class="nav">Nav stuff</div>
 </div>

鉴于上面的网格模板和 html,我在导航区域看到了导航的东西,但移动导航区域自然占据了屏幕高度的 1/3。因为没有具有 grid-area: mobile-head 的 div 我希望高度为 0px。

但如果我将 .nav 更改为

...
  .nav {
    grid-area: mobile-head 
}

我希望该行的高度固定为 200 像素。

我可以靠近。当移动区域中没有任何内容时,我可以让行的高度为 0:

  ...
  grid-template-rows: minmax(0px, auto) 1fr 1fr

但是高度是由内容决定的,而不是网格。

我相当确定实现我想要做的唯一选择是使用 2 个网格和媒体查询。 (我想将导航放在桌面的左侧,如果是移动设备,则放在顶部)

【问题讨论】:

  • 您的网格区域声明中没有定义mobile-nav 区域。你的意思是mobile-head?

标签: css css-grid


【解决方案1】:

我认为grid-auto-rows: min-content 是你所追求的。

<button class="toggle" style="margin-bottom:1em">Toggle hidden row</button>
<div class="container">
   <div class="nav">Nav stuff</div>
</div>
<style>
.container {
  box-shadow: inset 0 0 0 1px lightgray;
  background-color: #fafafb;
  display: grid;
  height: 100vh;
  grid-auto-rows: min-content;
  grid-template-areas: 
    "mobile-head mobile-head mobile-head" 
    "nav app app"
    "footer footer footer";
}

.nav {
  grid-area: nav;
  background-color: lightblue;
}
.nav.mobile {
  grid-area: mobile-head;
}
</style>
<script>
document.querySelector('.toggle').onclick = function(){
  document.querySelector('.nav').classList.toggle('mobile');
};
</script>

【讨论】:

    猜你喜欢
    • 2017-12-04
    • 2019-08-16
    • 2018-07-18
    • 2019-08-30
    • 2015-02-19
    • 2021-09-16
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多