ÍøÂçѧԺ 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 ä¯ÀÀ:10572 :: ::

Syntax
Óï·¨

The CSS syntax is made up of three parts: a selector, a property and a value:
CSSµÄÓï·¨ÓÉÈý²¿·Ö×é³É: Ò»¸öÑ¡ÔñÆ÷£¬Ò»¸öÊôÐÔºÍÒ»¸öÖµ£º

selector {property: value}

The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon, and surrounded by curly braces:
Ñ¡ÔñÆ÷ÊÇÄãÏ£ÍûÈ¥¶¨ÒåµÄHTMLÔªËØ/±êÇ©£¬¸Ä±äÊôÐÔ£¬Ã¿¸öÊôÐÔ¿ÉÒÔÓÐÒ»¸öÖµ£¬ÊôÐÔºÍÖµÓÉðºÅÇø·Ö¿ªÍâÃæÓôóÀ¨ºÅÀ¨ÆðÀ´£º

body {color: black}

Note: If  the value is multiple words, put quotes around the value:
×¢Ò⣺Èç¹ûֵΪ¶à¸öµ¥´ÊÔòÓÃË«ÒýºÅÀ¨ÆðÀ´£º

p {font-family: "sans serif"}

Note: If you wish to specify more than one property, you must separate each property with a semicolon. The example below shows how to define a center aligned paragraph, with a red text color:
×¢Ò⣺Èç¹ûÄãÏëÖ¸¶¨¶à¸öÊôÐÔ£¬Äã¾Í±ØÐ뽫ÿ¸öÊôÐÔÓ÷ֺŸô¿ª£¬ÏÂÃæµÄÀý×Ó¾ÍÑÝʾÁËÔõÑù¶¨Òå¾ÓÖкìÉ«ÎÄ×Ö¶ÎÂ䣺

p {text-align:center;color:red}

To make the style definitions more readable, you can describe one property on each line, like this:
ΪÁËÈÃÑùʽ¶¨Òå¸üÓпɶÁÐÔ£¬Äã¿ÉÒÔÏñÕâÑù·ÖÐÐÃèÊöÊôÐÔ£º

p
{
text-align: center;
color: black;
font-family: arial
}


Grouping
×éºÏ

You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be displayed in green text color:
Äã¿ÉÒÔ½«Ñ¡ÔñÆ÷×éºÏ¡£ÓöººÅ·Ö¸ôÿ¸öÑ¡ÔñÆ÷¡£ÏµÄÀý×Ó½«ËùÓеıêÌâÔªËØ×éºÏÆðÀ´£¬ËüÃǵÄÑÕÉ«¶¼±äΪÂÌÉ«£º

h1,h2,h3,h4,h5,h6 
{
color: green
}


The class Selector
Ñ¡ÔñÆ÷Àà

With the class selector you can define different styles for the same type of HTML element.
ÓÃÑ¡ÔñÆ÷ÀàÄã¿ÉÒÔ½«Í¬Ò»ÀàÐ͵ÄHTMLÔªËØ¶¨Òå³ö²»Í¬µÄÑùʽ¡£

Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:
±ÈÈçÄãÏëÔÚÄãµÄÎĵµÖÐÓÐÁ½ÖÖ²»Í¬ÑùʽµÄ¶ÎÂ䣺һÖÖÊÇÓÒ¶ÔÆë£¬ÁíÍâÊǾÓÖеġ£Õâ¾Í¸æËßÄã¸ÃÔõôÓÃÑùʽÀ´×öµ½Õâµã£º

p.right {text-align: right}
p.center {text-align: center}

You have to use the class attribute in your HTML document:
Äã±ØÐëÔÚÄãµÄHTMLÎĵµÖÐʹÓÃÀàÊôÐÔ£¨²ÅÄÜÏÔʾ³öЧ¹û£©£º

<p class="right">
This paragraph will be right-aligned.
</p>
<p class="center">
This paragraph will be center-aligned.
</p>

Note: Only one class attribute can be specified per HTML element! The example below is wrong:
×¢Ò⣺ÿ¸öHTMLÔªËØÖ»ÄÜÓÐÒ»¸öÀàÊôÐÔ£¬ÏÂÃæµÄÀý×ÓÊÇ´íÎóµÄ£º

<p class="right" class="center">
This is a paragraph.
</p>

You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:
ÄãÒ²¿ÉÒÔÊ¡ÂÔ±êÇ©Ãû³ÆÖ±½ÓÈ¥¶¨Ò壬ÕâÑù¾Í¿ÉÒÔÔÚËùÓеÄHTMLÔªËØÖÐʹÓÃÁË¡£ÏÂÃæµÄÀý×Ó¾ÍÄÜÈÃËùÓÐHTMLÖÐËùÓдøclass="center"µÄÔªËØ¾ÓÖÐÎÄ×Ö£º

.center {text-align: center}

In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:  
ÏÂÃæµÄ´úÂëÖÐH1ºÍPÔªËØ¶¼ÓÐclass="center"¡£Õâ¾ÍÒâζ×ÅÕâÁ½¸öÔªËØ¶¼½«×ñÑ­Ñ¡ÔñÆ÷"center"µÄ¹æÔò£º

<h1 class="center">

This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p>

 Do NOT start a class name with a number! It will not work in Mozilla/Firefox.
Çë²»ÒªÓÃÒÔÊý×Ö¿ªÍ·ÎªÃû³ÆµÄÀ࣬ÔÚMozilla/FirefoxÖв»ÄÜÕý³£ÔË×÷¡£



The id Selector
id Ñ¡ÔñÆ÷

You can also define styles for HTML elements with the id selector. The id selector is defined as a #.
Äã¿ÉÒÔʹÓÃidÑ¡ÔñÆ÷À´¶¨ÒåHTMLÔªËØµÄÑùʽ¡£idÑ¡ÔñÆ÷¿ÉÓÃ#À´¶¨Òå¡£

The style rule below will match the element that has an id attribute with a value of "green":
ÏÂÃæµÄÑùʽ¹æÔò¶ÔÈκÎÒ»¸ö´øÓÐidÊôÐÔֵΪ"green"µÄÔªËØ¶¼ÊÇÆ¥ÅäµÄ

#green {color: green}

The style rule below will match the p element that has an id with a value of "para1":
ÏÂÃæµÄÑùʽ¹æÔò½«Æ¥Åä´øÓÐidÊôÐÔֵΪ"para1"µÄpÔªËØ

p#para1
{
text-align: center;
color: red
}

 Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.
ºÍCLASSÒ»ÑùIDµÄÃû³ÆµÄ¿ªÍ·Ò²²»ÒªÊ¹ÓÃÊý×Ö,²»È»¾ÍÎÞ·¨ÔÚMozilla/FirefoxÖÐÕý³£ÔË×÷ÁË


CSS Comments
CSS ×¢ÊÍ

Comments are used to explain your code, and may help you when you edit the source code at a later date. A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this:
×¢ÊÍ¿ÉÓÃÀ´½âÊÍÄãµÄ´úÂ룬ÆðÂëÔÚÒÔºóÄãÐèÒªÖØÐ±༭Õâ¿é´úÂëµÄʱºòÕâЩעÊÍ»á¸øÄãЩ°ïÖú¡£ä¯ÀÀÆ÷»áºöÂÔÕâЩעÊÍ¡£CSS×¢Ê͵ĿªÍ·Îª"/*"£¬½áÊø·ûºÅΪ"*/"£¬¾ÍÏñÕâÑù£º

/* Õâ¾ÍÊÇÒ»¾ä×¢ÊÍ */
p
{
text-align: center;
/* ÕâÊÇÁíÒ»Ìõ×¢ÊÍ */
color: black;
font-family: arial
}

ÆÀÂÛ (8) 1 2 All