css笔记08:id选择器之父子选择器

1.父子选择器

(1)01.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>选择器</title>
<link rel="stylesheet" href="my.css"  type="text/css"/>
</head>


<body>
你好,北京!
   <span class="style1">新闻1</span>
   <span class="style1">新闻2</span>
   <span class="style1">新闻3</span>
   <span class="style1">新闻4</span>
   <span class="style1">新闻5</span>
   <!--父子选择器使用-->
   <span id="style2">这是一则<span>非常重要</span>的新闻</span><br />
   <span id="style2">这是一则<span>非常<span>重要</span></span>的新闻</span><br />
   
   <a href="#">goto sohu</a><br />
   <a href="#">goto sina</a><br />
</body>
</html>

 

(2)my.css

@charset "utf-8";
/* CSS Document */

/*html的选择器*/
body {
    color:orange;
}

a:link {
    color:black;
    text-decoration:none;
}

a:hover {
    text-decoration:underline;
}

a:visited {
    color:red;
}



/*.style1 就是类选择器*/
.style1 {
    font-weight:bold;
    font-size:20px;
    background-color:pink;
    color:black;
}


/*#style2就是id选择器*/

#style2 {
    font-size:30px;
    background-color:silver;
    color:black;
}

/*#style2 span 就是父子选择器, #style2是父,span是子*/
#style2 span {
    font-style:italic;
    color:red;
}

/*#style2 span 就是父子选择器, #style2是父,span是子,也包含层次关系*/
#style2 span span{
    font-size:50px;
}

效果图:

 

总结:这里子选择器的标签是必须存在,就是<span>不能改成别的不存在的标记;这里#style2 span也包含层次关系,比如也可以写成#style2 span span

这样三层以上 很少使用。

 

            父子选择器使用于id选择器和class选择器!!!

posted on 2015-07-06 11:04  鸿钧老祖  阅读(761)  评论(0编辑  收藏  举报

导航