Reasonable architecture CSS

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

In the current browser generally support the premise, CSS has been given to us an unprecedented mission. However, the more CSS you rely on, the bigger and more complex your stylesheet files become. At the same time, the test of document maintenance and organization will follow.

As long as a CSS file is enough-all rules (rule) together, adding and deleting changes are very convenient-but this day has long gone. When building a new website, it takes time to plan how to organize and structure CSS.

Organization of documents

The first step in building a CSS system is the elaboration of the outline. The importance of CSS organization planning is comparable to that of the site directory structure. (Htmlor Note: Use a little exaggeration to deepen your memory) there is no solution universal, so we will discuss some basic organizational scenarios and their respective pros and cons.

Master CSS File

You can usually use a master CSS file to place rules for all page shares. This file will contain default fonts, links, headers, and other styles. With the master CSS file, we began to explore advanced organizational strategies.

Method One: Based on prototype

The most basic strategy is to detach the CSS file based on the prototype page (archetype pages). If a site's home page, subpages and combination page design is different, you can adopt a prototype based strategy. (under this strategy) each page will have a proprietary CSS file.

In the case of a small number of prototypes, this method is straightforward and effective. However, the problem arises when page elements are not step-by-step in each prototype page. What should we do if the subpages and the combined pages share some elements and the home page doesn't?


Put the shared elements into the main CSS file. This is not the purest solution, but it applies to specific situations. But if the site is huge, the main CSS file expands rapidly-which violates the original intent of separating the files: Avoid importing unnecessary large files.
Put a style code in a CSS file that combines pages and subpages. That means maintaining redundant code, and obviously we don't want to.
Create a new file that is shared by both of these pages. That sounds good. But if there are only 10 lines of code, we create this file just to share the 10 lines of code? (Htmlor Note: Overkill?) This approach is pure, but if the site is huge and there are a lot of small elements to share with the page (Htmlor Note: For example, 30 pairs of pages to share 10 lines of code), it seems very cumbersome.
Create a separate CSS file that contains the styles for all shared elements. This method may be simpler, depending on the size of the site and the number of shared elements. A situation can be annoying: Import a large CSS file, but the page uses only a small number of styles-or that sentence, which violates the original intention of the separation of files.
  
This is what I call an overlapping dilemma (overlap dilemma). Fragmented CSS rules overlap and do not have a completely clear and unambiguous solution to organize them.

Method two: Based on page elements/blocks

This is good if the Web site uses server-side include. For example, if you use the header include, it has its own corresponding CSS file. The footer or other part of the include can do the same, just import your own CSS file. This method is simple and clean, but it may produce many small CSS files.

For example, if the footer style requires only 20 lines of CSS code, creating a single file will not work. And this method causes each page to contain a bunch of CSS files--because of how many include, how many CSS files.

Method Three: Based on the mark

This program is intuitive and practical, similar to the previous one. If your site has 30 pages, 10 of which contain a form, you can create a CSS file that deals with the style of form and only imports it on these 10 pages. If the other 10 pages contain a table, create a file that deals specifically with the table style ... Like。

Other organizational skills

In addition to organizing files in a subjective way, we also consider a variety of media types, such as printing, handheld devices, and screens. While this is clearly defined, it is still a factor to consider when creating a file structure. Once you have to support multiple media types, some of the rules in the master CSS file may have to be reconsidered.

In addition, branding may also be an important factor. (Htmlor Note: such as Google and Nike Joga) if it involves branding, you have to consider which elements should be adjusted to suit another brand. For example, use different CSS files, etc.
 
Another technique that is often overlooked is the use of nested @import statements. Contains a series of @import statements, or a few more CSS rules, you can create a CSS file. You can use this method to create a Web site's main CSS file (a @import to import parts of a style file). If each page of the site is imported from 4 to 5 different CSS files, you should consider using this technique.

Organization of rules and selectors

Talk about file organization, and then discuss how to organize the things in the document. Naturally, we want to navigate through the file and quickly find the selector (selector) or rule to edit.

Redundancy vs. subsidiary

As Dave Shea in his article "Redundancy vs." (redundancy vs. Dependency), you have to keep abreast of cascading (Cascade). You have to decide whether to group selectors (meaning to be attached) or to separate them (meaning redundancy). Grouping keeps the code simple and concise, but it creates dependencies, resulting in increased maintenance overhead. If you do not group, the file size is increased, making it difficult to keep a similar selector consistent. Only by doing this trade-off and trade-off can we make the right decision every time.

Group BY correlation/context

Since file organization can be subjective, it is clear that grouping is the best way to follow rules and selectors in relation to other parts. For example, if you use containers, headers, and footers to complete the layout, you should put them in a group.

It seems simple, but it's not. For example, when the navigation in the header is added to the CSS, is it grouped with the parent element or separately? In this case, it depends on the context of the rule. Typically, headers are related to page layouts and should be grouped with other layout elements. Instead of the header itself, navigation is a piece of the header that should be grouped with the other blocks of the header.

Working with annotations

Like most code, annotations are key to good organization. Should be based on the scope of the control of the CSS, clearly labeled each section. It is a good idea to ensure that annotations are visually prominent so that they are positioned quickly when content scrolls and Yimushihang.

Doug Bowman the CSS annotation in one of his articles CSS organizational techniques: tags (css from Tip #1: Flags). He explained in detail the equal sign before the section name in order to quickly jump to a section using the Text editor's lookup feature.

Don't forget

You should be careful and careful about specificity, cascade and inheritance, and make good use of them. Each of them can be your worst enemy or your most friendly friend. When building a huge web site, whether you understand these subtle subtleties, determine what you are building a rock-solid system, or can not withstand the wind and rain of the Tofu slag project. (Htmlor Note: This sentence completely transliteration, relatively cool)

Organization of attributes

Now that we know the organization of the file and the organization of the rules within the file, there is an important organizational link (not mentioned), which is attributes. Although the attributes are simpler than the first two concepts, there are some very good ways to keep the rules neat and well worth mentioning.

Alphabetical order

When it comes to attributes, if you need to follow any principles, it is: press-word-mother-row-order. In fact, this is not helpful for property browsing, but it prevents property values from overwriting this accidental occurrence.

For example, it's not clear how many times I've defined a margin value for a selector, and then one day I accidentally add another (or before or after). The latter property naturally works. Suppose you don't know that the second attribute exists, no matter how I adjust the first property value, refresh the browser, or see the page change. (Htmlor Note: I have a deep understanding of this problem) if you are in alphabetical order, you will find that the margin is defined two times (because they are together) and this problem can be avoided naturally.

Priorities

When the site is complex and involves too many CSS files, a lot of dependencies are created. Once you need to customize an element-specific style, the!important option seems to be the best choice. Yes,!important is a necessary solution, but it's best to figure out the root cause of the problem and then decide whether to use it, based on the Cascade relationship.

If you are familiar with the specificity, cascade, and inheritance mentioned above, you don't have to hold!important a tree. (Htmlor Note: The whole forest waiting for you ~) Of course it will come in handy, but before use to the specific situation in the chest. Do not take!important as a shortcut or a remedial solution because you don't know the crux of the problem.

Summary

When we become dependent on CSS and make stylesheets more complex, we need the right plan to avoid mistakes and make the code easier to maintain. Since the perfect solution does not exist, then the understanding of how CSS works and files, selectors and attributes of a variety of organizational solutions, will undoubtedly help us write high-quality code, stand the test of time.

Station: www.cfan365.cn I hope you will make some comments

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.