python 全栈开发,Day47(行级块级标签,高级选择器,属性选择器,伪类选择器,伪元素选择器,css的继承性和层叠性,层叠性权重相同处理,盒模型,padding,border,margin)

一、HTML中的行级标签和块级标签

块级标签

常见的块级标签:div,p,h1-h6,ul,li,dl,dt,dd

1、独占一行,不和其他元素待在同一行
2、能设置宽高
3、如果不设置宽高,默认为body100%宽度

行级标签

常见的行级标签:a,span,strong,u,em

1、能和其他元素待在同一行
2、不能设置宽高
3、宽高 是内容的宽高

行内块标签

常见的行内块标签:img,input,textarea
1、能和其他元素待在一行
2、能设置宽高

span默认是不能设置宽高的,但是设置了display: block;属性之后,就可以设置宽高了

它表示将此元素将显示为块级元素,此元素前后会带有换行符。

举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*设置块级元素*/
        span {
            display: block;
            width: 100px;
            height: 100px;
            background-color: yellow;
        }
    </style>
</head>
<body>
<span></span>
<a href="#">11</a>
</body>
</html>
View Code

网页效果:

可以看到,它默认换行了

二、高级选择器

高级选择器分为:后代选择器、子代选择器、并集选择器、交集选择器 

后代选择器

使用空格表示后代选择器。顾名思义,父元素的后代(包括儿子,孙子,重孙子

        .container p{
            color: red;
        }
        .container .item p{
            color: yellow;
        }
View Code

div里面的p

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*后代选择器*/
        div p {
            color: red;
        }
    </style>
</head>
<body>
<div>
    <p>内容</p>
</div>
<p>另一个内容</p>
</body>
</html>
View Code

网页效果:

class里面的p

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*后代选择器*/
        .father p {
            color: red;
        }
    </style>
</head>
<body>
<div class="father">
    <div>
        <p>内容</p>
    </div>
</div>
<p>另一个内容</p>
</body>
</html>
View Code

网页效果:

 class里面的class里面的p

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*后代选择器*/
        .father .a p {
            color: red;
        }
        .father p {
            color: yellow;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="item">
        <div class="a">
            <p>内容</p>
        </div>
    </div>
    <p>内容</p>
</div>
<div class="a">
    <p>另一个内容</p>
</div>
</body>
</html>
View Code

网页效果:

子代选择器

使用>表示子代选择器。比如div>p,仅仅表示的是当前div元素选中的子代(不包含孙子....)元素p。 

.container>p {
    color: yellowgreen;
}
View Code

举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*后代选择器*/
        .father .a p {
            color: red;
        }
        /*子代选择器*/
        .father>p {
            color: yellow;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="item">
        <div class="a">
            <p>内容</p>
        </div>
    </div>
    <p>内容</p>
</div>
<div class="a">
    <p>另一个内容</p>
</div>
</body>
</html>
View Code

网页效果:

并集选择器 

多个选择器之间使用逗号隔开。表示选中的页面中的多个标签。一些共性的元素,可以使用并集选择器

/*并集选择器*/
h3,a{
    color: #008000;
    text-decoration: none;
                
}
View Code

比如像百度首页使用并集选择器。

body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td {
      margin: 0;
      padding: 0
   }
/*使用此并集选择器选中页面中所有的标签,页面布局的时候会使用*/
View Code

统一样式

交集选择器

使用.表示交集选择器。第一个标签必须是标签选择器,第二个标签必须是类选择器 语法:div.active

比如有一个<h4 class='active'></h4>这样的标签。

那么

        h4{
            width: 100px;
            font-size: 14px;
        }
        .active{
            color: red;
            text-decoration: underline;
        }
        /*交集选择器*/
        h4.active{
            background: #00BFFF;
        }

它表示两者选中之后元素共有的特性。  

举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*交集选择器*/
        h4 {
            background: green;
        }
        .active {
            font-size: 14px;
        }
        h4.active {
            color: red;
        }
        li.active{
            background: yellow;
        }
    </style>
</head>
<body>
<ul>
    <li>
        <a href="#">1</a>
    </li>
    <li class="active">
        <a href="#">2</a>
    </li>
    <li>
        <a href="#">3</a>
    </li>
    <li>
        <a href="#">4</a>
    </li>
</ul>
<h4 class="active">我是一个4级标题</h4>
</body>
</html>
View Code

网页效果:

三、属性选择器

属性选择器,字面意思就是根据标签中的属性,选中当前的标签。

语法:

/*根据属性查找*/
            /*[for]{
                color: red;
            }*/
            
            /*找到for属性的等于username的元素 字体颜色设为红色*/
            /*[for='username']{
                color: yellow;
            }*/
            
            /*以....开头  ^*/ 
            /*[for^='user']{
                color: #008000;
            }*/
            
            /*以....结尾   $*/
            /*[for$='vvip']{
                color: red;
            }*/
            
            /*包含某元素的标签*/
            /*[for*="vip"]{
                color: #00BFFF;
            }*/
            
            /**/
            
            /*指定单词的属性*/
            label[for~='user1']{
                color: red;
            }
            
            input[type='text']{
                background: red;
            }
View Code

 举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*属性选择器*/
        [for]{
            color: red;
        }
        [type]{
            background-color: red;
        }
    </style>
</head>
<body>
<from action="">
    <label for="username">用户名:</label>
    <input type="text">
    <input type="password">
</from>
</body>
</html>
View Code

网页效果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*属性选择器*/
        label[for]{
            color: red;
        }
        input[type='text']{
            background-color: red;
        }
    </style>
</head>
<body>
<from action="">
    <label for="username">用户名</label>
    <input type="text">
    <input type="password">
</from>
</body>
</html>
View Code

网页效果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*属性选择器*/
        label[for]{
            color: red;
        }
        input[type='text']{
            background-color: red;
        }
        label[for^=vv]{
            color: blue;
        }
    </style>
</head>
<body>
    <from action="">
        <label for="username">用户名</label>
        <label for="vip">vip</label>
        <label for="vvip">vvip</label>
        <input type="text">
        <input type="password">
    </from>
</body>
</html>
View Code

网页效果:

四、伪类选择器

伪类选择器一般会用在超链接a标签中,使用a标签的伪类选择器,我们一定要遵循"爱恨准则"  LoVe HAte 

/*没有被访问的a标签的样式*/
        .box ul li.item1 a:link{
            
            color: #666;
        }
        /*访问过后的a标签的样式*/
        .box ul li.item2 a:visited{
            
            color: yellow;
        }
        /*鼠标悬停时a标签的样式*/
        .box ul li.item3 a:hover{
            
            color: green;
        }
        /*鼠标摁住的时候a标签的样式*/
        .box ul li.item4 a:active{
            
            color: yellowgreen;
        }
View Code

如果编辑器安装了Emmet插件,输入 div#box,按一下tab键,就会自动变成下面的样子

<div id="box"></div>

输入html:5也会补全代码,还有a,input,p,div...也会补全  

举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*伪类选择器*/
        /*设置a标签默认样式*/
        .box ul li.item a:link{
            color: #666;
        }
        /*a标签点击之后的样式*/
        .box ul li.item a:visited{
            color: yellow;
        }
        /*悬浮样式*/
        .box ul li.item a:hover{
            color: green;
            font-size: 30px;
        }
        /*点击时效果*/
        .box ul li.item a:active{
            color: pink;
            background-color: #fff;
        }
    </style>
</head>
<body>
<div id="box"></div>
<div class="box">
    <ul>
        <li class="item">
            <a href="#">超链接</a>
        </li>
    </ul>
</div>
</body>
</html>  

网页效果:

点击之后:

 

鼠标悬停效果:

鼠标点击效果:

再给大家介绍一种css3的选择器nth-child()

/*选中第一个元素*/
        div ul li:first-child{
            font-size: 20px;
            color: red;
        }
        /*选中最后一个元素*/
        div ul li:last-child{
            font-size: 20px;
            color: yellow;
        }
        
        /*选中当前指定的元素  数值从1开始*/
        div ul li:nth-child(3){
            font-size: 30px;
            color: purple;
        }
        
        /*n表示选中所有,这里面必须是n, 从0开始的  0的时候表示没有选中*/
        div ul li:nth-child(n){
            font-size: 40px;
            color: red;
        }
        
        /*偶数*/
        div ul li:nth-child(2n){
            font-size: 50px;
            color: gold;
        }
        /*奇数*/
        div ul li:nth-child(2n-1){
            font-size: 50px;
            color: yellow;
        }
        /*隔几换色  隔行换色
             隔4换色 就是5n+1,隔3换色就是4n+1
            */
        
        div ul li:nth-child(5n+1){
            font-size: 50px;
            color: red;
        }
View Code

举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*选中第一个元素,等同于nth-child(1)*/
        ul li:first-child{
            color: red;
        }
        /*选中最后一个元素*/
        ul li:last-child{
            color: green;
        }
        /*选中当前指定的元素   数值从1开始*/
        ul li:nth-child(3){
            color: purple;
        }
        /*偶数*/
        ul li:nth-child(2n){
            color: gold;
        }
        /*奇数*/
        div ul li:nth-child(2n-1){
            color: yellow;
        }
    </style>
</head>
<body>
<ul>
    <li>林志玲</li>
    <li>刘诗诗</li>
    <li>杨幂</li>
    <li>宋茜</li>
    <li>赵丽颖</li>
</ul>
</body>
</html>
View Code

五、伪元素选择器

 非常重要的语法,废话不多说,直接上代码!!!

/*设置第一个首字母的样式*/
        p:first-letter{
            color: red;
            font-size: 30px;

        }
        
/* 在....之前 添加内容   这个属性使用不是很频繁 了解  使用此伪元素选择器一定要结合content属性*/
        p:before{
            content:'alex';
        }
        
        
/*在....之后 添加内容,使用非常频繁 通常与咱们后面要讲到布局 有很大的关联(清除浮动)*/
        p:after{
            content:'&';
            color: red;
            font-size: 40px;
        }
View Code

举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*设置第一个首字母的样式*/
        p:first-letter{
            color: red;
            font-size: 30px;
        }
        /*在...之前添加内容,这个属性使用不是很频繁了解,使用此伪元素选择器一定要结合content属性*/
        p:before{
            content: 'CCTV';
        }
        /*这个非常重要,解决我们后面浮动产生的问题(布局)*/
        p:after{
            content: '.';
            display: block;
            height: 0;
            visibility: hidden;
            clear: both;
        }
    </style>
</head>
<body>
<p>董卿</p>
</body>
</html>
View Code

网页效果:

p:before添加的内容,鼠标是不能选择复制粘贴的。某些网页,会用到

六、css的继承性和层叠性

css有两大特性:继承性和层叠性 

继承性 

面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点:继承了父类的属性和方法。那么我们现在主要研究css,css就是在设置属性的。不会牵扯到方法的层面。

继承:给父级设置一些属性,子级继承了父级的该属性,这就是我们的css中的继承。

猜你喜欢

转载自www.cnblogs.com/Black-rainbow/p/9079960.html