Forum Moderators: not2easy

Message Too Old, No Replies

Backround Loading Question

css background loading

         

tony442200

8:45 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



I am pretty new to web design and css. One thing i am currently concerned with with my website, is the slow load time for the pages white background. I never really knew that a white background even needed much time to load. You can visit my site here <snip> . If you watch the page load you will see that my breadcrumb navigation div loads right away, but the rest of the page's white backround takes time to load. To me it looks like I have something messed up in my css code or something. I'm sorry about the confusing description, I am not even really sure if something is wrong here...

Again you can see what I am talking about at <snip>

Thanks again guys

[edited by: bill at 7:42 am (utc) on Apr 20, 2010]
[edit reason] No links to your sites or example. Please read the forum charter. [/edit]

tony442200

2:40 am on Apr 20, 2010 (gmt 0)

10+ Year Member



Any help, I'm just looking to see if there is any issues I need to fix...

rocknbil

5:52 pm on Apr 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...my breadcrumb navigation div loads right away, but the rest of the page's white backround takes time to load.


Welcome aboard tony442200, no one's answered because . . we need a little more info. :-)

Post **just** the CSS used for this background (anonymized per TOS,) is it 1) a div or the page background? Or is it 2) that the body's background renders first, and too dark, before the white BG of the content area displays?

If it's the first case, is it one of those huge full page BG's or a small repeating pattern? You can help this a little by optimizing the images without losing significant quality.

In really tough situations, you can include a minimal Javascript preloaded just before the css, like

<script type="text/javascript" src="/js/preloader.js"></script>
<link rel="stylesheet" href="/your-style.css">

... where preloader.js loads the basic minimal images for the layout. The logic is the preload executes *before* the CSS or source code is called, so the images are cached before anything significant renders in the page.

There are many preloader samples on this message board in the Javascript forum.

tony442200

6:10 pm on Apr 20, 2010 (gmt 0)

10+ Year Member



It is number two. The bodies background which is a small gif image loads before the white content area backround. It looks really funny this way because you can see all the image outlines while the white content area background is still loading.

Let me know if you need more info. I am having a hard time describing the issue because I honestly don;t know too much about this topic. I am pretty sure there is something wrong however, because this issue seems pretty recent and did not occur in the past...

[edited by: tedster at 6:32 am (utc) on Apr 23, 2010]

tony442200

1:44 am on Apr 25, 2010 (gmt 0)

10+ Year Member



Nobody has any ideas?

rainborick

2:02 am on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Take rocknbill's suggestion and use a JavaScript image preloader. A preloader does what the name implies - it pre-fetches a list of image files before the page is rendered. This should go a long way toward helping to eliminate the unwanted visual effect you're seeing.

You should also, of course, check the size of the problem image file to see if it's bigger than it should be. Another option to speed things up is to set up a subdomain to specifically hold commonly-used page elements like images. Depending on the complexity of the page, this can give you a nice speed boost.

tangor

2:21 am on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Dang it, I'll bite with a thought... why use an image file as your background? Default for all browsers is white background. Sometimes we can be too art before content...

Kill all the pretty stuff first to make sure the page loads upfront and center, then add the frills. And do so sparingly... after all, it is YOUR bandwidth you'll be paying for...

One can shoot themselves in the foot repeatedly by trying to be too clever.

Tangor, of the K.I.S.S. methodology of web presentation...

rocknbil

5:45 pm on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The bodies background which is a small gif image loads before the white content area backround....this issue seems pretty recent and did not occur in the past...


This is most likely because the gif image is now cached. Your page loads from the top down, the browser encounters the style first, then the <body> element, so the background image loads before the rest of the page renders.

Usually the problem is the other way around, which is a good case for preloading, but yes, I've recently encountered this and there are times when you want to NOT load up some elements until the page has fully loaded. And I have a workaround . . . but it involves Javascript. So consider your page without Javascript, if it still works and is legible, this may be a good solution. (See "untested alternate" below, may work without JS.)

Start by setting the initial style to white:

body { background:#fff; }

Now right after the CSS link line in the head, add a link to Javascript:

<link rel="stylesheet" type="text/css" href="your-current-style.css">
<script type="text/javascript" href="style-delay.js"></script>

The contents of style-delay.js would be something like this:


window.onload=function() { setBodyStyle(); }
//
// important to note: the below references the FIRST
// style sheet in the document, styleSheets[0]. If you
// have several style sheets linking from your document
// be sure to modify the LAST style sheet referenced,
// remembering arrays are zero based. So if it has multiple
// style sheets, the second one would be styleSheets[1],
// the third styleSheets[2], etc.
function setBodyStyle() {
// Non IE
if (document.styleSheets[0].cssRules) {
var oLength = document.styleSheets[0].cssRules.length;
// if it's just one direction of repeat, modify accordingly:
// background:url(/path-to/your-bg.gif) top left repeat-x;
document.styleSheets[0].insertRule('body { background:url(/path-to/your-bg.gif); }', oLength);
}
// IE
else if (document.styleSheets[0].rules) {
var newStyle = document.createStyleSheet();
newStyle.addRule('body' , 'background:url(/path-to/your-bg.gif);');
}
}


What is happening: window.onload() insures this bit is not executed until the entire document finishes loading.

After the style sheet is loaded, on full load of the document the default style sheet is appended to with a new style for body, which is why it's important it comes AFTER the link to the CSS. "Most browsers" and IE manipulate the style sheet object differently, hence the if/else if.

Untested Alternate: This too should work, and without Javascript. The down side is it requires a) a CSS link in an odd place, and I'm not sure if it will work (never done it,) or b) a CSS section inline with the document content, which I try to avoid.

Using the same concept, you want the entire document to load before setting the background image. So you'd put a request for it at the end of the document, just before the closing body tag.

a)
<link rel="stylesheet" type="text/css" href="set-body-image-only.css">
</body>

b)
<style type="text/css">
body { background:url(/path-to/your-bg.gif); }
</style>
</body>

One of those three should be sufficient to delay the load of the image until the document loads.

tony442200

3:44 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



Ill give that a try rocknbil, thanks.