Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set css grid child element's size on the grid, without specifying what column or row it should go into

Is it possible do set a div's height and width in amount of rows/columns they should span, without specifying exactly which columns or rows they should go into?

For example, if I have two classes, say .long and .wide, and I want the .long class to be 3 rows high, and 1 column wide, and the .wide class be 3 columns wide but only 1 row high.

My specific use case involves Angular 5, I'm dynamically loading in objects and would like to make some of them larger than others. I know I could do this using flexboxes and setting the height, but was curious to see if I could achieve the same (much neater) with a grid, but all I've been finding has every class specified with grid-column: x/y; grid-row: a/b and the like.

like image 804
Siebe Avatar asked Apr 20 '18 13:04

Siebe


People also ask

What does 1fr mean in CSS Grid?

Fr is a fractional unit and 1fr is for 1 part of the available space. The following are a few examples of the fr unit at work. The grid items in these examples are placed onto the grid with grid areas.

Which CSS rule will you use to place a grid item into the grid so that it starts from the second column and second row and spans three columns and two rows?

Auto-placement by column Using the property grid-auto-flow with a value of column . In this case grid will add items in rows that you have defined using grid-template-rows . When it fills up a column it will move onto the next explicit column, or create a new column track in the implicit grid.

How do I set grid column size?

You can specify the width of a column by using a keyword (like auto ) or a length (like 10px ). The number of columns is determined by the number of values defined in the space-separated list.

Which CSS property must be used to set up a grid container?

An HTML element becomes a grid container when its display property is set to grid or inline-grid .


1 Answers

There is an alternative syntax for grid-row and grid-column that sets a span:

.grid {
    display: grid;
    border: solid 1px black;
    grid-auto-rows: 30px;
    grid-auto-columns: repeat (5, 50px);
    grid-gap: 5px;
}

.grid div {
    background-color: lightgreen;
}

.wide {
    grid-column: span 3;
}

.long {
    grid-row: span 3;
}
<div class="grid">
<div></div>
<div class="long">LONG</div>
<div class="wide">WIDE</div>
</div>
like image 146
vals Avatar answered Oct 18 '22 13:10

vals