在HTML文档中使用CSS的三种方法
1.外部样式表
外部样式表是指将CSS编写在扩展名为.css的单独文件中,并从HTML元素引用它。这样在大多数情况下,一个站点的不同页面可以使用相同的规则集来获得基本的外观。
HTML文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>我是吴彦祖</title>
<link rel="stylesheet" href="styles/style.css">
<link rel="stylesheet" href="https://fonts.font.im/css?family=Open+Sans">
</head>
<body>
<img src="images/吴彦祖.jpg" alt="吴彦祖">
<h1>吴彦祖</h1>
<p>我的名字叫吴彦祖,<span class="special">哈哈</span>我非常的帅气......<em>嘻嘻</em></p>
<ul>
<li class="special">高</li>
<li class="special">富</li>
<li><em>帅</em></li>
</ul>
<p>bye</p>
<a href="https://baike.baidu.com/item/吴彦祖/182990?fr=aladdin" target="_blank">勿爱我</a>
</body>
</html>
CSS文件:
html{font-size: 10px;font-family: 'Open Sans',sans-serif;background-color: #00539F;}/*元素选择器*/
body{width: 600px;margin: 0 auto;background-color: #ff9500;padding: 0 20px 20px 20px;border: 5px solid black;}/*元素选择器*/
h1{margin: 0;padding: 20px 0;color: #00539f;text-shadow: 3px 3px 1px black;font-size: 60px;text-align: center;}/*元素选择器*/
img{display: block;margin: 0 auto;}/*元素选择器*/
p,li{font-size: 16px;line-height: 2;letter-spacing: 1px;}/*元素选择器*/
li{list-style-type: none;}/*元素选择器*/
.special{color: red;font-weight: bold;}/*类选择器*/
li em{color: rebeccapurple;}/*包含选择器*/
img+p{font-size: 200%;}/*相邻选择器*/
a:link{color: seagreen;}/*状态选择器*/
a:visited{color: red;}/*状态选择器*/
a:hover{text-decoration: none;}/*状态选择器*/
body h1+p .special{color: yellow;background-color: black;padding: 5px;}/*混合选择器*/
2.内部样式表
内部样式表是指不使用外部CSS文件,而是将CSS放在HTML文件中标签里的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>吴彦祖</title>
<style>
h1{
color:blue;
}
</style>
</head>
<body>
<h1>吴彦祖</h1>
</body>
</html>
3.内联样式表
内联样式表存在于HTML元素的style属性中,其主要特点是每个CSS表只影响一个HTML元素。这种写法将文档结构和文档表现混合起来了,使得代码变得难以阅读和理解。
<!DOCTYPE html>
<html>
</head>
<meta charset="utf-8">
<title>吴彦祖</title>
</head>
<body>
<h1 style="color:blue;">吴彦祖</h1>
</body>
</html>