Padding changes when the browser is zoomed in or out - html

I have a thumbnail image and another smaller image which overlaps the thumbnail image. But the padding changes for the smaller overlapping image as I zoom in and out and the problem exist only with the CHROME browser. Its working fine with IE and firefox. I tried using percentage to set the padding values for the smaller image but the problem still exist.
Here are the images.
This is the HTML
<div class="car-item">
<div class=" car-image">
<img src="/~/media/images/thumb.ashx" alt="Image 1" />
</div>
<div class="car video">
VIDEO
</div>
<div>
position for car video is absolute
position for car item is relative
and for car-image is static

You will have issues at times when using percentages. This is a good example of when to use absolute positioning.
I have no idea what your code looks like so here is a basic example of how to accomplish what you have pictured above with absolute positioning. I used a span tag instead of an additional image tag but it should work all the same.
You might have to modify your HTML and CSS a little furthor to get it to work in your environment.
http://jsfiddle.net/6C8gT/
Here is an updated jsFiddle (http://jsfiddle.net/6C8gT/1/) that uses your markup and another one with reduced markup (http://jsfiddle.net/6C8gT/2/). You don't really need those DIVs unless you have plans for them in the future.
I just did what I have posted below but modified the CSS to match your HTML. You'll have to check out the jsFiddles.
HTML
<div class="container">
<img class="thumb" src="http://lorempixel.com/300/200/" />
<span>Video</span>
</div>
CSS
.container {
position: relative;
}
.container img {
display: block;
}
.container span {
color: white;
background-color: black;
padding: 5px 10px;
position: absolute;
left: 0;
bottom: 0;
}

Related

Weird whitespace in browser while making responsive page

Disclaimer: This is some weird whitespace that glitches.
I was making a Image slideshow using html, css and js. But after I added images like this.
<div class="wrapper">
<div class="slides-container">
<div class="slide-image">
<img src="./public/assets/ironman.png" alt="captain" />
</div>
<div class="slide-image">
<img src="./public/assets/captain.png" alt="captain" />
</div>
<div class="slide-image">
<img src="./public/assets/blackwidow.png" alt="captain" />
</div>
<div class="slide-image">
<img src="./public/assets/blackPanther.png" alt="captain" />
</div>
</div>
</div>
</div>
.wrapper {
position: absolute;
width: 100%;
}
.slides-container {
position: relative;
z-index: 9;
height: 100vh;
transition: all 0.5s;
}
.slide-image {
position: absolute;
height: 100%;
}
.slide-image img {
height: 100%;
transform: translateX(80%);
}
But this is showing some weird white space after my footer. When I removes the Images this issue is fixed i.e. this issue is caused by images only.
But the actual issue is really confusing as this whitespace sometimes just disappears out, by just stretching the browser window and sometimes reappears out of nowhere. This really confuses me as it doesn't show any particular breaking point.
Here I did nothing to any code, but just refreshed the browser and did some stretching browser window. And now it's fixed.
And after few more stretches and after another refresh the whitespace reappeared.
What could be done. Please ignore the other part of the html, they're not any issue.
I'm so confused. Help would be appreciated.
I can't replicate the problem but I've had a similar issue in the past. I don't know if it's exactly the same but your images are absolutely positioned. That removes them from the document flow.
Try setting a background color on the wrapper and recreate the glitch. That'll tell you if you're seeing the wrapper as the white space. If that's the case (and maybe even if not) you could try putting something relatively positioned with 100% width and height before the images. They'll absolutely position themselves above it.
I don't like it as a solution but without recreating it myself I can only guess.

How to create a div in the same size as the contained image. Both should be responsive

I am creating a mobile e-mail template (means no javascript) which has to be responsive.
I want to place several images inline, which are scaled down as the screen gets narrower. I did this by using css table and table-cell, and let the image scale. No problem so far.
However, since images are often blocked by e-mail clients, I was requested to create a kind of placeholder in grey, showing the image "alt text" when the image is not loaded. I want this placeholder to be of the same size as the contained image, and to scale at narrower widths too.
I got quite far, as you can see in the following fiddle: http://jsfiddle.net/ow7c5uLh/29/
HTML:
<div class="table">
<div class="table-cell">
<div class="placeholder">
<img src="http://lorempixum.com/120/60/" alt="alt text" width="120" height="60" />
</div>
</div>
<div class="table-cell">
<div class="placeholder">
<img src="http://lorempixum.com/120/60/" alt="alt text" width="120" height="60" />
</div>
</div>
<div class="table-cell">
<div class="placeholder">
<img src="http://lorempixum.com/120/60/" alt="alt text" width="120" height="60" />
</div>
</div>
</div>
CSS:
.table {
display: table;
table-layout: fixed;
width: 100%;
}
.table-cell {
display: table-cell;
text-align: center;
padding: 0 5px;
border: 1px dotted black;
}
.placeholder {
max-width: 120px;
max-height: 60px;
margin: auto;
background-color: #505050;
color: #FFFFFF;
}
img {
max-width: 100%;
height: auto;
}
However, there are two problems:
As the screen gets narrower and the images are scaled, the background-color pops out from under the image. The placeholder-div is scaling just as the image, but its height is calculated (by the browser) to be some 5px more then the image height. Where does that difference come from?
When the images are not loaded (try in the fiddle by just making the image URL invalid) then the placeholder-div's height collapses. How can I make it keep the correct height?
FYI: The actually used images won't always be of the same size, but I will know their dimensions and can calculate their aspect-ratio. I would write those values (like 120px) inline instead of in a separate css-file like in the example.
Thanks in advance for any help!
Add display: block to your CSS img rule to make it a block element instead of inline and you are good to go: Fiddle
Change src="...." of one of them to src="" in the fiddle and you will see the the cell itself already scales.
By adding rule img[alt] { font-size: 2vw; overflow: hidden } to your CSS, the html alt="text" will scale too. overflow: hidden chops excess text when alt is larger than your 120x60px.
(note: [alt] is called an 'attribute' in CSS, search for 'css custom attribute' should you want to learn to create your own.)
See updated Fiddle
I would advise against loosing the width and height rules of the placeholder, but you could change it to min-height/min-width to show at least that something 'is missing'. Or change to max-width: 100% and remove max-height, but this depends on your requirements. You will need to limit the size of an image somewhere up or down the line (for example giving the table a width in px and it's children a (max-)width in % ).
Remove:
img {
height: auto;
}
problem-1 & 2:
img {
max-width: 100%;
max-height: 100%;
}

Why don't fluid background SVG files display correctly at all sizes?

I am currently trying to use svg files instead of images for modern browsers on a new fluid site. The idea is to use an SVG as a background image on a fluid div which can then be changed on hover and we can use modernizer (or similar) to fallback to the use of img backgrounds for unsupported browsers.
In theory this is all fine however on certain browsers (particularly Firefox) the right and bottom edges of the svgs have some strange pixelation at certain sizes which doesn't happen for imgs.
So if you view http://jsfiddle.net/deshg/xuq6812g/ you can see a grid of 4 x 25% columns each with a div or img (that is 100% width). Each one has either a div with svg or img background or an img element with the svg/img as the src. If you view this in FF and make it bigger/smaller you'll see at certain sizes the degradation i'm talking about. You can also see this in the image below (the areas circled in blue are the degraded bits which you can see occurs on the svg but not the img).
Can anyone shed some light on why this is happening and how to prevent it as it makes SVGs largely unusable in this way if it can't be fixed
CSS
body, html {
height: 100%;
}
body {
margin: 0;
padding: 0;
}
.container {
float: left;
width: 25%;
height: 100%;
}
.container img {
width: 100%;
}
.container div {
background-size: cover;
width: 100%;
padding-top: 100%;
}
HTML
<div class="container">
BACKGROUND SVG:<br>
<div style="background-image: url('https://dl.dropboxusercontent.com/s/rga8anccnpyublh/svg.svg');">
</div>
</div>
<div class="container">
BACKGROUND IMG:<br>
<div style="background-image: url('https://dl.dropboxusercontent.com/s/rb1u7l90q9ny8bh/img.png');">
</div>
</div>
<div class="container">
SVG IN IMG TAG:<br>
<img src="https://dl.dropboxusercontent.com/s/rga8anccnpyublh/svg.svg" alt="">
</div>
<div class="container">
IMG IN IMG TAG:<br>
<img src="https://dl.dropboxusercontent.com/s/rb1u7l90q9ny8bh/img.png" alt="">
</div>
From working with vector images for years and years, when you crop them accurately, yet they need aliasing, then the crop looks odd -- flattened at the curves. So circles, text, logos, and so forth need some extra edge in the view box. Here I've add a lot more, but you get the idea.
DEMO with before and after: http://jsbin.com/buquw/1/edit
ORIGINAL -- cropped accurately, but too close, because this image needs aliasing.
NEW VERSION -- you don't need this much, used to exaggerate the situation:

HTML, CSS - image inside image, how to do that?

I have this piece of HTML code:
<div>
<div>
<image src="image-inside-pic-png.png" alt="">
</div>
<image src="pic.png" alt="" />
</div>
The pic.png (300x300 px) is the main image. I would like to put the image-inside-pic-png.png (20x20 px) inside of it. When I apply position: absolute; on the small image, it works only momentarily.
If I change the size of either, it no longer works.
So my question is, how can I move the small image always in the big image - and this small image will be always 15px from the top and 30px from the right margin of the big image?
Thank you for help
I think this should work:
HTML:
<div>
<img src="image-inside-pic-png.png" alt="" class="inner-image"/>
<img src="pic.png" alt="" />
</div>
CSS:
div {
position: relative;
}
.inner-image {
position: absolute;
top: 15px;
right: 30px;
}
Anyway, make sure you need to do this with HTML. Maybe it's better to simply edit the image with Photoshop or Gimp. Or maybe one image it's only for styling purpose, then you should use CSS.
Without changing your markup this can be achieved e.g. using display:inline-block to the outermost div element (so it won't extend for 100% of the available width) and position relative + absolute for outermost div and thumbnail
see this fiddle: http://jsfiddle.net/cRqhT/3/
border and image size are defined for simplicity
put both images in one div which uses position:relatvie, then apply position:absolute to images, and adjust the value as you need.
**html**
<div class="images">
<img src="./images/Rectangle.png" alt="bg"/>
<img src="./images/lady.png" alt="lady" class="lady-image"/>
</div>
**css**
.images {
position: relative;
}
.lady-image {
position: absolute;
top: 0;
right: 0;
}

How can I resize an image to a percentage of itself with CSS?

I am trying to resize an image with a percentage of itself. For example, I just want to shrink the image by half by resizing it to 50%. But applying width: 50%; will resize the image to be 50% of the container element (the parent element which maybe the <body> for example).
Question is, can I resize the image with a percentage of itself without using JavaScript or server side? (I have no direct information of the image size)
I am pretty sure you cannot do this, but I just want to see whether there are intelligent CSS only solution. Thanks!
I have 2 methods for you.
Method 1.
This method resize image only visual not it actual dimensions in DOM, and visual state after resize centered in middle of original size.
img {
transform: scale(0.5);
}
<img src="https://via.placeholder.com/300x200" />
Browser support note: browsers statistics showed inline in css.
Method 2.
#wrap {
overflow: hidden;
position: relative;
float: left;
}
#wrap img.fake {
float: left;
visibility: hidden;
width: auto;
}
#img_wrap {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
#img_wrap img.normal {
width: 50%;
}
<div id="wrap">
<img class="fake" src="https://via.placeholder.com/300x200" />
<div id="img_wrap">
<img class="normal" src="https://via.placeholder.com/300x200/cccccc" />
</div>
</div>
Note: img.normal and img.fake is the same image.
Browser support note: This method will work in all browsers, because all browsers support css properties used in method.
The method works in this way:
#wrap and #wrap img.fake have flow
#wrap has overflow: hidden so that its dimensions are identical to inner image (img.fake)
img.fake is the only element inside #wrap without absolute positioning so that it doesn't break the second step
#img_wrap has absolute positioning inside #wrap and extends in size to the entire element (#wrap)
The result of the fourth step is that #img_wrap has the same dimensions as the image.
By setting width: 50% on img.normal, its size is 50% of #img_wrap, and therefore 50% of the original image size.
This has got to be one of the simplest solutions using the container element approach.
When using the container element approach, this question is a variation of this question. The trick is to let the container element shrinkwrap the child image, so it will have a size equal to that of the unsized image. Thus, when setting width property of the image as a percentage value, the image is scaled relative to its original scale.
Some of the other shrinkwrapping-enabling properties and property values are: float: left/right, position: fixed and min/max-width, as mentioned in the linked question. Each has its own side-effects, but display: inline-block would be a safer choice. Matt has mentioned float: left/right in his answer, but he wrongly attributed it to overflow: hidden.
span {
display: inline-block;
}
img {
width: 50%;
}
<span>
<img src="https://via.placeholder.com/300x200"/>
</span>
Edit: As mentioned by trojan, you can also take advantage of the newly introduced CSS3 intrinsic & extrinsic sizing module:
figure {
width: intrinsic;
}
img {
width: 50%;
}
<figure>
<img src="https://via.placeholder.com/300x200" />
</figure>
However, not all popular browser versions support it at the time of writing.
Scale the image:
img {
transform: scale(0.5);
}
<img src="https://via.placeholder.com/300x200" />
Another solution is to use:
<img srcset="example.png 2x">
It won't validate because the src attribute is required, but it works (except on any version of IE because srcset is not supported).
This is a very old thread but I found it while searching for a simple solution to display retina (high res) screen capture on standard resolution display.
So there is an HTML only solution for modern browsers :
<img srcset="image.jpg 100w" sizes="50px" src="image.jpg"/>
This is telling the browser that the image is twice the dimension of it intended display size. The value are proportional and do not need to reflect the actual size of the image. One can use 2w 1px as well to achieve the same effect. The src attribute is only used by legacy browsers.
The nice effect of it is that it display the same size on retina or standard display, shrinking on the latter.
This actually is possible, and I discovered how quite by accident while designing my first large-scale responsive design site.
The overflow:hidden gives the wrapper height and width, despite the floating contents, without using the clearfix hack. You can then position your content using margins. You can even make the wrapper div an inline-block.
.wrapper {
position: relative;
overflow: hidden;
}
.box {
float: left; /* Note: 'float:right' would work too */
}
.box>img {
width: 50%;
}
<div class="wrapper">
<div class="box">
<img src="https://via.placeholder.com/300x200" alt="">
</div>
</div>
function shrinkImage(idOrClass, className, percentShrinkage){
'use strict';
$(idOrClass+className).each(function(){
var shrunkenWidth=this.naturalWidth;
var shrunkenHeight=this.naturalHeight;
$(this).height(shrunkenWidth*percentShrinkage);
$(this).height(shrunkenHeight*percentShrinkage);
});
};
$(document).ready(function(){
'use strict';
shrinkImage(".","anyClass",.5); //CHANGE THE VALUES HERE ONLY.
});
This solution uses js and jquery and resizes based only on the image properties and not on the parent. It can resize a single image or a group based using class and id parameters.
for more, go here: https://gist.github.com/jennyvallon/eca68dc78c3f257c5df5
I think you are right, it's just not possible with pure CSS as far as I know (not cross-browser I mean).
Ok I didn't like my answer very much so I puzzled a little. I might have found an interesting idea which could help out.. maybe it IS possible after all (although not the prettiest thing ever):
Tested and working in Chrome, FF and IE 8&9. It doesn't work in IE7.
#img_wrap {
display: inline-block;
position: relative;
}
#rescaled_img_wrap {
width: 50%;
}
#original_img {
display: none;
}
#rescaled_img {
width: 100%;
height: 100%;
}
<div id="img_wrap">
<img id="original_img" src="https://via.placeholder.com/300x200" />
<div id="rescaled_img_wrap">
<img id="rescaled_img" src="https://via.placeholder.com/300x200/dddddd" />
</div>
</div>
This is a not-hard approach:
div {
position: absolute;
}
img,
div {
width: ##%;
height: ##%;
}
<div>
<img src="https://via.placeholder.com/300x200" />
</div>
Although it does not answer the question directly, one way to scale images is relative to the size (especially width) of the viewport, which is mostly the use case for responsive design. No wrapper elements needed.
img {
width: 50vw;
}
<img src="https://via.placeholder.com/300x200" />