Display flex with mixed design [duplicate] - html

This question already has answers here:
CSS-only masonry layout
(4 answers)
Flex box masonry in one flex-container
(1 answer)
Closed 3 years ago.
What would be the best way to make the following with flexbox? I would like 2 rows that are equal width columns, however, the last column to be 100% height and fill the rest of the section.
Would it be best to use multiple rows for this?
.row {
display: flex;
flex-wrap: wrap-reverse;
}
.col {display:1;width:30%;background:red;}
.col:nth-of-type(3) {background:blue;}
<div class="row">
<div class="col">
test
</div>
<div class="col">
test
</div>
<div class="col">
test
</div>
<div class="col">
test
</div>
<div class="col">
test
</div>
</div>

Here is a solution using CSS grid layout:
Define the row as grid container using display: grid.
Define the 3-column layout by using grid-template-columns: repeat(3, 1fr)
Define the 2-column layout by using grid-template-rows: repeat(2, 1fr)
Span the third column to all the rows by using grid-row: span 2.
Adjust the gaps between the rows & columns using grid-gap property.
See demo below:
.row {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
grid-template-rows: repeat(2, 1fr); /* 2 equal rows */
grid-gap: 10px; /* gap between the rows & columns */
}
.col {
background: red;
}
.col:nth-of-type(3) {
background: blue;
grid-row: span 2; /* span all the rows */
}
<div class="row">
<div class="col">test</div>
<div class="col">test</div>
<div class="col">test</div>
<div class="col">test</div>
<div class="col">test</div>
</div>

Related

dynamic css grid | height doesn't apply to second row

I want to create a dynamic grid.
this is my current css. What am i missing that the row hight is not applied to the second row?
.container{
display: grid;
grid-template-columns: repeat(auto-fill, 170px);
grid-template-rows: repeat(auto-fill, 300px);
}
html:
<div class="container">
<div>
css
</div>
<div>
html
</div>
</div>

How to make columns into rows in css grid [duplicate]

This question already has answers here:
How to create a CSS Grid Layout box that spans 2 columns and 2 rows?
(3 answers)
Make grid container fill columns not rows
(6 answers)
Closed 2 months ago.
I have a css-grid layout set up as 3 columns. When a certain media-query matches, i want it to instead flow as rows where the first column becomes the first row, covering the full with. Column two and three should become one row and share the space equally. How can i achieve that? (i know how to write a media query, code is for demonstrating only)
.thegrid {
display: grid;
grid-template-columns: 1fr auto auto;
gap: 40px;
}
<div class="thegrid">
<div>
content 1
</div>
<div>
content 2
</div>
<div>
content 3
</div>
</div>
If you keep the columns, and add grid-column: 1/-1 to the first div (this makes sure the div covers the complete grid area). Does that give the desired look?
You must display grid then when your #media screen hits its threshold it will begin to flex. You have to tell which rows you want to combine within your flexed column and how far to extend them. Hopefully this helps and makes sense. good luck!
.thegrid {
display: grid;
grid-template-columns: auto auto auto;
grid-template-rows: 80px 200px;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
#media only screen and (max-width: 600px) {
.thegrid {
display: grid;
grid-template-columns: auto auto auto;
grid-template-rows: 80px 200px;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.row-1 {
grid-column: 1/ span 2;
grid-row: 1;
}
.row-2 {
grid-row: 2;
}
}
<div class="thegrid">
<div class="row-1">
content 1
</div>
<div class="row-2">
content 2
</div>
<div class="row-2">
content 3
</div>
</div>

How can I span a number of grid cells to create an irregular layout, only using one class (for a post feed module)?

I want to style a post feed module (Divi theme) so that the first column is 50% width and the rest of the 7 are 25%, creating a grid like this https://www.screencast.com/t/GNJODhIG3
I have this CSS grid code so far, but I can't find a way of editing it to get the desired layout.
.ds-grid-blog .et_pb_ajax_pagination_container {
display: grid;
grid-template-columns: repeat(5, 18%);
grid-column-gap: 2.5%;
}
Is there some code I could add like "column-one, row-one: 50%;"
With grid, if you intend to divide into n-columns, it is no longer necessary (and is actually an anti-pattern) to calculate the percentages yourself, it is better to use fr units, which divide the free space into fractions. In addition, what you are trying to achieve requires setting a css rule that says "first child of the container should span two rows". An example of a possible solution is shown below:
.container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-column-gap: 2.5%;
grid-row-gap: 2.5%;
}
.container > :first-child {
grid-column: 1 / 3;
}
.box {
background-color: grey;
}
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>

Got some unwanted whitespace in css grid layout

Playing around with display: grid and got some unwanted space. Am I missing some specs in my css?
What I'm after is vertically align the 5 links at the bottom of the purple'ish plane.
Following are the codes I used:
.grid {
display: grid;
}
.-twoColumns {
grid-template-columns: repeat(2, 50%);
}
.-grid-gap {
grid-gap: 1rem;
}
<div class="grid -twoColumns -grid-gap">
<div class="column">
column
</div>
<div class="column">
column
</div>
</div>

How to target a specific column or row in CSS Grid Layout?

Is it possible to select a specific grid column or row with CSS?
For example, say I have a 3 row by 2 column CSS Grid Layout: grid-template-rows: 1fr 1fr 1fr; grid-template-columns: 1fr 1fr;. How would I select all elements from the 2nd column? For example: grid:nth-child(column:2) (just my idea, not valid code).
I have tried nth-child selectors on the div elements, but this does not allow me to specify row or column when the items are automatically placed by the Grid Layout engine.
body {
display: grid;
grid-template-rows: 1fr 1fr 1fr;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
}
.item {
background: #999;
}
<div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p>
</div>
<div class="item">
<p>Right Justify</p>
<p>Element 1 | Element 2</p>
</div>
<div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p>
</div>
<div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p>
</div>
<div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p>
</div>
<div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p>
</div>
<div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p>
</div>
To style an arbitrary row, you could use a wrapper element with its display set to contents. See the code snippet below:
.grid-container {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 2px;
}
.grid-item {
border: 1px solid black;
padding: 5px;
}
.grid-row-wrapper {
display: contents;
}
.grid-row-wrapper > .grid-item {
background: skyblue;
}
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-row-wrapper">
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
<div class="grid-item">10</div>
</div>
<div class="grid-item">11</div>
<div class="grid-item">12</div>
<div class="grid-item">13</div>
<div class="grid-item">14</div>
<div class="grid-item">15</div>
<div class="grid-item">16</div>
<div class="grid-item">17</div>
<div class="grid-item">18</div>
<div class="grid-item">19</div>
<div class="grid-item">20</div>
</div>
EDIT: As with all implementations, you should check to ensure it works in your target environment(s). You can check the compatibility table on MDN or caniuse.com for support for display: contents:
https://developer.mozilla.org/en-US/docs/Web/CSS/display#Browser_compatibility
https://caniuse.com/#search=display%3A%20contents
There are no column or row elements that you can target but if the grid is uniform (same number of cells in each row) you can select cells. Here are some examples.
1. Columns
Last column in a 5-column grid:
.item:nth-child(5n) { /* ... */ }
Fourth (2nd last) column in a 5-column grid:
.item:nth-child(5n-1) { /* ... */ }
First (5th last) column in a 5-column grid:
.item:nth-child(5n-4) { /* ... */ }
2. Rows
First row in a 5-column grid (first five cells):
.item:nth-child(-n+5) { /* ... */ }
Second row in a 5-column grid (cells from 6 to 10):
.item:nth-child(n+6):nth-child(-n+10) { /* ... */ }
Third row in a 5-column grid (cells from 11 to 15):
.item:nth-child(n+11):nth-child(-n+15) { /* ... */ }
Last row in a 5-column grid with 20 cells (cells from 16 onward):
.item:nth-child(n+16) { /* ... */ }
Not possible with CSS.
CSS targets HTML elements, attributes and attribute values.
Grid columns and rows have none of these "hooks".
You'll have to target the grid items directly.
You wrote:
For example, say I have a 3 row by 2 column CSS Grid Layout: grid-template-rows: 1fr 1fr 1fr; grid-template-columns: 1fr 1fr;. How would I select all elements from the 2nd column?
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 10px;
padding: 10px;
height: 50vh;
background-color: gray;
}
grid-item {
background-color: lightgreen;
}
grid-item:nth-child(2n) {
border: 2px dashed red;
}
<grid-container>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
</grid-container>
You can not. You have no such selectors.
But that is strange, because you can easily target row/colum from CSS
#item3 {
background-color: blue;
grid-row: span 2 / 7;
}
This is natural to expect something:
div[style*="display:grid"]:grid-row(3) {
background-color: blue;
}
div[style*="display:grid"]:grid-column(3) {
background-color: green;
}
Do not the reasons that draft for this is not proposed yet
UPD
Seems there are for columns: https://drafts.csswg.org/selectors-4/#the-nth-col-pseudo
UPD
Issue at W3C repo
If you ever want to style a row the same principle applies.
Taking that example from above:
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr;
grid-gap: 10px;
padding: 10px;
height: 50vh;
background-color: gray;
}
grid-item {
background-color: lightgreen;
}
grid-item:nth-child(4n+3),grid-item:nth-child(4n) {
border: 2px dashed red;
}
<grid-container>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
<grid-item></grid-item>
</grid-container>
In the near future we will be able to do it thanks to the Grid-Structural Selectors
The double-association of a cell in a 2D grid (to its row and column) cannot be represented by parentage in a hierarchical markup language. Only one of those associations can be represented hierarchically: the other must be explicitly or implicitly defined in the document language semantics. In both HTML and DocBook, two of the most common hierarchical markup languages, the markup is row-primary (that is, the row associations are represented hierarchically); the columns must be implied. To be able to represent such implied column-based relationships, the column combinator and the :nth-col() and :nth-last-col() pseudo-classes are defined. In a column-primary format, these pseudo-classes match against row associations instead.
The one you need here is :nth-col() that behave the same way as :nth-child()
The :nth-col(An+B) pseudo-class notation represents a cell element belonging to a column that has An+B-1 columns before it ref