w3pop.com :: ÍøÂçѧԺ :: CSS :: CSS Óï·¨
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 |
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 |
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}
|
You have to use the class attribute in your HTML document:
Äã±ØÐëÔÚÄãµÄHTMLÎĵµÖÐʹÓÃÀàÊôÐÔ£¨²ÅÄÜÏÔʾ³öЧ¹û£©£º
<p class="right"> <p class="center"> |
Note: Only one class attribute can be specified per HTML element! The example below is wrong:
×¢Ò⣺ÿ¸öHTMLÔªËØÖ»ÄÜÓÐÒ»¸öÀàÊôÐÔ£¬ÏÂÃæµÄÀý×ÓÊÇ´íÎóµÄ£º
<p class="right" class="center"> |
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"> <p class="center"> |
|
Do NOT start a class name with a number! It will not work in Mozilla/Firefox. |
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 |
| Do NOT start an ID name with a number! It will not work in Mozilla/Firefox. ºÍCLASSÒ»ÑùIDµÄÃû³ÆµÄ¿ªÍ·Ò²²»ÒªÊ¹ÓÃÊý×Ö,²»È»¾ÍÎÞ·¨ÔÚMozilla/FirefoxÖÐÕý³£ÔË×÷ÁË |
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×¢Ê͵ĿªÍ·Îª"/*"£¬½áÊø·ûºÅΪ"*/"£¬¾ÍÏñÕâÑù£º
/* Õâ¾ÍÊÇÒ»¾ä×¢ÊÍ */ |