• 05_css选择器的使用


    一、css选择器的类型

    • 1、标签选择器

    用法:直接写 写标签名:标签名{}

    示例:

    
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>标签选择器title>
    		<style type="text/css">
    			div {
              text-align: center;
              width: 600px;
              height: 400px;
              color: blue;
              background: blanchedalmond;
    			}
    		style>
    	head>
    	<body>
      	
    		<div>使用标签选择器添加样式div>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    • 2、id选择器

    用法:元素的id属性:#id名{}

    示例:

    
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>id选择器title>
    		<style type="text/css">
    			#type {
                text-align: center;
                width: 600px;
                height: 400px;
                color: blue;
                background: blanchedalmond;
    			}
    		style>
    	head>
    	<body>
        
    		<div id="type">使用id选择器添加样式div>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    • 3、类选择器

    用法:元素的class属性 .class 值{}
    (类选择器是使用最多的一种方式)

    示例:

    
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>class选择器title>
    		<style type="text/css">
    			.type {
                text-align: center;
                width: 600px;
                height: 400px;
                color: blue;
                background: blanchedalmond;
    			}
    		style>
    	head>
    	<body>
        
    		<div class="type">使用class选择器添加样式div>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    • 4、层级选择器

    用法:按照层级找到对应需要添加化样式的元素名

    示例:

    
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>层级选择器title>
    		<style type="text/css">
        div b span {
            text-align: center;
            width: 600px;
            height: 400px;
            color: blue;
            background: blanchedalmond;
    			}
    		style>
    	head>
    	<body>
            
    		<div >
                <b>
                    这时候div标签下面的b标签的内容,
                    <span>
                        这时候div标签下面的b标签下面的span的内容
                    span>新的内容
                b>
            div>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    • 5、组选择器

    用法:多个元素名,统一使用一个样式

    示例:

    
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>组选择器title>
    		<style type="text/css">
    			h1,h2,h3 {
            color: blue;
    			}
    		style>
    	head>
    	<body>
    		<h1>一级标题h1>
    		<h2>二级标题h2>
    		<h3>三级标题h3>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    二、css选择器之间的权重关系

    用法:

    1. 标签选择器权重值为:1
    2. 类选择器权重值为 :10
    3. id选择器权重值为 :100
    4. 通过style属性直接设置在标签里的样式,权重值为:1000
    5. 权重值无限大:!important(增加如下代码)

    示例:

    
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>css选择器之间的权重关系title>
    		<style type="text/css">
    			div{
    				/* 1. 标签选择器:权重值为:1 */
    				color: #1E90FF !important;
    				font-size: 60px;	
    			}
    			
    			/* 2. 类选择器:权重值为:10 */
    			.u3{
    				color: #0000FF;
    				font-size:60px;
    			}
    			
    			/* 3. id选择器:权重值为:100 */
    			#u2{
    				color: #912CEE;
    				font-size:60px;
    			}
    			
    			/* 4.通过style属性直接设置在标签里的样式,权重值为:1000 */
    			
    			/* 5.权重值无限大 */
    			/* 增加一个!important */
    		style>
    	head>
    	<body>
            <div>第一个div>
            <div class="u3">第二个div>
            <div class="u3" id="u2">第三个div>
            <div class="u3" id="u2" style="background: #90EE90;font-size: 30px;color: #0000FF;">这是直接在div标签里的样式,权重值为1000div>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    【Unity】安卓加密编译libmono.so失败记录(做个记录呗)
    ImmunoChemistry艾美捷平底MicroTiter平板说明书
    Jmeter之性能测试
    springboot晋韵戏剧点播网站设计毕业设计源码112304
    尚医通_第9章_部署医院模拟系统和搭建环境
    工具及方法 - 多邻国: Duolingo
    杂货配送服务公司Instacart申请1亿美元纳斯达克IPO上市
    网络安全之CSRF漏洞原理和实战,以及CSRF漏洞防护方法
    arduino框架开发esp32,arduino框架的优势是什么?为什么要用这个框架
    百度直播iOS SDK平台化输出改造
  • 原文地址:https://blog.csdn.net/qq_40236497/article/details/132813295