i have this jsfiddle: http://jsfiddle.net/SoSoDef/uhx3o62f/1/. It seems to work fine, the only issue
is that the image will be cut of a little bit at the top and at the bottom for some reason.
-webkit-background-size: cover;
Why ?
Appreciate all your help
The background image is being cut off due to CSS styling in relation to the dimensions of the JSFiddle Result viewport.
The CSS property 'background-size: cover' is constraining and scaling the image's overall proportional size based on the width of the JSFiddle viewport.
In your background property for the body style, 'center center fixed' is centering the center point of your image in a fixed position to the center of the viewport. If the scaled image (constrained by the width of the viewport) is taller than the viewport itself, the image is merely going beyond the upper and lower bounds of the JSFiddle Result viewport, and appearing clipped.
To illustrate, try removing 'center center fixed' from your background property on the body style, like this:
body {
background: url("http://f.cl.ly/items/260T100F3j2Y3L1S0g1w/bg.jpg") no-repeat #292929;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
The image will now position itself to the top of the viewport, and will no longer be clipped along the upper edge. Then, narrow the Result viewport, and the image will no longer be clipped along the bottom once it has sufficiently diminished in size.
I suspect that you are trying to scale your image to the available dimensions of the viewport, both horizontally and vertically. However, at any given time, you have a viewport of a random size, as well as an image that is of a certain aspect ratio. If you do not want to forcibly squash the image vertically or horizontally in order to achieve a total image fill, you must allow the image to scale in relationship to the width of the viewport (and potentially be clipped along the bottom edge), as suggested in this SO Answer.
If the content of the image cannot be clipped and must be viewable, instead of using CSS to place your image, use an 'img' element and scale that with CSS. The viewport will then be scrollable if the image content does go beyond the viewport's available dimensions.
I've created a codepen example of this latter approach for you.
From [W3Schools]
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
So basically it is behaving normally, when the body gets wider, the top and bottom of the background image will be cut off.
You could use contain instead and add a white background to keep the entire image at all times.
-webkit-background-size: cover;
background-color:white;
The background-size property let you expand and shrink according to size your div. Its responsive for more reference check out this LINK
Related
What I want to do on the background image is to expand width or height depending on the ratio of the browser size compared to that of the background image.
In this one the image is (almost) not streched; and I want the image to keep its original ratio. Basically, The width and height of background image would follow those of the browser screen.
In this screenshot, the image must be expanded to up and down because its width/height ratio is larger than the original. And the exceeded parts should be out of the browser pane.
In this one, the width/height ratio is smaller; as long as keeping its ratio and fitting the image height to the browser's height, the image should be stretched to left and right.
Hope you to understand what I want to do.. I think I might have to use something like if-then statement to detect the browser ratio and respectively change which side to fit to the image. Or are some other techs required to do this task?
Without the help of Javascript, I would suggest to use:
body {
background-image: url(image.jpg);
background-position: 50% 50%;
background-size: cover;
}
The background-position: 50% 50% will center the image horizontally and vertically. The background-size: cover will make the image cover the whole body.
Demo
I want to create a jumbotron that behaves like the one on the opening page of this site:
Website
No matter what size the viewport, it always fills the page with that gif and the rest of the site has sections which scale accordingly. Also no matter how you adjust the viewport, it does not distort the gif; it just re-scales or adjusts accordingly
I tried the following:
<div class="container-fluid special";>
<div class="jumbotron" style="background: url(chillin.jpg) no-repeat center center;-webkit-background-size: 100% 100%;
-moz-background-size: 100% 100%;
-o-background-size: 100% 100%;
background-size: 100% 100%;" ></div>
</div>
But it distorts as you change the viewport. What am I missing, do I just need to add some media queries?
By setting background-size to 100%, you're telling the browser to stretch the background to fill it's container, which will cause undesired distortion. Instead, try using background-size: cover. This tells the browser to make the image as large as necessary to completely fill it's container without distorting the image. Another way to think about it is that background-size: cover will minimally "crop" the image until it fits perfectly in it's container. (As a side note, background-size: contain tells the browser to make the image as large as possible without cropping the image, but this will result in white space above or below the image if the container is not the same size as the image.)
See my example here - http://jsbin.com/kutobedo/1/edit
When you shrink the browser window, the image resizes correctly and maintains it's aspect ratio. However I want the image to always stretch to fill the screen, but in this case it never stretches beyond it's native resolution.
If I set width:100% instead of max-width. It will stretch the image to fit the width but if you shrink the window vertically, it will start to distort the image.
If I set height: 100% instead of max-height I get overflow/scrollbars instead.
So I'm a bit stuck! Please note the images will be of different aspect ratios and resolutions.
I suppose in the long run, this might not be a good idea as a 1024x768 image blown up on a 4k screen may look a little nasty. Still would like a solution for now though.
Set image as background of a div & use background-size:cover;. This will stretch it to fill screen without losing aspect ratio, no matter what are image dimentions are.
With the property »cover« the image is scaled up to the entire background until the whole background is covered therewith. More at http://en.aufdemdach.org/css-en/css-center-background-images/
Example:
body{
background: url("http://lorempixel.com/g/1000/1000/") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I'm displaying some images as backgrounds on a webpage but the image isn't displaying entirely 'zoomed out'. Instead, it's taking just the left side for example.
How can I make the image display completely? Is it to do with the resolution?
#kitchenimage{
width:100%;
background: url("siteimages/kitchenimage.jpg") no-repeat center fixed;
padding:200px 0;
}
The image is 3249 x 1679.
Thanks.
Depending on which option prefer, define one of the following:
background-size: cover;
background-size: 100% 100%;
background-size: contain;
The first will scale the background image to be as large as possible so that the background area is completely covered by the background image, leaving some parts of the background cropped while keeping aspect ratio.
The second won't keep the aspect ratio and will cover the background without any cropping.
The third will scale the image to the largest size such that both its width and its height can fit inside the background area.
I'm building a thumbnail gallery. For each thumbnail, I am creating a clipper div and placing image inside that div. Thumbnail sizes are different for different images where as clipper div is 150x150px
<div class='clipper-div'>
<img class='image1' src='img/img2.jpg'/>
</div>
I want to place that image so that it occupies that div completely and rest of the image is clipped such that it is exactly centered.
In order to do this, I am seeing what is the smallest dimension of the image ( height/width ), making it 100%. And then for other dimension, which is now greater than div's 150px. I'm calculating difference, dividing it by 2 and setting top/left attribute to negative of that value so the image looks centered, and then adding overflow:hidden to clipper div so the excess portion is invisible.
I want to know if there is a better way of doing this, instead of me calculating these values can I just set CSS properties so that all images will be centered in that div (vertically/horizontally) and remaining part is clipped.
If my description is confusing, here is an example:
Let's say thumbnail size is 300x100px. So height is the smallest attribute, so I'm setting it to 100%. So height becomes 150 and width becomes 400px, so now I calculate the offset. In this case (400-150)/2 = 125px. Now I set image left to -125px
.image1{
position:absolute;
left:-125px;
height:100%;
}
Try this:
.Image1 {
background: url(images/bg.jpg) no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}