|
CSS Embeded, Linked, Inline?
Confused when to use each type sheet. |
MacBarry
#:1216202
| 12:23 am on Jan. 8, 2003 (utc 0) |
First post, I have been reading posts here for six weeks they are great! Have been on a crash corse to learn html and css for about ninty days. Have read several books and looked at several sites on this subject. I am starting to see some light at the end of the tunnel. Confused on when to use embeded,linked,inline sheets. Any help would be great. As I amy trying to get away from FP2002 BLOAT! Thanks MacBarry
|
piskie
#:1216203
| 12:43 am on Jan. 8, 2003 (utc 0) |
Welcome MacBarry, now you have taken the plunge, I am sure we will see you more often. I always use external css as normal practice. I only deviate from this when there is good reason to do so. Others may not agree but I think it enforces good housekeeping as well as enabling sitewide tweaks in a jiffy.
|
dingman
#:1216204
| 5:11 am on Jan. 8, 2003 (utc 0) |
I think piskie is right on the money. External whenever possible. It lets you re-use the styles, which you probably want to do anyway to keep a consistent look and feel, as well as letting you make site-wide changes in one place instead of several - a definite plus in my book! Furthermore, external sheets will be cached by the browser, so they don't have to be re-requested with each page. This reduces the ammount of time it takes for pages to load on the 2nd and subsequent times a user requests a page from your site. <link> vs. @import: linked style sheets are supported by some older browsers that do not recognize the @import directive. This makes <link>ing a good choice for a single external style sheet, since it will be picked up by the largest possible number of browsers. It also enables you to use a simple hack to give older browsers (pricipally NN4.x) a simple style sheet with a <link> and then add another with more complex features for newer browsers with @import. Much cleaner and mroe reliable than browser sniffing. Warning: many current browsers allow you to use <link> more than once and have all the <link>ed sheets cascade together. This is a bad idea. The standards state that if there are multiple style sheets referenced with <link>, the user should be offered a choice between them. Future browsers might even implement this standard, at which point pages that use cascaded <link>ed sheets will be in for a bit of a shock. Embeded style sheets: Really, even using @import involves an embeded style sheet - the @import directive, after all, is CSS and not HTML, so you have to create an embeded style sheet in which to put your @import directive. You can also put any other style rule in between your <style type="text/css"> and </style> tags. Since they are part of the HTML page, they of course get sent along with the page every time it is requested. That means you lose the benefit of external style sheets, but it could be good for changes you want to make to the presentation for a particular page. You could set style rules here that affect multiple elements on the page, but not that affect elements on multiple pages. Inline styles: These are the ones where you set the 'style' attribute on an object. Like embeded style sheets, they lose a lot of the benefits of CSS, and are really only good for elements where you aren't going to re-use the style. These can't even be re-used by other elements on the same page. The one and only place I have inline styles on any of my pages are some image sites where I prefer to use inches as the unit for specifying height and width of images, rather than pixels. (CSS supports more different measurement units than HTML)
|
MacBarry
#:1216205
| 9:44 pm on Jan. 8, 2003 (utc 0) |
Warning: many current browsers allow you to use <link> more than once and have all the <link>ed sheets cascade together. This is a bad idea. The standards state that if there are multiple style sheets referenced with <link>, the user should be offered a choice between them. Future browsers might even implement this standard, at which point pages that use cascaded <link>ed sheets will be in for a bit of a shock. So if i want to use more than one sheet what is the best way to proceed? As i would like to redesign my site useing all css. Please explain "user should be offered a choice" and how do i do that useing several sheets? Are am i looking at this from the wrong direction? MacBarry
|
dingman
#:1216206
| 10:06 pm on Jan. 8, 2003 (utc 0) |
<link rel="stylesheet" href="1st.css"/> <style type="text/css"> @import url(2nd.css); @import url(3rd.css); /* and so forth */ </style> Just make sure that any style rules you want browsers that don't understand @import to use are in '1st.css'. If you don't care about browsers that old, you could leave out the <link> all together. Other browsers will use the styles that result from combining all available sheets according to [url=http://www.w3.org/TR/REC-CSS2/cascade.html#cascade] the rules in the standard[/url]. If you use more than one <link>ed stylesheet, then when the browser is loading the page it *should* do something like popping up a dialog asking which of the <link>ed style sheets to use - and then only use the one chosen. I don't know if any browsers actually do that, but they should, and therefore even if none do now they may in the future.
|
MacBarry
#:1216207
| 10:48 pm on Jan. 8, 2003 (utc 0) |
<link rel="stylesheet" href="1st.css"/> <style type="text/css"> @import url(2nd.css); @import url(3rd.css); /* and so forth */ </style> The visual code example makes things clearer. As to the older browsers are we talking (NN4.x) and older? If so it does not look as though the old browsers have much relevence any longer. Maybe I am looking at them incorrect. MacBarry
|
WibbleWobble
#:1216208
| 11:20 pm on Jan. 8, 2003 (utc 0) |
| I don't know if any browsers actually do that [supply a dropdown list of stylesheets] |
| Mozilla and its variants can, and do. Its not really that wonderful a thing, because most of the sites that supply more than one style have a php or js selection form. ([i]phoenix = daddy)
|
dingman
#:1216209
| 12:11 am on Jan. 9, 2003 (utc 0) |
| most of the sites that supply more than one style have a php or js selection form. |
| Since there are common browsers out there that will cascade them instead, you'd more or less have to. More of a Galeon fan myself, but of course Galeon requires a *nix environment.
|