943,692 Members | Top Members by Rank

Ad:
Oct 6th, 2006
1

CSS - strech a background image?

Expand Post »
I am trying to stretch a background image to fit 100% of the page, I can do it when it's not a background image with the code below but how do I set it as a background image of 100%? Thanks. I keep seeing z-index, wtf is that? Thanks.

[html] #background {margin:0px;width:100%; height:100%; background-image:url("concrete2.jpg"); background-position:center; bgcolor:#000;}
#background img {width:100%; height:100%;}[/html]
Last edited by tgreer; Oct 20th, 2006 at 3:56 pm. Reason: Fixed closing code tag.
Similar Threads
Reputation Points: 37
Solved Threads: 1
Posting Whiz in Training
cmills83 is offline Offline
249 posts
since Jun 2004
Oct 18th, 2006
0

Re: CSS - strech a background image?

first of all # stands for ID.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Oct 20th, 2006
0

Re: CSS - strech a background image?

Click to Expand / Collapse  Quote originally posted by cmills83 ...
I am trying to stretch a background image to fit 100% of the page, I can do it when it's not a background image with the code below but how do I set it as a background image of 100%? Thanks. I keep seeing z-index, wtf is that? Thanks.

[html] #background {margin:0px;width:100%; height:100%; background-image:url("concrete2.jpg"); background-position:center; bgcolor:#000;}
#background img {width:100%; height:100%;}[html]
Unfortunately you are going the wrong way. The way you try to do it is wrong from the beginning.
1. I assume you are trying to set background to a DIV tag with the ID="background". The way you've made your #background definition, your css sets a background image for your DIV element, which cannot be accessed using an img definition in your css.

2. Your #background img definition however does not control the image for your background as well. The way the browser reads your second definition is something like this "Set width and height for any image contained in the object with id="background" to this width and height", however you expect and want to make the browser read it this way: "Set background image for object with ID=background to this width and height".

Unfortunately background image size cannot be controlled by css. These are your only options:

http://w3schools.com/css/css_reference.asp

Check the background section for full description and browser support.

You still can make it work the way you want, however you will end up in very complex absolute positionning on your page, that will make it a complete mess.

Still if you want to try to solve it, you can make 2 div elements. Set Z-index for your #background to be 2, position: absolute, top: 0, left: 0; width: 100%; height: 100%; for your #background img element should be smth like this width: 100%; height; 100%; and for your other div e.g. id="content" set z-index=1; position: absolute; top: 0; left: 0;

The code should be approx like this:
[html]
<div id="background" style="z-index: 2; position: absolute; top: 0; left: 0; width: 100%; height: 100%;">
<img src="your_image.jpg" style="height: 100%; width: 100%;" />
</div>
<div id="content" style="z-index: 1; position: absolute; top: 0; left: 0; width: 100%; height: 100%;> ENTER YOUR CONTENT HERE </div>[/html]
Hope it will work for you.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Oct 20th, 2006
0

Re: CSS - strech a background image?

but the problem with the above approach will be that the background will always remain stationary. It won't scroll down or up.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Oct 20th, 2006
0

Re: CSS - strech a background image?

Click to Expand / Collapse  Quote originally posted by vishesh ...
but the problem with the above approach will be that the background will always remain stationary. It won't scroll down or up.
Well, I would not recommend this aproach at all, as whatever picture you youse it will pixelate because of the stretch, or it will distort as a result of windows stretching. As far as scroll - I have not tried it, however, i think there will be no need to scroll the picture, as it will be always 100% both in width and height. It will look ugly anyway.

Moreover, positioning and readability may turn out to be a problem. The best way in my opinion is to use a nested table inside the content div in order to keep the positioning, but what's the point of css then - it will ruin the whole idea of using styles and reducing nested table usage.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Oct 20th, 2006
1

Re: CSS - strech a background image?

your table bg idea is good i have done that several times. but what i mean to tell u about scroll is that sometimes u really need that. You never know when your requirements change. Also using CSS you will have to change that z-index, every time you update your page maybe major). So table the best option. Another think I had gone out of the topic in my last post, i mean the from the question. Actually I remembered the question partially and didnt revised what was written.
Last edited by vishesh; Oct 20th, 2006 at 10:52 am.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Oct 20th, 2006
0

Re: CSS - strech a background image?

Click to Expand / Collapse  Quote originally posted by vishesh ...
your table bg idea is good i have done that several times. but what i mean to tell u about scroll is that sometimes u really need that. You never know when your requirements change. Also using CSS you will have to change that z-index, every time you update your page maybe major). So table the best option.
http://www.daniweb.com/techtalkforums/post265577.htm - this guy here tries to do the same thing - I hope he tries my way and we can see the result . Otherwise I will have to do try it, but I don't have a lot of time.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Oct 20th, 2006
1

Re: CSS - strech a background image?

Click to Expand / Collapse  Quote originally posted by Rhyan ...
http://www.daniweb.com/techtalkforums/post265577.htm - this guy here tries to do the same thing - I hope he tries my way and we can see the result . Otherwise I will have to do try it, but I don't have a lot of time.
I made this post and have tried your solution. Unfortunately although you set the height 100% it does not stretch the image to fit the browser windows height in IE and in Firefox it ust sets the image to 100% if it's normal size. And as Vishesh said this will not stop the background from staying static. This is not really a solution in my eyes. In the end I just made sure that my image displays the needed info on a 800*600 screen and a 1280*1024 screen. Oh well you better keep trying. BTW z-index is not a good idea for this because the only way you can set the contents position is by pixel or percentage which means you cannot center it.
Reputation Points: 178
Solved Threads: 15
Nearly a Posting Virtuoso
roryt is offline Offline
1,282 posts
since Oct 2005
Oct 20th, 2006
1

Re: CSS - strech a background image?

CSS cannot stretch an image: a browser is not an image editing program. CSS can repeat an image, but the image must be designed naturally to tile well.

The typical technique is to divide an image that must "stretch" horizontally into three images, a left, center, and right. The center image will be a solid color and will be set to repeat/tile in the x-dimension. This gives the effect of stretching.

The technique for an image that must expand vertically is to divide it in two, a top that remains static and a bottom that is a solid that is set to repeat/tile in the y-dimension.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Oct 20th, 2006
1

Re: CSS - strech a background image?

I saw what you're trying to do on the other page and I haven't tried my solution. By the way, I agree that the Z-index is not a solution, as it requires absolute positioning, which in my opinion should be avoided, as long as it is not essential for the page layout, as it may to cause extra prblems rather solving them .

As long as regarding your page, on 1600*1200 your black background is visible approx 40px on both left and right sides. If you want to see how it looks I can snapshot it for you and send it over mail.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Site Layout and Usability Forum Timeline: Css Fixed Background Attachment
Next Thread in Site Layout and Usability Forum Timeline: Need help with form - Host - Go daddy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC