CSS笔记(二)

样式表书写位置

内嵌式写法

<head>

<style type=”text/css”>

样式表写法

</style>

</head>

外链式写法

写在head里,<link rel=”stylesheet” href=”1.css”>

	<!-- 外链式写法-->
	<link rel="stylesheet" type="text/css" href="CSS练习.css">

行内样式表

	<!-- 行内样式表-->
	<div class="box" style="font-size: 30px;color: red;">
		<p><span>啦啦啦啊</span></p>
	</div>

◆三种写法特点:

★内嵌式写法,样式只作用于当前文件,没有真正实现结构表现分离。

★外链式写法,作用范围是当前站点,谁调用谁生效,范围广,真正实现结构表现分离。

★行内样式表,作用范围仅限于当前标签,范围小,结构表现混在一起。  (不推荐使用)

标签分类(显示方式)

块元素

典型代表,Div,h1-h6,p,ul,li

特点: ★独占一行

      ★可以设置宽高

     ★ 嵌套(包含)下,子块元素宽度(没有定义情况下)和父块元素宽度默认一致。

行内元素

典型代表 span  ,a,  ,strong , em,  del,  ins

特点:★在一行上显示

      ★不能直接设置宽高

      ★元素的宽和高就是内容撑开的宽高。

行内块元素(内联元素)

典型代表  input  img

特点:★在一行上显示

      ★可以设置宽高    

块元素、行内元素

块元素转行内元素

display:inline;

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		/*块元素转换为行内元素*/
		div,p{
			display: inline;
		}
	</style>

</head>
<body>
	<div>啦啦啦呕</div>
	<p>哈哈哈哈哈啊</p>
	<span>哈哈哈哈哈哇</span>
</body>
</html>

 不能设置宽高

行内元素转块元素

display:block;

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		/*行内元素转块元素*/
		span{
			display: block;
		}
	</style>

</head>
<body>
	<span>哈哈哈哈哈哇</span>
	<span>哈哈哈哈哈哇</span>
</body>
</html>

 块和行内元素转行内元素

display:inline-block;

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		/*块和行内元素转行内块元素*/
		div,a,span,strong{
			display: inline-block;
			width: 200px;
			height: 200px;
			background-color: yellow;
			text-align: center;
		}
	</style>

</head>
<body>
	<div>啦啦啦1</div>
	<a>啦啦啦啦2</a>
	<span>哈哈哈哈哈1</span>
	<strong>哈哈哈哈哈2</strong>
</body>
</html>

 css三大特性

层叠性

当多个样式作用于同一个(同一类)标签时,样式发生了冲突,总是执行后边的代码(后边代码层叠前边的代码)。和标签调用选择器的顺序没有关系。

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		.box{
			font-size: 20px;
			color: red;
		}
		.box2{
			font-size: 40px;
			color: blue;
		}
	</style>

</head>
<body>
	<div class="box box2">啦啦啦1</div>
	<a>啦啦啦啦2</a>
	<span>哈哈哈哈哈1</span>
	<strong>哈哈哈哈哈2</strong>
</body>
</html>

 继承性

继承性发生的前提是包含(嵌套关系)

   ★文字颜色可以继承

   ★文字大小可以继承

   ★字体可以继续

   ★字体粗细可以继承

   ★文字风格可以继承

   ★行高可以继承

   总结:文字的所有属性都可以继承。

◆特殊情况:

h系列不能继承文字大小。

a标签不能继承文字颜色。

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		.father{
			font-size: 20px;
			color: red;
		}
	</style>

</head>
<body>
	<div class="father">
		<p class="son">啦啦啦1</p>
	</div>
	<a>啦啦啦啦2</a>
	<span>哈哈哈哈哈1</span>
	<strong>哈哈哈哈哈2</strong>
</body>
</html>

 优先级

 默认样式<标签选择器<类选择器<id选择器<行内样式<!important  

         0                  1            10           100           1000      1000以上

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		div{
			font-size: 20px !important;
			color: red !important;
		}
		.box{
			color: green;
			font-size: 60px;
		}
		#con{
			color: pink;
			font-size: 30px;
		}

	</style>

</head>
<body>
	<div class="box" id="con" style="font-size: 12px">啦啦啦1</div>
	<a>啦啦啦啦2</a>
	<span>哈哈哈哈哈1</span>
	<strong>哈哈哈哈哈2</strong>
</body>
</html>

 

◆优先级特点

★继承的权重为0

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		.father{
			font-size: 20px;
			color: red;
		}
		p{
			font-size: 40px;
			color: blue;
		}
	</style>

</head>
<body>
	<div class="father">
		<p>啦啦啦1</p>
	</div>
	<a>啦啦啦啦2</a>
	<span>哈哈哈哈哈1</span>
	<strong>哈哈哈哈哈2</strong>
</body>
</html>

 

★权重会叠加

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		.father{
			font-size: 20px;
			color: red;
		}
		p{
			font-size: 40px;
			color: blue;
		}
		.son{
			font-size: 60px;
			color: pink;
		}
		p.son{
			font-size: 120px;
			color: yellow;
		}
	</style>

</head>
<body>
	<div class="father">
		<p class="son">啦啦啦1</p>
	</div>
	<a>啦啦啦啦2</a>
	<span>哈哈哈哈哈1</span>
	<strong>哈哈哈哈哈2</strong>
</body>
</html>

 链接伪类

a:link{属性:值;}  a{属性:值}效果是一样的。

a:link{属性:值;}       链接默认状态  

a:visited{属性:值;}     链接访问之后的状态

a:hover{属性:值;}      鼠标放到链接上显示的状态   a:active{属性:值;}      链接激活的状态

  :focus{属性:值;}     获取焦点

	/*链接默认状态*/
		a.link{
			color: red;
		}
		a{
			color: red;
		}
		/*链接访问之后的状态*/
		a:visited{
			color: green;
		}
		/*鼠标经过显示状态*/
		a:hover{
			color: yellow;
		}
		/*链接激活状态*/
		a:active{
			color:pink;
		}

文本修饰

text-decoration: none  |   underline   |     line-through

		a{
			color: red;
			text-decoration: none;
		}

背景属性

background-color     背景颜色

Background-position  left  |  right  |  center  |  top  | bottom  背景定位

Background-repeat    repeat(默认)  |  no-repeat |   repeat-x   |  repeat-y     背景平铺

background-image    背景图片

background-position: right;

方位值只写一个的时候,另外一个值默认居中。

background-position: right bottom;

写2个方位值的时候,顺序没有要求。

 background-position: 20px 30px;

写2个具体值的时候,第一个值代表水平方向,第二个值代表垂直方向。

Background-attachment   背景是否滚动   scroll  |  fixed

    background-attachment: scroll;

 

 background-attachment: fixed;

 背景属性连写

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
	.box{
		height: 100px;
		/*背景属性连写*/
		background: red url("btn_acomlete_1.png") no-repeat 20px 30px scroll;
	}
	</style>

</head>
<body>
	<div class="box"></div>

</body>
</html>

 连写的时候没有顺序要求,url为必写项。

猜你喜欢

转载自blog.csdn.net/baidu_29474379/article/details/86604861