HTML
h1
p
代码块
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>通用选择器</title> <style> *{ color: red; } </style></head><body> <h1>微笑是最初的信仰</h1> <p>微笑是最初的信仰</p></body></html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>通用选择器</title>
<style>
*{
color: red;
}
</style>
</head>
<body>
<h1>微笑是最初的信仰</h1>
<p>微笑是最初的信仰</p>
</body>
</html>
结果图
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>标签选择器</title> <style> h1{ color: red; } </style></head><body> <h1>微笑是最初的信仰</h1> <p>微笑是最初的信仰</p></body></html>
<title>标签选择器</title>
h1{
注意:标签选择器是指给指定的标签设置样式。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>标签选择器</title> <style> h1{ color: red; } </style></head><body> <h1>成功不是击败别人,而是改变自己。</h1> <h1>微笑是最初的信仰</h1> <p>微笑是最初的信仰</p></body></html>
<h1>成功不是击败别人,而是改变自己。</h1>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>标签选择器</title> <style> h1{ color: red; } p{ color:red; } </style></head><body> <h1>成功不是击败别人,而是改变自己。</h1> <h1>微笑是最初的信仰</h1> <p>微笑是最初的信仰</p></body></html>
p{
color:red;
.box
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>类选择器</title> <style> .box{ color:red; } </style></head><body> <h1 class="box">成功不是击败别人,而是改变自己。</h1> <h1>微笑是最初的信仰</h1> <p>微笑是最初的信仰</p></body></html>
<title>类选择器</title>
.box{
<h1 class="box">成功不是击败别人,而是改变自己。</h1>
注意:只要是class属性的值为.box的标签,不管它是什么标签,都会将字体颜色设置为红色,其余的CSS样式也是一致。
class
CSS
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>类选择器</title> <style> .box{ color:red; } </style></head><body> <h1 class="box">成功不是击败别人,而是改变自己。</h1> <h1 class="box">微笑是最初的信仰</h1> <p class="box">微笑是最初的信仰</p></body></html>
<h1 class="box">微笑是最初的信仰</h1>
<p class="box">微笑是最初的信仰</p>
id
#box
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>id选择器</title> <style> #box{ color:red; } </style></head><body> <h1 id="box">成功不是击败别人,而是改变自己。</h1></body></html>
<title>id选择器</title>
#box{
<h1 id="box">成功不是击败别人,而是改变自己。</h1>
注意:使用id选择器是给拥有指定的id属性值来设置样式,但是要注意在一个HTML页面中id的属性值必须是唯一的。
接下来让我们进入结合元素选择器实践,笔者以嵌入式的形式,通过h2标签class属性值为.box元素的字体颜色,设置为红色。
h2
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>结合元素选择器</title> <style> h2.box{ color:red; } </style></head><body> <h2 class="box">成功不是击败别人,而是改变自己。</h2> <span class="box">成功不是击败别人,而是改变自己。</span></body></html>
<title>结合元素选择器</title>
h2.box{
<h2 class="box">成功不是击败别人,而是改变自己。</h2>
<span class="box">成功不是击败别人,而是改变自己。</span>
注意:结合元素选取器执行原理说明如下:首先是先找到h2标签,然后再去h2标签中找class属性值为.box,如果找到class属性值为.box就给其设置样式。现在大家应该知道了span标签下面的class属性值为.box为什么没有渲染的原因了。
span
接下来让我们进入多类选择器实践,笔者以嵌入式的形式,将class属性值包含.box和.box1元素的字体颜色设置为红色。
.box1
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>多类选择器</title> <style> .box.box1{ color:red; } </style></head><body> <h2 class="box1 box">成功不是击败别人,而是改变自己。</h2> <span class="box box1">成功不是击败别人,而是改变自己。</span> <h2 class="box1 ">微笑是最初的信仰</h2> <span class="box">微笑是最初的信仰</span></body></html>
<title>多类选择器</title>
.box.box1{
<h2 class="box1 box">成功不是击败别人,而是改变自己。</h2>
<span class="box box1">成功不是击败别人,而是改变自己。</span>
<h2 class="box1 ">微笑是最初的信仰</h2>
<span class="box">微笑是最初的信仰</span>
注意:多类选择器执行原理说明如下:首先class属性值可以设置为多个以空格隔开即可,举例:如果一个class属性值包含.box和.box1将其设置样式,通过把两个类选择器链接在一起,仅可以选择同时包含这些类名的元素(类名的顺序不限)。如果一个多类选择器包含类名列表中没有的一个类名,匹配就会失败。现在大家应该知道了单独的class属性值为.box和.box1没有被渲染了。
原文链接:http://www.cnblogs.com/lq0001/p/11846116.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728