CSS3的引入和选择器

2019-11-19  本文已影响0人  1CC4

一、HTML中引入css

1、行内样式

<h1 style="color:red;">style属性的应用</h1>
<p style="font-size:14px; color:green;">直接在HTML标签中设置的样式</p>

2、内联样式表

<style>
        h1{color: green; }
</style>

3、外部样式表

<head>
  ……
<link href="style.css" rel="stylesheet" type="text/css" />
  ……
/*文件路径、使用外部样式表、文件类型*/
</head>

[2]导入式

<head>
……
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>

4、链接式与导入式的区别

5、CSS样式优先级

行内样式>内部样式表>外部样式表
就近原则

二、CSS3基本选择器

1、标签选择器

HTML标签作为标签选择器的名称
<h1>…<h6>、<p>、<img/>
语法:

p { font-size:16px;}
/*标签选择器{属性:(声明)值}*/

2、类选择器

语法:

.class { font-size:16px;}
<标签名 class= "类名称">标签内容</标签名>

3、ID选择器

#id { font-size:16px;}
/*id名称{属性:(声明)值}*/

小结:

三、CSS高级选择器

1.1、层次选择器


语法:

 body p{  background: red; }

1.2、子选择器

语法:

 body>p{  background: pink;}

1.3、相邻兄弟选择器

语法:

.active+p {  background: green;  }

1.4、通用兄弟选择器

语法:

.active~p{  background: yellow;  }

2、结构伪类选择器


示例:

ul li:first-child{ background: red;}
ul li:last-child{ background: green;}
p:nth-child(1){ background: yellow;}
p:nth-of-type(2){ background: blue;}

3、属性选择器

3.1、E[attr]

语法:

a[id] { background: yellow; }

3.2、E[attr=val]

语法:

 a[id=first] { background: red; }

3.3、E[attr*=val]

语法:

 a[class*=links] { background: red; }

3.4、E[attr$=val]

语法:

 a[href$=png] { background: red; }
上一篇 下一篇

猜你喜欢

热点阅读