Home >Web Front-end >CSS Tutorial >How to use CSS for web page layout
Preface
Hi everyone! This series of articles is written based on Ajie's own process of creating the w3cn.org site. Ajie has never produced a website that truly complies with web standards before. Now I am making reference to foreign materials and recording my thoughts and experiences in the process. I hope it will be helpful to everyone. Okay, let's get started.
The first day
Start making a site that meets standards. The first thing to do is to declare the DOCTYPE that meets your needs.
Look at the original code of the homepage of this site, you can see that the first line is:
Open some sites that meet standards, such as the famous web design software developer Macromedia and the personal design master Zeldman website, you will find the same code. The code for other standards-compliant sites (such as k10k.net) is as follows:
So what do these codes mean? Does it have to be placed?
What is DOCTYPE
We call the above codes a DOCTYPE statement. DOCTYPE is the abbreviation of document type and is used to indicate what version of XHTML or HTML you are using.
The DTD (such as xhtml1-transitional.dtd in the above example) is called the document type definition, which contains the rules of the document. The browser will interpret the identity of your page based on the DTD you define and display it. come out.
To build a standards-compliant web page, the DOCTYPE declaration is an essential and critical component; unless your XHTML determines a correct DOCTYPE, neither your logo nor your CSS will take effect.
XHTML 1.0 provides three DTD declarations to choose from:
Transitional (Transitional): A DTD with very loose requirements, which allows you to continue to use the HTML4.01 identifier (but must comply with xhtml writing method). The complete code is as follows:
Strict: For strict DTD, you cannot use any presentation layer identifiers and attributes, such as
. The complete code is as follows:
Frameset: A DTD specifically designed for frame pages. If your page contains a frame, you need to use this DTD. The complete code is as follows:
WHAT DOCTYPE DO WE CHOOSE
The ideal situation is of course a strict DTD, but for most of us designers who are new to web standards, Transitional DTD (XHTML 1.0 Transitional) is currently the ideal choice (including this site, which also uses transitional DTD). Because this DTD also allows us to use presentation layer identifiers, elements and attributes, it is also easier to pass W3C code verification.
Note: The "identification and attributes of the presentation layer" mentioned above refer to those tags used purely to control performance, such as tables for typesetting, background color identification, etc. In XHTML, tags are used to represent structures, not to achieve presentation. The purpose of our transition is to ultimately separate data and presentation.
For example: a mannequin changes clothes. The model is like data, and the clothes are the form of expression. The model and the clothes are separated, so you can change clothes at will. In the original HTML4, data and presentation were mixed together, and it was very difficult to change the presentation form at once. Haha, it's a bit abstract. This concept needs to be gradually understood during the application process.
Supplement
The DOCTYPE declaration must be placed at the top of every XHTML document, above all code and markup.
For more details, please visit the W3C website
After the DOCTYPE is declared, the next code is:
Usually our HTML4.0 code is just , what is "xmlns" here?
This "xmlns" is the abbreviation of XHTML namespace, which is called the "namespace" statement. What is the role of namespace? Ajie's own understanding is:
Since xml allows you to define your own logo, the logo you define may be the same as the logo defined by others, but have different meanings. Errors can easily occur when files are exchanged or shared. To avoid this error, XML uses namespace declarations, which allow you to identify your identity through a URL pointing to it. For example:
Both Xiao Wang and Xiao Li have defined a
A more popular explanation is: a namespace is to mark a document to tell others who this document belongs to. It's just that this "who" is replaced by a website address.
XHTML is a markup language that transitions from HTML to XML. It needs to comply with XML document rules, so it also needs to define a namespace. And because XHTML1.0 cannot customize the logo, its namespace is the same, which is "http://www.w3.org/1999/xhtml". It doesn't matter if you don't quite understand it yet, at this stage we just need to copy the code. The lang="gb2312" after
specifies that your document should be in Simplified Chinese.
The third step is to define your language encoding, similar to this:
In order to be correctly interpreted by browsers and pass W3C code verification, all XHTML documents must declare the encoding language they use. We generally use gb2312 (Simplified Chinese). When making multi-language pages, we may also use Unicode, ISO-8859-1, etc., which can be defined according to your needs.
Usually this definition is enough. However, it should be added that XML documents do not define language encoding in this way. XML is defined as follows:
You can see similar statements in the first line of code on the homepage of Macromedia.com. This is also the definition method recommended by W3C. So why don't we just adopt this approach? The reason is that some browsers have incomplete support for standards and cannot correctly understand such definition methods, such as IE6/windows. Therefore, under the current transition plan, we still recommend using the meta method. Of course, you can write both ways.
Looking at the source code of this website, you will find that there is one more sentence where the language encoding is defined:
This is written for older browsers to ensure that various browsers can interpret the page correctly.
Note: At the end of the above declaration statement, you see a slash "/", which is different from our previous HTML4.0 code writing. The reason is that XHTML syntax rules require that all tags must have a beginning and an end. For example,
and
, etc., for unpaired identifiers, it is required to add a space at the end of the identifier, followed by a "/". For example, Use web standards to design websites. The transition method is mainly to use XHTML+CSS. CSS style sheets are essential. This requires all web designers to be proficient in CSS. If you have not used it before, start learning now. To create a website that complies with web standards, you cannot design beautiful pages without knowing CSS.
In fact, all aspects of performance need to be implemented with CSS. We used to use table for positioning and layout, but now we have to use p for positioning and layout. This is a change in the way of thinking, which is a bit uncomfortable at first. Haha, there will be resistance to any change. In order to enjoy the "benefits" brought by standards, it is worthwhile to give up some old traditional practices.
Externally calling style sheets
In the past, we usually used two methods to use style sheets:
Inline method on the page: that is, inserting the style sheet into the page Write it directly in the head area of the page code. Similar to this:
External calling method: Write the style sheet in a separate .css file, and then call it with code similar to the following in the head area of the page.
In a design that complies with web standards , we use the external calling method, and the benefits are self-evident. You can change the style of the page without modifying the page, only modifying the .css file. If all pages call the same style sheet file, then changing one style sheet file can change the styles of all files.
Double-table method to call the style sheet
Look at the original code of some standards-compliant sites. You may see that there are the following two sentences where the style sheet is called:
Why do you have to write it twice?
In fact, under normal circumstances, it is enough to use the external link method (that is, the first sentence). The double table call I use here is just an example. The "@import" command is used to enter the style sheet. The "@import" command is invalid in Netscape 4.0 browsers. In other words, when you want certain effects to be hidden in the Netscape 4.0 browser and displayed in 4.0 or above or other browsers, you can use the "@import" command method to call the style sheet.
These tips mainly focus on meta tag settings. In fact, they have little to do with complying with web standards. Just pay attention to adding them at the end. Just "/" to close the tag, but since this is an introductory tutorial, let's write it in more detail.
Favorites icon
If you add this site to your favorites, you can see that the IE icon before the favorites URL becomes a special icon for this site. To achieve this effect is very simple, first make a 16x16 icon, name it favicon.ico, and place it in the root directory. Then embed the following code into the head area:
< ;link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
Content prepared for search engines
The code is as follows, just replace it with the content of your own site:
Allow search robots to search all links in the site. If you want certain pages not to be searched, it is recommended to use the robots.txt method
Set site author information
< meta name="author" content="ajie@netease.com,阿杰" />
Set site copyright information
Brief introduction of the site (recommended)
Keywords of the site (recommended)
Let’s introduce this much first. Supplementary explanation, the previous 5 sections were all about the code of the head area, and the actual page content has not been mentioned at all. Haha, don’t worry, in fact, the head area is very important. You can know the designer by looking at the head code of a page. Is it professional enough?
Before starting formal content production, we must first understand the code specifications of web standards. Understanding these specifications can help you avoid detours and pass code verification as soon as possible.
1. All tags must have a corresponding closing tag
In the past, in HTML, you could open many tags, such as
and < ;li> instead of necessarily writing the corresponding
and to close them. But this is not legal in XHTML. XHTML requires a strict structure and all tags must be closed. If it is a separate unpaired tag, add a "/" at the end of the tag to close it. For example:/b> must be modified to:
/p> The above is the detailed content of How to use CSS for web page layout. For more information, please follow other related articles on the PHP Chinese website!
That is to say, the nesting layer by layer must be strictly symmetrical.
4. All attributes must be enclosed in quotation marks ""
In HTML, you do not need to add quotation marks to attribute values, but in XHTML, they Must be quoted. For example:
In special cases, you need to use double quotes in the attribute value, you can use " , single quotes can use ', for example:
5. Use encoding to represent all < and & special symbols
Any less than sign (<), which is not part of the label, must be encoded as & l t;
Any greater than sign (>), which is not part of the label, must be encoded as >
Any ampersand (&) that is not part of an entity must be encoded as & a m p;
Note: There are no spaces between the above characters
##. #6. Assign a value to all attributes
XHTML stipulates that all attributes must have a value, and if there is no value, it will repeat itself. For example: < input type="checkbox" name="shirt" value="medium" checked> must be modified to:
7. Do not use "--" in the comment content
"--" can only occur at the beginning and end of XHTML comments end, that is, they are no longer valid in the content. For example, the following code is invalid:
Replace the internal dotted lines with equal signs or spaces