简体   繁体   English

CSS网格填充100vh而不影响行高

[英]css grid to fill 100vh without affecting row height

I have a grid with 3 columns and a variable/dynamic number of rows. 我有一个包含3列和可变/动态行数的网格。 The grid has a colored background and when there are NOT enough rows as to vertically fill the window, the remaining, empty vertical space is un-colored: 网格具有彩色背景,并且当没有足够的行垂直填充窗口时,剩余的空白垂直空间将不着色:

 .grid { align-self: flex-start; display: grid; grid-template-columns: min-content 2fr 1fr; grid-gap: 0px; } .index, .title { background: red; padding: 0 2vw 0 1vw; } .ticker { background: yellow; } 
 <div class="grid"> <div class="index">1</div> <div class="title">Pigs are cute</div> <div class="ticker">blah blah</div> <div class="index">2</div> <div class="title">Horses rise at night</div> <div class="ticker">bleh bleh</div> <div class="index">3</div> <div class="title">Cats run in packs</div> <div class="ticker">blih blih</div> </div> 

I would like this vertical space to be colored as the grid is. 我希望此垂直空间像网格一样被着色。 The apparently obvious solution is to give the grid a height of 100vh. 显而易见的解决方案是使网格的高度为100vh。 But this expectedly stretches each row's height accordingly, which is not the result I am looking for, as I need row height to adapt to content. 但这预计会相应地拉伸每行的高度,这并不是我想要的结果,因为我需要行高来适应内容。

 .grid { align-self: flex-start; display: grid; grid-template-columns: min-content 2fr 1fr; grid-gap: 0px; height: 100vh; } .index, .title { background: red; padding: 0 2vw 0 1vw; } .ticker { background: yellow; } 
 <div class="grid"> <div class="index">1</div> <div class="title">Pigs are cute</div> <div class="ticker">blah blah</div> <div class="index">2</div> <div class="title">Horses rise at night</div> <div class="ticker">bleh bleh</div> <div class="index">3</div> <div class="title">Cats run in packs</div> <div class="ticker">blih blih</div> </div> 

Is there any CSS grid way of expanding the grid vertically while still allowing row height to adapt to content? 是否有CSS网格垂直扩展网格的方式,同时仍允许行高适应内容?

An alternative javascript solution, such as adding blank rows to the grid until grid's height is >= the height of the window would also be acceptable. 一种替代的javascript解决方案,例如向网格中添加空白行,直到网格的高度> =窗口的高度,也是可以接受的。

A flex solution is, I believe, not acceptable, as I need a grid-like behaviour where columns and rows remain aligned. 我认为弹性解决方案是不可接受的,因为我需要一种类似网格的行为,其中列和行保持对齐。

Thank you 谢谢

Since different columns have different colors, extending the grid would not work, because the grid itself can't have two different colors. 由于不同的列具有不同的颜色,因此扩展网格将不起作用,因为网格本身不能具有两种不同的颜色。 This means only expanding the rows or adding new rows would work. 这意味着仅扩展行或添加新行将起作用。

Since one of the question's requeriments is to leave row height untouched, and as per this question , adding an extra row and expanding only that row to take the remaining space is not possible with CSS grid (but flexbox), the only solution seems to add rows via JS until the grid height is >= window height : 由于问题的要求之一是保持行高不变,并且按照此问题 ,使用CSS网格(但flexbox)无法添加额外的行并仅扩展该行以占用剩余空间,因此唯一的解决方案似乎是添加通过JS行,直到网格高度> = window height为止:

 var rowHeight = window.getComputedStyle(document.querySelector('.grid').firstElementChild).height; var row = "<div class='index' style='height:"+ rowHeight +"'></div><div class='title' style='height:"+ rowHeight +"'></div><div class='ticker' style='height:"+ rowHeight +"'></div>"; while (parseFloat(window.getComputedStyle(document.querySelector('.grid')).height) < window.innerHeight) { document.querySelector('.grid').innerHTML += row; } 
 html, body { margin: 0; padding: 0; } .grid { align-self: flex-start; display: grid; grid-template-columns: min-content 2fr 1fr; grid-gap: 0px; } .index, .title { background: red; padding: 0 2vw 0 1vw; } .ticker { background: yellow; } 
 <div class="grid"> <div class="index">1</div> <div class="title">Pigs are cute</div> <div class="ticker">blah blah</div> <div class="index">2</div> <div class="title">Horses rise at night</div> <div class="ticker">bleh bleh</div> <div class="index">3</div> <div class="title">Cats run in packs</div> <div class="ticker">blih blih</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM