• 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
  • 相关阅读:
    图像处理基础知识
    Audition RMS计算原理解析
    nginx配置-gzip
    iOS开发UITableView的使用,区别Plain模式和Grouped模式
    在局域网里怎么在windows 10里连接到龙梦福珑2.0的Fedora 28图形界面?
    Docker容器日志查看与清理(亲测有效)
    nginx 全相联结构的引申
    Golang开发-new关键字
    29_RTC实时时钟实验
    搭建自动化 Web 页面性能检测系统 —— 设计篇
  • 原文地址:https://blog.csdn.net/qq_40236497/article/details/132813295