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);
});
Related
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
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>
Hi I'm trying to create an image gallery that centers rows of images with fixed dimensions. The issue is the number of images in each row will change depending on the window size so I can't use mutiple containers and margin: auto them. Here is an example page that does what I'm after:
http://inspire-gwen.tumblr.com/
You'll notice that as you change the size of the window, the image rows change, but each one is still centered on the page. Is it possible to implement this with purely CSS? This is the code I have written, with some random images:
HTML
<!DOCTYPE HTML>
<body>
<div class="img_container">
<div><img src="http://www.tolooeyaran.com/wp-content/uploads/2014/09/City-Of-Paris-France-Tour-Eiffel-640x360.jpg"></div>
<div><img src="https://lh3.googleusercontent.com/-h0DGPrWkU-M/AAAAAAAAAAI/AAAAAAAABJY/0l_GW_IzQk4/photo.jpg"></div>
<div><img src="http://i.imgur.com/ElxvqJK.jpg"></div>
<div><img src="http://i-cdn.phonearena.com/images/articles/168223-image/First-HTC-One-M9-wallpaper.jpg"></div>
</div>
</body>
CSS
.img_container img {
max-height: 300px;
display: block;
width: auto;
height: auto;
vertical-align: bottom;
}
.img_container div {
padding: 5px;
}
Images are by default inline items, wrapping them in 'div' tags is currently causing them to be block items. You could get by with simpler with this:
HTML
<!DOCTYPE HTML>
<body>
<div class="img_container">
<img src="http://www.tolooeyaran.com/wp-content/uploads/2014/09/City-Of-Paris-France-Tour-Eiffel-640x360.jpg">
<img src="https://lh3.googleusercontent.com/-h0DGPrWkU-M/AAAAAAAAAAI/AAAAAAAABJY/0l_GW_IzQk4/photo.jpg">
<img src="http://i.imgur.com/ElxvqJK.jpg">
<img src="http://i-cdn.phonearena.com/images/articles/168223-image/First-HTC-One-M9-wallpaper.jpg">
</div>
</body>
CSS
.img_container {
text-align: center;
}
.img_container img {
max-height: 300px;
width: auto;
height: auto;
vertical-align: bottom;
margin: 5px;
}
Yes, this is very possible..
so far you have been using px to define the image width... what you need is %.
Here is the JSFIDDLE: http://jsfiddle.net/uh1wvaev/2/
Here is my code:
HTML
<div class="img_container">
<div class="img_wrapper">
<img src="http://www.tolooeyaran.com/wp-content/uploads/2014/09/City-Of-Paris-France-Tour-Eiffel-640x360.jpg">
</div>
<div class="img_wrapper">
<img src="https://lh3.googleusercontent.com/-h0DGPrWkU-M/AAAAAAAAAAI/AAAAAAAABJY/0l_GW_IzQk4/photo.jpg">
</div>
<div class="img_wrapper">
<img src="http://i.imgur.com/ElxvqJK.jpg">
</div>
<div class="img_wrapper">
<img src="http://i-cdn.phonearena.com/images/articles/168223-image/First-HTC-One-M9-wallpaper.jpg">
</div>
</div>
CSS
.img_wrapper {
width: 25%;
float: left;
}
img {
width: 100%;
}
What I did was, I gave each <DIV></DIV> a class of "img_wrapper". Then I gave each img_wrapper a width of 25% of the page. Therefore whenever the page is resized, the img will be given a width of 25% of the new window size.
If you have any questions, or need further assistance please leave a comment below
In my page i have 2 images that i want show one of them on the another one, this is my code, how can i set images:
<div class="profile-image img-responsive">
<img src="images/pro.png"/>
<img src="images/2.png" class="img-circle">
</div>
JSFIDDLE
I want to set 2.png onto the left and bottom pro.png like this:
How can I do this?
The following code will display the second image over the first image. All the images and the image panel are responsive.
1.CSS
.profile-img {
width: 100%;
position: relative;
}
.img-responsive {
max-width:100%;
height:auto;
}
.img-circle {
position:absolute;
z-index:99;
left:10px;
bottom:-50%;
}
2.HTML
<div class="profile-img">
<img class="img-responsive" src="images/pro.png" />
<img class="img-circle img-responsive" src="images/2.png" />
</div>
#bnuhero's answer is very good, and I would change it just a little.
First, basing off the jsfiddle, encapsulate the two images in a relative div. This will allow the image placed on top, when defined as "absolute" to be placed relative to the parent div, rather than relative to the entire window.
<div class="header-image">
<div class="profile-image" style="margin: 50px auto;">
<img src="http://s9.postimg.org/41fwo4k4v/pro.png" class="img-responsive"/>
<img src="http://s24.postimg.org/on764040x/image.png" class="img-circle img-responsive img-on-top">
</div>
</div>
</div>
Next, make a rule to define the realtive div. And make an additional class or id to target the image to be placed on top. Writing the rules with the same 'img-circle' class will interfere with the default 'img-circle' rules setup by Bootstrap
.header-image {
position:relative;
}
.img-on-top {
position:absolute;
z-index:99;
left:10px;
bottom:-50%;
}
Updated jsfiddle at http://jsfiddle.net/Bavc_Am/jG9bP/1/
Update your css to this
.img-circle {
border-radius: 50%;
position: relative;
top: -69px;
left: 10px;
}
I have a unknown number of images (max. three) that I am programmatically adding to a html output.
I would like to put the images in a container so that they are automatically resized in a way that the container itself does not exceed a fixed width.
I tried various solutions such as a modified version of the answer mentioned here (Center image in container element with fixed size (CSS, HTML)), but my solution below keeps flowing over the boundaries container if the images widths exceed its fixed width.
### HTML ###
<div id="container">
<div class="image-container">
<img src="#" alt="Image One" />
<img src="#" alt="Image Two" />
<img src="#" alt="Image Three" />
</div>
</div>
### CSS ###
#container {
width: 400px;
height: 100px;
display: table;
background-color: #ccc;
}
#container .image-container {
text-align: center;
vertical-align: middle;
display: table-cell;
}
#container .image-container img {
max-width: 100px;
max-height: 60px;
}
Edit:
I'm not a javascript wizz, but the following did the trick for me. The fixed image width and height seen in the example are set by the program that is putting this HTML output together.
<script type="text/javascript">
<!--
function resize_image( img ) {
var newSize = scaleSize(120,100,img.width,img.height);
// alert(newSize);
img.width=newSize[0]; img.height=newSize[1];
}
function scaleSize(maxW, maxH, currW, currH) {
var ratioW=currW/maxW;
var ratioH=currH/maxH;
var ratio=1;
if (ratioW>ratioH) ratio=ratioW; else ratio=ratioH;
if (ratio>1) {
currH=currH/ratio;
currW=currW/ratio;
}
return [currW,currH];
}
-->
</script>
Images are then loaded with
<div class="s1">
<img id="img0" src="1.png" class="img_class" onload="resize_image(document.getElementById('img0')); return true;"/>
<img id="img1" src="2.png" class="img_class" onload="resize_image(document.getElementById('img1')); return true;"/>
</div>
<style type="text/css">
.s1 { width:320px; height:100px; text-align: center; vertical-align: middle; }
.img_class { display: inline-block; vertical-align:middle; }
</style>