CSS 网格使用特定项目高度作为最大行高

UnL*_*oCo 5 css css-grid

我试图防止侧边栏高度超过内容高度。
“区域”将包含任意高度的图像,因此其高度不是固定的或预先已知的。

function toggleTabsContent() {
	const display = document.querySelector('.content').style.display;
  if (display == "none") {
  	document.querySelector('.content').style.display = "block";
  } else {
  	document.querySelector('.content').style.display = "none";
  }
}
Run Code Online (Sandbox Code Playgroud)
.grid {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 1fr auto;
  grid-template-areas:
    "tabs area"
    "footer footer";
}

.tabs {
  grid-area: tabs;
  background: #FF9800;
}

.area {
  grid-area: area;
  height: 200px;
  background: #673AB7;
}

.footer {
  grid-area: footer;
  background: #607D8B;
  height: 40px;
}

.content {
  height: 250px;
}
Run Code Online (Sandbox Code Playgroud)
<button onclick="toggleTabsContent()">Make tabs taller/shorter</button>

<div class="grid">
  <div class="tabs">
    <div class="content" style="display: none"></div>
  </div>
  <div class="area"></div>
  <div class="footer"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

JSFiddle

Tem*_*fif 1

height:0;min-height:100%;在元素上使用tabs。这将确保元素的高度不会影响其轨道的大小,然后通过添加min-height:100%强制它使用其他元素定义的轨道的高度:

function toggleTabsContent() {
	const display = document.querySelector('.content').style.display;
  if (display == "none") {
  	document.querySelector('.content').style.display = "block";
  } else {
  	document.querySelector('.content').style.display = "none";
  }
}
Run Code Online (Sandbox Code Playgroud)
.grid {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 1fr auto;
  grid-template-areas:
    "tabs area"
    "footer footer";
}

.tabs {
  grid-area: tabs;
  background: #FF9800;
  height:0;
  min-height:100%;
  overflow:auto;
}

.area {
  grid-area: area;
  height: 200px;
  background: #673AB7;
}

.footer {
  grid-area: footer;
  background: #607D8B;
  height: 40px;
}

.content {
  height: 250px;
}
Run Code Online (Sandbox Code Playgroud)
<button onclick="toggleTabsContent()">Make tabs taller/shorter</button>

<div class="grid">
  <div class="tabs">
    <div class="content" style="display: none"></div>
  </div>
  <div class="area"></div>
  <div class="footer"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

相关问题:如何将外部 div 的高度设置为始终等于特定内部 div 的高度?