how i change any image width and height values without stretching or cropping it - html

I have an image . i want to make it a body background image but the image is stretched all the solutions i found so far on internet to resolve this problem and maintain aspect ratio lead to cropping image and this is what happen when setting (bg-size : cover) the image aspect ratio is maintained and it fill the page but the image is cropped
so Q here is how i change any image width and height values without stretching or cropping it .......
first one is original , second is stretched third image is cropped

What you are asking for is impossible - for the image to cover the entire screen/window without cropping or stretching, it would have to have the exact same dimensions as the screen/window.

Related

How to keep one image on a certain part of a responsive background image, as the webpage resizes?

For example, if I have a background image of a cyclone and it's responsive/resizes with the window and covers the whole web page (background-size: cover), but the eye of the cyclone isn't in the center of the webpage but say slightly more to the right and I wanted another image, say a picture of an apple, to always cover the eye of the cyclone, no matter what size the window is, what could I do?
I've tried playing around with the margin percentages, the image of the apple is also responsive and shrinks with the background image to cover the eye of the cyclone but the images always tend to move out of sync just before I've dragged the window to it's smallest size.
The CSS background size property can have value of cover. The cover value tells the browser to automatically and proportionally scale the background images width and height so that they are always equal to or greater than the viewports
width/height.
In Img tag use height i and width in pixel then change in CSS
then output will show properly
Check the Multiple Backgrounds CSS feature which will achieve your request: https://www.w3schools.com/Css/css3_backgrounds.asp

Background issue - extended

I have a background issue.
The background is too extended. The image on this pen is 500 px wide, but originally i have an image that is larger, about 1500px? However the effect is the same as on pen.
No matter what the width the image is extended as it is on pen.
My codepen
change the css you made like the following:
instead of:
background-size:cover;
to
background-size:contain;
the difference between the two is that 'cover' will scale your image as large as possible to cover the entire background, but most of the times when doing that the image will not fit in(most of the time the image is just larger).
when using 'contain' it will scale your image to the largest size when both the height and the width will fit into the background.
now, if your image is smaller than the page then it will repeat itself, unless you add :
background-repeat:no-repeat;
i looked on your codepen link, and the image is too small, this is why when you will write the 'no-repeat' part it won't cover the entire background.
I suggest that you will find an image which is at least cover the height and the width of the background.

How can I keep aspect ratio of background-image in css

I'm currently working of the mobile version of a website and I want all images to fill 90% of the screen width. I think the best technique to achieve this is creating a div for every image which is using the image as background-image. The problem is that I don't know how to match the height of the div with the aspect ratio of the image. I have tried to set width: 90% and height: auto but it didn't worked.
Could you please help me?
Lennart
You have an option in CSS called background-size with the option cover:
background-size: cover;
A keyword that is the inverse of contain. Scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image "covers" the entire width or height of the container. When the image and container have different dimensions, the image is clipped either left/right or top/bottom. The image is automatically centered unless over-ridden by another property such as background-position.
I don't know if I might be getting the question wrong, but I don't understand why you want to use background images?
If you use a regular img-Tag, the image will keep its aspect ratio if you set it to
width: 90%
height: auto
Other than that you can keep the aspect ratio of a div-container by setting a padding-top to a percentage on a wrapper-div. That works because the percentage is calculated dependend on the width of the div. See more here: http://www.mademyday.de/css-height-equals-width-with-pure-css.html

Difference between background-size:cover and background-size:contain

Visually I can appreciate the difference, but in which situations should I prefer one over the other?
Is there any point using them at all or can they be replaced by percentages?
Currently I don't seem to be able to go beyond a trial-error approach when using these properties, which does my head in.
Also I can only find pretty vague explanations and especially I find the W3C doc quite baffling.
Values have the following meanings:
‘contain’
Scale the image, while preserving its intrinsic aspect ratio
(if any), to the largest size such that both its width and its height
can fit inside the background positioning area.
‘cover’
Scale the image, while preserving its intrinsic aspect ratio (if any), to the
smallest size such that both its width and its height can completely
cover the background positioning area.
I'm probably being a bit thick, but can anyone give me a plain English explanation with relative examples?
Please use this fiddle. Thanks.
CSS
body{
width:500px;
height:500px;
background:url(http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg);
background-size:contain;
background-repeat:no-repeat;
}
Note
The accepted answer is the one I currently find the most concise and complete.
Thanks everybody for their help.
You can consider looking at the pseudocodes that govern the output. The values allotted to the image's size depend directly on the aspect ratios of container wrt aspect ratio of the background image.
Note: Aspect ratio = width / height
Contain
if (aspect ratio of container > aspect ratio of image)
image-height = container-height
image-width = aspect-ratio-preserved width
else
image-width = container width
image-height = aspect-ratio-preserved height
Cover
if (aspect ratio of container > aspect ratio of image)
image-width = container width
image-height = aspect-ratio-preserved height
else
image-height = container height
image-width = aspect-ratio-preserved width
You see the relation? In both cover and contain, aspect ratio is preserved. But the if - else conditions reverse in both the cases.
This is what makes cover to cover full page, without any white portion visible. When aspect ratio of container is greater, scaling image so that its width becomes equal to container width. Now, height will be greater, as aspect ratio is smaller. Thus it covers the whole page without any white portion.
Q. Can they be replaced by percentages?
No, not simply by percentages. You'll need conditioning.
Q. In which situations should I prefer one over the other?
When you are creating a website, you wouldn't want any white portion in the fixed background. So use cover.
contain on the other can be used when you are using a repeating background (e.g. when you have a pattern image with very high aspect ratio wrt veiwport/container you can use contain and set background-repeat to repeat-y). But a more appropriate use for contain would be for a fixed height/width element.
Although the question assumes the reader already understands how the contain and cover values for background-size work, here's a plain-English paraphrasing of what the spec says, which can serve as a quick primer:
background-size: contain ensures that the entire background image will fit the background area, keeping its original aspect ratio. If the background area is smaller than the image, the image will shrink so that it can fit the background area. If the background area is either taller or wider than the image, then any parts of the area not occupied by the main image will either be filled by repetitions of the image, or letterboxes/whitespace if background-repeat is set to no-repeat.
background-size: cover makes the background image as large as possible such that it will fill the entire background area leaving no gaps. The difference between cover and 100% 100% is that the aspect ratio of the image is preserved, so no unnatural stretching of the image occurs.
Note that neither of these two keyword values can be expressed using any combination of lengths, percentages, or auto keywords.
So when do you use one over the other? Personally, I think cover has more practical uses than contain, so I will go with that first.1
background-size: cover
One common use case of background-size: cover is in a full-screen layout where the background image is rich in detail, such as a photo, and you want to feature this image prominently, albeit as a background as opposed to the main content.
You want just enough of the image to be able to completely cover the browser viewport or screen, regardless of the aspect ratio of the viewport, or whether the image or the viewport is in portrait or landscape. You're not concerned if any parts of the image are cropped out as a result of this, as long as the image fills up the entire background area and maintains its original aspect ratio.
Here's an example of a layout where the content is housed in a semitransparent white background, which hovers over a full-screen background. When you increase the height of the preview pane, notice that the image automatically scales up to ensure that it still covers the entire preview area.
html {
height: 100%;
background-image: url(http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg);
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
body {
width: 80%;
min-height: 100%;
background-color: rgba(255, 255, 255, 0.5);
margin: 5em auto;
padding: 1em;
}
If you use background-size: contain instead, what happens is that the background image shrinks in order for the entire image to fit in the preview pane. This leaves ugly white letterboxes around the image depending on the aspect ratio of the preview pane, which ruins the effect.
background-size: contain
So why would one use background-size: contain if it leaves ugly blank spaces around the image? One use case that immediately comes to mind is if the designer doesn't care about the blank spaces, so long as the entire image fits within the background area.
That may sound contrived, but consider that not every image looks bad with empty space around it. This is where the example of using a logo instead of a photo actually demonstrates this best, even though you probably won't find yourself using a logo as a background image.
A logo is typically an irregular shape sitting on either a blank or completely transparent background. This leaves a canvas that can be filled by a solid color or a different background. Using background-size: contain will ensure that the entire image fits the background without any parts of it being cropped out, but the image still looks right at home on the canvas.
But it doesn't necessarily have to apply to an irregularly-shaped image. It can apply to rectangular images as well. As long as you require that no cropping of the background image occurs, whitespace can either be seen as a reasonable tradeoff, or not a big deal at all. Remember fixed-width layouts? Think of background-size: contain as essentially that, but for background images and in both portrait and landscape orientations: if you can ensure that the content will always fit the boundaries of the background image at all times, then whitespace becomes a non-issue altogether.
Although background-size: contain will work whether or not the image is set to repeat, I can't think of any good use cases involving repeating backgrounds.
1 Note that if you're using a gradient as a background, both contain and cover have no effect because gradients do not have any intrinsic dimensions. In both cases, the gradient will stretch to cover the container, as though you had specified 100% 100%.
background-size:cover will cover the entire div with the image. This could be useful for showing thumbnail images of a main image where the entire image being displayed isn't that important, but you still want to conform to a size for all images. (for example, a blog post excerpt)
background-size:contain will show the entire image within the div. This can be useful if you specifically want to display the entirety of the images, but within a set container div size. (For example, a collection of company logos)
Both keep the image at the same aspect ratio
http://cdn.onextrapixel.com/wp-content/uploads/2012/02/cover-contain.jpg
background-size:contain;
When using contain you may still see white-spacing, due to the way that it sizes and contains itself within the element.
background-size:cover;
will completely cover said element, you will not see any white-spacing
source:
http://www.css3.info/preview/background-size/
see example H
edit: use background-size:contain if:
You want it so that your image is always displayed in the viewport. Please note that: while you can see the full image, it will leave white spacing either on the top or bottom of the image whenever the aspect ratio of the browser and window are not the same.
use background-size:cover if:
You want a background-image, but you don't want the negative effect of the white-spacing which contain does have, please note that: when using background-size:cover; you may experience that it will cut off some of the image.
source: http://alistapart.com/article/supersize-that-background-please
Contain:- Scale the image to the largest size such that both its width and its height can fit inside the content area.
Exmaple:
Cover:-Scale the background image to be as large as possible so that the background area is completely covered by the background image. Some parts of the background image may not be in view within the background positioning area.
Example:
We had a huge conversation about cover vs contain just want to share this thoughts:
landscape image on landscape screen - best to use cover
portrait image on landscape screen - best to use contain
portrait image on landscape screen - best to use contain
portrait image on landscape screen - best to use contain
Illustration:
if(iDonPutSomeCode) const result = iCantPasteLinkToCodePen
https://codepen.io/Kinosura/pen/EGZbdy?editors=1100

Determine responsive image's height

Is there a way to determine the height of a background image when it's being used as a responsive image?
IOW, I want to know the height of the background image at a specific screen-size (but I don't know the actual screen size because that's the responsive part).
I know how to find the height of the image via the actual image file, but that's not the same thing as the image's height within a particular screen size.