ÍøÂçѧԺ 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 ÐÞ¸Ä:2007-06-22 ä¯ÀÀ:10661 :: ::

Examples
Á½¸öÀý×Ó


How to Insert a Style Sheet
ÔõÑù²åÈëÑùʽ±í

When a browser reads a style sheet, it will format the document according to it. There are three ways of inserting a style sheet:
µ±ä¯ÀÀÆ÷ÔĶÁÑùʽ±í£¬Ëü»áÒÀ¾ÝËü£¨Ñùʽ±í£©À´¸ñʽ»¯Îĵµ¡£ÓÐÈýÖÖ·½·¨¿ÉÒÔ²åÈëÑùʽ±í£º

External Style Sheet
ÍⲿÑùʽ±í

An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section:
ʹÓÃÍⲿÑùʽ±íÊÇʹÑùʽӦÓÃÓÚ¶àÕÅÍøÒ³µÄÀíÏë·½·¨¡£Í¨¹ýÕâ¸ö·½·¨ÄãÖ»Ðè¸Ä¶¯Ò»¸öÎļþ¾ÍÄܸıäÕû¸öÍøÕ¾µÄÍâ¹Û¡£Ê¹ÓÃ<link>±êÇ©ÈÃÿ¸öÒ³Ãæ¶¼Á¬½Óµ½Ñùʽ±í¡£<link>±êÇ©ÔÚheadÇøÓòʹÓãº

<head>
<link rel="stylesheet" type="text/css"

href="mystyle.css" />
</head>

The browser will read the style definitions from the file mystyle.css, and format the document according to it.
ä¯ÀÀÆ÷½«´Ómystyle.cssÎļþÖжÁÈ¡Ñùʽ¶¨ÒåÐÅÏ¢£¬²¢ÒÀ¾ÝËüÀ´¸ñʽ»¯Îĵµ

An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:
ÍⲿÑùʽ±í¿ÉÒÔÓÃÈκÎÒ»¸öÎÄ×ֱ༭Æ÷À´Êéд¡£Îļþ²»Ó¦¸Ã°üº¬ÈκεÄhtml±êÇ©¡£²¢±£´æÎªÒ»¸öºó׺Ϊ.cssµÄÎļþ.ÏÂÃæÊÇÒ»¸öÑùʽ±íÎļþµÄÄÚÈÝ:

hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}

 

 Do NOT leave spaces between the property value and the units! If you use "margin-left: 20 px" instead of "margin-left: 20px" it will only work properly in IE6 but it will not work in Mozilla/Firefox or Netscape.
Çë²»ÒªÔÚÊôÐÔÖµºÍÆäµ¥Î»¼ä¼ÓÉϿոñ£¡Èç¹ûÄãÓÃ"margin-left: 20 px“À´´úÌæ"margin-left: 20px"µÄ»°£¬ÕâÒ²ÐíÔÚIE6ÖÐÄÜÕý³£¹¤×÷,µ«ÔÚMozilla/Firefox»òNetscapeÖоÍÎÞ·¨Õý³£ÏÔʾÁË¡£

Internal Style Sheet
ÄÚǶÑùʽ±í

An internal style sheet should be used when a single document has a unique style. You define internal styles in the head section by using the <style> tag, like this:
Ò»·ÝÄÚǶÑùʽ±íÓ¦¸ÃÔÚµ±Óе¥¶ÀÎĵµÓÐÌØÊâÑùʽµÄʱºòʹÓá£Ê¹ÓÃ<style>±êÇ©ÔÚheadÇøÓòÄÚ¶¨ÒåÑùʽ£¬ÏñÕâÑù£º

<head>
<style type="text/css">

hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
</style>
</head>

The browser will now read the style definitions, and format the document according to it.
ä¯ÀÀÆ÷½«Á¢¼´¶ÁÈ¡Ñùʽ¶¨Ò壬²¢ÒÀ¾ÝËüÀ´¸ñʽ»¯Îĵµ¡£

Note: A browser normally ignores unknown tags. This means that an old browser that does not support styles, will ignore the <style> tag, but the content of the <style> tag will be displayed on the page. It is possible to prevent an old browser from displaying the content by hiding it in the HTML comment element: 
×¢Ò⣺ä¯ÀÀÆ÷Ò»°ã»áºöÂÔδ֪µÄ±êÇ©¡£Õâ¾ÍÒâζ×ÅÀϵÄä¯ÀÀÆ÷²»ÄÜÖ§³ÖÑùʽ£¬»áºöÂÔ<style>±êÇ©£¬µ«<style>ÀïµÄÄÚÈÝ»áÏÔʾÔÚÒ³ÃæÉÏ¡£ÔÚHTML×¢ÊÍÔªËØÖÐÒþ²ØËü¿ÉÒÔÀ´±ÜÃâÕâÀàÇé¿öÔÚÀϵÄä¯ÀÀÆ÷Öз¢Éú£º

<head>
<style type="text/css">
<!--
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
-->
</style>
</head>

Inline Styles
ÐÐÄÚÑùʽ

An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this method sparingly, such as when a style is to be applied to a single occurrence of an element.
ʹÓÃÐÐÄÚÑùʽ¾ÍʧȥÁËÑùʽ±íµÄÓÅÊÆ¶ø½«ÄÚÈݺÍÐÎʽÏà»ìÏýÁË¡£Ò»°ãÕâÀà·½·¨ÔÚ¸ö±ðÔªËØÐèÒª¸Ä±äÑùʽµÄʱºòʹÓá£

To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:
ÔÚÏà¹ØµÄ±êÇ©ÉÏÓÃstyleÊôÐÔÀ´¼ÓÈëÐÐÄÚÑùʽ¡£ÑùʽÊôÐÔ¿ÉÒÔ°üº¬ÈκÎCSSÊôÐÔ¡£Á´×ÓÖн«Õ¹Ê¾ÔõÑù¸øÒ»¸ö¶ÎÂä¼ÓÉÏ×óÍâ²¹¶¡²¢½«ÑÕÉ«¸ÄΪsienna£º

<p style="color: sienna; margin-left: 20px">
This is a paragraph
</p>

 


Multiple Style Sheets
¶àÖØÑùʽ±í

If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. 
Èç¹ûһЩÊôÐÔ±»ÏàͬµÄÑ¡ÔñÆ÷ÉèÖóɲ»Í¬µÄÑùʽ£¬Öµ¾Í»áÏò¸üΪ¾ßÌåµÄÑùʽËù¼Ì³Ð£¨¾ßÌ廯£©¡£

For example, an external style sheet has these properties for the h3 selector:
¾Ù¸öÀý×Ó£¬Ò»¸öÍⲿÑùʽ±íÓÐÕâÑùµÄh3Ñ¡ÔñÆ÷ÊôÐÔ£º

h3 
{
color: red;
text-align: left;
font-size: 8pt
}

And an internal style sheet has these properties for the h3 selector:
ͬʱÓÐÒ»¸öÄÚǶÑùʽ±íÓÐÕâÑùµÄh3Ñ¡ÔñÆ÷ÊôÐÔ£º

h3 
{
text-align: right;
font-size: 20pt
}

If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:
Èç¹ûÒ³ÃæÔÚÓÐÄÚǶÑùʽ±íµÄͬʱÓÖÁ¬½Óµ½ÍⲿÑùʽ±íµÄ»°h3µÄÊôÐÔ½«±äΪ:

color: red; 
text-align: right;
font-size: 20pt

The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet.
ÑÕÉ«ÊǼ̳ÐÁËÍⲿÑùʽ±í¶øÎÄ×Ö¶ÔÆëºÍÎÄ×Ö´óС±»ÄÚǶµÄÑùʽ±íËùÌæ»»¡£

ÆÀÂÛ (9) 1 2 All