How to set in CSS in grid this disposition in picture? - html

.grid-container {
display: grid;
grid-gap: 10px;
grid-template-columns: 2fr 1fr;
grid-template-rows: 1fr 1fr;
}
.grid-item {
background: blue;
height: 100px;
}
.grid-item .item1 {
grid-row-start: 1;
grid-row-end: 3;
}
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item item1"> </div>
<div class="grid-item"> </div>
</div>
I have this, but it's not what I want
What I don't want
I want this, see this following picture :
What I want

Remove the height from the grid-items and set the rows to be 100px.
Then tell the "tall" item to be in column 2
.grid-container {
display: grid;
gap: 10px;
grid-template-columns: 2fr 1fr;
grid-auto-rows: 100px;
}
.grid-item {
background: blue;
}
.grid-item.item1 {
grid-column:2;
grid-row: 1 / span 2;
}
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item item1"> </div>
<div class="grid-item"> </div>
</div>

At first, You have a typo .grid-item .item1 , .item1 is not a child of .grid-item . You need to remove that space in between classnames : .grid-item.item1.
beside you have also to reset height for .item1 so it can grow the entire rows it spans, and if you also set in which columns it should stand, it avoids to see it elsewhere.
To help you debug your css, give different background-color to your items to see exactly where they stand.
possible CSS fix
.grid-container {
display: grid;
grid-gap: 10px;
grid-template-columns: 2fr 1fr;
grid-template-rows: 1fr 1fr;
}
.grid-item {
background: blue;
height: 100px;
}
.grid-item.item1 {
grid-row-start:1;
grid-row-end:3;
grid-column:2;
height:auto;
background:red;
}
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item item1"> </div>
<div class="grid-item"> </div>
</div>

CSS Grid Layout excels at dividing a page into major regions or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives.
So you setting height of grid-item is wrong, because it defeats the whole purpose of grid, which is to assign area to grid childrens based upon the layout divided by grid-lines.
.grid-container {
display: grid;
grid-gap: 10px;
grid-template-columns: 2fr 1fr;
grid-template-rows: 1fr 1fr;
height:200px;
}
.grid-item {
background: blue;
}
.item1 {
grid-row: 1/3;
grid-column: 2/3;
}
<div class="grid-container">
<div class="grid-item"> </div>
<div class="grid-item item1"> </div>
<div class="grid-item"> </div>
</div>

Related

How to make second column be the largest but also able to shrink?

I am new to CSS grid and trying to implement the second row only in the below picture.
I've tried to create six sections but have the second section spread out longer. For example I've tried:
grid-column: 2 / span 5;
But it seems to push the last four section to the next line cause it to wrap which I do not want.
my unsuccessful code:
.container {
border: solid black 3px;
height: 100px;
display: grid;
grid-template-columns: repeat(6, 1fr);
}
.item {
border: solid skyblue 1px;
}
.item:nth-of-type(2) {
/* grid-column: 2 / span 5; */
}
<div class="container">
<div class="item"></div>
<div class="item">Totals</div>
<div class="item">6000</div>
<div class="item">-</div>
<div class="item">194</div>
<div class="item">12.5%</div>
</div>
Try auto on the columns, with 1fr on the flexible one.
.container {
border: solid black 3px;
height: 100px;
display: grid;
grid-template-columns: minmax(100px, auto) 1fr repeat(4, minmax(100px, auto));
}
.item {
border: solid skyblue 1px;
}
<div class="container">
<div class="item"></div>
<div class="item">Totals</div>
<div class="item">6000</div>
<div class="item">-</div>
<div class="item">194</div>
<div class="item">12.5%</div>
</div>
jsFiddle demo
Try adding grid-auto-flow: column; to your .container and change grid-column: 2 / span 5; to grid-column: 2 / span 3;

How to properly css-grid this alignment solution? (individual row height, but maintaining specific order on min screens)

The code snippet below creates the desired behaviour for my components.
I want each row to have an individual height, both elements in that row to have the same height, and for small screens, only 1 column, and all "A" content should come first, then all "B" content.
It is just that my solution feels wrong to me. I feel like I am missing grid fundamentels on how to achieve this.
To be honest, I expected that giving A grid-column: 1 and B grid-column: 2 should have worked, but it did not.
FYI: the inline style height is just for simplification. In reality, I do not know the height. and the code looks something like this:
<Grid>
{CME puts all A here}
{CME puts all B here}
</Grid>
.A {
background-color: dodgerblue;
grid-column: 1;
}
.B {
/* grid-column: 2; */
background-color: red;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-flow: dense;
column-gap: 2%;
row-gap: 50px;
}
#media screen and (max-width: 600px) {
.grid {
grid-template-columns: 1fr;
}
}
<div class="grid">
<div class="A">A1</div>
<div class="A" style="height: 150px">A2</div>
<div class="A" style="height: 50px">A3</div>
<div class="B" style="height: 200px">B1</div>
<div class="B">B2</div>
<div class="B">B3</div>
</div>
You can be explicit about which column you want B to go into.
This snippet ensures it's in column 2 in the wide version and in column 1 in the narrow one.
.A {
background-color: dodgerblue;
grid-column: 1;
}
.B {
grid-column: 2;
background-color: red;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-flow: dense;
column-gap: 2%;
row-gap: 50px;
}
#media screen and (max-width: 600px) {
.grid {
grid-template-columns: 1fr;
}
.B {
grid-column: 1;
}
}
<div class="grid">
<div class="A">A1</div>
<div class="A" style="height: 150px">A2</div>
<div class="A" style="height: 50px">A3</div>
<div class="B" style="height: 200px">B1</div>
<div class="B">B2</div>
<div class="B">B3</div>
</div>

How to align grid items vertically using display:grid and fill all vertical empty space? [duplicate]

This question already has answers here:
How can I make a div span multiple rows and columns in a grid?
(3 answers)
Closed 1 year ago.
I use display:grid not flex to display some divs.
Here's what my page looks like:
And here is how I would like it to look:
And here's the code:
.container {
display: grid;
grid-template-columns: 1.5fr 2fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-auto-columns: 1fr;
grid-auto-rows: 1fr;
gap: 40px 40px;
grid-auto-flow: row dense;
justify-items: stretch;
align-items: stretch;
grid-template-areas: "left1 center-div right1" "left1 center-div right2" "left2 center-div right3";
width: 100%;
height: 100%;
}
.left1 {
grid-area: left1;
}
.left2 {
grid-area: left2;
}
.center-div {
grid-area: center-div;
}
.right1 {
grid-area: right1;
}
.right2 {
grid-area: right2;
}
.right3 {
grid-area: right3;
}
<div class="container owl-carousel owl-theme">
<div class="grid-item left1">
left image 1
</div>
<div class="grid-item left2">
left image 2
</div>
<div class="grid-item center-div">
center image
</div>
<div class="grid-item right1">
right image 1
</div>
<div class="grid-item right2">
right image 2
</div>
<div class="grid-item right3">
right image 3
</div>
</div>
In short, I want to bring the divs closer so that they appear as they are in the second photo
Using CSS Grid
Following demo shows how you can achieve the desired layout using just the grid layout.
Its a 6 x 3 grid where items on the left span 3 rows each and the items on the right span 2 rows each. The item in the center spans all 6 rows.
Each grid items is adjusted in its place using the grid-row and grid-column properties.
The trick to achieving this layout is having more rows than columns.
.container {
display: grid;
grid-template-columns: 1.5fr 2fr 1fr;
grid-template-rows: repeat(6, 1fr);
grid-gap: 30px;
height: 100vh;
}
.grid-item {
background: #eee;
width: 100%;
}
.center {
grid-row: 1 / span 6;
grid-column: 2 / span 1;
}
.left1 {
grid-row: 1 / span 3;
grid-column: 1 / span 1;
}
.left2 {
grid-row: 4 / span 3;
grid-column: 1 / span 1;
}
.right1,
.right2,
.right3 {
grid-column: 3 / span 1;
}
.right1 { grid-row: 1 / span 2; }
.right2 { grid-row: 3 / span 2; }
.right3 { grid-row: 5 / span 2; }
<div class="container">
<div class="grid-item left1">left1</div>
<div class="grid-item left2">left2</div>
<div class="grid-item center">center</div>
<div class="grid-item right1">right1</div>
<div class="grid-item right2">right2</div>
<div class="grid-item right3">right3</div>
</div>
Using CSS Grid along with Flexbox
Another option is to have a 1 x 3 grid and make each grid column a flex container.
You will need to change the structure of the HTML if you use this approach.
:root {
--spacing: 30px;
}
.container {
display: grid;
grid-template-columns: 1.5fr 2fr 1fr;
grid-gap: var(--spacing);
height: 100vh;
}
.grid-item {
display: flex;
}
.grid-item div {
background: #eee;
flex-basis: 100%;
}
.col-1,
.col-3 {
flex-direction: column;
justify-content: space-between;
}
.margin-bottom {
margin-bottom: var(--spacing);
}
<div class="container">
<div class="grid-item col-1">
<div class="margin-bottom">left1</div>
<div>left2</div>
</div>
<div class="grid-item col-2">
<div>center</div>
</div>
<div class="grid-item col-3">
<div class="margin-bottom">right1</div>
<div class="margin-bottom">right2</div>
<div>right3</div>
</div>
</div>

HTML Grid Layout not in accordance to CSS

So I'm trying to set up a grid-formatted website page with pure HTML and CSS, as you will see in my code below.
I'm trying to alternate between two div tags going down the left hand column of Header & inner-placeholder tags.
You will see the HTML layout alignment with the grid-template-area clearly laid out, along with the number of rows as specified by grid-template-rows
So why do I get just a red box at the corner of the screen when it's fairly obvious what I want to have as per the illustration - except for a curved box followed by a straight box, followed by a curved box etc going down the left hand side?
I have tried to change the fr number to accommodate the number of rows on the left hand side.
Thank you.
Illustration
.grid{
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-areas:
"Title Title"
"Header Content"
"inner-placeholder Content"
"Header Content"
"inner-placeholder Content"
"Sidebar Content"
"Footer Footer";
grid-gap: 10px;
}
.Title{
grid-area: Title;
}
.Header{
grid-area: Header;
}
.Sidebar{
grid-area: Sidebar;
}
.Content{
grid-area: Content;
}
.Footer{
grid-area: Footer;
}
.inner-placeholder{
grid-area: inner-placeholder;
}
.grid div:nth-child(even){
background-color: red;
}
.grid div:nth-child(odd){
background-color: green;
}
<div class="grid">
<div class="Title">Title
</div>
<div class="Header">Header
</div>
<div class="inner-placeholder">
</div>
<div class="Header">Header
</div>
<div class="inner-placeholder">
</div>
<div class="Sidebar">Sidebar
</div>
<div class="Content">Content
</div>
<div class="Footer">Footer
</div>
</div>
It seems you misunderstood how grid areas work. If any grid area spans more than 1 row or column, it needs to form a square or a rectangle. Which means they also need to be in one continuous sequence as a 2x2 or 1x3 and so on, in your case you split the Header area and placeholder area between each other, which breaks the grid.
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(7, 1fr);
grid-template-areas:
"Title Title"
"Header Content"
"inner-placeholder Content"
"Header2 Content"
"inner-placeholder2 Content"
"Sidebar Content"
"Footer Footer";
grid-gap: 10px;
}
.Title {
grid-area: Title;
}
.Header {
grid-area: Header;
}
.Header2 {
grid-area: Header2;
}
.Sidebar {
grid-area: Sidebar;
}
.Content {
grid-area: Content;
}
.Footer {
grid-area: Footer;
}
.inner-placeholder {
grid-area: inner-placeholder;
}
.inner-placeholder2 {
grid-area: inner-placeholder2;
}
.grid div:nth-child(even) {
background-color: red;
}
.grid div:nth-child(odd) {
background-color: green;
}
<div class="grid">
<div class="Title">Title</div>
<div class="Header">Header</div>
<div class="inner-placeholder"></div>
<div class="Header2">Header2</div>
<div class="inner-placeholder2"></div>
<div class="Sidebar">Sidebar</div>
<div class="Content">Content</div>
<div class="Footer">Footer</div>
</div>

Grid areas not laying out properly in CSS Grid

I want to make my website using CSS grid system but it seems not to be working. Here is my code:
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq" "about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>
When using the grid-template-areas property, string values must have the same number of columns.
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq" "about-us about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>
You can use a period, or an unbroken line of periods, to represent an empty cell (spec reference).
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq" " ... about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>
From the Grid spec:
7.3. Named Areas: the grid-template-areas
property
All strings must have the same number of columns, or else the declaration is invalid.
If a named grid area spans multiple grid cells, but those cells do not form a single filled-in rectangle, the declaration is invalid.
Non-rectangular or disconnected regions may be permitted in a future version of this module.
Note: As stated in the spec, in addition to an equal number of columns, grid areas must also be rectangular (see this post for more details).
If this:
Is the desired result, then you've only made a minor error.
You've set the grid to be a 2 x 2 square here:
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
But you aren't filling all the space.
grid-template-areas: "logo faq", "about-us";
That line of code is saying "In the top two squares put logo and faq respectively. In the bottom two rows put about-us" and that causes an error. If you want one grid-area to fill the entire space then you need to declare it twice. Thus the above line becomes:
grid-template-areas: "logo faq", "about-us about-us";
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq", "about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>