HTML structuring: Practice DIV + CSS webpage layout Getting Started Guide are you learning CSS layout? Isn't it possible to fully master the pure CSS layout? There are usually two ways to impede your learning:
The first possibility is that you have not understood the principle of CSS processing pages. Before you consider the overall performance of your page, you should first consider the semantics and structure of the content, and then add CSS for the semantics and structure. This article will show you how to structure HTML.
Another reason is that you are helpless with performance Layer attributes that you are very familiar with (such as cellpadding, hspace, align = "left", and so on, I don't know what CSS statements should be converted. When you solve the first problem and know how to structure your HTML, I will provide a list to list in detail what CSS is used to replace the original performance attribute.
Structured HTML
When we were just learning how to create a web page, we always thought about how to design the images, fonts, colors, and layout solutions. Then we use Photoshop or Fireworks to draw and cut into small images. Finally, you can edit HTML to restore all designs on the page.
If you want your HTML page to be displayed in CSS layout (CSS-friendly), you need to repeat it without considering the "appearance" first ", you must first consider the semantics and structure of your page content.
The appearance is not the most important. A well-structured HTML page can show any appearance. CSS Zen Garden is a typical example. CSS Zen Garden helps us realize the power of CSS.
HTML is not only read on computer screens. The images you carefully designed with photoshop may not be displayed on PDAs, mobile phones, and on-screen readers. However, a well-structured HTML page can be displayed anywhere on any network device through different CSS definitions.
Start thinking
First, we need to learn what is "structure". Some writers also call it "Semantics ". This term means that you need to analyze your content block and the purpose of each content service, and then create an HTML structure based on the content.
If you sit down and carefully analyze and plan your page structure, you may get a few pieces like this:
Logo and site name
Homepage content
Site Navigation (main menu)
Sub-menu
Search box
Functional area (such as shopping cart and cashier)
Footer (copyright and relevant legal disclaimer)
We usually use DIV elements to define these structures, like this:
<P id = "header"> </p>
<P id = "content"> </p>
<P id = "globalnav"> </p>
<P id = "subnav"> </p>
<P id = "search"> </p>
<P id = "shop"> </p>
<P id = "footer"> </p>
This is not a layout, but a structure. This is a semantic description of content blocks. After understanding your structure, you can add the corresponding ID to the DIV. The DIV container can contain any content block or nest another DIV. A content block can contain any HTML element, such as the title, paragraph, image, table, and list.
As described above, you already know how to structure HTML. Now you can define la S and styles. Each content block can be placed anywhere on the page, and the color, Font, border, background, and alignment attribute of the block can be specified.
Using selector is a wonderful thing.
The id is used to control a content block. By adding a unique id to the content block with a DIV, you can use the CSS selector to precisely define the appearance of each page element, including the question mark, list, picture, link or paragraph. For example, if you write a CSS rule for # header, it is totally different from the image rule in # content.
Another example is: You can use different rules to define the link styles in different content blocks. Similar to this: # globalnav a: link or # subnav a: link or # content a: link. You can also define different styles for the same element in different content blocks. For example, use # content p and # footer p to define the p style in # content and # footer respectively. In terms of structure, your page consists of pictures, links, lists, paragraphs, etc, these elements themselves do not affect the network devices (PDAs, mobile phones, or network TVs) displayed on which they can be defined as any representation.
A carefully structured HTML page is very simple, and every element is used for structural purposes. When you want to indent a paragraph, you do not need to use the blockquote tag. You only need to use the p tag and add a CSS margin rule to p to indent the paragraph. P is a structured tag, margin is a manifestation attribute, the former belongs to HTML, and the latter belongs to CSS. (This is the phase separation structure .)
The HTML page with a good structure has almost no labels showing attributes. The code is very clean and concise. For example, the original code <table width = "80%" cellpadding = "3" border = "2" align = "left"> can only be written in HTML <table>, everything that controls the performance is written into CSS. In structured HTML, table is a table, rather than anything else (such as being used for layout and positioning ).
Practice structuring in person
The above is just the most basic structure. In actual application, you can adjust the content block as needed. DIV nesting often occurs. You can see other layers in the container layer. The structure is similar to this:
<P id = "navcontainer">
<P id = "globalnav">
<Ul> a list </ul>
</P>
<P id = "subnav">
<Ul> another list </ul>
</P>
</P>
The nested p element allows you to define more CSS rules to control the performance. For example, you can give # navcontainer a rule to set the list to the right and # globalnav a rule to set the list to the left, the list for # subnav has a completely different performance.
Replacing traditional methods with CSS
The following list will help you replace the traditional method with CSS:
HTML attributes and corresponding CSS Methods
HTML attributes
CSS method description
Align = "left"
Align = "right" float: left;
Float: right; Use CSS to float any elements: images, paragraphs, p, titles, tables, lists, etc.
When you use the float attribute, you must define a width for the floating element.
Marginwidth = "0" leftmargin = "0" marginheight = "0" topmargin = "0" margin: 0; with CSS, margin can be set on any element, not just the body element. more importantly, you can specify the top, right, bottom, and left margin values for each element.
Vlink = "#333399" alink = "#000000" link = "# 3333FF" a: link # 3ff;
A: visited: #339;
A: hover: #999;
A: active: # 00f;
In HTML, the link color is defined as a property value of the body. The link style of the entire page is the same. With the CSS selector, the link styles of different parts of the page can be different.
Bgcolor = "# FFFFFF" background-color: # fff; in CSS, any element can define the background color, not just the body and table elements.
Bordercolor = "# FFFFFF" border-color: # fff; you can set a boeder for any element. You can define top, right, bottom, and left respectively.
Border = "3" cellspacing = "3" border-width: 3px; with CSS, you can define the border of a table as a uniform style, or you can define top, right, respectively, the color, size, and style of the bottom and left border.
You can use the table, td or th selectors.
If you need to set the border-less effect, you can use the CSS definition: border-collapse: collapse;
<Br clear = "left">
<Br clear = "right">
<Br clear = "all">
Clear: left;
Clear: right;
Clear: both;
Many 2-or 3-column la s are located using the float attribute. If you define the background color or background image in the floating layer, you can use the clear attribute.
Cellpadding = "3"
Vspace = "3"
Heat map = "3" padding: 3px; with CSS, you can set the padding attribute for any element. Similarly, you can set top, right, bottom and left for padding respectively. Padding is transparent.
Align = "center" text-align: center;
Margin-right: auto; margin-left: auto;
Text-align only applies to Text.
Blocks like p and p can be horizontally centered using margin-right: auto; and margin-left: auto;
Some unfortunate skills and work environment
Because browsers do not fully support CSS, we sometimes have to adopt some skills (hacks) or create an environment (Workarounds) to make CSS achieve the same effect as traditional methods. For example, block-level elements sometimes need the skills of horizontal center and Box Model bug. All these skills are described in detail in Molly Holzschlag's article Integrated Web Design: Strategies for Long-Term CSS Hack Management.
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.