How to make images respect grid row size constraints? - html

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>

Related

Grow parent to width of the child [duplicate]

This question already has answers here:
Why does the outer <div> here not completely surround the inner <div>?
(2 answers)
Closed 2 years ago.
I have a child element inside of a parent element. The child has a min-width set, which makes it (at some screen sizes) wider than the parent element. The child's width correctly is set to the min-width, but the parent's width seems to max out at the original screen width. I would like the parent to grow with the child. How can I achieve this?
.container {
display: block;
background-color: blue;
width: auto;
}
.grid {
min-width: 1000px;
display: grid;
padding: 10px;
grid-gap: 10px;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, auto);
margin-left: auto;
margin-right: auto;
align-items: start;
}
.grid > div {
background-color: yellow;
}
<div class="container">
<div class="grid">
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
<div>E</div>
<div>F</div>
<div>G</div>
</div>
</div>
In the example, 'container' (blue background) is narrower than the child 'grid' (the grid of yellow boxes). If you scroll right, you can see that the blue background stops at the edge of the screen, while the yellow grid continues. I would like the blue area to grow to contain the child grid.
I have tried setting width and min-width properties on the container to auto and/or 100%. I've also tried changing the display to inner-block. None of these work.
Using width: fit-content on the .container element would allow it to expand outside of the window bounds when necessary.
.container {
display: block;
background-color: blue;
width: fit-content;
}
.grid {
min-width: 1000px;
display: grid;
padding: 10px;
grid-gap: 10px;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, auto);
margin-left: auto;
margin-right: auto;
align-items: start;
}
.grid > div {
background-color: yellow;
}
<div class="container">
<div class="grid">
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
<div>E</div>
<div>F</div>
<div>G</div>
</div>
</div>

Prevent CSS grid from causing overflow

I have a CSS grid, but I'm finding that the grid rows are not all fitting onto the page - instead, they're causing an overflow. How can I resize the grid rows so that they don't overflow the page? I thought the 1fr value was supposed to do this, but it doesn't seem to be working in my code.
I've had a look at Prevent content from expanding grid items and tried the suggested answers there (such as changing grid-template-rows: repeat(5, 1fr) to grid-template-rows: repeat(5, minmax(0, 1fr)); but to no avail.
I've tried adding height: 100% to the grid and it's container, but it is still overflowing.
JsFiddle https://jsfiddle.net/4g9b2qkL/
body,
html {
margin: 0;
height: 100%;
}
#container {
display: grid;
grid-template-columns: 1fr 5fr;
height: 100%;
}
#left {
grid-column: 1 / 2;
background: lightblue;
height: 100%;
}
#right {
grid-column: 2 / 3;
height: 100%;
}
#results {
display: grid;
grid-template-rows: repeat(5, 1fr);
height: 100%;
}
img {
max-height: 100%;
max-width: 100%;
}
<div id="container">
<div id="left">
<p>
Some stuff on the left....
</p>
</div>
<div id="right">
<h1>
Title
</h1>
<div id="results">
<div class="result">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/62/Paracas_National_Reserve%2C_Ica%2C_Peru-3April2011.jpg" />
</div>
<div class="result">
Result 2
</div>
<div class="result">
Result 3
</div>
</div>
</div>
</div>
A few things to consider:
missing height reference
Using a percentage value to set the height of the img is problematic because there is no defined height on the container. Generally speaking, percentage heights should have a height reference on the parent for reliable rendering. Your declarations may or may not be ignored / misinterpreted.
See: Working with the CSS height property and percentage values
height: 100%
Setting the #results element to height: 100% is problematic, if you want to prevent a vertical overflow, because it doesn't factor in the height of the sibling (the h1).
height: 100% + height of h1 title > height of container (resulting in an overflow)
use a flexible height instead
Instead of using a percentage height, set a more flexible height, such as flex-grow. This tells the container to simply consume remaining space.
override the minimum height default
Grid and flex items are set by default to stop shrinking at the size of their content. Override that setting with min-height: 0.
See: Why don't flex items shrink past content size?
cross browser compatibility
Chrome can do the layout with less code (than posted below). It makes more assumptions about an author's intentions. Firefox, Edge and Safari assume less, so require more rules.
#container {
display: grid;
grid-template-columns: 1fr 5fr;
height: 100vh;
}
#left {
background: lightblue;
}
#right {
display: flex;
flex-direction: column;
height: 100vh;
}
#results {
flex-grow: 1;
min-height: 0;
display: grid;
grid-template-rows: repeat(5, 1fr);
}
.result {
min-height: 0;
}
img {
max-width: 100%;
max-height: 100%;
}
body {
margin: 0;
}
<div id="container">
<div id="left">
<p>Some stuff on the left....</p>
</div>
<div id="right">
<h1>Title</h1>
<div id="results">
<div class="result">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/62/Paracas_National_Reserve%2C_Ica%2C_Peru-3April2011.jpg" />
</div>
<div class="result">Result 2</div>
<div class="result">Result 3</div>
</div>
</div>
</div>
You need to consider min-height:0 in different places and make some adjustment like below:
body,
html {
margin: 0;
height: 100%;
}
#container {
display: grid;
grid-template-columns: 1fr 5fr;
height: 100%;
}
#left {
grid-column: 1 / 2;
background: lightblue;
/*height: 100%; removed */
}
#right {
grid-column: 2 / 3;
/*height: 100%; removed */
min-height:0; /* here */
/* added */
display:flex;
flex-direction:column;
/**/
}
#results {
display: grid;
grid-template-rows: repeat(5, 1fr);
/*height: 100%; removed */
flex-grow:1; /* added */
min-height:0 /* here */
}
.result {
min-height:0 /* here */
}
img {
max-height: 100%;
max-width: 100%;
}
<div id="container">
<div id="left">
<p>
Some stuff on the left....
</p>
</div>
<div id="right">
<h1>
Title
</h1>
<div id="results">
<div class="result">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/62/Paracas_National_Reserve%2C_Ica%2C_Peru-3April2011.jpg" />
</div>
<div class="result">
Result 2
</div>
<div class="result">
Result 3
</div>
</div>
</div>
</div>

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..

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

grid items don't fully occupy grid container

I have created a site design and everything is okay except that I am having problem in getting the grid items to occupy all the space available to them.
I have added the site in codepen here.
.container{
border-radius: 8px;
width:90%;
height: 90vh;
margin: 0 auto;
background: #ecf0f1;
display: flex;
flex-direction: column;
}
main{
display: grid;
grid-template-columns: 300px 1fr;
flex-grow: 1;
}
.data-box{
padding-top:1rem;
background: #e74c3c;
color: #fff;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 3rem 2.5rem 2.5rem auto;
justify-items: center;
align-items: center;
}
.display-box{
padding: 2em;
background: #2c3e50;
display: grid;
grid-template-columns: repeat(auto-fit,minmax(6em,1fr));
grid-gap:1.5rem;
align-items: center;
justify-items: center;
}
HTML:
<div class="container">
<div class="main-text">
<h1>Smart Parking System UI</h1>
<p>Currently viewing: <span class="lot-id">Beta</span></p>
</div>
<main>
<div class="data-box">
<p class="m-para">Parking Status</p>
<p class="total-para">Total Seats:</p><span class="total-data">15</span>
<p class="avail-para">Available: </p><span class="avail-data">12</span>
<div class="location">
<p>Your Location:</p><i class="fas fa-map-marker-alt"></i><p>Asgard</p>
</div>
</div>
<div class="display-box">
<div class="lot-box inactive">1</div>
<div class="lot-box">2</div>
<div class="lot-box">3</div>
<div class="lot-box">4</div>
<div class="lot-box">5</div>
<div class="lot-box">6</div>
<div class="lot-box">7</div>
<div class="lot-box">8</div>
<div class="lot-box">9</div>
<div class="lot-box inactive">10</div>
<div class="lot-box inactive">11</div>
<div class="lot-box">12</div>
<div class="lot-box">13</div>
<div class="lot-box">14</div>
<div class="lot-box">15</div>
</div>
</main>
</div>
The problem can be noticed in full page view. The container div is a flexbox and contains the main-text div with flex-grow 0 and main tag with flex-grow 1. The main tag is a grid container with two grid items display-box and data-box. I am having difficulty in making them occupy all the space available to them in main tag as seen by the white blankspace.
The main element actually is stretching the full height of the container. In other words, flex-grow: 1 is working. (You may know this already.) The child elements (data-box and display-box) are not stretching the full height of the parent because they simply don't have enough content in them yet.
You also have data-box set to:
grid-template-rows: 3rem 2.5rem 2.5rem auto;
That creates four rows of a defined or content-based height. Using fr units covers available space.
grid-template-rows: 3fr 2fr 1fr auto;
revised codepen