need help understanding css grid with variable image sizes - html

absolute css newbie here. all the tutorials and examples i've been finding over the past three days perfectly explain how to set up an image gallery using grid with images of a similar size, but I can't figure out how to set one up with images of variable sizes without having horrible gaps underneath smaller images, like here:
horrible gaps
I've tried using everything under object-fit, and nothing works. i know its because i'm grossly misunderstanding something. i'd like for my image gallery to look like: this mosaic image gallery and to understand how to accomplish this. i can easily do something similar it when it's just a bunch of empty boxes, but for some reason when images get thrown into the mix, i'm not understanding it.
here's my html:
<div class="gallery">
<img src="images/img-1.jpg" alt="nature">
<img src="images/img-2.jpg" alt="nature">
<img src="images/img-3.jpg" alt="nature">
<img src="images/img-4.jpg" alt="nature">
<img src="images/img-5.jpg" alt="nature">
</div>
and css:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.gallery{
display: grid;
grid-template-areas:
'img-1 img-2 img-3'
'img-4 img-5 .';
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-gap: 1rem;
}
img{
display: block;
overflow: hidden;
width: 100%;
}
img-1{
grid-area: img-1;
}
img-2{
grid-area: img-2;
}
img-3{
grid-area: img-3;
}
img-4{
grid-area: img-4;
}
img-5{
grid-area: img-5;
}
i've tried looking at similar answers which want me to set my img{ width: auto; height: 100%}, and when i do, everything disappears from the screen, giving me a blank page.
any and all help is greatly appreciated!

Related

CSS Grid auto-height columns to content

I'm trying to create a layout like this:
I want the grid height to be according to the content. The pagination area height to be according to its box-sizing and the image area to take up the rest of the available space. I'm relatively new to grid. Help would be highly appreciated.
This is what I'm getting:
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: repeat(2, min-content);
}
The problem is pagination and image sections takes half of the grid area. I do not want any section to have a fixed height in pixels.
Based on the response to a previous awnser here i have tried to make the changes that are being looked for.
From what i can gather you are describing 3 total elements Image, Pagination, Content
of these 3 total elements you would like
Content to occupy 100% of the width of the right side
Pagination to live on the left side and occupy a small amount that is just enough to fit the pagination
Image to live on the left side an occupy the remaining space
To do this we can still use grid we just need to specify different values for our template which I have done below. The key here is min-content
html body{
margin: 0px;
padding: 0px;
}
.your_main_class {
width: 100vw;
height: 100vh;
display: grid;
grid-template-columns: 50vw 50vw;
grid-template-rows: 1fr min-content;
grid-template-areas: "image cont"
"pagination cont";
}
div{
height: 100%;
width: 100%;
}
.image{
grid-area: image;
background: red;
}
.pagination{
grid-area: pagination;
background: blue;
}
.content{
grid-area: cont;
background: black;
}
<div class="your_main_class">
<div class="image"> Image </div>
<div class="pagination"> Pagination </div>
<div class="content"> content </div>
</div>
You can do that by specifying the area of the divs. Check the snippet bellow.
.your_main_class {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: repeat(3, 1fr);
}
.image{
grid-area: 1 / 1 / 3 / 6;
background: red;
}
.pagination{
grid-area: 3 / 1 / 4 / 6;
background: blue;
}
.content{
grid-area: 1 / 6 / 4 / 13;
background: black;
}
<div class="your_main_class">
<div class="image"> </div>
<div class="pagination"> </div>
<div class="content"> </div>
</div>

How can I add a border around individual images, while keeping images aligned side by side?

I'm trying to get a border around each individual image while keeping them spaced out and in a rows of five.
Lets do it step by step.
First to align images next to each other with 5 images in a row, we can use css-grid. For thatw e need to wrap all images within a container. We apply display: grid; to the container. Then we use grid-template-columns: repeat(5, 1fr); to apply 5 columns with equal size. So we will have 5 images within the same row of the exact size.
Applying a border will slightly break they layout. This is, because the border consumes extra space. To counter that, we can use box-sizing: border-box; to the images to size them all incl border and paddings. As such a broder will no longer extend the images size.
.image-container {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 5px;
}
.image-container img {
width: 100%;
display: block;
box-sizing: border-box;
}
.image-container img.border {
border: 2px solid red;
}
<div class="image-container">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0721.jpg">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0722.jpg">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0723.jpg" class="border">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0727.jpg">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0730.jpg" class="border">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0731.jpg">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0732.jpg">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0733.jpg">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0734.jpg">
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg">
</div>

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;
}

HTML CSS Image Gallery Not Making Squares with Auto or Stretch

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

How to center grid containing repeat(auto-fit, minmax(15rem, 1fr));

I am finishing my portfolio site, but I encountered a problem. I used auto-fit function, because I want it to be responsive, and I will keep adding new images there each month.
I found that I can't center it the way I want, because grid is filling empty space with additional columns which I don't need right now, so my grid is kissing left side of my website. Please help guys, you are the best! :)
<section class="projects">
<h2 class="h2">Projects</h2>
<div class="projects__part">
<h4 class="h4">Primary projects</h4>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
</div>
<div class="projects__part">
<h4 class="h4">Secondary projects</h4>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
</div>
</section>
.projects {
&__part {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
/*DESIRED RESPONSIVE EFFECT:
h4 {
margin-left: 22rem;
}
margin-left: 35rem;
*/
}
&-item {
&__img {
width: 14rem;
height: 14rem;
border-radius: 100%/100%;
}
}
h4 {
grid-column: 1 / -1;
}
}
Here's my code : https://codepen.io/maja5252/pen/XPPaBL?editors=1100#0
Again sorry if format of this post is not correct. Still don't know how to split my code to HTML and CSS part :)
A couple issues with your code:
-CSS grid likes to control the size of its columns, and you've got fixed height/width on your images. If you do a DevTools inspection of your code, you'll see your 3rd column is overflowing out of your grid because of this. I changed the height/width to 100%/100%. If you need square images, fix that issue in Photoshop not CSS.
-Centering the entire grid is pretty simple, all you need to do is add margin: 0 auto because it's a block-level element. But, a block level element takes up 100% width of the viewport, so centering a 100% width element won't produce any visible effect. I put the width at 80% to demonstrate how this might look centered, but you could use any non-100% value for this effect.
-I'm also not sure what you're doing with auto-fit in your grid. Just use 1fr and it works really well and the code is clean.
-I also made the whole grid responsive for you - not sure you want to do that, but might as well showcase it.
All the corrected CSS issues together:
.projects {
margin: 0 25px;
#media (min-width: 450px) {
width: 80%;
margin: 0 auto;
}
&__part {
display: grid;
grid-gap: 25px;
grid-template-columns: 1fr;
#media (min-width: 450px) and (max-width: 768px) {
grid-template-columns: 1fr 1fr;
}
#media (min-width: 768px) {
grid-template-columns: 1fr 1fr 1fr;
}
}
&-item {
&__img {
width: 100%;
height: 100%;
border-radius: 50%;
}
}
h4 {
grid-column: 1 / -1;
}
}
Demo:
https://codepen.io/staypuftman/pen/qMMPwp
I found out, that my H4 headings were disrupting an auto-fit effect, so it worked like auto-fill effect. Code is edited - I pulled headings out of the grid, and it got centered.
to #staypuftman - thanks for your advices - I'll keep them in mind in my future work. Now, when auto-fit works, you can see how responsive it is, without any media query. Again thanks for your effort to help me.