I am very new to CSS Grid and am trying to create a 2 X 2 grid. I am able to create the four image divs ( 2 on each row and 2 on each column) but I want the text to appear underneath each one. I have tried to use the grid-template-areas property but all the text just appears under one div all overlapped.
Any help would be appreciated, please.
.image {
width: 526px;
height: 360px;
border: 1px solid #000;
border-radius: 10px;
margin-top: 35px;
margin-bottom: 35px;
}
.grid-container {
display: grid;
grid-template-rows: auto;
grid-template-columns: 1fr 1fr;
column-gap: 10px;
row-gap: 1em;
grid-template-areas: "image image " "text text";
}
.text {
grid-area: text;
}
<div class="banner">
<div class="wrapper">
<div class="grid-container">
<div class="image"></div>
<p class="text">TEXT 1</p>
<div class="image"></div>
<p class="text">
TEXT 2
</p>
<div class="image"></div>
<p class="text">TEXT 3</p>
<div class="image"></div>
<p class="text">TEXT 4</p>
</div>
</div>
<!-- wrapper -->
</div>
<!-- banner -->
You can use nth-child() to correctly place your elements instead of grid-area:
.image {
width: 200px;
height: 150px;
border: 1px solid #000;
border-radius: 10px;
margin-top: 35px;
margin-bottom: 35px;
}
.grid-container {
display: grid;
grid-auto-flow: dense; /* this one is important to fill all the cells */
grid-auto-columns: 1fr;
column-gap: 10px;
}
.image:nth-child(4n + 3) {
grid-column: 2;
}
.text:nth-child(4n + 2) {
grid-column: 1;
}
<div class="banner">
<div class="wrapper">
<div class="grid-container">
<div class="image"></div>
<p class="text">TEXT 1</p>
<div class="image"></div>
<p class="text">TEXT 2</p>
<div class="image"></div>
<p class="text">TEXT 3</p>
<div class="image"></div>
<p class="text">TEXT 4</p>
</div>
</div>
</div>
This will work even if you add more image/text:
.image {
width: 200px;
height: 150px;
border: 1px solid #000;
border-radius: 10px;
margin-top: 35px;
margin-bottom: 35px;
}
.grid-container {
display: grid;
grid-auto-flow: dense; /* this one is important to fill all the cells */
grid-auto-columns: 1fr;
column-gap: 10px;
}
.image:nth-child(4n + 3) {
grid-column: 2;
}
.text:nth-child(4n + 2) {
grid-column: 1;
}
<div class="banner">
<div class="wrapper">
<div class="grid-container">
<div class="image"></div>
<p class="text">TEXT 1</p>
<div class="image"></div>
<p class="text">TEXT 2</p>
<div class="image"></div>
<p class="text">TEXT 3</p>
<div class="image"></div>
<p class="text">TEXT 4</p>
<div class="image"></div>
<p class="text">TEXT 5</p>
<div class="image"></div>
<p class="text">TEXT 6</p>
<div class="image"></div>
<p class="text">TEXT 7</p>
<div class="image"></div>
<p class="text">TEXT 8</p>
</div>
</div>
</div>
Manage placement without template areas using nth-of-type and flow via columns.
.image {
width: 100px;
height: 100px;
border: 1px solid #000;
border-radius: 10px;
margin-top: 35px;
margin-bottom: 35px;
}
.grid-container {
display: grid;
grid-template-rows: auto;
grid-template-columns: 1fr 1fr;
column-gap: 10px;
row-gap: 1em;
grid-auto-flow: column;
}
p:nth-of-type(odd),
.image:nth-of-type(odd) {
grid-column: 1;
}
p:nth-of-type(even),
.image:nth-of-type(even) {
grid-column: 2;
}
<div class="banner">
<div class="wrapper">
<div class="grid-container">
<div class="image"></div>
<p class="text">TEXT 1</p>
<div class="image"></div>
<p class="text">
TEXT 2
</p>
<div class="image"></div>
<p class="text">TEXT 3</p>
<div class="image"></div>
<p class="text">TEXT 4</p>
</div>
</div>
<!-- wrapper -->
</div>
<!-- banner -->
I removed the grid-area configuration from CSS code. Also, I have rearranged the sequence of your image and text HTML code. I hope this is what you expected.
.image {
width: 526px;
height: 360px;
border: 1px solid #000;
border-radius: 10px;
margin-top: 35px;
margin-bottom: 35px;
/*grid-area: image;*/
}
.grid-container {
display: grid;
grid-template-rows: auto;
grid-template-columns: 1fr 1fr;
column-gap: 10px;
row-gap: 1em;
/* grid-template-areas: "image image" "text text"; */
}
.text {
text-align: center; /* Aligning the text to center */
/* grid-area: text;*/
}
<div class="banner">
<div class="wrapper">
<div class="grid-container">
<div class="image"></div>
<div class="image"></div>
<p class="text">TEXT 1</p>
<p class="text">TEXT 2</p>
<div class="image"></div>
<div class="image"></div>
<p class="text">TEXT 3</p>
<p class="text">TEXT 4</p>
</div>
</div>
<!-- wrapper -->
</div>
<!-- banner -->
Related
I am trying to get 3 columns in a row and be the same height.
I am using the HTML/CSS below. I can't figure out why the boxes arent the same height.
.container {
text-align: center;
display: block;
width: 100%;
}
.grid {
width: 100%;
height: 100%;
}
.row {
display: flex;
height: 100%;
flex-direction: row;
}
.col {
background: #444;
padding: 2em;
flex: 1;
height: 100%;
border: 1px solid blue;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>
How can I make the boxes all the same height. thanks
Simply, remove the height: 100% from .col.
flex: 1; will do the job.
.row {
display: flex;
}
.col {
flex: 1;
background: #444;
padding: 2em;
border: 1px solid blue;
text-align: center;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>
I've setup a grid-template but a problem has arisen in the front-end template layer. Basically there comes a time when divs with assigned grid-areas conditionally render onto the page. Being that I have a grid-row-gap declaration in place, it maintains gaps of grid template areas that aren't present on the page. I was wondering if there was a way to collapse these gaps when this sort of thing occurs.
.grid {
display: grid;
grid-template-areas:
'a b'
'a c'
'a d'
'a e'
'a f';
grid-gap: 25px;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border: 1px solid #000;
}
.a {
grid-area: a;
}
.b {
grid-area: b;
}
.c {
grid-area: c;
}
.d {
grid-area: d;
}
.e {
grid-area: e;
}
.f {
grid-area: f;
}
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<!-- <div class="c cell">C</div> -->
<!-- <div class="d cell">D</div> -->
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
You can think it otherwise, use template and span instead area , and margin instead grid-gap.
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
margin: 1em;
background: #bee;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border: 1px solid #000;
grid-column: 2;
}
.a {
grid-column: 1;
grid-row: 1 / span 10;
/* here what you think is enough */
}
.cell:nth-child(1)~.cell {
margin-left: 24px;
}
.cell:nth-child(2)~.cell {
margin-top: 24px;
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="c cell">C</div>
<div class="d cell">D</div>
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="d cell">D</div>
<div class="f cell">F</div>
</div>
Don't use template-areas and reply on auto placement:
.grid {
display: grid;
grid-template-columns: 1fr 1fr; /* 2 columns */
column-gap: 25px;
margin:5px;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border: 1px solid #000;
}
.grid .a {
grid-row: span 5; /* take 5 rows */
}
/* to replace vertical gap, don't apply to "a" and last-child */
.cell:not(.a):not(:last-child) {
margin-bottom:25px;
}
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<!-- <div class="c cell">C</div> -->
<!-- <div class="d cell">D</div> -->
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="c cell">C</div>
<!-- <div class="d cell">D</div> -->
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="c cell">C</div>
</div>
I am trying to display images in a single line horizontal with scroll but I can't figure how to I would appreciate it if anyone could help me.
CSS
.img {
max-width: 400px;
position: relative;
}
.animal {
float: left;
background-color: #fff;
}
.image-grid{
flex-wrap: wrap;
width: 1400px;
margin: 0 auto;
}
.pet-photo {
padding: 10px;
display: block;
box-sizing: border-box;
flex-basis: 100%;
float: left;
padding: 20px;
width: 200px;
height: 300px;
display: block;
}
js
<div class ="image-grid">
<div class="animal">
<img class="pet-photo " src="${pet.photo}" >
<div class="olay" onclick="location.href='${pet.href}';" style="cursor: pointer;">
<div id="mydiv">
<h2 class="pet-name">${pet.name}
<h1 class="species">${pet.species}
</div>
<div></div></div>
</div>
</div>
That's all thank you for reading :) really appreciate it.
solution is display: flex; with flex-wrap: nowrap;
.container-items {
overflow: auto;
width: 100%;
}
.items {
display: flex;
flex-wrap: nowrap;
}
.items .item {
min-width: 150px;
text-align: center;
padding: 5px;
}
.items .item h2,
.items .item h3 {
margin: 0;
}
.items .item img {
width: 100%;
}
<div class ="container-items">
<div class ="items">
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3>here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
</div>
</div>
from the example I have three columns, when resized at a certain view-port it will wrap onto the next row, how can I target that individual div and make it fill the available width?
.boxes {
color: white;
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
.box {
background-color: blue;
padding: 10px;
}
<section class = "boxes">
<div class="box">
<p>Box 1</p>
</div>
<div class="box">
<p>Box 2</p>
</div>
<div class="box">
<p>Box 3</p>
</div>
</section>
Codepen:
CodePen
You need flexbox for this:
body {
color: white;
}
.boxes {
display: flex;
flex-wrap: wrap;
}
.box {
background-color: blue;
padding: 10px;
min-width: 250px;
box-sizing:border-box;
margin: 10px;
flex-grow: 1;
}
<section class="boxes">
<div class="box">
<p>Box 1</p>
</div>
<div class="box">
<p>Box 2</p>
</div>
<div class="box">
<p>Box 3</p>
</div>
</section>
Limit columns by max-width
you can stay with grid, but it look good only if you stay with 2 columns.
for more columns its more complicated and you can define what you want, to use grid or to use flex for convenient way.
html {
box-sizing: border-box;
}
body {
background-color: white;
color: white;
}
.boxes {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
max-width: 700px;
margin: 0 auto;
text-align: center;
}
.box {
background-color: blue;
padding: 10px;
}
.box:last-child:nth-child(odd) {
grid-column: 1/3;
}
<!DOCTYPE html>
<html>
<head>
<title>Column Resize</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<section class="boxes">
<div class="box">
<p>Box 1</p>
</div>
<div class="box">
<p>Box 2</p>
</div>
<div class="box">
<p>Box 3</p>
</div>
</section>
</body>
</html>
I have a simple flexbox layout like this:
html, body {
margin:0;
padding:0;
background:grey;
}
.container {
display:flex;
flex-direction:row;
}
.image1, .image2, .image3, .image4, .image5, .image6 {
padding:10px;
}
img {
max-width:100%;
}
<div class="container">
<div class="image1">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image2">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image3">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
<div class="container">
<div class="image4">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
<div class="container">
<div class="image5">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image6">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
I am currently doing it with 3 seperate flex containers, I am trying to combine everything in to 1.
Am I able to do this with flexbox or would CSS grid be more appropriate?
Am I able to do this with flexbox
Yes, using flex-wrap property and wrap value, which will force items to wrap onto multiple lines.
html, body {
margin: 0;
background: grey;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.image {
padding: 10px;
box-sizing: border-box;
}
img {
width: 100%;
}
.third {
width: 33.333%;
}
.half {
width: 50%;
}
.full {
width: 100%;
}
<div class="container">
<div class="image third">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image third">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image third">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image full">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image half">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image half">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
You could make it even shorter, using flex instead of width.
html, body {
margin: 0;
background: grey;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.image {
flex: 1 0 0;
padding: 10px;
box-sizing: border-box;
}
.image.full {
flex: none;
width: 100%;
}
img {
width: 100%;
}
<div class="container">
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image full">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
The layout (with a single container) is relatively easy and simple using CSS Grid.
Use Grid's line-based placement techniques to position grid items and create grid areas.
.container {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-gap: 10px;
}
.image1, .image2, .image3 {
grid-column: span 2;
}
.image4 {
grid-column: 1 / -1;
justify-self: center; /* optional */
}
.image5, .image6 {
grid-column: span 3;
}
img {
max-width: 100%;
}
body {
margin: 0;
padding: 10px;
background: grey;
}
<div class="container">
<div class="image1">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image2">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image3">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image4">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image5">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
<div class="image6">
<img src="https://dummyimage.com/1000x400/000/fff">
</div>
</div>
jsFiddle demo