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
Related
I have a simple slick carousel
<div class="your-class">
<div class="slider">
<img src="assets/img/train.jpg" alt="">
</div>
<div class="slider">
<img src="assets/img/plane.jpg" alt="">
</div>
<div class="slider">
<img src="assets/img/truck.jpg" alt="">
</div>
<div class="slider">
<img src="assets/img/ship.jpg" alt="">
</div>
</div>
.slick-track {
display: flex !important;
}
.slick-slide {
height: inherit !important;
display: flex !important;
justify-content: center;
}
.slider {
max-height: 400px;
}
.slider img {
width: 100%;
height: auto;
}
As you can see I want to change the size of slider to 400px but the image looks flattened. If I remove slider height then the picture is too big with vertical scroll,I need to make img's height be approx 400px. Can you please help what I can do ?
I'm wondering why wrapping img flex items in divs will cause them to scale correctly but img items alone do not.
If you check out this JSFiddle, you'll see that wrapping an image in a div does work as suggested by other posts on StackOverflow, but it doesn't seem to work otherwise. And align-self doesn't change anything as suggested here. I tried to align-items so that it would override the default stretch but that doesn't work. I also tried clearing the min-height and min-width values since those are by default set to auto. This answer justifies using the div wrapper except it doesn't explain why it works for div flexbox items and not for img items directly.
Here's some HTML:
.stack {
display:flex;
max-width:600px;
align-items:center; /* this does not cause image tags to resize */
}
.stack img {
min-height:0;
min-width:0;
height:auto;
align-self:center;
}
.test img {
width:100%;
}
<div class="stack">
<div class="test">
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<!--Wrapping in a div works-->
</div>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
</div>
Why is seemingly the only solution to wrap the image with a div? I don't understand why the only way to scale the image correctly is through a div wrapper.
Does this do what you need?
Markup:
<body>
<div class="stack">
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
</div>
</body>
CSS:
.stack {
display:flex;
}
.stack img {
align-self:center;
width: 100px;
}
https://jsfiddle.net/zbac1dxe/6/
The images are being stretched because the default value of align-self is 'stretch'. To maintain aspect ratio, set align-self to 'center'. I have updated your code snippet to demonstrate this.
Please keep in mind that you have 5 images at 200px width and your container width is defined as 600px, so displaying the images correctly at 200x200 causes the container to display a horizontal scrollbar.
Please see this StackOverflow question: Why does flexbox stretch my image?
.stack {
display:flex;
max-width:600px;
}
.stack img {
align-self: center;
}
<div class="stack">
<div class="test">
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<!--Wrapping in a div works-->
</div>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
<img src="https://placehold.it/200x200/E8117F/FFFFFF"/>
</div>
with CSS flexbox, you need to address the child element directly from a wrapping parent.
In your example, in order to prevent a repeating <div><img><div> structure, you just remove <div class="test"></div>.
in case you need to apply a class (like in your example) to the image you can.
.stack {
display: flex;
max-width: 600px;
align-items: center;
border:1px solid gold;
}
.stack img {
min-height: 0;
min-width: 0;
height: auto;
width:200px;
align-self: center;
}
.test img {
width: 100%;
}
.test:nth-of-type(1) {
border: 2px solid #000;
}
<body>
<div class="stack">
<img class="test" src="http://picsum.photos/200/200" />
<img class="test" src="http://picsum.photos/200/200" />
<img class="test" src="http://picsum.photos/200/200" />
<img class="test" src="http://picsum.photos/200/200" />
<img class="test" src="http://picsum.photos/200/200" />
<img class="test" src="http://picsum.photos/200/200" />
</div>
</body>
Resize your browser to see it work...
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%;
}
I need to align images like this photo. My code is:
<html>
<head>
<style>
.main
{
width: 634px;
height: 634px;
}
.img1
{
width: 315px;
height: 315px;
}
</style>
</head>
<body>
<img src="photo/01.jpg" class="main"><br>
<img src="photo/05.jpg" class="img1">
<img src="photo/01.jpg" class="img1">
<img src="photo/01.jpg" class="img1">
</body>
</html>
I want to create instaframe effect on html page. But I cant add images to the right side
You can use floating to achieve your desired effect:
.main {
width:80%; /* width can be anything */
overflow:auto; /* clears floating */
}
.main img {
width:33.33%; /* images are responsive, usually 3 images per row (33.33) */
height: auto; /* resize height based on width to keep image proportion */
float:left; /* float images to the left */
border:2px solid white; /* optional border */
box-sizing:border-box; /* makes sure border does not break total width */
}
.main img.big {
width:66.66%; /* big image takes 2/3 of grid size, so 66.66 of 100 */
}
<div class="main">
<img src="https://placehold.it/100x100" class="big">
<img src="https://placehold.it/100x100">
<img src="https://placehold.it/100x100">
<img src="https://placehold.it/100x100">
<img src="https://placehold.it/100x100">
<img src="https://placehold.it/100x100">
</div>
It is best to wrap the floated elements inside a common parent so that they do not affect the rest of page elements. In this case, the parent is <div class="main">.
jsFiddle
You can use CSS3 flexbox to achieve this;
See code below; you probably also want to use % or ems instead of fixed height/width;
using float as per answer above is more beautiful of course, flexbox is just one more way to achieve same results
Demo: jsFiddle
.main {
width: 300px;
height: 300px;
}
.img {
width: 150px;
height: 150px;
}
.rowContainer {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.columnContainer {
display: flex;
flex-direction: column;
}
.mainContainer {
width: 450px
}
<div class="mainContainer">
<div class="columnContainer">
<div class="rowContainer">
<img class="main">
<div class="columnContainer">
<img class="img">
<img class="img">
</div>
</div>
<div class="rowContainer">
<img class="img">
<img class="img">
<img class="img">
<img class="img">
<img class="img">
<img class="img">
</div>
</div>
</div>
I have this div with images inside it, what I am trying to do is get the images to go all the way across and have the user be able to scroll through them across...I hope this makes sense. Here is my code:
<div id="gallerySet1">
<img src="images/1.2.jpg">
<img src="images/1.3.jpg">
<img src="images/1.4.jpg">
<img src="images/1.5.jpg">
<img src="images/1.6.jpg">
<img src="images/1.7.jpg">
<img src="images/1.8.jpg">
<img src="images/1.9.jpg">
<img src="images/1.10.jpg">
<img src="images/1.11.jpg">
<img src="images/1.12.jpg">
</div>
and the CSS:
#gallerySet1{
display:none;
z-index:1000000;
position:absolute;
top:1500px;
}
#gallerySet1 img{
float:left;
height:400px;
}
I am trying to display my images like this:
http://annasafroncik.it/#galleria (click on one of the 3 images)
You need to use a second div to wrap all the items (this is how slideshows work also). If you're using strictly css, you need to make sure the min-width == the total width of the images. If you want this to be a bit more dynamic you may want to use some JavaScript/jQuery to calculate the width of all the images and set the width of .wrapper to that width (which I've included).
DEMO
HTML:
<div id="gallerySet1">
<div class="wrapper">
<img src="http://placekitten.com/300/300">
<img src="http://placekitten.com/100/300">
<img src="http://placekitten.com/400/300">
<img src="http://placekitten.com/240/300">
<img src="http://placekitten.com/100/300">
<img src="http://placekitten.com/40/300">
<img src="http://placekitten.com/300/300">
<img src="http://placekitten.com/200/300">
<img src="http://placekitten.com/100/300">
<img src="http://placekitten.com/200/300">
<img src="http://placekitten.com/200/300">
</wrapper>
</div>
CSS:
#gallerySet1 {
width: 600px;
height: 300px;
overflow-x: scroll;
}
.wrapper {
min-width: 2200px;
}
.wrapper:after {
clear: both;
content:'';
display: table;
}
img {
float: left;
}
jQuery:
$(document).ready(function () {
var imgs = $('img'),
width = 0,
wrapper = $('.wrapper');
imgs.each(function (index) {
width += $(this).width();
});
wrapper.width(width);
});