CSS初始:CSS的概念、样式、优势和选择器

CSS的概念:

CSS是一种文件样式的计算机语言(它的全称为:“层叠样式表 Cascading Style Sheets”)它作用在于定义HTML内容在浏览器的显示样式。


CSS的样式:优先级:行内>内部>外部(链接,导入)

行内样式:写在标签里的(style="声明样式")

<p style="color: red;">行内样式</p>

内部样式:写在head内,<style type="text/css">
                                                 选择器{声明样式}
                                            </style>

<style type="text/css">
		
</style>

链接样式:写在.CSS文件;<link rel="stylesheet" type="test/css" href="css文件的位置"/>

<link rel="stylesheet" type="text/css" href="css/new_file.css"/>

导入样式:写在CSS文件里@import url("css文件位置");

@import url("global.css");

CSS的优势:

  1. 内容与表分开

  2. 内容容易修改

  3. 页面布局更加灵活

  4. 减少代码量,增加浏览速度

  5. 容易寻找相关的位置


CSS的选择器:

基本选择器优先级:ID>类选择器>标签选择器

        1、标签选择器:

<style type="text/css">
          
 p{
                color: red;
            }
        </style>
    </head>
    <body>
        
<p>标签选择器</p>
    </body>

        2、类选择器

<style type="text/css">
         
 .lei{
                color: red;
            }
        </style>
    </head>
    <body>
        <p
class="lei">类选择器</p>
    </body>

        3、ID选择器

<style type="text/css">
            
#id{
                color: red;
            }
        </style>
    </head>
    <body>
        <div
id="id">ID选择器</div>

层次选择器:

1、后代选择器:父元素有关联的所有元素
2、相邻兄弟选择器:指定的子元素就近的后面同级元素
3、子选择器:父元素的子元素
4、通用兄弟:指定的子元素后所有的同级元素

伪类选择器:

P:first-child:找出第一个元素是否是P,是的话就会声明效果

<style type="text/css">
			p:first-child{
				color: red;
			}
		</style>
	</head>
	<body>
		<p>1</p>
		<p>2</p>
		<p>3</p>
		<p>4</p>
	</body>

P:last-child:找出最后一个元素是否为P,是的话就会声明效果 

<style type="text/css">
			p:last-child{
				color: red;
			}
		</style>
	</head>
	<body>
		<p>1</p>
		<p>2</p>
		<p>3</p>
		<p>4</p>
	</body>

P:nth-child(n):找出指定的第N个元素是否为P,是的话就会声明效果 

<style type="text/css">
			p:nth-child(2){
				color: red;
			}
		</style>
	</head>
	<body>
		<p>1</p>
		<p>2</p>
		<p>3</p>
		<p>4</p>
	</body>

 P:first-of-type:找出类型再看位置

<style type="text/css">
			p:first-of-type{
				color: red;
			}
		</style>
	</head>
	<body>
		<p>1</p>
		<p>2</p>
		<p>3</p>
		<p>4</p>
	</body>

P:last-of-type:找出类型再看位置

<style type="text/css">
			p:last-of-type{
				color: red;
			}
		</style>
	</head>
	<body>
		<p>1</p>
		<p>2</p>
		<p>3</p>
		<p>4</p>
	</body>

 P:nth-of-type(n):找到类型,再看n的位置

<style type="text/css">
			p:nth-of-type(3){
				color: red;
			}
		</style>
	</head>
	<body>
		<p>1</p>
		<p>2</p>
		<p>3</p>
		<p>4</p>
	</body>

 属性选择器:

P[属性]:对应的属性有效果

<style type="text/css">
			p[class]{
				color: red;
			}
		</style>
	</head>
	<body>
		<p class="">1</p>
		<p class="">2</p>
		<p class="">3</p>
		<p class="">4</p>
	</body>

P[属性="属性值"]:对应的属性的值一致

<style type="text/css">
			p[class="p1"]{
				color: red;
			}
		</style>
	</head>
	<body>
		<p class="p1">1</p>
		<p class="p1">2</p>
		<p class="">3</p>
		<p class="">4</p>
	</body>

P[属性^="属性值"]:对应属性的值开头的字符有效果 

<style type="text/css">
			p[class^="c1"]{
				color: red;
			}
		</style>
	</head>
	<body>
		<p class="a1">1</p>
		<p class="l1">2</p>
		<p class="p1">3</p>
		<p class="c1">4</p>
	</body>

P[属性$="属性值"]:对应属性的值结尾的字符有效果 

<style type="text/css">
			p[class$="3"]{
				color: red;
			}
		</style>
	</head>
	<body>
		<p class="a1">1</p>
		<p class="a2">2</p>
		<p class="a3">3</p>
		<p class="a4">4</p>
	</body>

 P[属性*="属性值"]:对应属性的值任意存在的字符有效果

<style type="text/css">
			p[class*="1"]{
				color: red;
			}
		</style>
	</head>
	<body>
		<p class="a1">1</p>
		<p class="b1">2</p>
		<p class="c1">3</p>
		<p class="d1">4</p>
	</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值