内联定义即是在对象的标记内使用对象的 style 属性定义适用其的样式表属性。
示例代码:
<p style="color:red">这一行的字体颜色将显示为红色</p>
在你的 HTML 文档的<head>
标记里插入一个<style>
块对象。
示例代码:
<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计算方法:
NOTE:从上到下优先级一次降低,且优先级高的样式会将优先级低的样式覆盖。大致公式(并不准确)如下。
value = a * 1000 + b * 100 + c * 10 + d
!important
规则的优先级最大原文:https://www.cnblogs.com/xiaxu/p/11512694.html