CSS Grid image and column issue in Chrome - html

In Chrome, my site is breaking on desktop and mobile.
Issues:
The second section isn't displaying the hero image correctly. The image should have two halves that when hovered, join together.
Hero images jump to the top of the page with only half of the image in view.
When I went to inspect the page, I saw the following in the console
[Deprecation] Percentages row tracks and gutters for indefinite height grid containers will be resolved against the intrinsic height instead of being treated as auto and zero respectively
I looked into it and came to find the fix for the said issue was to change percentages in grid-template-rows or grid-auto-rows to auto.
I applied the given fix and the issue still persisted. It only breaks in Google Chrome. The site looks fine in Firefox.
Here's the code.
HTML:
<section class="page">
<div class="details">
<h1>Skyline KPGC110</h1>
<h2>1973</h2>
</div>
<div class="hero">
<a href="./kenmeri.html">
<img class="model-left" src="./images/front-1.png" alt="model">
<img class="model-right" src="./images/front-2.png" alt="model">
</a>
</div>
</section>
<!-- ------------------------------ Seciton 2 ------------------------------ -->
<section class="page about">
<div class="details">
<h1>Skyline PGC10</h1>
<h2>1969</h2>
</div>
<div class="hero">
<a href="./pgc10.html">
<img class="model-left" src="./images/gtr-left.png" alt="chef">
<img class="model-right" src="./images/gtr-right.png" alt="chef">
</a>
</div>
</section>
<!-- ------------------------------ Section 3 ------------------------------ -->
<section class="page reel">
<div class="details">
<h1>Fairlady 240z</h1>
<h2>1973</h2>
</div>
<div class="hero">
<a href="./fairladyz.html">
<img class="model-left" src="./images/240z-left.jpg" alt="model">
<img class="model-right" src="./images/240z-right.jpg" alt="model">
</a>
</div>
</section>
Desktop view:
.page {
display: grid;
grid-template-columns: 5% 1fr 1fr 1fr 5%;
min-height: 90vh;
}
.about,
.reel {
position: absolute;
bottom: 0%;
left: 0%;
width: 100%;
opacity: 0;
pointer-events: none;
}
.hero {
display: flex; /* gets rid of gap between imgs */
align-self: center;
justify-self: center;
height: 500px;
overflow: hidden;
}
.hero a {
display: flex; /* removes seperation created by a tag */
}
.hero img {
height: 500px;
transition: transform 0.3s ease-out;
cursor: pointer;
}
Breakpoint 1(Laptop):
#media screen and (max-width: 1024px) {
.page {
grid-template-columns: 5% 1fr 5%;
grid-template-rows: 2fr 1fr;
align-items: center;
}
.hero {
height: auto;
grid-column: 2;
}
.hero img {
height: 425px;
}
}
Breakpoint 2(Mobile):
#media screen and (max-width: 768px) {
.page {
grid-template-rows: 1fr;
}
.page-1 h3 {
font-size: 1.125rem;
padding-right: 20px;
}
.hero img {
width: auto;
}
.details h2 {
font-size: 28px;
}
}
The link to the repo is here if you want to get a full look at the code.

In your code,
img {
display: block;
width: 100%;
height: 100%;
}
just remove the width: 100% and it will work fine.

Related

Container not taking all the space in the page

I have started web dev not that long ago, and today I really tried to understand the basics of css and html, and I've got almost everything to work correctly. But I'm facing a problem that I can't seem to be able to solve by myself.
So here I am. I'm trying to replicate this design I did on figma, as a challenge for myself, and as a huge fan of Family Guy :
See the image here
So I started by learning how to create a css grid (I thought it was going to be usefull for each part of the page), then how to create a side bar and all of that. But there I am stuck :
My whole page is cranked into a tiny part :
This
For further investigation, here's the grid in inspect mode :
This
So how can I make it take the whole page like it should :
Here's my html and css
HTML
<!DOCTYPE html>
<head>
<title>FamilyGuy</title>
<link rel="stylesheet" type="text/css" href="./assets/css/styles.css">
</head>
<body>
<div class="container">
<div class="navbar">
</div>
<div class="card-1">
<div class="card-image-container">
<img class="card-image" src="./assets/img/Family_Guy_Season1.png">
</div>
<div class="card-title-container">
<h3 class="card-title">Episode 1</h3>
</div>
</div>
<div class="card-2">
<div class="card-image-container">
<img class="card-image" src="./assets/img/Family_Guy_Season1.png">
</div>
<div class="card-title-container">
<h3 class="card-title">Episode 2</h3>
</div>
</div>
</body>
</html>
CSS
html {
display: block;
margin: 0;
padding: 0;
}
body {
background: #1E1E1E;
font-family: 'Inter', sans-serif;
border: 0;
margin: 0;
padding: 0;
}
.container {
display: grid;
max-width: 1920px;
max-height: 1080px;
grid-template-columns: [first] 18.23% [second] 2.6% [third] auto [fourth] 2.6% [fifth] auto [sixth] 2.6% [seventh] auto [eighth] 2.6% [ninth] auto [tenth] 2.6% [eleventh] 42.19%;
grid-template-rows: [first] 2.78% [second] 2.78% [third] 5.28% [fourth] 13.89% [five] 3.06% [sixth] 19.44% [seventh] 2.6% [ninth] 19.44% [tenth] 2.6% [eleventh] 19.44% [twelveth] 2.6%;
}
div.navbar {
background: #2F3136;
max-width: 350px;
max-height: 1080px;
grid-column-start: first;
grid-column-end: span second;
grid-row-start: first;
grid-row-end: span twelveth;
}
div.card-1 {
position: inherit;
background: #2F3136;
max-width: 140px;
max-height: 210px;
grid-column-start: third;
grid-column-end: span fourth;
grid-row-start: sixth;
grid-row-end: seventh;
border-radius: 7.14%;
}
div.card-2 {
position: inherit;
background: #2F3136;
max-width: 140px;
max-height: 210px;
grid-column-start: fifth;
grid-column-end: span sixth;
grid-row-start: sixth;
grid-row-end: seventh;
border-radius: 7.14%;
}
div.card-image-container {
display: flex;
justify-content: center;
position: relative;
max-width: 120px;
max-height: 158.68px;
border-radius: 7.14%;
padding: 7.14%;
border-radius: 7.14%;
width: 85.71%;
height: 75.56%;
}
img.card-image {
display: flex;
border-radius: 7.14%;
justify-content: center;
}
div.card-title-container {
display: flex;
justify-content: center;
align-items: center;
position: relative;
max-width: 140px;
max-height: 41px;
}
h3.card-title {
display: flex;
margin-bottom: 0;
margin-top: .2em;
margin-left: 0;
margin-right: 0;
}
h3 {
font-size: 1rem;
color: #FFFFFF;
font-weight: 500;
}
Please keep in mind that I just started static web dev recently, and that there may be a lot of errors in my code. I already apologize for the possible big noob error I could have done. This is actually my first time really trying to do something without any template or base code, so yeah, I'm kinda going blindfolded into this challenge. Already thanks for the help !
As stated. I've tried to replicate my figma design, but my body/html won't spread all the way on the page.
UPDATE : looks like using :
<div class="container" style="height: 852px">
In the HTML, fixed my problem. Alhough, I'm not entirely satisfied of this, as it creates a scroll bar on the right. Which I'm not particularly a fan of. If any of you has ideas, I would gladly appreciate it.
There are two major problems with your code, apart from your issue:
The page is constructed as one large grid. Espescially CSS Grid can be very intimidating creating such grid, where Flexbox Layout will probably feel more intuitive.
From your image it is clear that you have designed a main page with three main sub panels, each with its own distinct function. Tools, Navigation & Image Grid and Preview.
Each sub panel can be divided into several child panels and in some cases ever deeper nested panels.
Fixed units like px are your enemy, particularly when you want to design a responsive web page that can fit on various devices. Percentages based on a fixed value, while relative, will still eventually result in fixed values, e.g. 10% of 1920px will always be 192px. This will likely lead to cascading element size and positioning issues.
Instead of helping you with an issue in code you most likely will want to change one way or another, I took the liberty of creating a responsive base framework you can extend and modify to your needs:
I divided your design into the nested panels mentioned before and removed all fixed units, except for a few in the media queries.
Introduced a few Flexbox Layout shortcut (one letter) attribute selectors for easy 'on-the-fly' flexbox use.
Element font sizes defined as parent dependent (em unit).
Element sizes and spacing as root dependent (rem unit).
Comments are in the CSS where applicable.
Downloadable version of the code at Codepen.
There is still a lot to do, I didn't create the entire page for you, but maybe this will give you a head start...
Snippet
/* * { outline: 1px dashed white }/* for debugging only */
/********************************/
/* some convenient global rules */
/********************************/
* { box-sizing: border-box }
html, body { width: 100%; max-width: 100% }
body { min-height: 100vh; margin: 0 }
/* Linear equation y=mx+b using points p1(320,12) p2(1280,18) */
body { font-size: calc(0.625vmin + 0.625rem) } /* responsive page font */
img {
display: block; /* removes whitespace below image */
object-fit: cover; /* fill, but clip when lack of room */
width: 100%; /* width is leading */
height: 100%;
}
iframe { border: none; max-width: 100% }
/* this and thats */
.icon { display: inline-grid; place-items: center; padding: 0.25rem }
.big { font-size: 1.75em; font-weight: bolder }
.small { font-size: 0.75em }
/* Easy flexbox shorcuts for on-the-fly use */
[F] { display: flex; flex-wrap: wrap }
[R] { flex-direction: row } /* flexbox default */
[C] { flex-direction: column }
[N] { flex-wrap: nowrap } /* Flexbox default */
[F] > * { flex: 1 } /* allow flexbox kids to grow */
body {
background-color: #1e1e1e; color: White;
font-family: 'Inter', sans-serif;
line-height: 1.5;
}
header, footer, nav {
display: flex; align-items: center;
width: 100%;
padding: 0.5rem 0;
font-weight: bold;
}
header { font-size: 1.5rem; justify-content: center }
footer { font-size: 0.8rem; justify-content: end }
/***************/
/* end globals */
/***************/
/**********************/
/* Main app container */
/**********************/
.main {
display: flex; flex-wrap: wrap;
width: 100%; min-height: 100vh; /* fill parent */
background-color: #2f3136;
}
/******************/
/* All sub panels */
/******************/
.sub {
/* Flexbox column (of rows) container */
display: flex; flex-flow: column wrap;
gap: 1rem; /* generic gap between various content panels */
/* this will make a panel fit snuggly in a 320px or 360px device */
min-width: min(18rem, 100%); /* = 288/16 or 100% */
padding: 0.5rem 2rem;
}
.sub footer { margin-top: auto } /* move to bottom */
/* Footers are here to show height of panels */
/* panel spacing ratio 1:2:2 (comparable to grid 'fr' unit) */
/* when available and respecting .sub min-width */
.sub.tools { flex: 1 } /* allow to grow, where */
.sub.episodes { flex: 2 } /* flex-shrink = 1 and */
.sub.preview { flex: 2 } /* flex-basis = 0% */
/*******************/
/* Sub tools panel */
/*******************/
.search label { width: 100%; }
.search .icon { font-size: 1.5em }
.search input { font-size: 1em; width: calc(100% - 1.5em - 0.5rem) }
/* 100% minus icon size minus L/R icon padding */
/**********************/
/* Sub episodes panel */
/**********************/
.sub.episodes {
/* Define custom property for numer of image columns */
--img-columns: 2; /* mobile first: 2 columns only */
/* Overridden by Media Queries */
background-color: #36393f;
}
.navigation { font-size: 1.75em }
/* Default for small devices < 720px */
.episode-grid {
/* Use column value set by Media Queries */
display: grid; grid-template-columns: repeat(var(--img-columns), 1fr);
gap: 0.5rem;
}
/* Media queries for gradually bigger devices, change as required */
/* Common use */
/*#media screen and (min-width: 721px) { .episode-grid { grid-template-columns: repeat(3, 1fr) } } /* > 720px */
/*#media screen and (min-width: 1081px) { .episode-grid { grid-template-columns: repeat(4, 1fr) } } /* > 1080px */
/*#media screen and (min-width: 1367px) { .episode-grid { grid-template-columns: repeat(5, 1fr) } } /* > 1366px */
/*#media screen and (min-width: 1921px) { .episode-grid { grid-template-columns: repeat(7, 1fr) } } /* > 1920px */
/* Custom property use */
/* Custom properties defined at parent element level, modified and used at element child level */
#media screen and (min-width: 721px) { .episode-grid { --img-columns: 3 } } /* > 720px */
#media screen and (min-width: 1081px) { .episode-grid { --img-columns: 4 } } /* > 1080px */
#media screen and (min-width: 1367px) { .episode-grid { --img-columns: 5 } } /* > 1366px */
#media screen and (min-width: 1921px) { .episode-grid { --img-columns: 7 } } /* > 1920px */
.card .small { text-align: center }
/*********************/
/* Sub preview panel */
/*********************/
.sub.preview .content { gap: 1rem }
.thumblist, .settings {
gap: 0.25rem;
padding: 1rem;
background-color: #36393f;
border-radius: 0.5rem;
}
.thumb {
padding: 0.5rem;
background-color: #2f3136;
}
.card,.thumb { border-radius: 0.5rem; padding: 0.5rem; background-color: #2f3136 }
:is(.card,.thumb) img { border-radius: 0.75rem }
<!-- Main app container -->
<div class="main">
<!-- Tools sub panel -->
<div class="sub tools">
<header>
<div class="logo">
<img src="https://i.imgur.com/gEQ9EK6.png">
</div>
</header>
<div class="search" F C>
<div class="bar">
<label><input placeholder="Search"><span class="icon">ⓘ</span></label>
</div>
<div class="results">
<div class="list" F C>
<div>result one</div>
<div>result two</div>
<div>result three</div>
<div>result four</div>
<div>result five</div>
<div>result six</div>
</div>
<div class="dots">...</div>
</div>
</div>
<div class="more-tools" F C>
<div class="tool one" >tool one</div>
<div class="tool two" >tool two</div>
<div class="tool three">tool three</div>
</div>
<footer>tools</footer>
</div>
<!-- Episodes sub panel -->
<div class="sub episodes">
<header></header>
<div class="page-control" F C>
<nav class="navigation" F>
<div class="option"><span class="icon">ⓘ</span><span class="text">text</span></div>
<div class="option"><span class="icon">ⓘ</span><span class="text">text</span></div>
<div class="option"><span class="icon">ⓘ</span><span class="text">text</span></div>
<div class="option"><span class="icon">ⓘ</span><span class="text">text</span></div>
</nav>
<div class="title">
<div class="big">FamilyGuy.Com</div>
<p class="small">Lorem ipsum dolor sit amet, eos ad wisi eius deserunt, cu iusto nonumes volumus usu, ne aeque aperiam saperet pro. Cu eum laudem sanctus, apeirian torquatos assentior mea ea, eos ut tantas altera sadipscing. Nam splendide voluptatum ad, integre quaeque definitiones mea et.</p>
</div>
</div>
<div class="episode-grid">
<div class="card">
<div F C>
<img src="https://picsum.photos/300/535?random=1">
<div class="small">byline</div>
</div>
</div>
<div class="card">
<div F C>
<img src="https://picsum.photos/300/535?random=2">
<div class="small">byline</div>
</div>
</div>
<div class="card">
<div F C>
<img src="https://picsum.photos/300/535?random=3">
<div class="small">byline</div>
</div>
</div>
<div class="card">
<div F C>
<img src="https://picsum.photos/300/535?random=4">
<div class="small">byline</div>
</div>
</div>
<div class="card">
<div F C>
<img src="https://picsum.photos/300/535?random=5">
<div class="small">byline</div>
</div>
</div>
</div>
<footer>episodes</footer>
</div>
<!-- Movie preview sub panel -->
<div class="sub preview">
<header>Live preview</header>
<div class="content" F C>
<div class="movie">
<iframe src="https://www.youtube.com/embed/N-nJ3EMueyE"
title="Family Guy - Fight with the hologram (Season 21 Episode 6)"
frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
</div>
<div>
<div class="episode-selector">Episode selector</div>
</div>
<div class="highlights" F C>
<div>Highlights</div>
<div class="thumblist" F R>
<div class="thumb"><img src="https://picsum.photos/200/112?random=6"></div>
<div class="thumb"><img src="https://picsum.photos/200/112?random=7"></div>
<div class="thumb"><img src="https://picsum.photos/200/112?random=8"></div>
<div class="thumb"><img src="https://picsum.photos/200/112?random=9"></div>
</div>
</div>
<div class="settings" F>
<div class="subtitle" F C>
<p>Available Subtitles</p>
<label class="small">English <input type="checkbox"></label>
<label class="small">French <input type="checkbox"></label>
<label class="small">Spanish <input type="checkbox"></label>
</div>
<div class="audio" F C>
<p>Available Audio tracks</p>
<label class="small">English <input type="checkbox"></label>
<label class="small">French <input type="checkbox"></label>
<label class="small">Spanish <input type="checkbox"></label>
</div>
</div>
</div>
<footer>preview</footer>
</div>
</div>

Horizontal gallery scroller centered on a responsive page

I'm trying to make a page with a horizontal scrolled image gallery using flex grid.
The gallery should be centered on the page with bars on the sides. To accomplish this, I have created a css grid with areas 'mainleft maincenter mainright'. It shuld look something like this:
The problem is the page is not responsive. So, if I do not set max-width of the gallery the site looks like this:
The gallery overflows the entire page. Setting max-width to 100% do not work. Setting max-widt to something like 700px works but then the page is not responsive anymore.
Code for the page:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HScroll gallery test</title>
<style>
main {
background-color:aqua;
display: grid;
grid-template-columns: 100px auto 100px;
grid-template-rows: auto;
grid-template-areas: 'mainleft maincenter mainright';
}
.left {
grid-area: mainleft;
background-color:coral;
}
.right {
grid-area: mainright;
background-color:coral;
}
.gallery {
grid-area: maincenter;
position: relative;
max-width: 100%; /* Not working */
padding: 0 10;
}
.gallery_scroller {
/* snap mandatory on horizontal axis */
scroll-snap-type: x mandatory;
overflow-x: scroll;
overflow-y: hidden;
display: flex;
align-items: center;
height: 300px;
/* Enable Safari touch scrolling physics which is needed for scroll snap */
-webkit-overflow-scrolling: touch;
}
.gallery_scroller img {
/* snap align center */
scroll-snap-align: center;
scroll-snap-stop: always;
margin:22px;
}
</style>
</head>
<body>
<main class="main">
<div class="left">
</div>
<div class="gallery">
<div class="gallery_scroller">
<img src="https://placeimg.com/300/300/animals/grayscale"/>
<img src="https://placeimg.com/360/480/animals/grayscale"/>
<img src="https://placeimg.com/640/480/animals/grayscale"/>
<img src="https://placeimg.com/360/360/animals/grayscale"/>
<img src="https://placeimg.com/2560/960/animals/grayscale"/>
<img src="https://placeimg.com/360/360/animals/grayscale"/>
</div>
</div>
<div class="right">
</div>
</main>
</body>
</html>
main {
background-color:aqua;
display: grid;
grid-template-columns: 100px auto 100px;
grid-template-rows: auto;
grid-template-areas: 'mainleft maincenter mainright';
}
.left {
grid-area: mainleft;
background-color:coral;
}
.right {
grid-area: mainright;
background-color:coral;
}
.gallery {
grid-area: maincenter;
position: relative;
width: 100%; /* Not working */
padding: 0 10;
}
.gallery_scroller {
/* snap mandatory on horizontal axis */
scroll-snap-type: x mandatory;
overflow-x: scroll;
overflow-y: hidden;
display: flex;
align-items: center;
height: 300px;
/* Enable Safari touch scrolling physics which is needed for scroll snap */
-webkit-overflow-scrolling: touch;
}
.gallery_scroller img {
/* snap align center */
scroll-snap-align: center;
scroll-snap-stop: always;
margin:22px;
}
<main class="main">
<div class="left">
</div>
<div class="gallery">
<div class="gallery_scroller">
<img src="https://placeimg.com/300/300/animals/grayscale"/>
<img src="https://placeimg.com/360/480/animals/grayscale"/>
<img src="https://placeimg.com/640/480/animals/grayscale"/>
<img src="https://placeimg.com/360/360/animals/grayscale"/>
<img src="https://placeimg.com/2560/960/animals/grayscale"/>
<img src="https://placeimg.com/360/360/animals/grayscale"/>
</div>
</div>
<div class="right">
</div>
</main>
Here is a possible solution with minimum modifications to the original code.
The main change here is in the grid-template-columns property: Instead of
grid-template-columns: 100px auto 100px;
it's
grid-template-columns: 100px minmax(0, 1fr) 100px;
If I'm not wrong, a value of auto takes the inner content in account as well as the availalable width so that breaks the desired width limitation. On the other hand, minmax(0, 1fr) means that 1fr would be the maximun width for this column. 1fr is one fraction of what remained after the other fixed-width columns took their own space, and since there is only one column remained it takes the entire remaining width.
Another minor change to the code is fixing the broken padding by changing 10 to 10px.
main {
background-color: aqua;
display: grid;
grid-template-columns: 100px minmax(0, 1fr) 100px;
grid-template-rows: auto;
grid-template-areas: 'mainleft maincenter mainright';
}
.left {
grid-area: mainleft;
background-color: coral;
}
.right {
grid-area: mainright;
background-color: coral;
}
.gallery {
grid-area: maincenter;
position: relative;
width: 100%;
padding: 0 10px;
}
.gallery_scroller {
/* snap mandatory on horizontal axis */
scroll-snap-type: x mandatory;
overflow-x: scroll;
overflow-y: hidden;
display: flex;
align-items: center;
height: 300px;
/* Enable Safari touch scrolling physics which is needed for scroll snap */
-webkit-overflow-scrolling: touch;
}
.gallery_scroller img {
/* snap align center */
scroll-snap-align: center;
scroll-snap-stop: always;
margin: 22px;
}
<main class="main">
<div class="left">
</div>
<div class="gallery">
<div class="gallery_scroller">
<img src="https://placeimg.com/300/300/animals/grayscale" />
<img src="https://placeimg.com/360/480/animals/grayscale" />
<img src="https://placeimg.com/640/480/animals/grayscale" />
<img src="https://placeimg.com/360/360/animals/grayscale" />
<img src="https://placeimg.com/2560/960/animals/grayscale" />
<img src="https://placeimg.com/360/360/animals/grayscale" />
</div>
</div>
<div class="right">
</div>
</main>
I know isn't the best solution but it works for you because images are causing overflow
.gallery {
grid-area: maincenter;
position: relative;
width: calc(100vw - 200px);
/*width: 100%; Not working */
padding: 0 10;
}
btw
your padding isn't working because doesn't have units, maybe u wanted px

Images inside a display:flex item partially distorted or cut off

I am currently building a horizontal scroller for an image gallery.
The scroller works so far, but unfortunately the images are partially distorted or part of it is cut off.
I suspect that it has something to do with Flexbox ...
Here you can see the code of the scroll slider (markup + scss)
https://jsfiddle.net/3cdkxbvm/
.scroll-slider-title {
display: flex;
justify-content: center;
flex-direction: column;
align-items: flex-start;
margin-bottom: 0.5em;
font-weight: 700;
}
.scroll-slider-title svg {
margin-right: 1rem;
font-size: 1.5em;
}
.scroll-slider-title a {
color: inherit;
}
.scroll-slider {
width: 100%;
overflow: hidden;
position: relative;
margin-bottom: 4rem;
}
.scroll-slider .scroll-slider-pane {
width: 100%;
overflow: auto;
padding: 2rem 0 3rem;
box-sizing: border-box;
-webkit-overflow-scrolling: touch;
scroll-behavior: smooth;
}
.scroll-slider .scroll-slider-track {
display: inline-block;
padding: 0;
margin: 0;
}
.image-gallery {
display: flex;
}
.image-gallery .image-gallery-item {
margin-right: 1rem;
overflow: hidden;
flex-shrink: 0;
width: auto;
}
.image-gallery .image-gallery-item img {
height: 100%;
width: auto;
}
<div class="scroll-slider" id="js_image_gallery">
<div class="scroll-slider-pane">
<div class="scroll-slider-track">
<div class="image-gallery">
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/l800/83/1083_af8cbfd999d37bbac86691e9c5ffb76f">
</div>
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/p800/84/1084_72a6840640a8650cc01f41d55346973c">
</div>
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/p800/85/1085_5f18260d83ea0790be5a8029b7638872">
</div>
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/p800/86/1086_66daf815df8570e11564f3426c074f3c">
</div>
</div>
</div>
</div>
</div>
Now the all-important question:
Why are my images partially distorted or part of it is cut off.? And how can I solve this?
It is important that the pictures always have the same height. However, the width may vary depending on the picture.
Here you can find a working fiddle where you can see the problem with the images. I have tested it in the latest Chrome version.
https://jsfiddle.net/3cdkxbvm/
Thanks in advance for your help
You probably need something like this:
HTML
<div class="scroll-slider-track">
<div class="image-gallery">
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/l800/83/1083_af8cbfd999d37bbac86691e9c5ffb76f">
</div>
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/p800/84/1084_72a6840640a8650cc01f41d55346973c">
</div>
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/p800/85/1085_5f18260d83ea0790be5a8029b7638872">
</div>
<div class="image-gallery-item">
<img src="https://s1.waazz.com/thumbs/p800/86/1086_66daf815df8570e11564f3426c074f3c">
</div>
</div>
</div>
SCSS
.scroll-slider-track {
// do not set a height for this container
// because the horizontal scroller also needs
// space and this varies in every browser
width: 800px; // set the width you need
overflow: auto;
}
.image-gallery {
height: 300px; // height of the gallery images, set one you like best
display: flex;
flex-flow: row nowrap;
}
.image-gallery-item {
flex: 0 0 auto; // turn off shrink and grow behavior
// needed, so the img element can calculate its height
// using a value in percent:
height: 100%;
&:not(:first-child) { // addressing all items except the first one
margin-left: 1rem;
}
img {
height: 100%;
width: auto; // this will keep the image ratio intact
flex: 0 0 auto; // turn off shrink and grow behavior
display: block; // making sure, no whitespace or line-height issues occur
}
}
Remove margin-right or add margin-right more than 1rem

How to adjust rectangular images to show up as a square in an instagram-like grid of images without losing page responsiveness

If you guys take a look at the code, when I add squared images (first row), the grid will fit perfectly just like instagram. I want to make different shaped images fits in this same squared width and height when I add them (second row).
The problem is, if I just try to make it a square by changing the width and height of the .column > img selector from 100% to 300px for example, the images won't resize in page responsiveness and everything becomes a mess of images overlaying each other.
body {
font-family: "Open Sans", Arial, sans-serif;
}
* {
box-sizing: border-box;
}
.container {
width: 100%;
max-width: 930px;
margin-right: auto;
margin-left: auto;
}
.column {
width: 33.33%;
margin-right: 30px;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 28px;
}
.column>img {
width: 100%;
height: 100%;
}
#media (max-width: 760px) {
.row {
margin-bottom: 3px;
}
.column {
margin-right: 3px;
}
}
#media (max-width: 450px) {
.row {
flex-direction: column;
justify-content: center;
}
.column {
width: 100%;
margin-bottom: 6px;
}
}
<div class="container">
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1498471731312-b6d2b8280c61?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1515023115689-589c33041d3c?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1502630859934-b3b41d18206c?w=500&h=500&fit=crop">
</div>
</div>
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1523354177913-be035fcee55e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1567&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563281746-48bb478e9b2a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1400&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563170446-9c3c0622d8a9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1534&q=80">
</div>
</div>
</div>
Is there a way to make all images a square and at the same time keep the responsiveness of the width and height when resizing the page? Thanks in advance.
I made a solution. In my solution I've used object-fit property to resize image. Check it. Hope it will be helpful to you.
body {
font-family: "Open Sans", Arial, sans-serif;
}
* {
box-sizing: border-box;
}
.container {
width: 100%;
max-width: 930px;
margin-right: auto;
margin-left: auto;
}
.column {
width: 33.33vw; /*These properties are changed*/
height: 33.33vw; /*These properties are changed*/
margin-right: 30px;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 28px;
}
.column>img {
width: 33.33vw; /*These properties are changed*/
height: 33.33vw; /*These properties are changed*/
object-fit: cover; /*These properties are changed*/
}
#media (max-width: 760px) {
.row {
margin-bottom: 3px;
}
.column {
margin-right: 3px;
}
}
#media (max-width: 450px) {
.row {
flex-direction: column;
justify-content: center;
}
.column {
width: 100%;
margin-bottom: 6px;
}
}
<div class="container">
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1498471731312-b6d2b8280c61?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1515023115689-589c33041d3c?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1502630859934-b3b41d18206c?w=500&h=500&fit=crop">
</div>
</div>
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1523354177913-be035fcee55e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1567&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563281746-48bb478e9b2a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1400&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563170446-9c3c0622d8a9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1534&q=80">
</div>
</div>
</div>
You can abuse padding for this.
tl;dr: https://jsfiddle.net/Hoargarth/u247vz0k/ Just look at the fiddle, pretty self explanatory
First of all we are using flexboxes, for this we need a flex container and flex items in it.
<div class="flex-container">
<div class="flex-item">
<div class="image-wrapper">
<div class="image" style="background-image: url('https://www.url.to/image.png');">
</div>
</div>
</div>
<!-- Any number of flex items inside container -->
</div>
I'm not explayning the flex-system here, I guess you should know about it.
But for everyone not knowing about flex, here is a pretty good explanation: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
So we start at the flex-item:
position: relative because we need the next element
(image-wrapper) to be absolute positioned to flex-item
width: 32%
becasue you want 3 images per row, don't forget to give your
flex-container a width
padding-bottom: 32% the padding is the key, 32% padding is relative to the width of the flex-container. So if you use padding-bottom: 100%, the flex-item's height would be exact same like flex-container width. In our case we want a quadratic image, so we use 32%
.flex-item {
position: relative;
width: 32%;
padding-bottom: 32%;
}
Next is image-wrapper:
position: absolute because we don't wanna take this container (or any other container inside this one) to take up any more space.
overflow: hidden to make sure nothing can escape our quadratic image and overlaps to another container
.image-wrapper {
position: absolute;
overflow: hidden;
width: 100%;
height: 100%;
}
And last, the image itself:
No magic here, just add your background image as background-image: url() in html so you don't have to create a lot of extra classes.
position: relative I don't remember why I added it, maybe some crossbrowser related stuff. But you can try to remove it, it's working on Firefox without.
background *-size: cover so all the quadratic space gets filled, no matter what size the image is; *-position: 50% to completely center the image (vertically and horizontally); *-repeat: no-repeat you don't really need it since background-size: cover fills the whole space, I just added it so there is no special case where the image would be repeated
.image {
position: relative;
height: 100%;
background-size: cover;
background-position: 50%;
background-repeat: no-repeat;
}
This solution is responsive out of the box, but you can change the number of Images per Row with simple Media Queries:
Example:
/*Window Width smaller 800px with two images per row*/
.flex-item {
position: relative;
width: 48%;
padding-bottom: 48%;
}
/*Window Width larger or equal 800px with three images per row*/
#media (min-width: 800px){
.flex-item {
width: 32%;
padding-bottom: 32%;
}
}
I hope it's understandable, if not let me know!

Change order of grid items on smaller screens

I am trying to use a media query so that once my layout gets below 800px wide, it goes into one column with the relevant description underneath the image.
But the order is incorrect and I get the images first and then the descriptions.
Where am I going wrong?
.container {
display: grid;
grid-template-columns: 2fr 1fr;
}
.one img {
height: 100%;
width: 100%;
object-fit: cover;
}
.two img {
height: 100%;
width: 100%;
object-fit: cover;
}
.three {
padding: 20px;
background: wheat;
}
.four {
padding: 20px;
background: gray;
}
#media only screen and (max-width: 900px) {
.container {
display: grid;
grid-template-columns: 1fr;
}
}
<div class="container">
<div class="one">
<img src="https://picsum.photos/id/600/600/300">
</div>
<div class="two">
<img src="https://picsum.photos/id/600/601/300">
</div>
<div class="three">
This is the description for the first image
</div>
<div class="four">
This is the description for the second image
</div>
</div>
By default the grid items will be place one below the other (as per the order the grid items appear in the markup) if you specify grid-template-columns: 1fr. You can use grid-row: 2 to the description to the first image (three) - this places it correctly.
See demo below:
.container {
display: grid;
grid-template-columns: 2fr 1fr;
}
.one img {
height: 100%;
width: 100%;
object-fit: cover;
}
.two img {
height: 100%;
width: 100%;
object-fit: cover;
}
.three {
padding: 20px;
background: wheat;
}
.four {
padding: 20px;
background: gray;
}
#media only screen and (max-width: 900px) {
.container {
display: grid;
grid-template-columns: 1fr;
}
.three {
grid-row: 2; /* added */
}
}
<div class="container">
<div class="one">
<img src="https://picsum.photos/id/600/600/300">
</div>
<div class="two">
<img src="https://picsum.photos/id/600/601/300">
</div>
<div class="three">
This is the description for the first image
</div>
<div class="four">
This is the description for the second image
</div>
</div>
From my comment and maybe not the kind of answer you look for ( I was waiting for a feed back, so i go with the idea ... too long to be just a comment).
When you run your HTML witout style, img and description do not match.
You could use figure and figcaption to describe the content and link image with its description , img and description in the same container is enough , a div + a p is fine too.
Default, will let them stack on top of each others, this is what you expect when the screen is less than 900px wide. nothing to do there .
You need to mind when it is wider.that's where your mediaquerie comes usefull.
Here is the demo of the coding idea :
/* commun style */
img {
box-sizing:border-box;
display:block;
width: 100%;
height: 100%;
object-fit: cover;
padding: 1em 1em 0 1em ;
}
figure figcaption{
display:block;
margin:0 1em;
background: wheat;
}
figure:nth-child(even) figcaption{
background:gray
}
/* reordering visual layout when window is wider than 900px */
#media only screen and (min-width: 901px) {
/* grid , what you want to use */
.container {
display:grid;
grid-template-columns:2fr 1fr;
}
/* not the best , we will try to make figure side by side looking like 2 rows .... */
figure {
display:flex;
flex-direction:column;
}
/* to fake the rows, trying to set heights each should fill */
img {
flex:10
}
/* works for about 2 1em lines, then visual breaks */
figcaption {
flex:1
}
/* use of supports in case browser is able to get rid of figure in the way for the grid sytem set on container, This your initial idea, to use the grid model for img and text and draw a grid with cell alignement */
#supports (display:contents) {
figure {
display:contents
}
img {
grid-row:1;
}
}
}
<div class="container">
<figure>
<img src="https://picsum.photos/id/600/600/300">
<figcaption>
This is the description for the first image<br>another line
</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/id/600/601/300">
<figcaption>
This is the description for the second image
</figcaption>
</figure>
</div>
A different structure and the use of display:contents is surely not what you expected , i hope it brings you to learn something instead your answer. See links to usefull ressources below.
The codepen to play with : https://codepen.io/gc-nomade/pen/EJBmee
about display :
https://css-tricks.com/get-ready-for-display-contents/
The issue is that the only way for elements to participate in the same CSS grid together (or flexbox for that matter) is for them to be siblings. So, in some cases we might be incentivized to forego HTML semantics for the benefit of layout (not great).
One answer to this is display: contents;—a magical new display value that essentially makes the container disappear, making the child elements children of the element the next level up in the DOM.
about figure :
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure
The HTML <figure> (Figure With Optional Caption) element represents self-contained content, potentially with an optional caption, which is specified using the (<figcaption>) element. The figure, its caption, and its contents are referenced as a single unit.
about #supports :
https://developer.mozilla.org/en-US/docs/Web/CSS/#supports
The #supports CSS at-rule lets you specify declarations that depend on a browser's support for one or more specific CSS features. This is called a feature query. The rule may be placed at the top level of your code or nested inside any other conditional group at-rule.
The elements in one column are showing up in the order they appear in your code. You have not created any reason for them to appear in any other order.
You could re-order the HTML so they appear in your preferred order.
Or, here's one CSS method – using grid-template-areas – that may work for you:
.container {
display: grid;
grid-template-columns: 2fr 1fr;
grid-template-areas: "one two"
"three four";
}
.one { grid-area: one; }
.two { grid-area: two; }
.three { grid-area: three; }
.four { grid-area: four; }
img {
height: 100%;
width: 100%;
object-fit: cover;
}
.three {
padding: 20px;
background: wheat;
}
.four {
padding: 20px;
background: gray;
}
#media only screen and (max-width: 900px) {
.container {
display: grid;
grid-template-columns: 1fr;
grid-template-areas: "one"
"three"
"two"
"four";
}
}
<div class="container">
<div class="one">
<img src="https://picsum.photos/id/600/600/300">
</div>
<div class="two">
<img src="https://picsum.photos/id/600/601/300">
</div>
<div class="three">
This is the description for the first image
</div>
<div class="four">
This is the description for the second image
</div>
</div>