Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Exactly Happens When Some CSS Code Found on the Footer

Tags:

I understand that CSS is used to decide about Layout and other styling things on Web Page. and If CSS is at the bottom of the page then everything (html elements, text, image, etc) will be displayed by using Browser's own styling and when browser find our CSS then it redesign pages again for us. It may be called repainting!

So, I understand that it will look very ugly repainting the page and user seeing it (FOUT - Flash of Unstyled Text - as expert named). But still, I want to understand about:

  1. How much time this repainting can take? Approx value! I understand this can depend on content on the page. What else happen or can happen?

  2. My main concern right now is about using font-awesome CSS file (externally hosted on their own cdn which download css and font files). I want to know what will happen across devices if I place this at bottom of the page or delay its loading ? Currently it is placed on <head> section as

    link rel='stylesheet' href='http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css' type='text/css' media='screen'

Use Del so that it should not look main part of the question. Main part of the question is about Some CSS at the bottom then What will happen to repaint, Blocking, etc. with measurement given or supported by measurement etc.

In the above case or in case when only part of document will get affected by CSS at the bottom then what will happen? Browser repaint everything, and what else? How much time it can take. Suppose, font-awesome is used at 10 icons placed in <i>.

I am never sure of what actually happens when CSS is at the bottom. So, please if you have any video or image showing flow then please mention here.

Base everything on performance across devices, and off course user experience as well. Thank you.

Update: I got something more for myself and for everyone. Here is a function (delayLoadCss) Google suggest for css for below-the-fold content. Though, I am not going to go that extreme but what about doing that for Font-Awesome kind of CSS? enter image description here

like image 353
Satya Prakash Avatar asked Dec 06 '13 06:12

Satya Prakash


People also ask

What is the purpose of CSS code?

CSS is the language for describing the presentation of Web pages, including colors, layout, and fonts. It allows one to adapt the presentation to different types of devices, such as large screens, small screens, or printers. CSS is independent of HTML and can be used with any XML-based markup language.

How do I know where my CSS is coming from?

You may use web inspector in Chrome. Right click on failing element and select inspect element. You should end up with web inspector window with two sections: left is html nodes tree and right is styles and properties of selected node. Failing element should be selected already.

Does the order of your CSS matter?

CSS Order Matters In CSS, the order in which we specify our rules matters. If a rule from the same style sheet, with the same level of specificity exists, the rule that is declared last in the CSS document will be the one that is applied.

Does CSS go at the top or bottom?

CSS should always be defined in <head> . AFAIK, the files that "should" be at the end of your document are scripts, not styles.


2 Answers

In my experience the loading of css will be virtually instantaneous no mater where it appear on the page--except in one instance: what will cause a delay in the browser applying your css is placing your css after a script element that takes time to complete.

This is why it is considered best practice to end your body section with your scripts, that way your page is rendered and styled before the browser commits to crunching through your scripts.

So if you html looks like this:

 <head>
      <link rel="stylesheet" type="text/css" href="/css/styles.css">
      <script>
           [long loading js]
      </script>     
 </head>
 <body>
      ... content
      <script>
           [long loading js]
      </script>
 </body>

Then your css will still be applied right off.

However if you structure it like this:

 <head>
      <script>
           [long loading js]
      </script>
      <style>
          [css here]
      </style>  
 </head>
 <body>
      ... content
      <script>
           [long loading js]
      </script>
 </body>

or even

 <head>
      <script>
           [long loading js]
      </script>    
 </head>
 <body>
      ... content
      <script>
           [long loading js]
      </script>
      <style>
          [css here]
      </style> 
 </body>

Then your css will not be applied to the document until after the js has completed.

The first is best practice and I recomend keeping style tags out of your document completely, but certainly out of the body of your document. External style sheets placed above you script tags is the way to go... This is true for font awesome's externally hosted css also. The browser should not hang on rendering that unless your link to it appears after a script element that is taking up the browsers attention.

* EDIT *

However this post directly contradicts what I just said.

like image 80
Jeremythuff Avatar answered Oct 11 '22 06:10

Jeremythuff


There are two cascades that occur with CSS.

The small Cascade: this is the parsing of an individual style sheet

The Big Cascade: This is where the browser performs three "small cascades", in this order:

  1. User Agent (the web browser's native stylesheet)
  2. Author (the stylesheet that you write)
  3. User (the stylesheet the end user can write).

Your question is about what would happen if you put styles anywhere but the head. Let's discuss:

  1. The browser has its own native stylesheet sitting in the background
  2. The browser loads your HTML document first
  3. The browser then reads the <head>
  4. the browser loads assets in the <head>
  5. the browser parses the rest of the document, i.e. the <body>. assets and style rules located here will be processed last.
  6. the last <style> block, or the last stylesheet in your document is the one whose styles over ride everything else.

In a nutshell, the browser applies styles in the order in which they are seen. A stylesheet at the footer would make things worse, not better. I can't offer a quantifiable measurement of worse because I don't have your stylesheets or website.

All Browsers have FOUC (or FOUT). The duration of it is based on the speed of the browser and the quality of your stylesheet. A minified stylesheet which applies text styles immediately after the reset, and before other styles, usually has the least amount of FOUC.

The styles in the footer are not blocked from being processed, and they will not block styles in the <head>, either. Styles in the footer are simply processed last.

like image 27
paceaux Avatar answered Oct 11 '22 06:10

paceaux