css background image resize width crop height - css

I am using this code from css tips and tricks to cover a background image. The problem is that it rescales the image to fit both width and height and changes the aspect ratio. I want the background image to rescale to full screen width and then crop the height only (starting from the top of the image, not the center) to cover the view port. In this way, the image aspect ratio will be maintained.
A secondary problem I have is that it doesn't seem to work unless I use the FQDN for the image instead of just the url below.
html {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

Instead of
background-size: cover;
you will want to use
background-size: 100% auto;
Cover will stretch the image to the smallest size such that both its width and its height can fit inside the content area, hence your issue. See more about that over here.

Image aspect ratio is not changed when you use cover. However, I believe what you want is to fix bottom of the image, since you told that you want it to be cropped from top, not center, which I thought you wanted to say not from top and bottom.
If you want to keep the bottom at the bottom, (maybe a grassland with an avoidably large sky?) then you will need to change positioning to center bottom from center center.
If you make it 100% auto as Nit suggested, then, on a window where height/width ratio of the html element is larger than your image's, you will see empty space at the top which can be what you wanted especially in case you are using an image that fades into the same color at top...
For your second problem, it is not fqdn, it is the relative/absolute referencing of your path. According to your css, there should be an image folder where your css file is which has a bg.jpg in it.
index.html
main.css
images/
bg.jpg

I would use JS for this instead. CSS doesn't seem to handle aspect ratios very well yet.
Try out something like this:
First, create a div and put an image in it
<div class="big-image">
<img src="path.jpg" width="1000" height="1000" alt="whatever">
</div>
Then do this in your CSS:
.featuredImage img {
position: fixed;
top: 0;
left: 0;
z-index: -10;
}
/* this class will be added via JS */
.bgwidth { width: 100%; }
.bgheight { height: 100%; }
And finally, your JS
var theWindow = $(window),
$bg = $(".big-image img"), // Use your image selector here.
aspectRatio = $bg.width() / $bg.height();
$bg.removeAttr('width');
$bg.removeAttr('height');
function resizeBg() {
if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
$bg
.removeClass()
.addClass('bgheight');
} else {
$bg
.removeClass()
.addClass('bgwidth');
}
}
theWindow.resize(function() {
resizeBg();
}).trigger("resize");
Let me know if this makes sense. Not exactly just CSS I know, but it's a solution

Related

Is there anyway to set background-size to both contain and cover?

Here is a pen to my attempt https://codepen.io/alexyap/pen/VbvGvw
<div id="bg">
</div>
* {
margin: 0;
padding: 0;
}
#bg {
background: url('https://static.pexels.com/photos/198747/pexels-photo-198747.jpeg');
height: 60vh;
width: 100%;
background-repeat: no-repeat;
background-size: 100vw;
}
I nearly had it figured out but I just can't seem to copy how this website https://sierradesigns.com/ did it (check slider images on landing page), mine is cut off on the bottom no matter what value i give the height
By setting the background size to cover you are giving the browser the prerogative to adjust the image until it completely covers the area; it will ignore width and height values you assign.
With contain you allow the browser to decide how to adjust the image so that the entire image fits within the area, which may be based on height or width to accomplish this (depending on the orientation of the image wide or tall).
background-size: 100% 100% is probably what you're looking for, but that will disproportionately adjust the image (ie: stretch or compress depending on orientation). However, it does sound like that's what you want when you say "both cover and contain".
There are many ways to place and scale images used as backgrounds (where background does not necessarily mean the CSS background property)
Below is a simplified example of how I've accomplished this (assuming images that are roughly 700x300 px)
.container-wrap {
width:100%;
}
.container {
position:relative;
padding:42.86% 0 0 0;
/* where padding = the proportion of the images width and height
which you can get by division: height / width = 0.42857 */
}
.container img {
position:absolute;
top:0px;
right:0px;
bottom:0px;
left:0px;
}
it is important that your images maintain a close proportion to each other -- if they are slightly off, the slight distortion shouldn't be visible to most people for most images
Again, there are other methods to accomplish this. The website you linked to applies a similar concept. The concept is the same, method is slightly different (for example they are using width:100% on the images instead of absolutely positioning them), where the concept = "using some sort of method to proportion the images to the container so it will magically scale"
Note that the same method can be applied to video containers (such as from YouTube).
The page that you link is keeping the aspect ratio of the container constant.
You can get this effect using both vw units for width and height (for instance)
#bg {
background: url('http://lorempixel.com/1000/600/');
height: 30vw;
width: 50vw;
background-size: cover;
}
<div id="bg">
</div>

How do I make header background image scale at same rate as body background?

I have a background-image in a fixed navigation bar and the same background-image in the main body of our website. The goal is to create the effect of the contents going under the navigation when the user scrolls like on this website:
http://bonobomusic.com/news.php
Here is the site I'm building where the problem is:
http://rattletree.com/wordpress2/
When I have the page at full width for my screen everything looks good, but when I resize the window then the background-image in the header is resizing at a different rate than the one on the body. This is the css I'm using for the header:
#main-header{
position:fixed!important;
background-image:url("img");
background-position-y: -62px;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I'm seeing that the image in the header is scaling smaller and smaller in both the x and y axis the whole time, and on the background image it scales for a while on both axis but then at a certain point, it is only scaling horizontally and not vertically. Any help would be appreciated!
Please note: the problem is most obvious when viewing the page at different screen sizes-like a mobile device vs a computer.
Sorry I can comment so I post an answer.
The problem is that your header has a position with 30px in Y. The easiest way is to set the value to 0px.
Else you'll have to use a second background img for header by removing 30px of the original.
Set your header to full width then give the body and the header the same background image and options. Also make sure the background is centered.
Now add something like this. You will most likely have to remove some of the !importants.
#media screen and (max-width: 999px) {
body {
background-size: contain;
}
#main-header {
background-position-y: -30px;
}
}
I used http://quirktools.com/screenfly/ to check upon different screen sizes
Have you tried positioning absolute both the header and body and make them 100% width and height of the window. Then put the background image on both as cover. Then you can make a div that is positioned however far from the top as your header should be and give it overflow hidden.

Background image size won't stay 100% height

I have got so far on the background of my new website and now i am stuck, the background image goes less than 100% height if you shrink the browser window.
I want it to stay full size and if you shrink it, I don't want the height to go any less than 100% (showing white)
Code here http://www.bestlincs.co.uk/new/
you can use below code:
html or .classname {
background: url(ImageUrlhere) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Use:
body {margin: 0; padding: 0}
and set the background-size property to cover: http://www.w3schools.com/cssref/css3_pr_background-size.asp
In your code you have not defined a height to the image give it a height 100% and it works i tried it in my browser and works fine
The solution depends on your needs - one way would be to specify a min-width and min-height attributes in css instead of pure width. As it will scale to whatever size it needs, then position it fixed to the top left corner (mind you, any "overflow" on the right will be cut off).
Source:
http://css-tricks.com/perfect-full-page-background-image/
A detailed explanation of your problem:
If you set an image to 100% width of its container and do not specify a height, it will always be stretched until it fills out the container, while the height is scaled to keep the image's aspect ratio.
E.g: Take an image that is 200px x 100px large, put it into a 300px wide container with it's width set to 100%. It will be scaled by a factor of 300/200 = 1.5 along both dimension, resulting in an image sized: 300px x 150px.
What will happen, if your image has a different aspect ratio than the user's screen? It will simply stretch to full width, then leave the rest blank. Setting a height as well would introduce even more problems, as your image would get distorted.
HTML:
body {
margin:0;
background: url(image.gif) no-repeat;
padding: 0;
}
Then the background size will be 100%

background-size: cover not working?

This is my page http://inogroup.it/preload/index.htm
Width of image boxes is responsive
How to set the height to be responsive too? Like 50% of the screen?
If I do this change:
.pattern{
background-size: contain;
margin-bottom:25px;
width:100%;
height:50%;
}
it's not working
Thank you very much!
background-size: needs to come AFTER background: I don't know if this is true of all browsers, but it's certainly a feature of Chrome that can drive you crazy.
This might be a bit late but in some cases it's necessary to add background-attachment: fixed; to get background-size: cover; working.
You need to use background-image property to define background image.
So this won't work
<img class="image" style="background: url(image.jpg);" />
.image { background-size: cover; }
because background is the shorthand code and takes default values for all omitted parameters.
But if you do this
<img class="image" style="background-image: url(image.jpg);" />
.image { background-size: cover; }
This wil solve the problem.
The height of the div can be set using css height property, or (by default) by the height of it's children elements. As the images are being set as background images the div is unable to determine the height it should be from that, and there is no pure css method of adjusting the height of a div to fit the dimensions of a background image. What you can do, is set the background images to be positioned in the centre of the div and have the background size as cover:
.pattern-container .pattern {
background-size: cover;
background-position: 50% 50%;
<!-- other rules here -->
}
Positioning the background images as 50% 50% vertically and horizontally centres it in the containing div regardless of the dimensions of the div. That said, the image itself may crop at the edges if the aspect ratio of the div is less than the aspect ratio of the image (e.g. if the div is 30px wide and 10px high, and the image is 40px wide and 10px high, then the image is going to lose 5px from both sides).
You can use ;
background: url('/path');
background-size: cover;
//in the style sheets
In some cases, there may be empty space in the edges of your image itself (e.g., an icon that is surrounded by 16px of blank white space on each side) making it seem like background-size: cover; is not working when it actually is.
Just a reminder to double-check your source image :)
The image boxes are responsive, but this does not mean that the corresponding images are. For a more fluid and dynamic structure, I recommend using a framework that does the work for you, like Bootstrap.
In the latest version of Bootstrap you could use the following code to make a responsive image (both in width and height):
<img src="images/my_img" class="img-responsive" />
In order for this to work, you will need to download the latest version of Bootstrap from their website (http://getbootstrap.com/) and reference in your code.

Can't get background header image to fit to width.. tried everything

I've searched for hours upon hours and now I figure it's time for me to ask the question. I can't get my background image that is placed in my header to fit to screen. It works for every kind of computer resolution fine, but where I run into trouble is when I am viewing on a phone, it doesn't want to shrink. I've done min-height, max-height, I've tried everything, the problem partly I think is that the header div itself is smaller than this image, but I also don't really know and need some guidance, i'm relatively new to the CSS scene.
Here is what I have:
#header {
background-image: url('http://hamsoc.polymath.io/wp-content/uploads/2013/05/hamsocheader.png');
background-repeat: no-repeat;
background-position: center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 209px;
}
Website url is http://hamsoc.polymath.io
Thank you for your help in advance!
Duncan Beattie gave me the answer and it worked like a charm. Here is what he said:
"You have background-size: cover which is fitting the height of the
background image to the fixed height of your div, 209px. remove the
fixed height and replace with padding-bottom:15% this will kep the
aspect ratio of the div the same and scale the image as viewport gets
smaller."
You have background-size: cover which is fitting the height of the background image to the fixed height of your div, 209px.
remove the fixed height and replace with padding-bottom:15% this will kep the aspect ratio of the div the same and scale the image as viewport gets smaller.
I would suggest having the header image in your HTML rather than a background image and then setting a max-width like so:
#header img{
max-width: 100%;
height: auto;
}
This will also allow you to make the image "clickable" which is generally wanted in a header logo.
DEMO FIDDLE
FULLSCREEN
Have you used the a precentage to set the height of the image in the div?
So set the image height to be say 100% of the div?
If not then maybe you could use some javascript code to detect whether they are on a mobile device, and set the height of it accordingly?
The hard coded height value is messing you up. Try playing with the height: 290px value and watch the header fit properly on smaller screens.
Instead of a background image, you can try putting the image in the html and using a CSS property to help the content scale down on smaller screens.
img {
max-width: 100%;
}

Resources