Align a number of images in a container - html

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>

Related

Apply a fixed px unit for each image

I have an image that is a card i need to repeat it 30 times, and on each time i want to apply for a specific card a left position so it overlaps the card before staying on a position like a deck.
The thing is, when i apply a left position to the image card, it applies the same left position to all cards, so there is no overlap position.
So i have something like this:
<template>
<div class="triPack">
<img :style="{'left': -index * 30}" v-for="index in 30" :key="index" src="../../assets/images/cardThemes/blue.png" alt="">
</div>
</template>
<script>
export default {
}
</script>
<style lang="scss">
.triPack {
display: flex;
img {
width: 80px;
height: 100px;
position: relative;
}
}
</style>
i want something like this:
Thank you guys.
You can do it with only css margin-left except the first image :not(:first-child)
So remove it :style="{'left': -index * 30}"
.triPack {
display: flex; }
img {
width: 80px;
height: 100px;
position: relative;
}
img:not(:first-child) {
margin-left: -45px;
}
<div class="triPack">
<img src="https://i.stack.imgur.com/XG0gL.png" alt="">
<img src="https://i.stack.imgur.com/XG0gL.png" alt="">
<img src="https://i.stack.imgur.com/XG0gL.png" alt="">
<img src="https://i.stack.imgur.com/XG0gL.png" alt="">
</div>

css instaframe effect on html page

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>

CSS center row of images on window resize

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

Scale image bug

My html:
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/ef/1e/45/ef1e450945a5a7ff0c4b7776810d4f90.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/0e/3b/85/0e3b858ffcfdbfa02b562c3dc7e3b5e1.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/df/c7/88/dfc7889e5dd99ad0c45834b4e4675389.jpg" alt="my img" />
</div>
My css:
.wrapper {
margin: 0;
padding: 0;
}
.scale-img {
transform: scale(0.5);
transform-origin: top left;
}
In my page I have several imgs, and each of the img's size is different, I don't want to set size in css for each img, and I just want to scale each img to half of its original size. But the wrapper div stay the img its original size, I don't know where go wrong?
Try this... For better understanding, I have used a dummy image.
.wrapper {
margin: 0;
padding: 0;
border: 1px solid #000;
position:absolute;
}
.scale-img{
width:100px;
height:100px;
position: relative;
}
<div class="wrapper">
<img class="scale-img" src="http://www.gettyimages.ca/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg" alt="my img" />
</div>
The reason transform doesn't affect the parent is because it "modifies the coordinate space of the CSS visual formatting model".
So in effect, it's sort of like doing this:
#container {
height:200px;
width:200px;
border:1px solid;
position:relative;
}
#container span {
background:red;
position:absolute;
top:50px;
left:50px;
right:50px;
bottom:50px;
}
<div id="container">
<span></span>
</div>
In an attempt to fix your issue, you'll more than likely need a bit of JavaScript:
var objScaleIMGs = document.querySelectorAll('img.scale-img') // Grab all the images that need scaling.
for (var i = 0; i < objScaleIMGs.length; i++) { // Loop through all the images, setting their dimensions half of what they currently are.
var h = objScaleIMGs[i].height;
var w = objScaleIMGs[i].width;
objScaleIMGs[i].height = h / 2;
objScaleIMGs[i].width = w / 2;
}
.wrapper {
margin: 0;
padding: 0;
border: 1px solid;
float: left;
}
.scale-img {
float: left;
}
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/ef/1e/45/ef1e450945a5a7ff0c4b7776810d4f90.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/0e/3b/85/0e3b858ffcfdbfa02b562c3dc7e3b5e1.jpg" alt="my img" />
</div>
<div class="wrapper">
<img class="scale-img" src="https://s-media-cache-ak0.pinimg.com/736x/df/c7/88/dfc7889e5dd99ad0c45834b4e4675389.jpg" alt="my img" />
</div>
You should transform the wrapper instead if you want to change the div's size with the change in img original size. The reason being that scale is used for img and not for wrapper div.
.wrapper {
margin: 0;
padding: 0;
height: 960px;
width: 540px;
}
.scale-img {
height:100%;
width:100%
}
You should refer: CSS Transform with element resizing
The problem I noticed is that when element scales, browser change its pixels ratio, not pixels amount. Element is smaller but it doesn't change its actual pixel size in DOM. Because of that I don't think that CSS-only solution exist.

CSS have div of images go across and scroll

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);
});