Images do not rescale with CSS - html

I have three .svg images that need to resize. For some reason they do not resize, except when the screen is exceptionally small.
html:
<div class='au-images'>
<div class='computer'>
<p data-sr style='max-width: 180px;'><img src='images/computericon.svg'></p>
</div>
<div class='tablet'>
<p data-sr='wait 0.5s' style='max-width: 180px;'><img src='images/tableticon.svg'></p>
</div>
<div class='phone'>
<p data-sr='wait 1s' style='max-width: 180px;'><img src='images/phoneicon.svg'></p>
</div>
I am also using scroll-reveal, which explains the 'data-sr' tag.
My CSS:
.computer,.tablet, .phone {
max-width: 33%;
display: inline-block;
padding: 0em 2.5em;
}
.computer img, .tablet img, .phone img {
max-width: 100%;
height: auto;
}
Can someone explain to me why my images don't resize?

The problem is that you don't set the width on the parent elements of the images. CSS can't calculate a max-width of a max-width, so it uses the max-width of the only width it knows, namely the body.
Your code in a fiddle (modified to show some actual images)
So the solution is to give the parent divs a width instead of a max-width. Since you used padding, I also changed the box sizing to include that.
box-sizing: border-box;
width: 30%;
Updated code in a fiddle.
Please note that I reduced the width to 30%, because the divs have spaces between them; with 33%, the total width would be 99% plus the width of those two spaces, which may exceed the window.
If you don't want that, remove all whitespace between the inner divs.
Even more updated fiddle, now using widths of 33.33%

You can try to use the width unit vw. This unit is the viewport width expressed in percent. That is 100vw is the full width of your device.
Take a look at this example: http://jsfiddle.net/v929q8mn/1/
div.blablabla img {
display: inline-block;
margin: 0;
width: 30vw;
}
It is supported in most browsers.

Related

CSS image at 100% in a scrollable division which expands to fill available space

This code shows the image at 100%. The images are too big to be shown at 100% in the space where this code will be inserted, thus I need to show them using scrollbars.
<div style="overflow: auto; margin: auto; margin-bottom: 1em;">
<img src="">
</div>
This code shows the image at 100% with scrollbars within a 500px x 500px division. I don't; however, want a fixed size for the outer division.
<div style="width: 500px; height: 500px; overflow: auto; margin: auto; margin-bottom: 1em;">
<img src="">
</div>
I want to adjust the outer division to act as a picture viewer for seeing full size images with scrollbars.
I want the outer division to fill the available horizontal space, which I do not know and will change depending upon the viewer's monitor. The height should auto adjust but not fill the available space as there will be a series of such picture viewer divisions stacked vertically.
The images I will be inserting are large and will 99.9% of the time exceed the available space thus I need the scrollbars.
I don't want the pictures to be resized to the size of the outer division or vice versa.
I can't use any scripts or active content. It must be pure css and html only.
I cannot hard code the pixel sizes of the images as a application will be inserting the image code via a loop and the application does not have any ability to insert the image's width or size.
Keep the outer wrapper at width: 100% and figure out a height that fits your use case.
For responsiveness, I get it suits you better to select the height based on viewport units so that it doesn't fill or stretch beyond the available height (I am taking 50vh here as an example)
Maybe this also helps you.
Snippet below:
body{
margin: 0;
}
*{
box-sizing: border-box;
}
.wrapper{
overflow: auto;
border: 1px solid;
width: 100%;
height: 50vh; /* adjust / omit this as per your requirement*/
}
.wrapper img{
display: block;
}
<div class="wrapper">
<img src="http://placehold.it/1500x1500">
</div>
I think you can do this with either an iframe, or using overflow: scroll with 100% width. It's the height you'd need to figure out. I would suggest using media queries maybe for the height.
Here's a sample jsfiddle that I think is pretty close to what you're looking for. Again, your height is what you have to figure out. The parent div will always take up the full height of the children unless you specify.
.image-wrapper {
width: 100%;
height: 50vh; /* as suggested below in another answer, or you can use media queries */
overflow: scroll;
}
<div class="image-wrapper">
<img src="http://www.spyderonlines.com/images/wallpapers/image/image-20.png">
</div>
https://jsfiddle.net/adpro/bt7aar4b/

CSS: Set Max Width of an Image to Its Actual Pixel Width

I am playing around with a responsive layout and I am trying to get my image handling to behave a specific way.
I want the max-width of my image to never exceed its actual resolution, however if that is too wide for the screen I'd like the width to be 90% of the screen width. The only solution I can come up with for this is set width: 90%; and then to hard-code the max-width for every image I want to display like this, which is problematic if I want to change the image on the fly or update it frequently.
Is there any CSS I can use to describe this scenario or do I have to rely on javascript tricks to set the max-width from the image's actual width once the image has finished loading?
I think the following may work. Set the max-width: 90% and the let the image take its natural width (width: auto, default value).
See the samples below.
There is an end-point (corner case) when the image size is identical to the width of the containing block (screen size). In this case, the image will take 90% of the width of the parent block. If you need this to be 100%, you would need jQuery/JavaScript to take care of the exception.
div {
border: 1px dotted blue;
margin: 10px 0;
}
div img {
max-width: 90%;
vertical-align: top; /* Removes white space below baseline */
}
.ex1 {
width: 500px;
}
.ex2 {
width: 400px;
}
.ex3 {
width: 300px;
}
<div class="ex1">
<img src="http://placehold.it/400x100">
</div>
<div class="ex2">
<img src="http://placehold.it/400x110">
</div>
<div class="ex3">
<img src="http://placehold.it/400x120">
</div>

Image auto height to scaled width

I have certain images that are smaller in width which i stretch the width to fit the container, however would like the hieght to scale up as well, thanks in advance, Phil
In your css:
img {
width: 100%;
height: auto !important;
}
CSS can naturally handle this. The image will automatically take up 100% of the width of its container, and the height will scale to match.
There is no need to specify the height.
Take a look to my example, here is my jsfiddle
http://jsfiddle.net/d575tr49/
Here is the HTML
<div class="ele">
<img src="http://viralstash.net/wp-content/uploads/2014/03/521013543_1385596410.jpg" border="0" />
</div>
Here is the CSS
.ele {
outline: red solid 1px;
width: 250px;
}
.ele img {
width: 100%;
display: block;
}
If you control the width on the Parent element, just the with, you will not need to worry about the width and height values of the image.
The image it self will set its width using the parents size, and by default the height value will be proportional to the width, so no need to specify the height value at all, not even in the parent.

Stop Div from Shrinking to Content in Fluid Layout

I have a centered div (#content) on my responsive site that resizes to the browser window, like this:
<div id="container">
<div id="content">
<p>Here is some sample text.</p>
</div>
</div>
#container {
width: 100%;
}
#content {
max-width: 805px;
}
It works, except for when the stuff inside #content doesn't take up enough horizontal space to push the div's width out to the max-width I have set. For example, instead of being 805px, one of my pages with little content between the paragraph tags is 740px.
I have tried adding width: 100% to #content, but that stops the div from resizing.
What's the best way to fix this? Do I need to use a media query?
I found the answer. I did need width: 100%, but I also needed box-sizing: border-box, and then I needed to include the added width of my padding into the max-width, like so:
#content {
box-sizing: border-box;
width: 100%;
max-width: 885px;
padding: 40px;
}

Make an image width 100% of parent div, but not bigger than its own width

I’m trying to get an image (dynamically placed, with no restrictions on dimensions) to be as wide as its parent div, but only as long as that width isn’t wider than its own width at 100%. I’ve tried this, to no avail:
img {
width: 100%;
height: auto;
max-width: 100%;
}
Many of these images are way wider than their parent div, which is why I’d like them to resize accordingly, but when a small image pops in there and gets scaled up beyond its normal dimensions, it really looks terrible. Is there any way of doing this?
Just specify max-width: 100% alone, that should do it.
Found this post on a Google search, and it solved my issue thanks to #jwal reply, but I made one addition to his solution.
img.content.x700 {
width: auto !important; /*override the width below*/
width: 100%;
max-width: 678px;
float: left;
clear: both;
}
With the above I changed the max-width to the dimensions of the content container that my image is in. In this case it is: container width - padding - boarder = max width
This way my image won't break out of the containing div, and I can still float the image within the content div.
I've tested in IE 9, FireFox 18.0.2 and Chrome 25.0.1364.97, Safari iOS and seems to work.
Additional: I tested this on an image 1024px wide displayed at 678px (the max width), and an image 500px wide displayed at 500px (width of the image).
Setting a width of 100% is the full width of the div it's in, not the original full-sized image. There is no way to do that without JavaScript or some other scripting language that can measure the image. If you can have a fixed width or fixed height of the div (like 200px wide) then it shouldn't be too hard to give the image a range to fill. But if you put a 20x20 pixel image in a 200x300 pixel box it will still be distorted.
In line style - this works for me every time
<div class="imgWrapper">
<img src="/theImg.jpg" style="max-width: 100%">
</div>
You should set the max width and if you want you can also set some padding on one of the sides. In my case the max-width: 100% was good but the image was right next to the end of the screen.
max-width: 100%;
padding-right: 30px;
/*add more paddings if needed*/
I was also having the same problem, but I set the height value in my CSS to auto and that fixed my problem. Also, don't forget to do the display property.
#image {
height: auto;
width: auto;
max-height: 550px;
max-width: 1200px;
margin-left: auto;
margin-right: auto;
display: block;
}
I found an answer which worked for me and can be found in the following link:
Full Width Containers in Limited Width Parents
I found max-width:inherit; worked for me
I wrote this code:
div.image {
height: auto;
width: 100%;
}
div.image img {
height: inherit;
width: inherit;
}
max-width: fit-content; worked for me.
If the image is smaller than parent...
.img_100 {
width: 100%;
}
I would use the property display: table-cell
Here is the link