ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: CSS :: CSS ½éÉÜ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

CSS
CSS2 Aural
CSS µ¥Î»
CSS ÑÕÉ«
CSS ÑÕɫֵ
CSS ÑÕÉ«Ãû³Æ
CSS ±àÂëÓë×éÖ¯¼¼..
CSS Ä£¿é»¯Éè¼ÆË¼..
CSSÍêÃÀ¼æÈÝIE6/I..
CSS @ Ten: ÏÂÒ»¸..

CSS ½éÉÜ


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:12729 :: ::

What You Should Already Know
ÄÄЩÊÇÄãÓ¦¸ÃÒѾ­ÖªµÀµÄ

Before you continue you should have some basic understanding of the following:
ÔÚ¼ÌÐøÇ°ÄãÓ¦¸Ã¾ß±¸ÏÂÃæÏà¹ØµÄ»ù´¡ÖªÊ¶£º

  • HTML / XHTML

If you want to study this subject first, find the tutorials on our Home page.
Èç¹ûÄãÏëÏÈѧϰÕâ¸öÖ÷Ìâ,ÇëÔÚÎÒÃǵÄÊ×Ò³²éÕÒÏà¹Ø½Ì³Ì


What is CSS?
ʲôÊÇCSS£¿

  • CSS stands for Cascading Style Sheets
    CSS Òâ˼¾ÍÊÇ µþ²ãÑùʽ±í
  • Styles define how to display HTML elements
    Ñùʽ¶¨ÒåÁËHTMLÔªËØÔõÑùÈ¥ÏÔʾ
  • Styles are normally stored in Style Sheets
    Ñùʽһ°ã´æ´¢ÔÚÑùʽ±íÖÐ
  • Styles were added to HTML 4.0 to solve a problem
    ÑùʽÌí¼Óµ½HTML4.0ÖÐÓÃÀ´½â¾öÎÊÌâ
  • External Style Sheets can save you a lot of work
    ÀûÓÃÍⲿÑùʽ±í¿ÉÒÔÌá¸ßÄãµÄ¹¤×÷ЧÂÊ
  • External Style Sheets are stored in CSS files
    ÍⲿÑùʽ±í´æ´¢ÔÚCSSÎļþÖÐ
  • Multiple style definitions will cascade into one

CSS Demo
CSSÑÝʾ

With CSS, your HTML documents can be displayed using different output styles:
ʹÓÃCSS£¬ÄãµÄHTMLÎĵµ¿ÉÒÔÓò»Í¬µÄÑùʽÊä³öÀ´ÏÔʾ£º

See how it works
¿´¿´Ëü¹¤×÷µÄÇé¿ö


Styles Solve a Common Problem
ÓÃÑùʽÀ´½â¾öÒ»¸ö¹²Í¬µÄÎÊÌâ

HTML tags were originally designed to define the content of a document. They were supposed to say "This is a header", "This is a paragraph", "This is a table", by using tags like <h1>, <p>, <table>, and so on. The layout of the document was supposed to be taken care of by the browser, without using any formatting tags.
HTML±êÇ©Æð³õ±»Éè¼Æ³ÉΪ¶¨ÒåÎĵµµÄÄÚÈÝ¡£Í¨¹ýʹÓÃÏñ<h1>,<p>,<table>ÕâÑùµÄ±êÇ©ËûÃÇÓ¦¸Ã±í´ïµÄÊÇ"ÕâÊÇÒ»¸ö±êÌâ","ÕâÊÇÒ»¸ö¶ÎÂä","ÕâÊÇÒ»Õűí¸ñ",¶ø²¼¾Ö¸ÃÓÉä¯ÀÀÆ÷À´´¦Àí²¢·ÇʹÓøñʽ»¯±êÇ©.

As the two major browsers - Netscape and Internet Explorer - continued to add new HTML tags and attributes (like the <font> tag and the color attribute) to the original HTML specification, it became more and more difficult to create Web sites where the content of HTML documents was clearly separated from the document's presentation layout.
×÷ΪÁ½´óä¯ÀÀÆ÷ - Íø¾° ºÍ IE - ²»¶ÏµØ½«ÐµÄHTML±êÇ©ºÍÊôÐÔ(Ïñ<font>±êÇ©ºÍÑÕÉ«ÊôÐÔ)Ìí¼Óµ½Ô­À´µÄHTML¹æ¸ñÖÐ,ÕâÈý¨Á¢ÇåÎúµÄHTMLÎĵµÄÚÈݲ¢´ÓÐÎʽÖзÖÀë³öÀ´±äµÄÔ½À´Ô½À§ÄÑ.

To solve this problem, the World Wide Web Consortium (W3C) - the non profit, standard setting consortium, responsible for standardizing HTML - created STYLES in addition to HTML 4.0.  
ΪÁ˽â¾öÕâ¸öÄÑÌâ,W3C Õâ¸ö·ÇÓ®ÀûµÄ,½¨Á¢±ê×¼µÄ×éÖ¯,ΪHTML4.0Ôö¼ÓÁËÑùʽ

All major browsers support Cascading Style Sheets.
ËùÓÐÖ÷Á÷ä¯ÀÀÆ÷¶¼Ö§³ÖÑùʽ±í


Style Sheets Can Save a Lot of Work
Ñùʽ±í¿ÉÒÔÌá¸ß¹¤×÷ЧÂÊ

Styles sheets define HOW HTML elements are to be displayed, just like the font tag and the color attribute in HTML 3.2. Styles are normally saved in external .css files. External style sheets enable you to change the appearance and layout of all the pages in your Web, just by editing one single CSS document!
Ñùʽ±í¶¨ÒåÔªËØÔõÑùÈ¥ÏÔʾ,¾ÍÏñÔÚHTML3.2ÖÐ×ÖÌå±êÇ©ºÍÑÕÉ«ÊôÐÔÒ»°ã±£´æÔÚÒ»¸öÍⲿµÄ.cssÎļþÒ»Ñù.ÍⲿÑùʽ±íÄܹ»ÈÃÄã¸Ä±äËùÓгöÏÖÔÚÄãWEBÖеÄÍâ¹ÛºÍ²¼¾Ö,¶ø½ö½öͨ¹ý±à¼­Ò»¸öµ¥¶ÀµÄCSSÎĵµ.(Ô­Àí¾ÍÊÇÒ»¶¯¶à±ä)

CSS is a breakthrough in Web design because it allows developers to control the style and layout of multiple Web pages all at once. As a Web developer you can define a style for each HTML element and apply it to as many Web pages as you want. To make a global change, simply change the style, and all elements in the Web are updated automatically.
CSSÊÇÒ»¸öÔÚÉè¼ÆÁìÓòÖеÄÍ»ÆÆ,ÒòΪËüÔÊÐí¿ª·¢ÕßһϾÍÄÜ¿ØÖƶà¸öWEBÒ³µÄÑùʽºÍ²¼¾Ö.×÷ΪÒëÃûWEB¿ª·¢ÕßÄã¿ÉÒÔΪÿ¸öHTMLÔªËØºÍÓ¦ÓÃËûµÄÿ¸öÒ³Ãæ¶¨ÒåÒ»¸öÄãÏëÒªµÄÑùʽ.À´ÊµÏÖÈ«ÃæµÄ¸Ä±ä,¼òµ¥µÄ¸Ä±äÑùʽ,ËùÓÐÓëÖ®Ïà¹ØµÄÔªËØ¶¼»á×Ô¶¯¸üÐÂ


Multiple Styles Will Cascade Into One

Style sheets allow style information to be specified in many ways. Styles can be specified inside a single HTML element, inside the <head> element of an HTML page, or in an external CSS file. Even multiple external style sheets can be referenced inside a single HTML document. 
Ñùʽ±íÔÊÐíÑùʽÐÅÏ¢ÓöàÖÖ·½Ê½À´¶¨Òå.Ñùʽ¿ÉÒÔÔÚÒ»µ¥¶ÀµÄHTMLÔªËØÖÐÖ¸¶¨,ÔÚ<head>ÔªËØÖлòÔÚÒ»ÍⲿCSSÎļþÖÐ.ÉõÖÁ¶à¸öÍⲿÑù±íÄܼ¯ÖÐÔÚÒ»¸öµ¥Ò»µÄHTMLÎĵµÖÐ

Cascading Order

What style will be used when there is more than one style specified for an HTML element?
µ±¶àÓÚÒ»ÖÖµÄÑùʽָ¶¨Ò»HTMLÔªËØÊ±Ê¹ÓÃÔõÑùµÄÑùÊ½ÄØ?

Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority:
Ò»°ã˵À´ËùÓÐÑùʽÓÐÏÂÃæµÄ¹æÔò(µÚËĸö×îÓÐÓÅÏÈÐÔ)

  1. Browser default
    ä¯ÀÀÆ÷ĬÈÏ
  2. External style sheet
    ÍⲿÑùʽ±í
  3. Internal style sheet (inside the <head> tag)
    ÄÚǶÑùʽ±í(ÔÚ<head>±êÇ©ÄÚ)
  4. Inline style (inside an HTML element)
    ÐÐÄÚÑùʽ(ÔÚÒ»HTMLÔªËØÄÚ)

So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style declared inside the <head> tag, in an external style sheet, or in a browser (a default value).
ËùÒÔдÔÚHTMLÔªËØÖеÄÑùʽÓÐ×î¸ßµÄÓÅÏÈȨ(дÔÚHTMLÔªËØÄÚµÄ),Ëü»áÌæ´úÆäËûÐÎʽµÄÑùʽ.

ÆÀÂÛ (15) 1 2 3 All