Scale image bug - html

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.

Related

Create a collage of images in CSS

I'm working on a project and I got to a place where I decided to realized a collage.
I have done the collage already and it is here: Fiddle
But I would like to do the same thing without using the property float on the elements of div1.
If possible, I would like to use only the properties right and left on the element of div1 without reducing the size of the elements of that div.
Sorry I'm not a native English speaker, I'm trying my best.
Hopefully, you would like to see something like this:
body {
width: 380px;
margin: 0 auto;
}
.div1{
text-align:justify;
}
.div1:after {
content: "";
display: inline-block;
width: 100%;
}
.div1 span img{
width:45%;
}
<div class="div1">
<span><img src="https://picsum.photos/189/324?random=1" alt="rd1"/></span>
<span><img src="https://picsum.photos/189/324?random=2" alt="rd2"/></span>
</div>
<div class="div2">
<img src="https://picsum.photos/380/325?random=3" alt="rd3"/>
</div>
My comment: I have set an 'img' width value to 45%, but you could set it to any value which will be equal or less than 50%. By the way, I suggest you to use tag 'span' instead of 'p' because 'span' ,in my opinion, is more suitable for this purpose. If you will use my approach you will be able to add any number of images to parent 'div' tag. For example, if you will add any number of images and their total width will be equal or less than 100%, these images are will be placed evenly on row.
This is what you asked for. But I recommend not to go with this approach. Extending it will be a nightmare. If image sizes change then it will be impossible to fix with with just CSS.
body {
width: 380px;
margin: 0 auto;
}
.div1{
position:relative;
height:324px;
}
.div1 p {
margin-bottom: 0;
margin-top: 0;
width:50%;
position:absolute;
}
.div1 p:first-child {
left:0px;
}
.div1 p:last-child {
left:50%;
}
<div class="div1">
<p><img src="http://lorempixel.com/189/324/" alt=""/></p>
<p><img src="http://lorempixel.com/189/324/" alt=""/></p>
</div>
<div class="div2">
<img src="http://lorempixel.com/380/325/" alt=""/>
</div>
you can use display: flex; like that you dont have to use float
body {
/* width: 520px;*/
margin: 0 auto;
}
/*.div1 p {
margin-bottom: 0;
margin-top: 0;
}*/
.div1 {
display:flex;
justify-content:center;
}
.div2{
display:flex;
justify-content:center;
}
<div class="div1">
<img src="http://lorempixel.com/189/324/" alt=""/>
<img src="http://lorempixel.com/189/324/" alt=""/>
</div>
<div class="div2">
<img src="http://lorempixel.com/380/325/" alt=""/>
</div>
you mean something like this?
.div1 {
left: 0;
right: 0;
text-align: center;
margin: auto;
}
.div2 {
left: 0;
right: 0;
text-align: center;
margin: auto;
}
<div class="div1">
<img src="http://lorempixel.com/189/324/" alt=""/>
<img src="http://lorempixel.com/189/324/" alt=""/>
</div>
<div class="div2">
<img src="http://lorempixel.com/380/325/" alt=""/>
</div>

Block that simultes images {width: 100%; height : auto} behaviour

I want to create the following layout :
Is a stripe of a variable number of images that have various widths and heights, that are:
proportional
scaled at the same height;
and the sum of their widths are equal to the parent width.
***It's kind of complicated to express myself;
I was wondering if it's possible for a block to simulate the img neat proportion behavior when you set a width to a percentage and it calculates the height of it automagically.
I've made up a diagram that maybe explain better what I want to achieve :
I want for the image to have collectively 100% width of the parent element, scaled with at the same height without loosing their proportion.
I've tried various implementations trying to figure out a way in which I can translate compute a percentage height in css that fills all the width for a block, just how the image behaves when there are {width: 100%; height : auto} properties.
So here is what I've got so far :
Strike #1, tried a simple solution
Problem: container height must be predefined.
.container {
width : 100%;
border: 1px solid black;
height: 50px; /* I would like to say here auto */
}
.image-wrapper {
white-space: nowrap;
height: 100%;
max-width: 100%;
border: 1px dashed gray;
}
.image {
height: 100%;
width: auto;
}
<div class="container">
<div class="image-wrapper">
<img class="image" src="http://placehold.it/100x200" />
<img class="image" src="http://placehold.it/300x200" />
<img class="image" src="http://placehold.it/800x400" />
<img class="image" src="http://placehold.it/10x80" />
<img class="image" src="http://placehold.it/800x400" />
</div>
</div>
Strike #2, display: table anyone ?
Problem: Don't even need to mention it, images are cropped the container size doesn't follow its parent size .
.container-wrapper {
width: 40px;
height: 50px;
}
.container {
width : 100%;
border: 1px solid black;
display: table;
height: 100%;
}
.image-wrapper {
display: table-row;
height: 100%;
border: 1px dashed gray;
}
.item {
display: table-cell;
border: 1px solid blue;
width: 100%;
height: auto;
}
.image {
height: 100%;
width: auto;
}
<div class="container-wrapper">
<div class="container">
<div class="image-wrapper">
<div class="item">
<img class="image" src="http://placehold.it/100x200" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/300x200" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/800x400" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/10x80" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/800x400" />
</div>
</div>
</div>
</div>
***I must say that I am looking for a HTML/CSS solution without the involvement of JavaScript code.
Do you have a clue on how can I approach this ?
So a trick I just came up with is to use the automagic scaling of an image to scale the containing filmstrip div, but hide it with opacity (in a real example, I'd use a transparent .png as well). This sets the height of the filmstrip relative to its width. If you want your filmstrip to be 5:4 or 16:9 or whatever, just change the proportions of the .magic image.
The container inside is then set to be absolutely positioned so it inherits the size of the .magic image.
The images themselves are set to take up the full height of the filmstrip, and are given different widths. The actual image is set with background-image which uses background-size: cover and background-position: center to fill the div.
.filmstrip {
width: 100%;
position: relative;
/* just to make it easier to see what's going on */
border: 1px solid red;
}
.magic {
width: 100%;
height: auto;
display: block;
/* we don't actually want to see this, we're just using it for it's ratio */
opacity: 0;
}
.contents {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
}
.contents .image {
height: 100%;
background-size: cover;
background-position: center;
float: left;
margin-right: 2%;
/* just to make it easier to see what's going on */
border: 1px solid blue;
box-sizing: border-box;
}
.contents .wide {
width: 30%;
}
.contents .narrow {
width: 10%
}
<div class="filmstrip">
<img class="magic" src="http://placehold.it/400x100" />
<div class="contents">
<div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
<div class="narrow image" style="background-image: url('http://placehold.it/300x100');"></div>
<div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
</div>
</div>
Browser support should be: Chrome 3+, Firefox 3.6+, IE 9+, Opera 10+, Safari 4.1+ which is basically because of the use of background-cover.
Have a look at my stackoverflow 33117027 answer in which I made suggestions about creating a filmstrip. It has a reference to an eleborate Codepen example. You can easily strip/add what you need...

Locate image above another with responsiveness

I would like to put one image above another, but all answers I find are using position absolute with specific pixels. I want to achieve it in a responsiveness way, so I would like to avoid strict composition.
Now my code looks like:
<style>
.header{
margin:20px;
text-align:center;
}
.data{
text-align:center;
}
</style>
<body>
<h1>Hello Stack Overflow!</h1>
<div class="header">
<img src="http://lorempixel.com/g/800/200/" class="background">
<img src="http://lorempixel.com/100/100/" class="logo">
</div>
<div class="data">
<label>enter your name</label>
<input>
</div>
I would like to move the .logo img above the background img and, have different classes to move it:
Center center;
Center right;
Center left;
So, my goal is having the possibility to accomplish the following image with a class that can "move" my logo, but always maintaining it above the background image. Note that the background image could have not always the same size.
I did a plunkr to test with where you can reproduce it.
Although this answer uses absolute positioning it is responsive and should work they way you want. This answer also assumes that you have high quality background images that will not lose quality when scaled to a large size.
The logo is centered by default. There is a .logo-right and .logo-left class for side positioning.
There is a small edge case that the logo breaks out a little at small screens when the height of the background image is less than the logo. To account for this you can set the logo width to a percentage and also give it a max-width so that it doesn't enlarge too far if you are using a rasterized (non-svg) logo (see Snippet) .
.header {
margin:20px;
text-align:center;
position: relative;
}
.data{
text-align:center;
}
.background {
height: auto;
width: 100%;
}
.logo {
bottom: 0;
height: 50%;
left: 0;
margin: auto;
max-height: 100px;
position: absolute;
right: 0;
top: 0;
width: auto;
}
.logo-right {
margin-right: 5%;
}
.logo-left {
margin-left: 5%;
}
<h1>Centered</h1>
<div class="header">
<img src="http://lorempixel.com/g/800/200/" class="background">
<img src="http://lorempixel.com/100/100/" class="logo">
</div>
<div class="data">
<label>enter your name</label>
<input>
</div>
<h1>Right</h1>
<div class="header">
<img src="http://lorempixel.com/g/800/200/" class="background">
<img src="http://lorempixel.com/100/100/" class="logo logo-right">
</div>
<div class="data">
<label>enter your name</label>
<input>
</div>
<h1>Left</h1>
<div class="header">
<img src="http://lorempixel.com/g/800/200/" class="background">
<img src="http://lorempixel.com/100/100/" class="logo logo-left">
</div>
<div class="data">
<label>enter your name</label>
<input>
</div>
Maybe something like this?
http://plnkr.co/edit/wd6wui?p=preview
<style>
.header {
margin: 20px;
text-align: center;
position: relative;
}
.img-container {
position: absolute;
top: 0;
width: 100%;
}
.data {
text-align: center;
}
img {
max-width: 100%;
height: auto;
display: block;
margin: 0 auto;
}
<div class="header">
<div class="img-container">
<img src="http://lorempixel.com/g/800/200/" class="background">
</div>
<div class="img-container">
<img src="http://lorempixel.com/100/100/" class="logo">
</div>
</div>
Not sure I understood your question ;)
wrapping the images in div will do the work
<div><img src="http://lorempixel.com/100/100/" class="logo"></div>

Preventing a height change when adding a border with box-sizing

I'm unable to find something that describes this issue, but if I'm missing it, just let me know.
Below is a demo (Tested in IE11 and Chrome) which shows the problem perfectly
Essentially, I'm trying to add a border to an element, and keep the size the same. It's working perfectly for the width, but the height, it only accommodates half of it, so I have an extra 3px in height.
Is there a way to prevent this / get around it without using a script? I can make changes specifically to the element(s) which have the border, but I don't know the actual height (200px is used here just for demo purposes, so simply reducing the height isn't an option.
Thanks.
EDIT (Since apparently it's unclear what I'm asking)
Is there a way to prevent the height changing without using a script?
DEMO:
div {
width: 200px;
float: left;
margin-right: 3px;
}
div img {
max-width: 100%;
max-height: 100%;
}
.div {
border: 3px dotted blue;
box-sizing: border-box;
}
<div id="div1">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div" style="clear:left">
<img src="http://placehold.it/200x200" alt="" />
</div>
You can set add line-height:0px if there is no text for .div as image is inline-block element it add whitespace
div {
width: 200px;
float: left;
margin-right: 3px;
}
div img {
max-width: 100%;
max-height: 100%;
}
.div {
border: 3px dotted blue;
box-sizing: border-box;
line-height:0px;
}
<div id="div1">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div" style="clear:left">
<img src="http://placehold.it/200x200" alt="" />
</div>
Either add the line-height attribute to the div as #Vitorino suggested or add vertical-align: middle to the img. Both will fix the whitespace issue for an inline-block
div img {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
.div {
border: 3px dotted blue;
box-sizing: border-box;
line-height:0px;
}
Here is a variant of solution:
div {
width:200px;
float:left;
margin-right:3px;
box-sizing:border-box;
}
.div img {
max-width:100%;
max-height:100%;
box-sizing: border-box;
border:3px dotted blue;
}

HTML: Images floating left?

I wanna make a page with lots of images being shown.
<style>
img{margin-bottom: 3px; float: left}
</style>
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
It looks good if they all have the same width and height.
But what happens when one of them has a larger size? It messes all the layout,
showing a white space that makes it look ugly.
How can this be fixed?
why don't you put width: 150px or whatever the size you want inside that css?
<style>
img{margin-bottom: 3px; width: 150px; float: left}
</style>
With a bit javascript you could bring them all to the same size. But it won't look much better.
Maybe the same width and a simple repeating background which won't be annoying would be a compromise.
Limit sizes
img {
max-width: 100px;
max-height: 100px;
border: 1px solid grey; /* show visible element size */
}
Sample:
http://jsfiddle.net/UqGW7/3/
Fixes sizes (strech)
img {
width: 100px;
height: 100px;
border: 1px solid grey; /* show visible element size */
}
Sample:
http://jsfiddle.net/UqGW7/4/
Crop images (using a wrapper)
<span class="wrapper">
<img src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/592211_21253884267_736366040_q.jpg" />
</span>
.wrapper {
width: 100px;
height: 100px;
display: inline-block;
overflow: hidden; /* crop larger images */
border: 1px solid grey; /* show visible element size */
text-align:center; /* center horizontally */
}
Sample:
http://jsfiddle.net/UqGW7/5/
If it is not very necessary to show the entire image you can consider placing your images in div tags, either as background images, centering them and then setting a fixed width and height for the div elements.
<style>
.imgpanel{
margin-bottom: 3px;
float: left;
background-repeat:no-repeat;
background-position:center;
width:200px;
height:200px;
}
</style>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
Or you can place the image within the div tags and set the overflow to hidden:
<style>
.imgpanel{
margin-bottom: 3px;
float: left;
width:200px;
height:200px;
overflow:hidden;
}
</style>
<div class="imgpanel"><img src="/images/sample.png" /></div>
<div class="imgpanel"><img src="/images/sample.png" /></div>
<div class="imgpanel"><img src="/images/sample.png" /></div>
Optionally, you can use a thumbnail generation script to create images that are always the specific size you want, if re-sizing the image is okay. Check http://sourceforge.net for code samples of a thumbnail generator.