I have a full screen background image, which keeps the aspect ratio and works in all browsers fine but not in Safari. I'm good at CSS but at this point I don't know exactly what the problem is. It seems that Safari has a problem keeping the vertical center and creates gaps at the top and bottom:
<div id="bg">
<div style="display: table-cell">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="loading"><img src="images/home.jpg" /></td>
</tr>
</table>
</div>
</div>
CSS:
html,body,#bg,#bg table,#bg td {
width:100%;
height:100%;
overflow:hidden;
position: relative
}
#bg div{
position:absolute;
width:200%;
height:200%;
top:-50%;
left:-50%
}
#bg td{
vertical-align:middle;
text-align:center
}
#bg img{
min-height:50%;
min-width:50%;
margin:0 auto;
display: table
}
Any CSS crhacks here?
If you are looking to use an actual image as a background instead of a background image in your CSS, a JavaScript solution might be better suited for your needs as it doesn't introduce extraneous markup into your design.
Check out jQueryMaxImage: http://www.aaronvanderzwan.com/maximage/ and here's one that I wrote, jquery.FullScreenBG: https://github.com/huntedsnark/jquery.fullScreenBG
If you don't want to do it with a script there is a way to do it using CSS# plus non-standard CSS for compatability:
html {
background: url(images/yourImage.jpg) no-repeat center center fixed;
/* CSS3 */
background-size: cover;
/*other browser specific */
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
/* non-standard for IE */
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.yourImage.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='yourImage.jpg', sizingMethod='scale')"
}
You really should not be adding all kinds of table markup for the sake of a background image.
Related
I have an image for a website and I wish to resize it so it covers ANY given screen.
Note:
I don't want the scrollbars to appear.
I am trying to resemble something like the following website
http://frontrow-demo.squarespace.com/
The only solution I can think of was re-sizing the height and width but that makes the scroll bars appear. I am guessing its a CSS solution I am struggling to create.
Edit:
This is the code I tried, but its "Zooming" the pic to make it stretch across the screen. I would like it to resize it so the quality and the actual pic would show up.
#bg {
position: fixed;
top: 0;
left: 0;
min-width: 100%;
min-height: 100%;
}
Just use some simple css
#theimg {
position : fixed; /*can also use absolute and the parent element can have relative*/
top : 0;
left : 0;
width : 100%;
height : 100%;
}
here is a demo fiddle
http://jsfiddle.net/rg3eK/
You want to use the CSS background property: http://css-tricks.com/perfect-full-page-background-image/
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;
}
Works in:
Safari 3+
Chrome Whatever+
IE 9+
Opera 10+ (Opera 9.5 supported background-size but not the keywords)
Firefox 3.6+ (Firefox 4 supports non-vendor prefixed version)
Anchor it to the sides and set the size to auto.
img#fullscreen {
height:auto;
width:100%;
left:0;
right:0;
top:0;
position:absolute;
}
Working fiddle.
This won't resize to the bottom (the one on the linked page also doesn't) but also doesn't mess the aspect of the image.
You may want to check photo-resize option.
Or you may try adding this:-
<img src="picture.jpg" width="100%" alt="">
ie, if you set only one dimension of an image, the other will end up being proportional to it.
or this:-
#image{
min-width:100%;
min-height:100%;
height:auto;
width:auto;
left:0;
right:0;
bottom:0;
top:0;}
Use background-size: cover in CSS on the HTML element
html {
background: url(http://i.imgur.com/tENv1w4.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
See this fiddle:
jsFiddle
For a better css.you must use this
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;
}
Im trying to add a background photo to a part of a website using css. I'd like to set the photo as "cover" to stretch and resize it as the browser from a visitor lets it to.
The thing is, i'd like to split the website in 2 parts, #top and #bottom. Whereas the top half is about 79% and bottom 21%.The #top part will contain the "cover" photo, whereas the bottom half will have a certain background color.
My problem is, that my current setup "sort of" works for IE (stretching is a bit off), but does not for Firefox and Chrome. I've tried many different things, but just can't get it right.
The problem that occurs is, that in Firefox and Chrome the "cover" photo clips behind the bottom background, whereas in IE, the photo actually works as intended, and fills the #top part.
<html>
<head>
<title>Test</title>
<style>
html {
width:100%;
height:100%;
color:white;
}
body {
height:100%;
width:100%;
}
#top {
background: url(PZwx1) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../img/BV-site-bgfoto.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../img/BV-site-bgfoto.jpg', sizingMethod='scale')";
height:79%;
}
#bottom {background: #F9525F;height:21%;}enter code here
</style>
</head>
<body>
<div id="top"></div>
<div id="bottom"></div>
</body>
</html>
I've worked out the example here to; http://www.webdevout.net/test?02X
Any help is much appreciated!
Remove the "background:fixed" (it's what make it use 100% of the page height) and use "100% 100%" instead of "cover". That should do it.
#top {
background: url(lalala.jpg) no-repeat center center;
background-size:100% 100%;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='lalala.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='lalala.jpg', sizingMethod='scale')";
height:79%;
}
If the fixed is important, then use background-size:100% 79% and change the background:center to background:top
#top {
background: url(lalala.jpg) no-repeat top fixed;
background-size:100% 79%;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='lalala.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='lalala.jpg', sizingMethod='scale')";
height:79%;
}
(also, add padding:0,margin:0 to your body)
I have a background image covering the entire width and height of the browser screen. It works in chrome and IE but in Firefox the image is being pushed down. Any ideas what's happening?
/* CSS */
<style type="text/css">
#bg {
position:fixed;
top:0;
left:-50%;
width:200%;
height:75%;
}
#bg img {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
min-width:50%;
min-height:50%;
}
</style>
/* HTML */
<div id="bg">
<img src="http://image.iloqal.com/lib/fe5c1570746107757c1c/m/1/BMW_528_bkg.png" id="back1" alt="" />
</div>
You should try the following to make the image fullscreen:
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;
}
For details, have a look here.
Justin first of all, by definition a BACKGROUND image, is set via CSS and not inline as you do it. So, a first solution is rather simple:
#bg {background: url("images/yourimage.jpg") no-repeat center fixed;}
It will put your background in the center, and there you go. the #bg acts as a wrapper for the rest of your content/markup.
Now if you want your background to automatically resize depending on the browser window, there is no way to do it if you can't use CSS3.
ps: note that you can fake a background taking the whole screen by photoshoping a very long and wide image that will repeat a background color, and by using the background shorthand like that:
#example { background: #fff url(image.gif) no-repeat; }
So if your car image is covered by a white background color, it will be ok not only for the original image resolutions, but also for bigger ones as the white color will cover the browser window.
Please comment if you have more specific needs.
If you can't use CSS3 - change following code
#bg img {
position:absolute;
top:-50%;
left:0;
right:0;
bottom:0;
margin:0 auto;
min-width:50%;
min-height:50%;
}
I got this problem trying to get the header-bg div to always be the full browser width.
I have a background image with some clouds and when I use ctrl+scroll the image stays the original width and stays left aligned. At original page view the bg is perfect 100% width, but I want it to be perfect with al screen widths. Is this even possible or am I wrong?
Heres the code:
body, html {
height:100%;
width:100%;
margin:0;
padding:0;}
#header-bg {
background-image:url(images/header/header-bg.jpg);
background-repeat:no-repeat;
min-width:100%;
width:100%;
margin:0;
position:fixed;
height:402px;}
<div id="header-bg">
<div id="header">
<div id="navigation">
</div>
</div>
</div>
Thanks for looking!
By default, the background image cannot resize. The background-size property has to be used to get your background to fill the whole DIV:
#header-bg {
background-image: url(images/header/header-bg.jpg);
-moz-background-size: 100% 100%;
-o-background-size: 100% 100%;
-webkit-background-size: 100% 100%;
background-size: 100% 100%;
/*Other CSS*/
}
This CSS feature is supported by:
Firefox 3.6+
Opera 9.5+
Chrome 1+
Safari 3+
Internet Explorer 9+
If you cannot use this property, your only remaining option is using a <img> element.Fiddle: http://jsfiddle.net/Hf79s/
<style>
#header-bg{
width:100%;
height: 50px;
}
#header-bg-img {
height: 100%;
width: 100%;
}
</style>
<div id="header-bg">
<img src="images/header/header-bg.jpg" id="header-bg-img" />
</div>
You could just make the background image extra wide, so it will fill big screens too. The only downside is that smaller screens won't see the entire image, but when it is only decorative clouds, it should be fine.
The other solution is background-size. But this has the disadvantage that the background will either be scale disproportionately, or the bottom of the image is clipped. Also older IE versions don't support it.
I have a large image I would like as my background, but for some reason it repeats a little bit on my large screen. Is there a way I can just have the image size up or down according to screen size?
EDIT: So I have changed my HTML to look like this:
<body id="wrapper">
<div id="body">
<img src="/images/sky2.jpg" class="stretch" alt="" />
</div>
and my CSS to this:
#body {
width: 100%;
height: 100%;
position: absolute;
left: 0px;
top: 0px;
z-index: 0;
}
.stretch {
width:100%;
height:100%;
}
And the background won't show on preview. I have 3 other div elements that show but only to a white background =/.
move background-repeat: no-repeat; to the #body instead of #body img
You aren't actually showing any of your html here, just some embedded CSS and some (I assume linked?) CSS. You are loading the image as a background-image on the body element in that first bit of css, which is great. Because it's loaded as a background-image in CSS, and not and tag in HTML, your second bit of CSS (with the #body img selector) is not affecting it in any way.
What you actually have, in effect, is this:
#body {
position:fixed;
top:-50%;
left:-50%;
width:200%;
height:200%;
position:relative;
background-image: url(images/sky2.JPG);
}
Which is a very odd bit of code. But the only relevant part to your question is the background-image part. The answer has several parts. In CSS2: no, you cannot adjust the size of a background image. You can set it not to repeat (as others have shown) and you can set it's position:
body {
background-position: center left;
}
In CSS3 you can change the size, and you have several options (you are looking for cover, I think) but it only works for the latest browsers. The property is called background-size, but because it is still experimental, you have to declare it individually for each browser:
/* this is the default */
body {
-moz-background-size: auto;
-webkit-background-size: auto;
-o-background-size: auto;
background-size: auto;
}
/* this will size the image proportionally so that it is contained, but not cropped */
body {
-moz-background-size: contain;
-webkit-background-size: contain;
-o-background-size: contain;
background-size: contain;
}
/* this will size the image proportionally so that it fills all the area */
body {
-moz-background-size: cover;
-webkit-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
/* this will size the image as a percentage of the area */
.example #percent {
-moz-background-size: 50% 50%;
-webkit-background-size: 50% 50%;
-o-background-size: 50% 50%;
background-size: 50% 50%;
}
/* this will size the image to exact specifications */
.example #absolute {
-moz-background-size: 100px 25px;
-webkit-background-size: 100px 25px;
-o-background-size: 100px 25px;
background-size: 100px 25px;
}
#img.source-image {
width: 100%;
position: absolute;
top: 0;
left: 0;
}
Demo page:
http://css-tricks.com/examples/ImageToBackgroundImage/
Source:
http://css-tricks.com/how-to-resizeable-background-image/
I think it's worth to read that page :)
1) The CSS property background-repeat: no-repeat; should be on the body element itself, i.e. on the element you're specifying the background of.
2) In the CSS, you write #body... I guess you want to talk about the body element? Then you should just write body in the CSS. #body would be for an element declared as, say, <div id="body">.
3) Also, I'm unsure about #body img. #body img means “an img element inside the body”. Do you really have an img element inside the body? I mean, is your markup like this?
<body>
...
<img ... >
...
</body>
And do you really want to style that img element?
Anyway, the style that applies to the img element has nothing to do with the body's background.
<style type="text/css">
body {
background-image: url(images/sky2.JPG);
background-repeat: no-repeat;
}
</style>
You need to set it for the same element or class or whatever.
Also you could move the body css into your css.
Ok, I'm sorry there are some other things wrong, like #body {. I don't think you have an element with an id "body".
Not trying to RTFM, but maybe read some tutorials on CSS?
To scale the image, maybe have a look at: Stretch and scale a CSS image in the background - with CSS only