I have a background image that I want to be fixed on larger screens and scroll on smaller devices.
This works great...
#main_page {
height: 100%;
min-height: 100%;
background-image:url('url');
background-position: 50% 50%;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
however I want to be able to use...
#media (max-width: 991px) {
#main_page { background-attachment:scroll; }
}
Using the background-attachment:scroll seems to kill background-size:cover and show it at it's full size (even bigger I think). When I test background-attachment: scroll; in the first set of CSS it does the same thing.
This exact thing seems to work on this site.... http://www.julytalk.com/ what am I missing?
I think I see what you mean. In the event that #main_page extends below the bottom of the viewport (I saw no problems here when it did not), the background image seems to inexplicably jump in size as soon as the media query breakpoint is reached and the background-attachment changes.
There's actually a good reason for this, and it relates primarily to your use of background-size: cover. While the style for #main_page is background-attachment: fixed, the area that this background needs to cover is only the size of the viewport, since the background never changes position relative to it.
However, once you cross that media query breakpoint and the style for #main_page changes to background-attachment: scroll, this area suddenly changes. Since the background now moves relative to the viewport, any part of #main_page that extends below the viewport needs to be covered as well. To account for this new area to cover, the background image instantly scales, resulting in this jump in size. (Exhibited in this JSFiddle.)
The site you linked to uses effectively similar styles to you, but ensures that their counterpart of #main_page never extends more than 100% of the viewport. For example, this JSFiddle uses your CSS to achieve a similar effect to them (no image size jump), since #main_page doesn't contain any content that would force it to exceed 100% of the viewport height.
Hope this helps! Let me know if you have any questions.
Related
I came upon a problem while designing a jumbotron for a website.
It's outside the container. Created a background for it, set background-size to cover, worked great.
When I opened the front page on a wider screen, the jumbotron became larger and there were gaps since the background image was too wide.
I then tried background-size: 100% 100%, thinking the image would stretch. No changes. Thought I perhaps had a margin/padding issue. No changes again.
Is this a problem with the image or the jumbotron? I'm not sure how to solve this. No matter what image I pick, it behaves very differently on different devices.
Behaving differently on different devices is not a bug, it's a feature. Having said that, try this...
background-image: url(INSERT_URL_HERE);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-attachment: fixed;
If you want the image to stretch to fit the full element (even if it means losing proportion), you need to set background-attachment to fixed and the background-size to 100%. Like:
.jumbotron {
background-image: url(image.url);
background-size: 100% 100%;
background-attachment: fixed;
}
Working example:
https://jsfiddle.net/4w9u7m1a/2/
The effect works fine, but the image is zoomed in on. Any clue as to why?
#hero{
background-image:url(../images/metalWorx_hero.jpg);
width:1020px;
min-height:398px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
Well, background-sizing: cover; is the reason why your background image seems "zoomed" as it is making the background image so big that it fully fits over its container. What happens on your case (feeling like its being zoomed in) is that the aspect ratio of the background isnt the same as the aspect ratio of the container. Instead of stretching the background image, background-sizing: cover will oversize the background so much until it covers everything up, leaving no gaps, but the "zooming" might happen.
Here is an illustration of the reason, as I know how bad my english :D
So you can see, the background image will be resized that it fits for the height, but because of the aspect ratio, both of the sides will go out of the container.
Edit #2 - Added some more informations and help
Depending on the real aspect ratios and sizes there are different solutions to it. The "quick and dirty solution" is to use background-size but instead of setting it to "cover", we will set its width and height to 100%. Code:
#hero{
background-image:url(../images/metalWorx_hero.jpg);
width:1020px;
min-height:398px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: 100% 100%; /* Set width and height to 100% */
}
Its a very simple "fix", but its obvious what can happen when the aspect ratio gets distorted:
Real and only way to fix it ;)
If you really want to fix it, you should make sure that your container and background image have about the same aspect ratio and then going back to background-size: cover; (just as in your first post)
The background image shows perfectly on PC and simulator (tablet and phone), but it won't show on actual phone/tablet.
code
div#photo_break {
#include viewport-unit(height, 28vw);
background-image: url('<img>');
background-repeat: no-repeat;
background-size: 100% auto;
-webkit-background-size: 100% auto;
-moz-background-size: 100% auto;
-o-background-size: 100% auto;
background-position: center top;
background-attachment: fixed;
z-index: 99999;
}
website
As you can see in this picture, in Chromes simulatorthe picture is shown.
The size of the image is not really large, or is it?
Nice site. :D Let's try to solve your issue.
Make sure you do not have that CSS code inside any media query, since it might block that content.
You might also try to add a new media query with the same CSS you have just for mobile, like this #media (min-width: 481px) { }.
Clean your mobile browser's cache before loading the site again.
Your background image is 12,000 pixels wide. It takes a long time to load to, I would suggest making the background of a parent div black inside the header, then putting an image in of just the person as the background of the element within that container if that makes sense. Save your mobile users data!
mobileand tablet browsersdo not support background-attachment: fixed.
changing this, solved the problem
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.
I have a background-image which I made responsive by adding background-size: 80%; to my CSS code. When I minimise the browser window it works to a certain extent, the thing is: I want the background-image to stop reducing at a certain point, as the other elements on my site do. But it reduces infinitely. To illustrate what I mean, check it up on my website: http://www.filmfutter.com/forum/
It's the white image at the top.
Currently the CSS code looks like that:
background-image: url(../images/blueTemptation/blueTemptationHeader.png);
background-size: 80%;
background-repeat: no-repeat;
background-position: top center;
You might add a media query breakpoint at that specific point. The given pixel value are approximate.
#media only screen and (max-width: 1140px) {
body {
background-size: 897px;
}
}