Firefox has a curious bug relating to background-attachment and background-size. I call it a bug, I suspect it isn't, but this renders the way I think it should in the other major browsers (emphasis on I think). The desired effect is to size a vertically repeating background to 100% width while maintaining image proportion.
Here's my original CSS, in this version, the background won't render at all for firefox (even with firefox specific background tags).
body {
background: #000 url('../background.jpg') left top repeat-y;
background-attachment: scroll;
background-size: 100% auto;
}
Changing background-attachment: scroll; to background-attachment: fixed; causes the background to render, but the "floating background" behavior is not desired.
[UPDATE]
Removing background position causes the image to render correctly again.
e.g.
body {
background: #000 url('../background.jpg') repeat-y;
background-size: 100% auto;
}
This is actually a problematic fix because my position values were actually 'left 113px'. I can fix this by applying the background to a wrapping div below the site header instead of the body element, but the fact remains that Firefox has trouble rendering background images with background-size: 100% auto; when position values are used and attachment is scroll.
[UPDATE 2]
I realize the bug only shows up when an additional piece of CSS is present. That's not to say the additional piece of CSS was at fault (it isn't), it's to say that this Firefox specific rendering bug is somehow caused by it.
The additional CSS (used for creating layouts with flexible 100% height)
html { width: 100%; height: 100%; position: absolute; }
And the complete body CSS to go with to replicate the bug
body {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
margin: 0;
background: #000 url('image.jpg') top left repeat-y;
background-size: 100% auto;
}
[SOLUTION]
I solved my problem by creating an additional wrapped div immediately within the body and attaching the background there without specifying position properties.
#wrapper {
position: absolute;
min-height: 100%;
width: 100%;
background: #000 url('image.jpg') repeat-y;
background-size: 100% auto;
}
But because I now have an additional wrapper anyway, I can go ahead and remove the position, height, and width attributes from html, rendering the bug moot and allowing me to set the position of the background as well.
I solved my problem by creating an additional wrapped div immediately within the body and attaching the background there without specifying position properties.
#wrapper {
position: absolute;
min-height: 100%;
width: 100%;
background: #000 url('image.jpg') repeat-y;
background-size: 100% auto;
}
But because I now have an additional wrapper anyway, I can go ahead and remove the position, height, and width attributes from html, rendering the bug moot and allowing me to set the position of the background as well. (see UPDATE 2 in my original question)
Related
I found the below code on stack overflow and was hoping for some clarity. I am updating my portfolio page and wanted to put a landscape image background but how it looks on my laptop (the way I want it to look) does not translate over to my second monitor which is much larger. If I were to implement something similar to the below code would this help alleviate the issue so I can ensure that my page is identical across all sceens?
.background-wrapper {
left: 0px;
top: 0px;
width: 100%;
height: 250px;
display: block;
overflow: hidden;
background-color:#ccc;
background-image: url(https://psuk.s3.amazonaws.com/asset/p4/image/c3c59e5f8b3e9753913f4d435b53c308/Shaun/3040ce690fce2054c2011cf6d2f8b537.jpg);
background-position: 50% 50%;
/* By defining this, your image _will_ be cut off when the screen is
* wider than 1920px, but since you don't want to stretch this would
* happen anyway. */
background-size: 1920px auto;
}
You can try this for a simple example of a fullscreen background image. You can edit the min-height value to get the effect you want.
Example: https://codepen.io/brooksrelyt/pen/REzMZW
HTML:
<div class="full-home" style="background-image:url('https://via.placeholder.com/1200x800');" ></div>
CSS:
.full-home {
width: 100%;
background-repeat:no-repeat;
background-position:50% 50%;
background-position:50% 50%\9 !important;
background-size: cover;
min-height: 100vh;
}
I have a react app with router. I also have a navbar.
I want my main page to have a background image that fills the entire page:
html, body {
height: 100%;
}
.Main .bg {
width: 100%;
height: 100%;
background-image: url("../resources/top-bg6.jpeg");
background-position: top;
background-repeat: no-repeat;
background-size: cover;
}
However, this causes the image to be cropped at height 56px.
I think the cause is all the encapsulating divs that are caused by the different components and the router.
If I try
.Main .bg {
...
height: 100vh;
...
}
A scroll bar appears. I do I deal with it?
Edit: Added a screenshot:
It's all is great but a little bit you have change in your code.
Apply 100vh height on your .Main div only and change .bg background-position = center.
Now your image come at centre of your background .it's depend on your image size.
For some reason, background images are not showing up at all in IE11 (Windows 7 Pro).
.home {
position:relative;
height: 620px;
background-image: url(/images/bg_home3.jpg);
background-position: 540px 190px;
background-repeat: no-repeat;
}
Any clues?
If you are trying to set background to HTML5 element there is a chance that it doesn't have some default style in IE11. For example if the element is main try to set display: block to it. You may also try to set width, but without seeing more of your code can't help you more. Look the example below.
.home {
display: block;
width: 100%;
position:relative;
height: 620px;
background-image: url("/images/bg_home3.jpg");
background-position: 540px 190px;
background-repeat: no-repeat;
}
necessary use width: 100px; <= you size px
.home {
position:relative;
height: 620px;
width: 200px;
background-image: url('/images/bg_home3.jpg');
background-position: 540px 190px;
background-repeat: no-repeat;
}
Use a png image as it seems IE 11 cannot display jpg with non web colors.
I think with two easy steps you can eliminate the problem:
Set display: block; The display property specifies the display behavior on your page. IE 8 and upper versions fully supports this property.
Put your url between quotation marks ("").
background-image: url("/images/something.jpg");
I found setting the background size of the image made it display in IE11.
I also had to set the width and height. Setting the value of these attributes to auto it didn't work.
I've been experimenting with a lot of ways to create a background image on a website that works well, however I am yet to find a version that does not causes some sort of an issue.
Here is what I am looking for:
When in 1920 x 1080 then the image is perfect, fits the screen as intended.
When the user zooms out the browser, the image stays intact.
When the user zooms in, the image zooms in as it should (scales with the website)
When the users makes the window smaller, the image stays centered and reduces left and right to keep the center of the image focused.
When the user zooms out and resizes browser, image stays centered, shrinks left and right and the image doesn't shrink.
Here is what I tried, and problems I have found:
Method 1
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;
}
Demo here: http://css-tricks.com/examples/FullPageBackgroundImage/progressive.php
Problem: Scroll down to the bottom of the page, refresh a few times. You will notice that the image doesn't load every time until you start scrolling. This issue only happens on chrome browser (Seeing how this is the most used browser by our visitors, its an issue) Also I have found that the image doesn't load as fast as with other methods listed below.
No fallback for older browsers (or at least I don't know how to set it)
Method 2:
img.bg {
/* Set rules to fill background */
min-height: 100%;
min-width: 1024px;
/* Set up proportionate scaling */
width: 100%;
height: auto;
/* Set up positioning */
position: fixed;
top: 0;
left: 0;
}
#media screen and (max-width: 1024px) { /* Specific to this particular image */
img.bg {
left: 50%;
margin-left: -512px; /* 50% */
}
}
Demo: http://css-tricks.com/examples/FullPageBackgroundImage/css-1.php
Problem: When browser is resized, image gets stretched.
Method 3:
<div id="bg">
<img src="images/bg.jpg" alt="">
</div>
#bg {
position: fixed;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
#bg img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 50%;
}
Demo: http://css-tricks.com/examples/FullPageBackgroundImage/css-2.php
Problem: Image shrinks and becomes bigger and browser resize (this doesn't happen in other examples) When you zoom out and resize, image stretches, doesn't stay centred.
I understand that what I'm asking for might not be possible with CSS, however I think if we can come up with a single solution that can work cross browsers for an image that stays centred and is not shrunk or stretched, that would benefit a lot of people.
Usually i use first method.
Else am usin an jquery plugin called Backstretch. Link here
Let me know how it goes after testing it.
Using web responsive design I want to put a big image as background on the frontpage, which always stays 100% width and height. Very similar to https://ghost.org
Here is how I'm trying to do it:
http://jsfiddle.net/Jp5AQ/1144/
div img {
max-width: 100%;
min-height: 100%;
height: auto;
outline: solid 1px red;
}
But it doesn't work correctly. The image is disturbed by resizing the window.
If you want to do this with pure CSS you can use the CSS3 background-size property:
div {
width: 100%;
height: 100%;
background: url(your-image.jpg) no-repeat center center;
background-size: cover;
}
Here is a fiddle demonstration:
http://jsfiddle.net/Jp5AQ/1149/
This is the same technique used on the Ghost site you gave as a reference.
Note that I've given the html and body elements height:100% so that the div stretches to fill the height of the viewport.
You are using <img /> tag.
Call the image through CSS and use background-size: cover;.
Here is the DEMO
Your image will always be the max-width of the browser window; however, the height will not stay at 100% of the browser window because the image on the screen needs to stay in proportion with the original image. As the width of the browser window shrinks, so will the height.Otherwise your image would be skewed.
You can use the property background: cover.
The post maybe be helpful http://css-tricks.com/perfect-full-page-background-image/.
Its hard to do that with an img tag.
it will work a lot better with:
background-size: cover;
background-position: center;
here is a Fiddle of it
just remove the image inserted with an image tag and instead put the image in a div as background.
div: should get height and width of window screen size (most likely with javascript)
insert image with the css style:
div{
background: url(path_to_img/img.jpeg) center center no-repeat;
background-size: contain; // you also can use cover, just look how you wish the picture should be cut or not. for background-size you also need some prefix for some browsers
}
here is the link for css background: http://www.w3schools.com/cssref/css3_pr_background.asp
and here is the link for background-size : http://www.w3schools.com/cssref/css3_pr_background-size.asp
try this css:
.banner-bg {
background-image: url(../images/header-bannerbg.png);
background-position: fixed;
background-repeat: no-repeat;
height: 580px;
-webkit-background-size: 100% 100%;
-moz-background-size: 100% 100%;
-o-background-size: 100% 100%;
background-size: 100% 100%;
}
http://jsfiddle.net/aashi/Jp5AQ/1146/