CSS background fixed and cover - css

I want to set a fixed background image that also covers the div but for some reason when I add fixed to the CSS, the image gets stretched way beyond the boundaries of the div.
These are 2 examples, one with fixed (incorrect dimensions) and the other with the correct dimensions but it's not fixed (it scrolls with page)
#incorrect{
min-height:100px;
background: url('http://034.83e.myftpupload.com/wp-content/uploads/2015/04/entete_image.png') no-repeat fixed center/cover;
}
#correct{
min-height:100px;
background: url('http://034.83e.myftpupload.com/wp-content/uploads/2015/04/entete_image.png') no-repeat center/cover;
}
<div id="incorrect"></div>
<br>
<div id="correct"></div>
How do you get both of these properties to work together? Are they incompatible?
EDIT: for some reason,the fixed property is relative to the viewport and not to the element itself The higher the screen, the bigger the image gets. Is there a turnaround?

What you are trying to do is not possible with pure CSS.
When you use background-attachment: fixed; it makes the image act the same as position:fixed.
position:fixed explained via https://developer.mozilla.org/en-US/docs/Web/CSS/position
Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and don't move it when scrolled. When printing, position it at that fixed position on every page.
So what it's doing is taking your background image "out of the div" and sizing it relative to the viewport itself. This is why it is "zooming" and "clipping" your background image.
You can work around this issue with JavaScript or jQuery. Here is a snippet with your code used as an example:
$(window).scroll(function() {
var scrolledY = $(window).scrollTop();
$('#incorrect').css('background-position', 'left ' + ((scrolledY)) + 'px');
});
#incorrect{
min-height:100px;
background: url('http://034.83e.myftpupload.com/wp-content/uploads/2015/04/entete_image.png') no-repeat scroll center/cover;
}
#correct{
min-height:100px;
background: url('http://034.83e.myftpupload.com/wp-content/uploads/2015/04/entete_image.png') no-repeat center/cover;
}
div{margin-bottom:200px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="incorrect"></div>
<br>
<div id="correct"></div>

Try the following:
#incorrect{
min-height:100px;
background-image: url('http://034.83e.myftpupload.com/wp-content/uploads/2015/04/entete_image.png');
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
}

Related

DIV collapses background image

I have a section container that holds a div (.tablet). Both section and Div are responsive
When the view port reaches > 400px, I want to display a background image in my div (.tablet)
HOWEVER, unless I specify a px width and height, the div collapses and disappears. It ignores the background image. It's Almost as if the background image does not exist and the div has no content!
Is it possible to create a resposnive DIV with a flexible background image that does not collapse? I want the background image to fill the DIV and respond with the div size?
i've tried using the following rules with the .tabletonly div
background-size: cover;
background-position: center;
background-attachment: fixed; /* Fixbackground * */
//--------------------------------------------------------------------
HTML
<!DOCTYPE html PUBLIC>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles/style1.css"/>
</head>
<body>
<div id="wrapper">
<div id="section">
<div id="tabletonly" class="span_1_of_2">
<div><div>
</div>
</div> // end wrapper
</body>
</html>
CSS code ----------------------------------------
.span_1_of_2 {
width: 45%;
position: relative;
}
img {
max-width: 100%;
}
#media screen and (min-width: 400px) {
#tabletonly div{
background: url("images/shop.jpg" );
background-repeat: no-repeat;
}
}
If you know the aspect ratio of the background image, there's a trick you can use to keep the box from collapsing - use border-box box-sizing, set the height to 0, and give the div a padding-bottom that reflects that ratio.
.image-box {
background: url('http://placehold.it/600x300/eee/ccc');
background-size: cover;
height: 0;
padding-bottom: 50%; /* match this to the aspect ratio of your picture */
}
fiddle
That being said, the other commenter had a point; depending on your use context, maybe this should be an inline image rather than a background one?
Try something like that. If I do understand your question, it will Center the background image and it will keep the ration as well.
background-size:contain;
background-position: center center;
background-repeat:no-repeat;
background-image: url(../images/YourBackgroundImage.png);
Using contain will skink or enlage your image, but will be fully visible. Make sure your background image is good enough for big screen,
The position will center from Top and from Bottom.
Make sure the image does not repeat as that make look bad.
And of course the Image you want to put as background.
I think your tabletonly div has height of 0 because it has no displayable content in it.
Perhaps you may specify min-height (i.e. may use vh, % or px) or populate div with content.
It is difficult to tell as I do not know what you need the div for.

How to use a sprite for a background on a div, positioning to a part of the sprite to the right of the div

The div that I want to apply a background on is larger than my sprite, and the part of the sprite that I want to use as the background is 100px from the top of the sprite.
How can I set the background of the div to be the part of the sprite I want, and have it be positioned to the right side of the div.
What I have: http://jsbin.com/yibesu/1/edit?html,css,output
I want the "what-i-want" background to be pushed to the right side of the div, and the rest of it be the red color.
My CSS:
div {
background: url('http://i.imgur.com/VBe5ey2.png') red;
background-position: 0 -100px;
background-repeat: no-repeat;
width: 400px;
height: 200px;
}
It's like I want the background position to be "0 -100px" and pushed to the right of the div. The sprite's width is smaller than the div. Is this possible? Or do I need to just rip the part out of the sprite and have it be it's own image? Thanks!
You're close. The only thing you need to do is use background-position to align it to the right. The new css is as follows:
div {
background: url('http://i.imgur.com/VBe5ey2.png') red;
background-position: right -100px;
background-repeat: no-repeat;
width: 400px;
height: 200px;
}
I tested it with Firefox, Chrome and IE11 and all seemed to work ok.
When I want to do this I will position my sprite to the right side of the sprites PNG, and with enough vertical space below it to ensure the next sprite in the graphic won't be visible at the bottom of the div.
In CSS I would define the background image's horizontal position as right:
div {
background: red url('sprite.png') right -100px no-repeat;
}
Here's an edited verion of your JSBin: http://jsbin.com/kohivibu/1/edit

Prevent background disappearing

So I've got a body with a CSS gradient background. Then I've got an absolute positioned div nested just within that with a background overlay. In turn, the content wrapper div is then nested within this. I want background div to be fixed and the web page to scroll over the top. The problem is, when the page scrolls the background overlay div kind of disappears like a roller blind...
Here's my fiddle to demonstrate the issue... http://jsfiddle.net/WPk6h/ (try scrolling the result pane to see the effect I mean).
HTML....
<body>
<div id="bgwrapper">
<div id="wrapper">
Content...
</div>
</div>
</body>
and CSS...
body {
background-color:#fcf
}
#bgwrapper{
position:absolute;
top:0;bottom:0;left:0;right:0;width:100%;height:100%;
background: transparent url(http://www.freesmileys.org/smileys/big/big-smiley-001.gif) no-repeat right bottom;
background-size:cover;
background-attachment:fixed;
}
#wrapper {
width:300px;
margin: 0 auto;
}
Any ideas how to prevent this so that the background overlay remains visible at all times?
note... I've not tested it heavily in all browsers yet - the issue is in the first browser I've been using, Chrome so I haven't got round to testing in others yet.
EDIT...
People are wondering why I don't just apply the background image to the HTML or BODY tags - well, there is a clash between CSS gradients and background images - you cannot have them both in the same element, as can be seen with the two examples below. This is why I'm using an additional background wrapper div to create the effect of an 5% alpha image overlaying the gradient bg.
http://jsfiddle.net/tqbtm/ (attempting to add gradient and bg image to body tag)
http://jsfiddle.net/ca5wa/ (adding bg image to bg wrapper div over the body gradient)
You need to remove position: absolute from #bgwrapper div:
#bgwrapper{
width:100%;
height:100%;
background: transparent url(http://www.freesmileys.org/smileys/big/big-smiley-001.gif) no-repeat right bottom;
background-size:cover;
background-attachment:fixed;
}
Update jsfiddle
You could also have a look at the following link:
http://css-tricks.com/perfect-full-page-background-image/
which details several different methods of doing full-screen, fixed, backgrounds
the method I currently use is method 1 (CSS3) for this kind of technique
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;
}
As Doug told you just add background-attachment:fixed; background-size:cover; width:100%; height:100%; to your #bgwrapper style.
Set the Position to be fixed
position:fixed
In ...
#bgwrapper{
position:fixed
top:0;bottom:0;left:0;right:0;width:100%;height:100%;
background: transparent url(http://www.freesmileys.org/smileys/big/big-smiley-001.gif) no-repeat right bottom;
background-size:cover;
background-attachment:fixed;
}

how to position two image as a background image on div by css

here is my css by which i position one image on at center.
.BusyStyles
{
background-image: url('../images/busy.gif');
background-repeat: no-repeat;
background-position: center center;
height: 350px;
width: 300px;
}
can i enhance the above css as a result i can place another image at center on the div just below the busy.gif......is it possible? if yes then please give me the css by which i can position two image as background for div at center one after one. thanks
Check sample for two background image in a single div tag.
CSS:
.container{
width:300px;
height:150px;
background:url(http://img.b8cdn.com/images/icons/loading_large_icon.gif) no-repeat 50% 28%, url(http://twitter-badges.s3.amazonaws.com/t_logo-a.png) no-repeat 50% 60%;
border:1px solid #CCCCCC;
}
You can only do this in CSS 3 (http://stackoverflow.com/questions/423172/can-i-have-multiple-background-images-using-css)
body {
background-image: url(images/bgtop.png), url(images/bg.png);
background-repeat: repeat-x, repeat;
}
I agree with LiamB's solution to this if you have the ability to only support browsers that are compatible with CSS 3.
However, if you need to support more browsers than that I recommend you solve this problem by having 2 divs. Both divs will be positioned on top of each other. The div positioned below contains only a background image. The div positioned on top contains another background image (positioned to look as if it is below the background image from the other div) and any content you want to have.

CSS body background-image issue

OK, this one works, the ocean picture shows up in the background but repeated 4 times to fill entire screen.
<style type="text/css">
body {background-image:url('ocean.png');}
</style>
<body>
</body>
Then change to
body {background-image:url('ocean.png') no-repeat center center;}
Now nothing shows up background picturewise.
Here's how you can do this and stretch it to the full height/width of the screen (note: if it is not the right proportion, some distortion will occur):
body {
background-image: url(ocean.png);
background-repeat: no-repeat;
background-size: 100%;
}
IIRC, background-size and being able to write url(...) without quotes are part of the CSS3 standard.
Because background-image can't take more paramters - change it to background: url..., which is the correct property to set all of those in one line.
body {background: url('ocean.png') no-repeat center center;}
Try it this way:
body {
background: url('ocean.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
You can size the background image in the background property as follows:
background: url('ocean.png') no-repeat top center 100% auto;
or
background-size:100% auto;
The first parameter is for the width, the second for the height. The first example will put the image top center and stretch it to fill the browser window in width, and set the height proportionately. You can also use "cover" in place of "100% auto" and the browser will fill the image in whatever way it needs to to fill your background.
Note: this is CSS3, and as such the background-size propery will only work in newer browsers, IE9, Firefox 4, ect.

Resources