CSS grid while keeping aspect ratio - css

Im trying to create css grid while keeping the same height of columns and I want to keep 16:9 ratio for first item. I also want to fit these images in but it seems they are not fitting by few pixels.
Is there any way to do that with grid? Or will I have to do it with flexbox?
.container {
width: 480px;
max-width: 480px;
height: 480px;
max-height: 480px;
}
.grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: auto;
grid-gap: 0.375rem;
}
.item:nth-child(1) {
aspect-ratio: 16 / 9;
grid-column: span 3;
}
.item:nth-child(2) {
grid-column: span 2;
grid-row: span 2;
}
.item:nth-child(3) {
aspect-ratio: 4 / 3;
grid-column: span 3;
grid-row: auto;
}
.item img {
width: 100%;
height: auto;
object-fit: cover;
}
<div class="container">
<div class="grid">
<div class="item"><img src="https://picsum.photos/1280/720" alt="" srcset="" /></div>
<div class="item"><img src="https://picsum.photos/300/600" alt="" srcset="" /></div>
<div class="item"><img src="https://picsum.photos/480/320" alt="" srcset="" /></div>
</div>
</div>

You can simplify your code like below:
.container {
width: 480px;
height: 480px;
}
.grid {
display: grid;
grid-template-columns: 4fr 3fr; /* adjust this like you want */
grid-gap: 0.375rem;
}
img:nth-child(1) {
aspect-ratio: 16 / 9;
}
img:nth-child(2) {
grid-row: span 2;
/* the below will make sure the image get the height of first column*/
height: 0;
min-height: 100%;
/* */
}
img {
width: 100%;
object-fit: cover;
}
<div class="container">
<div class="grid">
<img src="https://picsum.photos/1280/720" alt="" srcset="">
<img src="https://picsum.photos/300/600" alt="" srcset="">
<img src="https://picsum.photos/480/320" alt="" srcset="">
</div>
</div>

Related

how avoid figcaption wrapping with media query

i'm working now with media query and once i reach the breakpoint of 768px the text goes
all below the images, not following them.
I would like the text to follow the images and to be under each one once it reach the 768px breakpoint.
any suggestion to avoid this problem?
thanks.
img {
width: 100%;
height: 100%;
object-fit: contain;
}
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
margin-top: 200px;
grid-gap: 20px;
justify-content: center;
align-content: center;
}
#media (max-width: 768px) {
/* navbar */
* {
margin: 0;
box-sizing: border-box;
}
.nav-links, .search{
display: none;
}
.logo {
height: 64px;
width: 126px;
filter: invert(100%);
padding: 1px;
}
/* contenuto1 */
.grid {
display: flex;
flex-wrap: wrap;
margin-top: 100px;
grid-gap: 150px;
}
<div class="titolo-grid">
<h2>In evidenza</h2>
</div>
<div class="grid-container">
<div class="grid">
<img src="1w.webp" class="img"/>
<img src="2w.webp" class="img"/>
<img src="3w.webp" class="img"/>
<figcaption class="caption">I riflessi fluidi dell'autunno</figcaption>
<figcaption class="caption">Maglieria</figcaption>
<figcaption class="caption">Scarpe Made In Italy</figcaption>
</div>
</div>
I suggest you do not use a grid unless you know how to manipulate it. I suggest you use display flex because it's easier to manipulate and more simple.
and to put text under each image, you must understand the importance of HTML structure for that. so put each image with its text in a container, then style them according to that.
I used display flex here:
img {
width: 100%;
min-width: 300px;
height: 100%;
object-fit: contain;
}
.grid {
display: flex;
/* grid-template-columns: repeat(3, 1fr); */
margin-top: 200px;
grid-gap: 20px;
justify-content: center;
align-content: center;
flex-direction: row;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#media only screen and (max-width: 768px) {
/* navbar */
* {
margin: 0;
box-sizing: border-box;
}
.nav-links, .search{
display: none;
}
.logo {
height: 64px;
width: 126px;
filter: invert(100%);
padding: 1px;
}
.grid {
flex-direction: column;
}
img {
width: 80%;
min-width: 200px;
}
/* contenuto1 */
/* .grid {
display: flex;
flex-wrap: wrap;
margin-top: 100px;
grid-gap: 150px;
} */
}
<div class="titolo-grid">
<h2>In evidenza</h2>
</div>
<div class="grid-container">
<div class="grid">
<div class="img1 container" >
<img src="1w.webp" class="img"/>
<figcaption class="caption">I riflessi fluidi dell'autunno</figcaption>
</div>
<div class="img2 container" >
<img src="2w.webp" class="img"/>
<figcaption class="caption">Maglieria</figcaption>
</div>
<div class="img3 container" >
<img src="3w.webp" class="img"/>
<figcaption class="caption">Scarpe Made In Italy</figcaption>
</div>
</div>
</div>
Put <img> and <figcaption> inside <figure>:
<figure>
<img src="1w.webp" class="img"/>
<figcaption class="caption">I riflessi fluidi dell'autunno</figcaption>
</figure>
<figure>
<img src="2w.webp" class="img"/>
<figcaption class="caption">Maglieria</figcaption>
</figure>
Try this:
.grid-container {
display: flex;
flex-wrap: wrap;
flex-direction:row;
margin-top: 100px;
}
img {
width: 100%;
object-fit: contain;
}
.grid{
width:31%;
margin:1%;
}
#media (max-width: 768px) {
/* navbar */
* {
margin: 0;
box-sizing: border-box;
}
.nav-links, .search{
display: none;
}
.logo {
height: 64px;
width: 126px;
filter: invert(100%);
padding: 1px;
}
.grid{
width:100%;
}
.caption{
margin:10px auto 25px;
}
}
<div class="titolo-grid">
<h2>In evidenza</h2>
</div>
<div class="grid-container">
<div class="grid">
<img src="1w.webp" class="img"/>
<figcaption class="caption">I riflessi fluidi dell'autunno</figcaption>
</div>
<div class="grid">
<img src="2w.webp" class="img"/>
<figcaption class="caption">Maglieria</figcaption>
</div>
<div class="grid">
<img src="3w.webp" class="img"/>
<figcaption class="caption">Scarpe Made In Italy</figcaption>
</div>
</div>
you need to put img and figcaption inside the grid element and use Flex instead of Grid some codes:

How to create layout with grid

I want to create layout where the last element is centered. I should do with grids, but cant center the last element
Desired view
.box {
min-width: 50px;
min-height:50px;
background: red;
margin: 5px;
}
.container {
width: 200px;
display: grid;
grid-template-columns: auto auto auto;
}
.box7 {
grid-column-start: 2;
grid-row-start: 3;
grid-column-end: 4;
}
<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 box7'>7</div>
<div>
Desired view
.box {
min-height: 50px;
background: red;
margin: 5px;
grid-column: span 2;
}
.container {
width: 200px;
display: grid;
grid-template-columns: repeat(6, 1fr);
}
.box7 {
grid-column-start: 2;
grid-column-end: 6;
}
<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 box7'>7</div>
<div>
You can also find multiple ways on this discussion
I hope this approach of mine will help you:
.box {
min-width: 50px;
min-height:50px;
background: red;
margin: 5px;
}
.container {
width: 200px;
display: grid;
grid-template-rows: repeat(3, [row] auto );
grid-template-columns: repeat(3, [col] auto) ;
}
.subgrid {
grid-column: col / span 3;
grid-row: row 3;
}
.box7
{
width:66%;
margin-left:auto;
margin-right:auto;
}
<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="subgrid">
<div class='box box7'>7</div>
</div>
<div>

is there a way to make images one size using css?

the issue is that i want the images to be the same size in the container but what i get is the container growing or shrinking to fit the image or the image stretching out of the container.
<div class="grid-container">
<article class="card">
<div class="content">
<h4>Title</h4>
<p>something about the website</p>
</div>
<div class="img-box">
<a href="#"><img src="./img/work-1.jpg" alt="">
</a>
</div>
</div>
.grid-container {
display: grid;
}
.card {
display: grid;
grid-template-columns: repeat(2, 1fr);
background-color: aqua;
margin: 1rem 0;
padding: 0.5rem;
align-items: start;
justify-items: start;
}
.card img {
display: block;
}
.img {
max-width: 100%;
}
this might help you
img{
height: auto;
width: 100%;
}

Container with grid layout and grid area

Hello I'm trying to make a container grid with grid areas to achieve this goal:
But I am having difficulties in defining the areas of the father and the children.
.grid_layout {
display: grid;
grid-template-areas:
"a b c"
"a d e";
grid-gap: 6px;
grid-template-columns: 1fr fr 1fr;
height: 100%;
}
<div className="grid_layout">
<div className="a" />
<div className="b" />
<div className="c" />
</div>
You don't need template areas to accomplish this...but you do need six columns.
.grid_layout {
display: grid;
grid-gap: 6px;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: auto;
width: 80%;
margin: 1em auto;
}
.item {
display: flex;
justify-content: center;
align-items: center;
background: lightgreen;
border: 1px solid green;
padding: 1em;
}
.item:nth-child(1),
.item:nth-child(2),
.item:nth-child(3) {
grid-column: span 2;
}
<div class="grid_layout">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>
if you want to use areas , you still need to relay on 6 columns as stated in the comments.
.grid_layout {
display: grid;
grid-template-areas:
"a a b b c c "
"d e f g h i";
/*grid-template-columns:repeat(6,1fr); if you need this */
grid-gap: 6px;
height: 100%;/* 100% of what here ? parent has no height set*/
grid-auto-flow:row dense
}
img {width:100%;/* of the grid cell defined */}
img:nth-child(1) {
grid-area:a ;
}
img:nth-child(2) {
grid-area:b;
}
img:nth-child(3) {
grid-area:c;
}
img:nth-child(4) {
grid-area:d;
}
img:nth-child(5) {
grid-area:e;
}
img:nth-child(6) {
grid-area:f;
}
img:nth-child(7) {
grid-area:g;
}
img:nth-child(8) {
grid-area:h;
}
img:nth-child(9) {
grid-area:i;
}
<div class="grid_layout">
<img src="http://dummyimage.com/300x150/456">
<img src="http://dummyimage.com/300x150/789">
<img src="http://dummyimage.com/300x150/029">
<img src="http://dummyimage.com/300x150/fa0">
<img src="http://dummyimage.com/300x150/bad">
<img src="http://dummyimage.com/300x150/g0d">
<img src="http://dummyimage.com/300x150/f00">
<img src="http://dummyimage.com/300x150/555">
<img src="http://dummyimage.com/300x150/0af">
</div>
For a repeating pattern, grid-template-areas is not at best, but grid-template-columns would be better helped with grid-auto-flow:row dense; while telling text to span 2columns.
possible example :
.grid_layout {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-gap: 6px;
height: 100%;
/* 100% of what here ? parent has no height set*/
grid-auto-flow: row dense;
}
p {
border: solid;
margin: 0;
}
img {
width: 100%;
}
.a {
grid-column: 1 / span 2;
text-align: center;
}
.b {
grid-column: 3 / span 2;
text-align: center;
}
.c {
grid-column: 5 / span 2;
text-align: center;
}
div {
counter-reset: ps
}
p:before {
counter-increment: ps;
content: 'N°'counter(ps)' - ';
}
<div class="grid_layout">
<img class="a" src="http://dummyimage.com/300x150/456">
<p class="a">some text</p>
<img class="b" src="http://dummyimage.com/300x150/789">
<p class="b">some text</p>
<img class="c" src="http://dummyimage.com/300x150/029">
<p class="c">some text</p>
<img src="http://dummyimage.com/300x150/fa0">
<img src="http://dummyimage.com/300x150/bad">
<p class="a">some text</p>
<img src="http://dummyimage.com/300x150/g0d">
<img src="http://dummyimage.com/300x150/f00">
<p class="b">some text</p>
<img src="http://dummyimage.com/300x150/555">
<img src="http://dummyimage.com/300x150/0af">
<p class="c">some text</p>
</div>

Four items in two columns

I have this HTML
<div class='wrapper'>
<div class='item-1'>One</div>
<div class='item-2'>Two</div>
<div class='item-3'>Three</div>
<div class='item-4'>Four</div>
</div>
In CSS, is it possible for me to create a two column grid where item-1 and item-2 are in the first column, and item-3 and item-4 are in the second?
The heights of the divs are variable, so this is not strict 2x2 grid.
Basically, I'd like it to look like the example below, but I do not have the luxury of wrapping my items.
THANKS!
.wrapper {
display:grid;
grid-template-columns: 1fr 1fr;
}
<div class='wrapper'>
<div class='wrapper-1'>
<div class='item-1' style='height:100px;background-color:red;'>One</div>
<div class='item-2' style='height:80px;background-color:blue;'>Two</div>
</div>
<div class='wrapper-2'>
<div class='item-3' style='height:40px;background-color:orange;'>Three</div>
<div class='item-4' style='height:40px;background-color:green;'>Four</div>
</div>
</div>
The CSS for the wrapper is correct. The only thing you need to specify is from which line of the grid should each item start and end.
.item-1,
.item-2{
grid-column: 1/2;
width: 100%;
}
.item-3,
.item-4{
grid-column: 3/4;
width: 100%;
}
This should suit your needs. Note that the wrapper needs to have an explicit height in order for the columns to wrap, otherwise it's going to endlessly expand.
.wrapper {
display: flex;
flex-flow: column wrap;
height: 200px;
}
.item {
width: 50%;
}
.i-1 {
height: 100px;
background-color: green;
}
.i-2 {
height: 80px;
background-color: red;
}
.i-3 {
height: 40px;
background-color: blue;
}
.i-4 {
height: 40px;
background-color: yellow;
}
<div class='wrapper'>
<div class='item i-1'>One</div>
<div class='item i-2'>Two</div>
<div class='item i-3'>Three</div>
<div class='item i-4'>Four</div>
</div>

Resources