Are you learning CSS layout? Are you still unable to fully master pure CSS layout? There are usually two situations that hinder your learning:
The first possibility is that you haven't 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 tell you how to structure HTML.
Another reason is that you are at a loss for those very familiar presentation layer properties (such as cellpadding, hspace, align=left, etc.) and don't know what CSS statements to convert them into. Once you have solved the first problem and know how to structure your HTML, I will give you a list detailing what CSS to use to replace the original presentation attributes.
Structured HTML
When we first learn web page production, we always consider how to design it first, considering the pictures, fonts, colors, and layout plans. Then we use Photoshop or Fireworks to draw it and cut it into small pictures. Finally, edit the HTML to restore all the designs to the page.
If you want your HTML page to be laid out with CSS (CSS-friendly), you need to go back and start over. Don't think about "appearance" first, but first think about the semantics and structure of your page content.
Appearance is not the most important thing. A well-structured HTML page can be presented in any appearance, and CSS Zen Garden is a classic example. CSS Zen Garden helps us finally realize the power of CSS.
HTML isn't just for reading on a computer screen. Your carefully designed images in Photoshop may not be displayed on PDAs, mobile phones, and screen readers. But a well-structured HTML page can be displayed anywhere and on any network device through different definitions of CSS.
start thinking
Start by learning what structure is, which some writers also call semantics. What this term means is that you need to analyze your content blocks and the purpose each piece of content serves, and then build the corresponding HTML structure based on these content purposes.
If you sit down and carefully analyze and plan your page structure, you might end up with something like this:
Logo and site name
Main page content
Site Navigation (Main Menu)
submenu
search box
Functional areas (e.g. shopping cart, checkout)
Footer (copyright and related legal notices)
We usually use DIV elements to define these structures, similar to this:
<div id=header></div>
<div id=content></div>
<div id=globalnav></div>
<div id=subnav></div>
<div id=search></div>
<div id=shop></div>
<div id=footer></div>
It's not the layout, it's the structure. This is a semantic description of content blocks. When you understand your structure, you can add the corresponding ID to the DIV. Any content block can be contained within a DIV container, and another DIV can be nested within it. Content blocks can contain any HTML element --- titles, paragraphs, images, tables, lists, etc.
Based on the above, you already know how to structure HTML, and now you can define layout and styles. Each content block can be placed anywhere on the page, and the color, font, border, background, alignment properties, etc. of the block can be specified.
Using selectors is a wonderful thing
The name of the id is a means of controlling a certain content block. By wrapping this content block with a DIV and adding a unique id, you can use CSS selectors to precisely define the appearance of each page element, including titles, lists, Pictures, links or paragraphs, etc. For example, if you write a CSS rule for #header, it can be completely different from the image rule in #content.
Another example: you can use different rules to define link styles in different content blocks. Something like 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, define the styles of p in #content and #footer respectively through #content p and #footer p. Structurally speaking, your page is composed of pictures, links, lists, paragraphs, etc. These elements themselves do not affect which network device they are displayed on (PDA, mobile phone or Internet TV). They can be defined as Any performance appearance.
A carefully structured HTML page is very simple, and every element is used for structural purposes. When you want to indent a paragraph, you don't need to use the blockquote tag. Just use the p tag and add a CSS margin rule to p to achieve the indentation purpose. p is a structured tag and margin is a presentation attribute. The former belongs to HTML and the latter belongs to CSS. (This is the separation of structure and expression.)
Well-structured HTML pages have almost no attribute tags. The code is very clean and concise. For example, the original code <table width=80% cellpadding=3 border=2 align=left> can now only write <table> in HTML, and all things that control the performance are written in CSS. In structured HTML , table is a table, not anything else (such as being used for layout and positioning).
Practice structuring yourself
The above is just the most basic structure. In actual application, you can adjust the content blocks according to your needs. DIV nesting often occurs, and you will see that there are other layers in the container layer, with a structure similar to this:
<div id=navcontainer>
<div id=globalnav>
<ul>a list</ul>
</div>
<div id=subnav>
<ul>another list</ul>
</div>
</div>
Nested div elements allow you to define more CSS rules to control presentation. For example, you can give #navcontainer a rule to center the list to the right, give #globalnav a rule to center the list to the left, and give #subnav another rule to center the list entirely. Different performances.
Replace traditional methods with CSS
The following list will help you replace traditional methods with CSS:
HTML attributes and corresponding CSS methods
HTML attribute CSS method description
align=left
align=right float: left;
float: right; Use CSS to float any element: pictures, paragraphs, divs, titles, tables, lists, etc.
When you use the float attribute, you must define a width for the floated element.
marginwidth=0 leftmargin=0 marginheight=0 topmargin=0 margin: 0; Using CSS, margin can be set on any element, not just the body element. More importantly, you can specify the top, right, bottom and left of the element separately. margin value.
vlink=#333399 alink=#000000 link=#3333FF a:link #3ff;
a:visited: #339;
a:hover: #999;
a:active: #00f;
In HTML, the color of a link is defined as an attribute value of the body. The link style is the same throughout the page. Using CSS selectors, link styles can be different in different parts of the page.
bgcolor=#FFFFFF background-color: #fff; In CSS, the background color can be defined for any element, not just body and table elements.
bordercolor=#FFFFFF border-color: #fff; Any element can set a border (boeder). You can define top, right, bottom and left respectively.
border=3
cellspacing=3 border-width: 3px; Using CSS, you can define the table's borders as a unified style, or you can define the color, size and style of the top, right, bottom and left borders respectively.
You can use table, td or th selectors.
If you need to set a borderless effect, you can use CSS definition: border-collapse: collapse;
<br clear=left>
<br clear=right>
<br clear=all>
clear: left;
clear: right;
clear: both;
Many 2-column or 3-column layouts use the float attribute for positioning. If you define a background color or background image in the floating layer, you can use the clear attribute.
cellpadding=3
vspace=3
hspace=3 padding: 3px; Using CSS, the padding attribute can be set on any element. Similarly, padding can be set on top, right, bottom and left respectively. padding is transparent.
align=center text-align: center;
margin-right: auto; margin-left: auto;
Text-align only works with text.
Block-level elements like div and p can be horizontally centered using margin-right: auto; and margin-left: auto;
Some regrettable techniques and work environment
Due to the imperfect support of CSS by browsers, we sometimes have to adopt some tricks (hacks) or establish an environment (Workarounds) to allow CSS to achieve the same effect as traditional methods. For example, block-level elements sometimes need to use horizontal centering techniques, box model bug techniques, etc. All of these techniques are detailed in Molly Holzschlag’s article Integrated Web Design: Strategies for Long-Term CSS Hack Management.
Another resource on CSS techniques is "Position is Everything" by Big John and Holly Bergevin.
Understanding float behavior
Eric Meyer's "Containing Floats" will help you master how to use float attributes for layout. Float elements sometimes need to be cleared. Reading "How To Clear Floats Without Structural Markup" will be very helpful.