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>
Related
thanks for helping!
I set 2 images of -
800X1000
Image 1 :
Set in a container that set to :
width: 100%.
The image I also set to -
width: 100%
Image 2 :
Set without a container, without any special order.
I set a border to my container -
And when resizing the screen, to about the 800px mark -
The container started to shrink and didn't take the 100% width I set.
(Therefore , the image is also shrunk).
Once I removed image number 2, it as expected - take the full 100% width of the screen.
Why is that?
Why once I had 2 images inside, the second Image basically cause the to shrink?
This is my code ( nothing special, 2 CSS lines)
.alon {
width: 100%;
border: 5px solid green;
}
.alon img {
width: 100%;
}
<div class="alon">
<img src="images/donald.jpg" alt="">
</div>
<img src="images/donald.jpg">
HTML above.
Your second image has no styling, you basically just dropped an image in the body container and never specified a width for it.
With the code you provided, the second image will keep its default width, while the image in the .alon div will take the width of its parent container. In this case, the image in the .alon div is taking 100% width of the browser, and the image within is taking 100% width of the .alon div.
I've included an example below that applies 100% width to the second image as well.
body img {
width: 100%;
}
* {
box-sizing: border-box;
}
.alon {
width: 100%;
border: 5px solid green;
padding: 0px;
}
.alon img {
width: 100%;
padding: 0px;
}
body img {
width: 100%;
}
<div class="alon">
<img src="https://cdn.pixabay.com/photo/2020/10/28/11/08/castle-5693094__340.jpg" alt="">
</div>
<img src="https://cdn.pixabay.com/photo/2020/10/28/11/08/castle-5693094__340.jpg">
After running the code you will notice that the second image takes 100% width of the browser like your first image.
If you delete the following CSS:
body img {
width: 100%;
}
The second image will go back to its default size.
Additionally, to further answer your question, since your first image is enclosed in a div that has a 5px green border, the default box-sizing on a browser will not calculate your border as part of the declared width of 100%, which in turn, will offset your image container by 5px.
To make the browser calculate the 5px border as part of the container width of 100% you can apply a universal box-sizing: border-box to your document.
I've included this in the snippet above for reference.
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/
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.
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.
I want a <img> whose width is 40% of the page, and it gets stretched.
How can I resize it without stretching?
For example, if I have a image whose file originally looks like this:
____8888________
____8888________
____8888________
In my webpage, normally, it should looks like:
____8888________
____8888________
____8888________
As soon as I make the browser a little more narrow, the max-width(let's say 10 characters in this example) would take effect.
When that happens, I would like it to be:
____8888__
____8888__
____8888__
(just like it's been cut from the right side. Of course from both sides are better),
Rather than:
__888_____
__888_____
__888_____
Any trick (putting it into a <div>'s background) is okay.
Width and height are unknown.
Thank you all for your previous answers, but, sorry, I think I haven't put enough emphasis on "After limiting its width to 40% of the page", which means before width-limiting it should looks normal.
The trick is to put the image into a containing block element, eg a DIV. Once inside set the width of the image to 100%, this will instruct the browser to fit the image width flush with the left and right edges of the DIV.
You then control the width of the DIV via CSS, I find keeping the image in a block element makes manipulation much easier when creating fluid layouts.
Example:
img.stretchy {
width: 100%; /*Tells image to fit to width of parent container*/
}
.container {
width: 33%; /*Use this to control width of the parent container, hence the image*/
}
<div class="container">
<img src="http://i.stack.imgur.com/fv6Ib.jpg" alt="Beach Scene" class="stretchy" />
</div>
If you wan the image to be clipped/cropped in any way, set it to be larger than it's parent, and set the parent's overflow css to hidden.
Example:
img.clipped {
width: 150%; /*Scales image to 150% width of parent container*/
float: left; /*Floats image to left of container - clipping right hand side*/
float: right; /*Floats image to right of container - clipping left hand side*/
}
.container {
width: 33%; /*Use this to control width of the parent container, hence the image*/
overflow: hidden;
}
<div class="container">
<img src="http://i.stack.imgur.com/fv6Ib.jpg" alt="Beach Scene" class="clipped" />
</div>
Hope this helps...
Add this class to the img html tag, it will keep the image as it is, but will take the necessary specified space ie.40% x 40% without stretching the image
.img{
width:40%;
height:40%; //change to whatever your choice
/*Scale down will take the necessary specified space that is 40% x 40% without stretching the image*/
object-fit:scale-down;
}
Here's a few options. (see the demo of all these options here: http://jsfiddle.net/Squeegy/Gcrdu/ )
The first as a plain image of unknown size. This displays at whatever size it happens to be.
<img src="http://www.google.com/logos/classicplus.png">
But as it turns out, you can preserve the aspect ratio of an image if you only set the width, or only the height. The other dimension will adjust itself to keep things from stretching.
// HTML
<img src="http://www.google.co.jp/logos/classicplus.png" class="aspectshrink">
// CSS
img.aspectshrink {
width: 100px;
}
But when you use CSS background images you can do some creative cropping based on where anchor the background.
This says "Go"
// HTML
<div class="cropped-right"></div>
// CSS
.cropped-right {
width: 100px;
height: 100px;
background: url(http://www.google.com/logos/classicplus.png);
background-position: left center;
background-repeat: no-repeat;
border: 1px solid red;
}
And this says "gle":
// HTML
<div class="cropped-left"></div>
// CSS
.cropped-left {
width: 100px;
height: 100px;
background: url(http://www.google.com/logos/classicplus.png);
background-position: right center;
background-repeat: no-repeat;
border: 1px solid red;
};
Try to use ImageResizer.
Here's the link : http://imageresizing.net/
Do you mean cropping the image? If so look into CSS overflow property. Also you could put it into the background and centre it in the div