| Modular CSS Stylesheets |
|
|
|
| Monday, 27 February 2006 | |
|
Using modular stylesheets is the next step in creating modular and reuseable styles. The World Wide Web Consortium (W3C) decided to modularize the CSS3 spec, because modularization makes sense. You can use modularization concepts on your Web site to organize your CSS code and reduce bugs. Why Modularization?Modularization is the process of dividing your code into seperate components, that can be combined as needed. Rather than creating one large monolithic CSS stylesheet for your Web site, you can create smaller, more manageable CSS stylesheets that act as components. Personally, I lose patience rather quickly when working with a long stylesheet, because it is usually:
How to Think ModularizationUsing modularization, you can divide the code for your various designs into component stylesheets that are easier to edit and manage. For example, rather than having all your CSS styles in long CSS file like:
You could modularize your code into multiple stylesheets like this:
The way in which you modularize depends on each Web site's design needs and how much CSS code you need to manage. If all the code nicely fits in a single CSS file and is easy to manage, then there is no need to modularize. If you have many different table styles for displaying different types of content, then you might want just a tables.css file. It really depends on the needs of a particular Web site. The whole idea to isolate code into sections that so you are dealing with smaller, manageable sections of code. Why have CSS that applies only the Forums section in a stylesheet used in the Support section? When you are working on Forums CSS styles, you should not have to wade through CSS for the Support section. Why scroll up and down a 200 line stylesheet looking for that one particular class? Why copy and paste sections of code from part of stylesheet to another part of the same stylesheet in an attempt to keep it organized? Just modularize your code into separate stylesheets. Here are some common scenarios to give you ideas. Scenario 1: Modularize by Site DesignWhen you modularize according to site design, you are dividing up the CSS code into one or more CSS stylesheets that would apply to certain sections of the Web site.
Scenario 2: Modularize by PurposeWhen you modularize according to purpose, you are dividing up the CSS code into one or more CSS stylesheets that define certain user-interface elements.
Scenario 3: Modularize by LanguageWhen you modularize by language, you are creating different formatting styles for different languages, such as English and Japanese, which benefit from having their own set of margins, padding, fonts, line-heights, use of bold, etc.
Scenario 4: Modularize by Technical RequirementsWhen you modularize by technical requirements, you are dividing up your CSS code hacks by categories such as Internet Explorer vs. Firefox and/or other technical requirements, such as a stylesheet just for printing.
Scenario 5: Modularize by User RequirementsWhen you modularize by technical requirements, you are dividing up your CSS code hacks by categories such as Internet Explorer vs. Firefox.
Scenario 6: A Real-World ScenarioIn a real-world scenario, you would probably have a mix of the above schemes.
Implementing ModularizationThere are two methods to implement modular stylesheets: linking and importing. Linking StylesheetsEveryone who uses external stylesheets uses linking, but many people use only one stylesheet link. There is no reason you cannot use multiple links, like so: <link type="text/css" rel="stylesheet" href="css/styles.css" /> <link type="text/css" rel="stylesheet" href="css/layout.css" /> <link type="text/css" rel="stylesheet" href="css/frontpage.css" /> Importing StylesheetsImporting stylesheets is not something everyone uses. I prefer importing over just linking. To use importing, you can use it directly in an HTML <style> tag, but then you might as well use link because import would be doing the same thing: including that stylesheet in the HTML page. In order to really take advantage of importing, you should import other stylesheets into a master stylesheet that is linked to the HTML page. First link one stylesheet to the HTML page: <link type="text/css" rel="stylesheet" href="css/main.css" /> And then import the other stylesheets into the one linked to the HTML page. So in this example, import the other stylesheets into style.css: /* Import the other stylesheets into main.css */ @import url("styles.css"); @import url("layout.css"); The main advantage of importing stylesheets is that updating stylesheet links is easier, because you update them in a singe CSS file that is imported rather than in every HTML page. Concluding ExamplesNow I will show you a more complete example of linking and importing stylesheets rather than just brief snippets. HTML Page Links to Two Stylesheets<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link type="text/css" rel="stylesheet" href="css/main.css" /> <link type="text/css" rel="stylesheet" href="css/docs.css" /> <title>Example</title> </head> Then main.css imports Two More Stylesheets and Also Defines Some Styles/* Import the other stylesheets into main.css */ @import url("styles.css"); @import url("layout.css"); /* Now define some additional styles */ div.main-banner {border:none;width:600px;} div.newsscroller {background-color:black;} |
|
| Last Updated ( Monday, 27 February 2006 ) |
| < Prev | Next > |
|---|










