CSS笔记——选择器(二)

以前做j2ee,超级烦写css,属性太多,看来还是绕回来了!

1、元素选择器

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>师然学习笔记</title>
        
        <!--描述:元素选择器 -->
        
        <style type="text/css">
            div{
                background-color: red;
                font:italic normal bold 14pt normal 楷体_GB2312;
            }
            
        </style>
    </head>
    <body>
        <divr>
            师然学习JAVA
        </div>
    </body>
</html>

  效果如下:

 

 

 

 

 

 

 

2、ID选择器

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>师然学习笔记</title>
        
        <!--描述:元素选择器 -->
        
       <style type="text/css">
            div{
                background-color: red;
                font:italic normal bold 14pt normal 楷体_GB2312;
            }
            
            #shiran{
                
                width:200px;
                height:30px;
                background-color: #ddd;
                padding:3px
                
            }
            
        </style>
    </head>
    <body>
        <div id="shiran">
            师然学习JAVA
        </div>
    </body>
</html>

  效果如下:

如果只想对某一id的某一个元素起作用,可以这样写

例如:只对多个div元素中的某一个id为shiran的元素起作用:

div#shiran{

 width:200px;
                height:30px;
                background-color: #ddd;
                padding:3px

}

 

3、class选择器

我以前会叫它“类选择器”,别人也这么叫,但是这样的称呼并不合适,它和类没有关系。只是好像把一些属性放到了一起而已,谁用都可以。

这个class选择器也和ID选择器一样,可以写上元素名。

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>师然学习笔记</title>

       <style type="text/css">
       
            .shiranClass{
                background-color: red;
            }
        
        </style>
    </head>
    <body>
        <div class="shiranClass">师然学习JAVA</div>
    </body>
</html>

  效果如下:

 

4、包含选择器

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>师然学习笔记</title>
        
        <style>
            
            div{
                
                font-family: "微软雅黑";
            background-color: yellow;
            }
            
            div.shiran{
            
            font-family: "微软雅黑";
            background-color: red;
            }
            
        </style>
    </head>
    <body>
    
    <div>师然学java</div>
    
    <div>
        <section>
            <div class="shiran">师然学HD5</div>
        </section>
    </div>
    
    <p class="shiran">师然学iOS</p>
</html>

  效果如下:

5、子选择器

子选择器和包含选择器有点像,区别在于:

包含选择器的目标元素还要在父元素下就可以,孙子元素也可以。

但是子选择器就必须是儿子,也就是说目标元素必须是直接子元素,孙子都不行。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>师然学习笔记</title>
        
        <style type="text/css">
            
            div{
                width: 200px;
                height: 30px;
                
                background-color:red ;
                
                margin: 5px;
            }
            
            div>.shiran{
                
                
                width: 300px;
                height: 40px;
                
                background-color:yellow ;
                
                margin: 10px;
            }
            
        </style>
    <body>
        
        <div>我是1号DIV</div>
        
        <div><p class="shiran">我是2号DIV</p></div>
        
        <div><section><p class="shiran">我是3号DIV</p></section></div>
    </body>
</html>

 效果如下:

6、兄弟选择器

这个有点难理解,我理解的意思是这样,目标选择器作用的位置元素在匹配的某选择器的 后面。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>师然学习笔记</title>
        
        <style type="text/css">
            
            #android ~ .long{
                background-color: #00FF00;
            }
        </style>
    </head>
    <body>
        
        <div>
        <div>师然学习Java</div>
        
        <div class="long">师然学习H5</div>

        <div id="android">师然学习iOS</div>
        
        <p class="long"> 师然学习安卓</p>
        
        <div class="long">师然学习.NET</div>
        
        </div>
        
    </body>
</html>

看代码,android是我的第一个选择器,是一个ID选择器,然后有一个class选择器叫long.

那么,再看我的几个html元素。在ID为android后面的元素,如果class是long,这个才生效。

 

转载于:https://www.cnblogs.com/kellybaby/p/6640634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值