HTML CSS Image Gallery Not Making Squares with Auto or Stretch - html

I want to create a 2 column, 3 row image square image gallery.
For some reason when writing code, the height of the boxes are Not filling up grid. How do I make the height of images become square with width?
Code , CSS and HTML below. Images should be touching edge to edge and would like to refrain from naming Pixel size if possible. Isn't there a stretch property or something? Trying to get that to work,
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 0;
padding: 0px;
}
img {
width: 100%;
height: auto;
padding: 0px;
}
<div class="grid-container">
<img src="https://www.woodlandtrust.org.uk/media/100078482/Sycamore01.jpg?cb=-11897985&preset=gallery-tab-main-image">
<img src="https://statesymbolsusa.org/sites/statesymbolsusa.org/files/styles/symbol_thumbnail__medium/public/primary-images/Applesfreshpicked.jpg?itok=YmYkBfY7">
<img src="https://openbookphilly.com/wp-content/uploads/2016/11/bookstack.png">
<img src="https://media.wired.com/photos/5b86fce8900cb57bbfd1e7ee/master/w_582,c_limit/Jaguar_I-PACE_S_Indus-Silver_065.jpg">
<img src="https://atlantis.nyc3.digitaloceanspaces.com/styled/1bec9ec74aac91e70b3ef91fee1fc0f9">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3DXqVk9AhGSx2PIYoUepA1UfZFnGt_kY6iJTq3hb10ZLGhFwPQg">
</div>

If you want to fill up the box height. You should use align-items "stretch" property to the grid container.
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);.
grid-template-columns: repeat(3, 1fr);
grid-gap: 0;
padding: 0px;
align-items: stretch;
}
Demo Code

This is your solution and when you resize your window then images will automatically resize.
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 0;
padding: 0px;
align-items: stretch; /* Default. Items are stretched to fit the container */
}
img {
width: 100%;
height:auto;
padding: 0px;
}
<div class="grid-container">
<img src="https://www.woodlandtrust.org.uk/media/100078482/Sycamore01.jpg?cb=-11897985&preset=gallery-tab-main-image">
<img src="https://statesymbolsusa.org/sites/statesymbolsusa.org/files/styles/symbol_thumbnail__medium/public/primary-images/Applesfreshpicked.jpg?itok=YmYkBfY7">
<img src="https://openbookphilly.com/wp-content/uploads/2016/11/bookstack.png">
<img src="https://media.wired.com/photos/5b86fce8900cb57bbfd1e7ee/master/w_582,c_limit/Jaguar_I-PACE_S_Indus-Silver_065.jpg">
<img src="https://atlantis.nyc3.digitaloceanspaces.com/styled/1bec9ec74aac91e70b3ef91fee1fc0f9">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3DXqVk9AhGSx2PIYoUepA1UfZFnGt_kY6iJTq3hb10ZLGhFwPQg">
</div>
This is your source link your source code

Try Following code.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
img {
width: 100%;
height: 140px;
}
<div class="grid-container">
<img src="https://www.woodlandtrust.org.uk/media/100078482/Sycamore01.jpg?cb=-11897985&preset=gallery-tab-main-image">
<img src="https://statesymbolsusa.org/sites/statesymbolsusa.org/files/styles/symbol_thumbnail__medium/public/primary-images/Applesfreshpicked.jpg?itok=YmYkBfY7">
<img src="https://openbookphilly.com/wp-content/uploads/2016/11/bookstack.png">
<img src="https://media.wired.com/photos/5b86fce8900cb57bbfd1e7ee/master/w_582,c_limit/Jaguar_I-PACE_S_Indus-Silver_065.jpg">
<img src="https://atlantis.nyc3.digitaloceanspaces.com/styled/1bec9ec74aac91e70b3ef91fee1fc0f9">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3DXqVk9AhGSx2PIYoUepA1UfZFnGt_kY6iJTq3hb10ZLGhFwPQg">
</div>
Also make sure to use same size images in case you want to use height:auto

Related

How to make images respect grid row size constraints?

I'm trying to create a layout where a section with images takes up a certain fraction of the container's height. I tried to use grid-template-rows with fr units, but the image always prefers to stay in its original size and doesn't respect the row sizes of the parent grid. Also, I've tried to follow a few tips online and added object-fit: contain to let the image scale as needed and wrapped the image in a div, neither of which helped.
Simplified snippet:
.container {
display: grid;
grid-template-columns: repeat(1, minmax(0, 1fr));
grid-template-rows: 3fr 2fr;
height: 100vh;
}
img {
object-fit: contain;
}
.hello {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
}
<div class="container">
<div>
<img
src="http://via.placeholder.com/600x300"
/>
</div>
<div class="hello">
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
</div>
</div>
What I'm expecting to see is two rows in a 3/2 ratio, instead, the image takes up as much space as it needs, not respecting the ratio at all.
Can I somehow make the grid to force the row sizes or the image to allow scaling?
The problem seems to be that the element containing the image is picking up a height from the content (ie the image).
This snippet sets the width and height of the img element to 100% of its parent div but it positions it absolute so its actual dimensions have no effect on its parent.
.container {
display: grid;
grid-template-columns: repeat(1, minmax(0, 1fr));
grid-template-rows: 3fr 2fr;
height: 100vh;
}
.container>div:first-child {
position: relative;
}
img {
object-fit: contain;
position: absolute;
height: 100%;
width: 100%;
}
.hello {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
}
<div class="container">
<div>
<img src="http://via.placeholder.com/600x300" />
</div>
<div class="hello">
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
</div>
</div>

CSS Grid Styling

I am trying to understand how CSS grids work. I've tried to make an example of a store item as practice, but I am at a loss.
Here's my how my CSS currently looks. Cut off at the top, weird spacing, and the right side is not coming together at all.
How's how it would ideally look
Here is my current CSS, I hope someone can help explain where I am misunderstanding the use of
CSS grids.
.store-currency {
height: 3vh;
}
.item {
display: flex;
align-items: center;
grid-row: 1 / span 2;
}
.currency {
display: flex;
align-items: center;
}
#num-bought-item0 {
display: flex;
align-items: center;
justify-content: right;
margin-right: 10px;
grid-column: 1 / span 2;
}
.store-item {
height: 15vh;
width: 100%;
display: grid;
grid-template-columns: 2fr;
grid-template-rows: 1fr 1fr;
font-size: 24px;
color: white;
border: 5px white solid;
justify-content: left;
align-items: center;
}
.store-item img {
margin: 10px;
height: 8vh;
}
.store-container {
display: flex;
height: 100%;
width: 30vw;
z-index: 0;
background-color: saddlebrown;
left: 0;
position: absolute;
text-align: center;
}
HTML:
<div class="store-container">
<div class="store-item" id="item0">
<div class ="item">
<img src="dumbell.png" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item0">
<span>Owned</span>
<span id="count-item0">0</span>
</div>
<div class="currency">
<img class="store-currency" src="coin.png" alt="">
<span>100000</span>
</div>
</div>
you did the first steps.
To get started you have to define a container element as a grid with display: grid, set the column and row sizes with grid-template-columns and grid-template-rows, and then place its child elements into the grid with grid-column and grid-row.
.store-container {
display: grid | inline-grid;
}
grid – generates a block-level grid
inline-grid – generates an inline-level grid
With grid-template-columns you can define how many columns will appear in your layout.
P.S Fr unit is a fractional unit and 1fr is for 1 part of the available space. In this example each column would take ~ 25% from the available space.
.container {
grid-template-columns: 1fr 1fr 1fr 1fr;
}
For your task, you can use grid-template-areas feature.
The grid-template-areas CSS property specifies named grid areas,
establishing the cells in the grid and assigning them names.
For example:
.item-a {
grid-area: header;
}
.item-b {
grid-area: main;
}
.item-c {
grid-area: sidebar;
}
.item-d {
grid-area: footer;
}
.container {
display: grid;
grid-template-columns: 50px 50px 50px 50px;
grid-template-rows: auto;
grid-template-areas:
"header header header header"
"main main . sidebar"
"footer footer footer footer";
}
This will generates something like that in modern browsers:
If you need more examples, take a look here:
https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas
https://css-tricks.com/snippets/css/complete-guide-grid/
Some of the examples are taken from the second site.
It looks like you are mixing flex and grid properties. grid-row and grid-column are only avalaible for a grid display (2D), not a flex display (1D).
You can try to play around with flex (worse choice since it is drawing a 1D layout) , you can use grid , which is made for this kind of layout.
Here a couple example with flex and grid
/* GRID make it simple*/
.grid {display:grid;}
#num-bought-item2 {grid-row:1/3;grid-column:2;}
#num-bought-item2 {display:grid;margin:auto;text-align:center}
/* layout done */
/* some reset for the demo*/
*{box-sizing:border-box;}
.store-container {display:grid;justify-content:center;}
.store-item {border:solid;}
.store-item>div {padding:0.5em;}
img{vertical-align:middle;}
[src="https://dummyimage.com/25/ff0"]{border-radius:50%}
big{color:darkgreen;background:lightyellow;}
/* FLEX make it a mess */
.flex {display:flex}
.column {flex-flow:column wrap;height:120px;}/* here an height is to be set so it wraps*/
/* since it is not made for this, we need to mess around */
.flex #num-bought-item1{order:2}/* reorder item */
.flex .item {height:0;min-height:60%;}/* hide it, then show it */
.flex .currency {height:0;min-height:40%;}/* hide it, then show it */
.flex #num-bought-item1{display:flex;flex-direction:column;justify-content:center;text-align:center;margin:auto;}
/* and flex did not do it */
<p>Let's try via flex</p>
<div class="store-container">
<div class="store-item flex column" id="item1">
<div class="item">
<img src="https://dummyimage.com/50" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item1" >
<span>Owned</span>
<span id="count-item1">0</span>
</div>
<div class="currency">
<img class="store-currency" src="https://dummyimage.com/25/ff0" alt="">
<span>100000</span>
</div>
</div>
</div>
<p>And via <big>grid</big> </p>
<div class="store-container">
<div class="store-item grid" id="item2">
<div class="item">
<img src="https://dummyimage.com/50" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item2" >
<span>Owned</span>
<span id="count-item1">0</span>
</div>
<div class="currency">
<img class="store-currency" src="https://dummyimage.com/25/ff0" alt="">
<span>100000</span>
</div>
</div>
</div>

CSS Grid row issue

buy-page {
display: grid;
grid-template-columns: repeat(2, 1fr);
position: relative;
grid-auto-rows: 1fr;
grid-template-rows: 200px;
grid-gap: 10px;
width: 200px;
height: 400px;
}
<main class="buy-page">
<div class="buy-1 buy">
<img src="../picsfolder/BUY-1.jpg" alt=""> </div>
<div class="buy-2 buy">
<img src="../picsfolder/buy02.jpg" alt=""> </div>
<div class="buy-3 buy">
<img src="../picsfolder/SOLD-1.jpg" alt=""> </div>
</main>
Hi, I am trying to figure out buy-1 goes to the second row.
here is my code,
there are 3 pictures, the first picture can go to the second column with grid-column: span 3;
however, when I am tring buy-1 {grid-row-start:1; grid-row-end:3;} nothing happens.
Would you tell me how to make the buy-1 stretch to the second row, please?
Thank you
Im not sure i fully understand what you are trying to achieve here but
make sure you're referencing your class correctly with .buy instead of buy-page
.buy {
display: grid;
grid-template-columns: repeat(2, 1fr);
position: relative;
grid-auto-rows: 1fr;
grid-template-rows: 200px;
grid-gap: 10px;
width: 200px;
height: 400px;
}

CSS - Make a responsive square grid, using grid [duplicate]

This question already has answers here:
CSS grid square layout [duplicate]
(4 answers)
Closed 2 years ago.
I want to make a 3x3 grid(the cells are images), using "grid" in css, with this:
Each cell of the grid is a square
The grid is responsive(I don't want something like display:flex; flex-wrap: wrap) I mean when the screen become smaller the cells of the grid are still squares and the grid take 100% width
Other thing that I don't know how to solve is:
In the cells of the grid, put images(the ratio is not like a square), but the images maintain their ratio, I mean like crop a image
What I've tried: I've make the html sintax:
<div class="grid">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Istanbul_Skyline_Be%C5%9Fikta%C5%9F_%C5%9Ei%C5%9Fli.JPG/750px-Istanbul_Skyline_Be%C5%9Fikta%C5%9F_%C5%9Ei%C5%9Fli.JPG">
<img src="https://www.turismoviajar.com/wp-content/uploads/2019/10/paris-2020.jpg">
<img src="https://ice-2020.com/wp-content/uploads/2018/11/GettyImages-674739845-1200x800.jpg">
<img src="https://eufcn.com/wp-content/uploads/2017/11/madrid_filmmadrid.jpg">
<img src="https://www.futbolred.com/files/article_main/uploads/2020/05/29/5ed193de4ae3f.jpeg">
<img src="https://lp-cms-production.imgix.net/2019-06/GettyImages-538096543_medium.jpg?fit=crop&q=40&sharp=10&vib=20&auto=format&ixlib=react-8.6.4">
<img src="https://blog.global-exchange.com/wp-content/uploads/2018/08/Moscu-calles-840.jpg">
<img src="https://www.turismoviajar.com/wp-content/uploads/2019/10/Rio-de-Janeiro-2020.jpg">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQj3iLwvZZO-JzJAPquVMTORT4CPOFURK6fzA&usqp=CAU">
</div>
and the css is
.grid{
display: grid;
width: 100%;
grid-template-columns: auto auto auto;
height: auto;
/*This doesn't work, the height is fit to the images*/
grid-template-rows: auto auto auto;
/* This doesn't work, the cells are not squares*/
}
.grid > img{
width: 100%;
height: 100%;
/*In this way I lose the ratio of the image*/
}
Grid layout is a grid based layout system,with row and columns, it easier to design web pages without using position.
#grid is a incorrect class selection. # is use to select element by Id.
.grid is the correct one.
I hope this code will help you to solve you problem.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
img{
width: 400px;
height: 250px;
}
.grid{
display: grid;
grid-template-rows: repeat(2,1fr);
grid-template-columns: auto auto auto;
grid-gap: 2px;
}
</style>
</head>
<body>
<div class="grid">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Istanbul_Skyline_Be%C5%9Fikta%C5%9F_%C5%9Ei%C5%9Fli.JPG/750px-Istanbul_Skyline_Be%C5%9Fikta%C5%9F_%C5%9Ei%C5%9Fli.JPG">
<img src="https://www.turismoviajar.com/wp-content/uploads/2019/10/paris-2020.jpg">
<img src="https://ice-2020.com/wp-content/uploads/2018/11/GettyImages-674739845-1200x800.jpg">
<img src="https://eufcn.com/wp-content/uploads/2017/11/madrid_filmmadrid.jpg">
<img src="https://www.futbolred.com/files/article_main/uploads/2020/05/29/5ed193de4ae3f.jpeg">
<img src="https://lp-cms-production.imgix.net/2019-06/GettyImages-538096543_medium.jpg?fit=crop&q=40&sharp=10&vib=20&auto=format&ixlib=react-8.6.4">
<img src="https://blog.global-exchange.com/wp-content/uploads/2018/08/Moscu-calles-840.jpg">
<img src="https://www.turismoviajar.com/wp-content/uploads/2019/10/Rio-de-Janeiro-2020.jpg">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQj3iLwvZZO-JzJAPquVMTORT4CPOFURK6fzA&usqp=CAU">
</div>
</body>
</html>
I saw that you used grid-template-rows twice in your CSS.
If you just change the second one to columns, you should be getting what you want.
.grid {
display: grid;
width: 100%;
height: auto;
grid-template-rows: auto auto auto;
grid-template-columns: auto auto auto;
}
.grid img {
width: 100%;
height: 100%;
}
Result:
Shown Here!
I got it, I solve this in this way
.image-grid {
display: grid;
margin: auto;
height: 90vw; /*This is optional*/
width: 90vw; /*With 100 there is a scrollbar(horizontal),
and I don't find a way to fit 100% without the scrollbar, but
this works fine*/
border: 2px solid black;
grid-template-columns: repeat(3, 30vw);
grid-template-rows: repeat(3, 30vw);
}
.image-grid > img {
width: 100%;
height: 100%;
object-fit: cover;
}

can i fill pictures in a single row and keep aspect ratio simple with css grid layout

I want create a picture gallery like this:
1.pictures fill into a specify width container with its orginal aspect ratio
2.each row has its own height, which is automatic fit as more picture as possible
3.it must fluid
can it be achieved only by CSS grid layout?
skeleton codepen is here:
https://codepen.io/minzojian/pen/MWwopXP
<div class="grid">
<img src="https://media-public.canva.com/MADTkhFsZ-A/1/thumbnail_large-2.jpg"/>
<img src="https://media-public.canva.com/MADGvpvY3HA/7/thumbnail_large.jpg"/>
<img src="https://media-public.canva.com/MADkaQ9LP_Y/1/thumbnail_large.jpg"/>
<img src="https://media-public.canva.com/MADR_1AI5vk/1/thumbnail_large-1.jpg"/>
<img src="https://media-public.canva.com/MADQ5O2kfiE/1/thumbnail_large-1.jpg"/>
<img src="https://media-public.canva.com/MADGyf4KZA8/4/thumbnail_large.jpg"/>
<img src="https://media-public.canva.com/MADT4-tuwa0/1/thumbnail_large-2.jpg"/>
<img src="https://media-public.canva.com/MADSTxsB9aY/1/thumbnail_large-1.jpg"/>
</div>
.grid {
width: 312px;
display: grid;
gap: 8px;
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
}
.grid img {
object-fit: contain;
}
you can set your grid-template-columns and grid-template-rows values ... then create grid-template-areas then assign what you want to go into which area.
grid-template-columns: .....whatever
grid-tmeplate-rows: .....whatever
grid-template-areas: "A", "B", "C", "D";
photo-b { grid-area: A 1; }
photo-c { gird-area: B 1; }
etc..