关闭顶部展开顶部

css教程:如何正确的使用CSS?_CSS教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!
Examples A
代码调试框 [www.mb5u.com]

[ 可先修改部分代码 再运行查看效果 ]

Examples B
代码调试框 [www.mb5u.com]

[ 可先修改部分代码 再运行查看效果 ]


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区域使用:
示例代码 [www.mb5u.com]
<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的文件.下面是一个样式表文件的内容:
示例代码 [www.mb5u.com]
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("http://www.mb5u.com/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("http://www.mb5u.com/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注释元素中隐藏它可以来避免这类情况在老的浏览器中发生:
示例代码 [www.mb5u.com]
<head>
<style type="text/css">
<!--
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("http://www.mb5u.com/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:
示例代码 [www.mb5u.com]
<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选择器属性:
示例代码 [www.mb5u.com]
h3
{
color: red;
text-align: left;
font-size: 8pt
}

And an internal style sheet has these properties for the h3 selector:
同时有一个内嵌样式表有这样的h3选择器属性:
示例代码 [www.mb5u.com]
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的属性将变为:
示例代码 [www.mb5u.com]
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.
颜色是继续了外部样式表而文字对齐和文字大小被内嵌的样式表所替换。

来源:无忧整理//所属分类:CSS教程/更新时间:2006-11-09
相关CSS教程
闂傚倷鐒﹂惇褰掑春閸曨垰鍨傞梺顒€绉甸崑銈夋煛閸ヨ埖绶涚紓宥嗙墵閺屾盯鍩勯崘顏佸闂佹娊鏀辩敮妤呭Φ閸曨垰鍗虫俊銈傚亾濞存粍鍎抽—鍐Χ韫囨洜肖闂佺懓鍤栭幏锟�
濠电姷鏁搁崑娑㈩敋椤撶喐鍙忔い鎾卞灩绾惧鏌熼悙顒佺伇婵℃彃鐗撻弻锟犲磼濠靛洨銆婃繝鈷€鍕姇濞e洤锕幊鐘活敆婢跺棗浜炬繝闈涱儐閻撶喖鏌曡箛濠冩珔闁诲繑鐓¢弻鐔煎礄閵堝棗顏�
濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ帒瀚峰Λ鎴犵磼鏉堚晛浠滄い鎾冲悑瀵板嫮鈧綆浜濋楣冩煟鎼达絾鍤€閻庢凹鍘界粩鐔煎幢濞嗘劕搴婃繛鎴炴煣缁舵岸寮婚敐澶婎潊闁宠桨鑳舵导鍫ユ⒑閻熸澘娈╅柟鍑ゆ嫹
濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ帒瀚峰Λ鎴犵磼鏉堚晛浠滄い鎾冲悑瀵板嫭绻濋崟闈涙倯闂傚倷娴囬鏍垂鎼淬劌绀嬫い鎾楀嫅姘舵⒒娴g瓔鍤欓梺甯到宀h儻顦查摶鐐寸箾閹存瑥鐏柛瀣樀閺屻劑鎮ら崒娑橆伓
闂傚倸鍊烽懗鑸电仚缂備胶绮崝娆撶嵁婵犲啯鍎熼柕濞垮劤閸旓箑顪冮妶鍡楃瑨闁稿﹤鎽滈弫顕€宕奸弴鐐殿啇闁诲孩绋掑玻鍧楁儗閹烘柡鍋撶憴鍕缂佽鐗撳顐﹀箻缂佹ɑ娅㈤梺璺ㄥ櫐閹凤拷
闂傚倸鍊烽懗鑸电仚缂備胶绮崝娆撶嵁婵犲啯鍎熼柕濞垮劤閸旓箑顪冮妶鍡楀潑闁稿鎹囬弻娑橆潩椤掑鍓崇紓渚囧枛閿曨亪寮崒鐐茬鐟滃繘鎮¢幘缁樼厽閹兼惌鍨冲畝娑㈡煛閸涱喚绠為柟顖氳嫰閳诲酣骞嬮悩纰夌闯濠电偞鎸婚懝楣冩晝閵壯€鍋撳鐐
闂傚倸鍊峰ù鍥Υ閳ь剟鏌涚€n偅灏伴柕鍥у瀵粙濡歌濡插牓姊烘导娆忕槣闁革綇缍佸璇测槈濡攱鏂€闂佸綊鍋婃禍鐐烘嚄閾忓湱纾藉ù锝夋涧婵¤姤淇婇悙鑸殿棄妞ゆ洩缍佹俊鎼佸煛婵犲啯娅栨繝鐢靛仜濡瑩宕归悽鐢电當闁跨喓濮甸埛鎴︽煕濠靛棗顏╅柍褜鍓欓…鐑界嵁婵犲洤绠婚柤鍛婎問濞肩喖姊虹捄銊ユ珢闁瑰嚖鎷�
闂傚倸鍊风粈渚€骞栭锕€鐤柣妤€鐗婇崣蹇擃渻鐎n亝鎹g紒鈧繝鍌ょ唵闁兼悂娼ф慨鍫ユ煟閹垮嫮绉柟顔款潐濞碱亪骞忓畝濠傚Τ婵犵數鍋涢惇浼村磹閺囥垺绠掗梻浣瑰缁诲倸螞濞嗘垹鐭嗛柍褜鍓熷娲川婵炴碍鍨块幃褔鎮╅懠顒佹闂佽澹嗘晶妤呭疾閹间焦鐓ラ柣鏇炲€圭€氾拷
婵犵數濮烽。钘壩i崨鏉戝瀭妞ゅ繐鐗嗙粈鍫熺節闂堟稒锛嶉柣鏂挎娣囧﹪顢涘顒佸€柣搴㈡皑缁垶濡甸崟顖氱閻庨潧鎽滈悾娲煟鎼淬垻鍟查柟鍑ゆ嫹
濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ帒瀚峰Λ鎴犵磼鏉堚晛浠滄い鎾炽偢瀹曞爼濡搁妷顔荤穿濠碉紕鍋戦崐鏍箰閼姐倖宕查柛鎰典簴閸嬫挸顫濋搹顐ゅ涧婵烇絽娲ら敃顏堛€佸☉妯滄棃鍩€椤掍焦娅犻柛娆忣槺缁♀偓濡炪値鍋掗崰妤冣偓姘炬嫹
婵犵數濮烽。钘壩i崨鏉戝瀭妞ゅ繐鐗嗙粈鍫熺節闂堟稒锛嶉柣鏂挎閹便劌顪冪拠韫闁诲氦顫夊ú锕傚礈閻旂鈧礁鈻庨幘宕囶槯闂佺粯鎸哥花鍫曞磻閹剧粯鍋ㄩ柛娑橈功閸樻悂姊洪崜鎻掍簼缂佽瀚悺顓炩攽閻樻剚鍟忛柛锝庡灦楠炲繘鏁撻敓锟�
婵犵數濮烽。钘壩i崨鏉戝瀭妞ゅ繐鐗嗙粈鍫熺節闂堟稒锛嶉柣鏂挎閹便劌顪冪拠韫闂備礁鎼懟顖炲箠濮椻偓瀹曟椽鍩€椤掍降浜滈柟鍝勬娴滈箖姊虹€圭媭鍤欓梺甯秮閻涱噣骞嬮敃鈧粻娑㈡⒒閸喓鈯曟い鏂挎濮婄粯鎷呴崨濠冨創闁诲孩纰嶉幃鍌炵嵁韫囨稒鏅搁柨鐕傛嫹
婵犵數濮烽。钘壩i崨鏉戝瀭妞ゅ繐鐗嗙粈鍫熺節闂堟稒锛嶉柣鏂挎閹便劌顪冪拠韫闁诲氦顫夊ú鈺冪礊娓氣偓閻涱喚鈧綆浜栭弨浠嬫煕椤愵偄浜楃紒鎲嬫嫹
濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ帒瀚峰Λ鎴犵磼鏉堚晛浠滄い鎾冲悑瀵板嫭绻濋崟顓炲箑闂傚倷鑳堕幊鎾绘偤閵娧勫床闁告洦鍨奸弫鍌炴倵濞戞鑲╂崲閸℃ǜ浜滈柡宥庣厛濞堟柨顭胯瀵墎鎹㈠┑瀣剁稏妞ゆ棁妫勯锟�
闂傚倷娴囬褎顨ラ幖浣稿偍婵犲﹤鐗嗙粈鍫熺節闂堟侗鍎愰柛瀣儔閺岀喖骞嗛悧鍫闂佹椿鍘介〃鍛村煘閹达附鍋愰柟缁樺笚濞堫參姊烘總鍛婃锭缂傚秴锕濠氬Χ婢跺娈熼梺闈涱槶閸庨亶锝炲澶嬧拺缂備焦岣挎竟鍕磽瀹ュ嫮顦﹂柣锝呭槻閳诲酣骞樼€涙ɑ顏熼梻浣芥硶閸o箓骞忛敓锟�
UB闂傚倸鍊风粈浣革耿鏉堚晛鍨濇い鏍仜缁€澶嬩繆閵堝懏鍣界€规挷绶氶弻娑㈠Ψ閵忊剝鐝曢柣搴㈢瀹€鎼佸箖濡も偓閳藉鈻嶉搹顐㈢伌闁挎繄鍋ら弫鎾绘晸閿燂拷
闂傚倸鍊峰ù鍥ㄧ珶閸喆浠堢紒瀣儥濞兼牕鈹戦悩宕囶暡闁绘帡绠栭幃妤呮偨閻㈢偣鈧﹪鏌涚€n偅宕岄柣娑卞櫍瀹曞綊顢欓崣銉ф/濠电姷鏁告慨顓㈠磻閹剧粯鐓ラ柣鏇炲€圭€氾拷
闂傚倷娴囬褎顨ラ崫銉т笉鐎广儱顦崹鍌炴煕濠靛棗顏甸柤鏉挎健閺屾盯濮€閵忕姵顔�
闂傚倸鍊风粈渚€骞夐垾鎰佹綎缂備焦蓱閸欏繐顪冪€n亝鎹g紒鈧繝鍌ょ唵閻犻缚娅i悘閬嶆煕濡や胶顣茬紒缁樼洴瀹曞崬螖閳ь剟顢楅姀銈嗙厱闁绘劕妯婂Σ鍦磼缂佹ḿ銆掔紒杈ㄧ懇閹晠宕崟顐㈣厫闂傚倷鑳舵灙濡ょ姴绻橀獮蹇涙晸閿燂拷
缂傚倸鍊搁崐鎼佸磹閻戣姤鍊块柨鏇炲€归崑锟犳煥閺囩偛鈧悂宕归崒娑氱瘈濠电姴鍊搁顐c亜閳哄懏鏁遍柕鍥у楠炴帒顓奸崶鑸敌滅紓鍌欑劍瑜板啫锕㈤柆宥呯劦妞ゆ帒鍊告禒顖炴煕婵犲啰绠樼紒杈ㄦ尭椤繃锛愬┑鍥ㄦ珦闂備浇娉曢崳锕傚箯閿燂拷
©2017 www.mb5u.com婵犵數濮烽。钘壩i崨鏉戝瀭妞ゅ繐鐗嗙粈鍫熺節闂堟稒锛嶉柣鏂挎閹便劌顪冪拠韫闁诲孩顔栭崳顕€宕抽敐澶婄畺闁冲搫鎳庣粻鐢告煙閻戞ɑ灏柣锔兼嫹
闂傚倸鍊峰ù鍥Υ閳ь剟鏌涚€n偅宕岄柡宀€鍠栭、娑樷堪閸愮偓姣夋俊鐐€戦崕濠氬箯閿燂拷&闂傚倸鍊风粈渚€骞夐敍鍕殰闁圭儤鍤﹀☉銏犵睄闁割偆鍣ュΛ鍛存⒑鐠恒劌娅愰柟鍑ゆ嫹