內聯定義便是在對象的標記內使用對象的 style 屬性定義適用其的樣式表屬性。
示例代碼:css
<p style="color:red">這一行的字體顏色將顯示爲紅色</p>
在你的 HTML 文檔的<head>
標記裏插入一個<style>
塊對象。
示例代碼:html
<style> .test2 { color: red; } </style>
先創建外部樣式表文件*.css
,而後使用 HTML 的 link 對象。或者使用 @import
來引入。
示例代碼:瀏覽器
<!-- Use link elements --> <link rel="stylesheet" href="core.css"> <!-- Use @imports --> <style> @import url("more.css"); </style>
<div> <p>Sample Paragraph</p> <p>Sample Paragraph</p> <p>Sample Paragraph</p> </div> <style type="text/css"> p { color: blue; } </style>
<div> <p>Sample Paragraph</p> <p class="special bold">Sample Paragraph</p> <p>Sample Paragraph</p> </div> <style type="text/css"> p { color: blue } .special { color: orange; } .bold { font-weight: bold; } </style>
<div> <p id="special">Sample Paragraph</p> </div> <style type="text/css"> #special { color: red; } </style>
<div> <p>Sample Paragraph</p> <p>Sample Paragraph</p> </div> <style type="text/css"> * { color: blue; } </style>
<div> <form action=""> <input type="text" value="Xinyang" disabled> <input type="password" placeholder="Password"> <input type="button" value="Button"> </form> </div> <style type="text/css"> [disabled] { background-color: orange; } [type=button] { color: blue; } </style>
<div> <a href="http://sample-site.com" title="Sample Site">Sample Site</a> </div> <style type="text/css"> /* 僞類屬性定義有順序要求! */ a:link { color: gray; } a:visited { color:red; } a:hover { color: green; /* 鼠標懸停 */ } a:active { color: orange; /* 鼠標點擊 */ } </style>
權重主要分爲 4 個等級:字體
style=""
,權值爲1000#content
,權值爲100.content
,權值爲10div p
,權值爲1計算方法:url
NOTE:從上到下優先級一次下降,且優先級高的樣式會將優先級低的樣式覆蓋。大體公式(並不許確)以下。code
value = a * 1000 + b * 100 + c * 10 + d
!important
規則的優先級最大