How can I make my images responsive aligned in a scrollable row? - html

I'm trying to align my images for a photography portfolio in a single, scrollable row and also want to make them responsive to window resizing and other screen sizes. Essentially to just have them shrink and grow as the screen changes. Previously I had them in a scrollable row but now that I've tried to make them responsive, they just adjust and shrink down and show the entire image gallery without scroll, but are really small since they are all trying to fit inside the window.
Here's what I have so far
img {
display: block;
width: 100%;
transition: .3s;
}
.flex-container {
display: flex;
padding: 5px;
transition: .3s;
overflow-x: scroll;
}
.sub-container {
display: flex;
flex-direction: row;
overflow-x: auto;
/*width: 50%;*/
}
.gallery__thumb {
position: relative;
}
<div class="flex-container">
<div class = "sub-container">
<figure class="gallery__thumb">
<img src="highq.jpg" alt="">
</figure>
<figure class="gallery__thumb">
<img src="highq.jpg" alt="">
</figure>
<figure class="gallery__thumb">
<img src="highq.jpg" alt="">
</figure>
<figure class="gallery__thumb">
<img src="highq.jpg" alt="">
</figure>
<figure class="gallery__thumb">
<img src="highq.jpg" alt="">
</figure>
</div>
</div>

You may want to consider adding a min-width to the images in percentage units of measurement. This will allow the images to grow in size and shrink always keeping the minimum amount of length as a percentage of the parents total width.
In the example below I reset margin and padding to 0 using a universal selector => *, this will remove any unseen/unwanted margin or padding set by default browser settings for elements.
Then we align the flex sub-container vertically using align-items: center; each figure element .gallery__thumb will set min-width: 33%. A slight gap of 10px margin between each remaining .gallery__thumb using general sibling combinator ~ => .gallery__thumb ~ .gallery__thumb { margin-left: 10px; }.
Now as you resize the browser there will always be 3 images showing no matter the size of the browser. If you want 4 to show, change to 25%, 5 to 20%, etc... In some cases, you may need to add a calculated measurement using width: calc(33% - 20px), where 20 pixels may represent 10 pixels of left and right margin on a parent container for example.
See resizing browser GIF
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
img {
display: block;
width: 100%;
}
.gallery__thumb ~ .gallery__thumb {
margin-left: 10px;
}
.sub-container {
display: flex;
overflow-x: auto;
align-items: center;
}
.gallery__thumb {
min-width: 33%;
}
<div class="flex-container">
<div class="sub-container">
<figure class="gallery__thumb">
<img src="https://placeimg.com/300/260/any" alt="">
</figure>
<figure class="gallery__thumb">
<img src="https://placeimg.com/320/260/any" alt="">
</figure>
<figure class="gallery__thumb">
<img src="https://placeimg.com/310/280/any" alt="">
</figure>
<figure class="gallery__thumb">
<img src="https://placeimg.com/340/280/any" alt="">
</figure>
<figure class="gallery__thumb">
<img src="https://placeimg.com/300/260/any" alt="">
</figure>
</div>
</div>

Related

Why is my 1st code images behaving differently compared to my 2nd code background-images despite using the same width unit # 30%?

1st Code:
https://jsfiddle.net/dmbgc4qz/1/
div {
display: inline-block;
height: 60vh;
width: 30%;
margin: 2.5rem 1.5rem 0;
background: url("https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-
1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80");
text-align: center;
background-size: cover;
background-position: center;
}
The images from this code which are background-images gets progressively thinner when I change the size of the screen and it eventually turn to 1 image as the size of the screen gets smaller.
2nd Code:
https://jsfiddle.net/dmbgc4qz/2/
img {
width: 30%;
}
The images from this code which are html do not get progressively thinner and remain as triplet per line even when the size of the screen is altered.
Can anybody please explain this phenomenon to me? Why is the image behaving so differently despite same width unit provided? Is it because of the display property?
This is because you didn't specify the margin for the img so if you add this margin: 2.5rem 1.5rem 0; it will act similarly to the first one
img {
width: 30%;
margin: 2.5rem 1.5rem 0;
height:60vh;
object-fit:cover;
object-position:center;
}
<body>
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
<img src="https://images.unsplash.com/photo-1648070201547-9d6ef53d3a51?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80" alt="">
</body>
</html>

images with different sizes side by side?

I have 5 images and their width and height are different to each other.
I'm trying to put these 5 pictures side by side in the center and make sure so the height of every imags is the same. I have tried flex, grid, float and positions to make it as close as possible, but still, some images are some pixels heigher or lower while one picture is very small compared to other.
html.
<div class="container">
<img class="leagues" id="l1" src="assets/img/ligue1.png" alt="">
<img class="leagues" id="pl" src="assets/img/premierleague.png" alt="">
<img class="leagues" id="cl" src="assets/img/championsleague.png" alt="">
<img class="leagues" id="la" src="assets/img/laliga.png" alt="">
<img class="leagues" id="bl" src="assets/img/bundesliga.png" alt="">
</div>
css.
.leagues {
display: flex;
flex-wrap: wrap;
flex-direction: row;
float: left;
overflow: hidden;
max-height: 160px;
transform: translate(-50%, -50%);
left: 50%;
top: 50%;
}
If there are any tutorials or sites that would show how to put images with different sizes side by side and make the same height by using Grid or Flexbox, that would be nice.
Thank you.
EDIT:
cleaned up the CSS and it looks like this now.
.container {
display: flex;
justify-content: center;
}
.leagues {
height: 200px;
width: auto;
}
and here is the image of how it looks.
as you can see, "premier league" logo is much smaller than others and the ligue1 logo is 2-3px smaller than rest.
https://imgur.com/a/aS9UPth
TL,DR; This is probably what you're looking for:
.container > img {
height: 100%;
width: auto;
}
.container {
height: 160px;
display: flex;
justify-content: center;
}
Actual answer:
You need to specify which is the height all images should be displayed at.
You might want to specify it using CSS or use either the shortest or the tallest image:
Hardcode using CSS:
.container > img {
height: 150px;
width: auto;
}
<div class="container">
<img src="https://via.placeholder.com/150x100" alt="">
<img src="https://via.placeholder.com/150x125" alt="">
<img src="https://via.placeholder.com/150x150" alt="">
<img src="https://via.placeholder.com/150x175" alt="">
<img src="https://via.placeholder.com/150x200" alt="">
</div>
Use the shortest image:
$(window).on('load', function(){
let height;
$.each($('.container'), function(_i, container){
$.each($('img', container), function (_i, img) {
height = height
? Math.min(height, $(img).height())
: $(img).height();
});
$.each($('img', container), function(_i, img){ $(img).height(height) });
});
})
.container > img {
width: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<img src="https://via.placeholder.com/150x100" alt="">
<img src="https://via.placeholder.com/150x125" alt="">
<img src="https://via.placeholder.com/150x150" alt="">
<img src="https://via.placeholder.com/150x175" alt="">
<img src="https://via.placeholder.com/150x200" alt="">
</div>
Use the tallest image:
$(window).on('load', function(){
let height;
$.each($('.container'), function(_i, container){
$.each($('img', container), function (_i, img) {
height = height
? Math.max(height, $(img).height())
: $(img).height();
});
$.each($('img', container), function(_i, img){ $(img).height(height) });
});
})
.container > img {
width: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<img src="https://via.placeholder.com/150x100" alt="">
<img src="https://via.placeholder.com/150x125" alt="">
<img src="https://via.placeholder.com/150x150" alt="">
<img src="https://via.placeholder.com/150x175" alt="">
<img src="https://via.placeholder.com/150x200" alt="">
</div>
As you can see, as long as you specify the same height for all, and width:auto they will already be displayed beside one another and all you need to do is center the .container in its parent, by adding display: flex; justify-content: center; to .container's parent.
Example:
$(window).on('load', function(){
let height;
$.each($('.container'), function(_i, container){
$.each($('img', container), function (_i, img) {
height = height
? Math.min(height, $(img).height())
: $(img).height();
});
$.each($('img', container), function(_i, img){ $(img).height(height) });
});
})
.container > img {
width: auto;
}
.center-me {
display: flex;
justify-content: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="center-me">
<div class="container"
><img src="https://via.placeholder.com/150x100" alt=""
><img src="https://via.placeholder.com/150x125" alt=""
><img src="https://via.placeholder.com/150x150" alt=""
><img src="https://via.placeholder.com/150x175" alt=""
><img src="https://via.placeholder.com/150x200" alt=""
></div>
</div>
P.S: I used jQuery to get and set image's height as it's less verbose than vanilla. If anyone needs it, I can write the jQuery-less equivalent, but it seemed overkill here.
P.S 2: You might want to replace .container selector with a more specific one if you don't want all the images in each of your .containers get reduced or enlarged to the shortest or tallest one in that particular container. This applies to both JS and CSS.
Last, but not least, the height might as well be hard-coded on the parent element and that would require no JavaScript, pretty much like the first example:
.container > img {
height: 100%;
width: auto;
}
.container {
height: 120px;
display: flex;
justify-content: center;
}
<div class="container">
<img src="https://via.placeholder.com/150x100" alt="">
<img src="https://via.placeholder.com/150x125" alt="">
<img src="https://via.placeholder.com/150x150" alt="">
<img src="https://via.placeholder.com/150x175" alt="">
<img src="https://via.placeholder.com/150x200" alt="">
</div>
display:flex must be given to the parent element , and if you want them same height , you should set a value for height and let width to be auto , if total width of all images don't be bigger than their parent , they will be in a row .
see flex box tutorial on w3schools :
CSS flex Property
CSS Flexbox
either make their height the same (changing the picture by cropping it) or giving them a specific height

Issue with display: flex & column-count: 2

I am trying to create two columns that are a certain width. Both columns have an image and text below the image. I have managed this with:
.quick-links img {
height: 7.5em;
width: 7.5em;
border-radius: 10em;
margin: 0 15em;
}
.quick-links {
display: flex;
justify-content: center;
}
However, whenever the website shrinks down past about a width of 1200px something weird begins to happen:
https://gyazo.com/5f01aa7248873d999dd6552e089b1c2f
This is extremely annoying because what I thought would be something quite simple to make, has turned out to be a nightmare!
I have also tried:
.quick-links img {
height: 7.5em;
width: 7.5em;
border-radius: 10em;
margin: 0 15em;
}
.quick-links {
column-count: 2;
display; inline-block;
}
However, this issue then occurs:
I am very stumped and would love for someone to help me.
For reference, here is the HTML:
<section class="quicklinks">
<h1>Quicklinks</h1>
<div class="quick-links">
<!-- Client Reviews -->
<figure class="reviews">
<img src="images/RatingsTick.png" alt="Ratings Tick Image">
<figcaption class="port-desc">
<p><strong>Reviews</strong></p>
<p>-Filler Text-</p>
</figcaption>
</figure>
<!-- Portfolio -->
<figure class="portfolio">
<img src="images/Portfolio.png" alt="Portfolio Image">
<figcaption class="port-desc">
<p><strong>Previous Work</strong></p>
<p>-Filler Text-</p>
</figcaption>
</figure>
</div>
</section>
To allow flex children to wrap you need to set flex-wrap:wrap;.
You may also set a min-width to create a break point almost alike mediaquerie would.
.quick-links {
display: flex;
align-items: center;
justify-content: center;
max-width: 100%;
text-align: center;
flex-wrap: wrap;
}
figure {
min-width:25em;/* or whatever value suits your needs */
border: solid;/* see me */
}
h1 {
text-align: center;
}
<section class="quicklinks">
<h1>Quicklinks</h1>
<div class="quick-links">
<!-- Client Reviews -->
<figure class="reviews">
<img src="images/RatingsTick.png" alt="Ratings Tick Image">
<figcaption class="port-desc">
<p><strong>Reviews</strong></p>
<p>-Filler Text-</p>
</figcaption>
</figure>
<!-- Portfolio -->
<figure class="portfolio">
<img src="images/Portfolio.png" alt="Portfolio Image">
<figcaption class="port-desc">
<p><strong>Previous Work</strong></p>
<p>-Filler Text-</p>
</figcaption>
</figure>
</div>
</section>
About column-count, it can seem usefull but this is a CSS rule that unfortunately remained in the css draft, flex seems much more efficient here.
Remove the margin from the img elements and it will center properly.
Use .quick-links figure { min-width: 500px;} to set a minimum width on the text.

Center middle part of image within div

The image is wider than the surrounding div.
This is for mobil units. The image within the div
has width:auto; and the div holding it is as wide
as the visible area. So if the div is 500px the
image might vary having 1000px or so. But I want
to show the middle part of image only.
Im using an old version of OWL Carousel v1.3.3
<div id="custom-owl-slider" class="owl-slide" data-scroll-speed="2">
<div class="item">
<img src="images/slider/IMG_3455.png" alt="">
</div>
<div class="item">
<img src="images/slider/IMG_771.JPG" alt="">
</div>
<div class="item">
<img src="images/slider/IMG_321.JPG" alt="">
</div>
<div class="item">
<img src="images/slider/IMG_344.jpeg" alt="">
</div>
<div class="item">
<img src="images/slider/IMG_563.jpeg" alt="">
</div>
<div class="item">
<img src="images/slider/IMG_135.jpeg" alt="">
</div>
</div>
Thanks!
FIXED with the following:
#custom-owl-slider .owl-item {
display: flex !important;
justify-content: center !important;
height: auto; /* or other desired height */
overflow: hidden !important;
}
#custom-owl-slider .owl-item img {
flex: none !important; /* keep aspect ratio */
}
try this for img middle
.item > img {
text-align: center;
overflow: hidden;
}
PD: if you want, this code should fit the image on the div
.item > img{
width: 100%;
}

How do I make 3 images which span across the width of the site responsive?

So I would like to have 3 images along the full width of the website, each of them acting as 1/3. I would like them to be responsive and scale accordingly on different screen sizes and have them maintain aspect ratio, any ideas how I would go about that?
So far I have managed this, however they shrink very unnaturally when the site is scaled down.
Here's the code for it so far:
#content {
height: auto;
width: 100%;
font-size: 0px;
display: flex;
}
.images {
width: 33.33%;
height: 800px;
}
<div id="content">
<img src="images/phantom.png" class="images" alt="Image of an actor from the musical Phantom of the Opera">
<img src="images/lion_king.png" class="images" alt="Image of an actor from the musical Lion King">
<img src="images/wicked.png" class="images" alt="Image of an actor from the musical Wicked">
</div>
you were almost there,wrap the images in a div then set those divs flex-basis one third, you can do that by using calc() and set (max-)width:100% to imgs
EDIT - OP's comments
This works, however the heights of the image are messed up and all different heights, not flush with the footer. How would I fix the
heights too
Because you have images with different ratio sizes, you have to add display:flex to div
body {
margin: 0
}
#content {
height: auto;
width: 100%;
display: flex;
}
#content>div {
flex: 0 calc(100%/3);
display: flex;
/*demo*/
box-sizing: border-box;
border: 2px solid red
}
img {
width: 100%;
display: block
}
<div id="content">
<div>
<img src="//placehold.it/300x800" class="images" alt="Image of an actor from the musical Phantom of the Opera"> </div>
<div>
<img src="//placehold.it/300x800" class="images" alt="Image of an actor from the musical Lion King">
</div>
<div>
<img src="//placehold.it/300x800" class="images" alt="Image of an actor from the musical Wicked"></div>
</div>
Put them in a container element and set the images to width: 100% or max-width: 100%.
#content {
height: auto;
width: 100%;
font-size: 0px;
display: flex;
}
.images {
width: 100%;
}
<div id="content">
<div>
<img src="http://kenwheeler.github.io/slick/img/fonz1.png" class="images" alt="Image of an actor from the musical Phantom of the Opera">
</div>
<div>
<img src="http://kenwheeler.github.io/slick/img/fonz1.png" class="images" alt="Image of an actor from the musical Lion King">
</div>
<div>
<img src="http://kenwheeler.github.io/slick/img/fonz1.png" class="images" alt="Image of an actor from the musical Wicked">
</div>
</div>