CSS笔记+案例

一、CSS简介
1、什么是CSS

CSS,Cascading Style Sheet层叠样式表

是一组样式设置规则,用于控制页面的外观样式

2、为什么使用CSS
  • 实现内容与样式的分离,便于团队开发、
  • 样式复用,便于网站的后期维护
  • 页面的精确控制,让页面更精美
3、CSS作用
  • 页面外观美化
  • 布局和定位
二、基本用法
1、CSS语法
<head>
    <style>
        选择器{
            属性名:属性值;
            属性名:属性值;
        }
    </style>
</head>
  • 选择器:要修饰的对象

  • 属性名:修饰对象的哪个属性

  • 属性值:样式的取值

    HTML.CSS案例
    案例1 CSS语法
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
    	<style>
    	    p{
    	    	color:red;
    	    }
    	    h2{
    	    	color:blue;
    	    }
    	</style>
    </head>
    <body>
    	<p>CSS从入门到精通</p>
    	<h2>hahaha</h2>
    </body>
    </html>
    

    网页效果:
    在这里插入图片描述

2、CSS应用方式

也称为CSS引用方式,有三种方式:内部样式、行内样式、外部样式

2.1 内部样式

也称为内嵌样式,在页面头部通过style标签定义

对当前页面当中所有符合样式选择器的标签都起作用

2.2 行内样式

也称为嵌入样式,使用HTML标签的style属性定义

只对设置style属性的标签起作用

2.3 外部样式

使用单独的CSS文件定义,然后在页面中使用 link标签 或 @import指令 引入

  • 使用 link标签 链接外部样式文件(推存用法)
<link rel="stylesheet" type="text/css" href="css样式文件路径">

type属性可以省略

  • @import指令 导入外部样式文件
<style>   
    @import "CSS样式文件的路径"; 
    @import url(CSS样式文件的路径); 
</style
案例2 CSS应用样式
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    /*1.内部样式*/
	    p{
	    	color:red;
	    }
	</style>
	<!-- 外部样式  link标签链接样式文件 -->
	<!-- <link rel="stylesheet" href="text/css" href="style/hello.css"> -->
	<!-- 外部样式采用@import导入样式文件 -->
	<style>
	     /* @import "style/hello.css" */
	     @import url(style/hello.css);
	</style>
</head>
<body>
	<p>welcome to CSS!</p>
	<p>欢迎来到CSS课堂!</p>
    <!-- 2.行内样式 -->
	<h2 style="color:blue">WEB前端工程师</h2>
	<h2>JAVA开发工程师</h2>



	<div>哈哈</div>
	<div>呵呵</div>
</body>
</html>
div{
	color:green;
}

网页效果:
在这里插入图片描述

三、选择器
1、基础选择器
1.1标签选择器

也称为元素选择器,使用HTML标签作为选择器名称

以标签名作为样式应用的依据

1.2 类选择器

使用自定义名称,以 .点号 作为前缀,然后通过HTML标签的class属性调用类选择器
以标签的class属性作为样式应用的依据
注意事项:

  • 调用时不能添加 .点号
  • 同时调用多个类选择器时以 空格 分隔
  • 类选择器定义时,名称不能以数字开头
1.3 ID选择器

使用自定义名称,以 # 号作为前缀,然后通过HTML标签的id属性进行名称匹配
以标签的id属性作为样式应用的依据,一对一的关系

2.复杂选择器
2.1复合选择器

标签选择器和类选择器、标签选择器和ID选择器,一起使用

必须同时满足两个条件才能应用样式

2.2组合选择器

也称为集体声明

将多个具有相同样式

2.3 嵌套选择器

在某个选择器的内部再设置选择器,通过空格隔开

只有满足层次关系最里层的选择器所对应的标签才会应用样式

注意:使用 空格 时不区分父子还是后代,使用CSS3中新增的>时必须是父子关系才行

2.4伪类选择器

根据不同的状态显示不同的样工,一般用于 标签

四种状态:

  • :link 未访问的链接
  • :visited 已访问的链接
  • :hover 鼠标移动到链接上,即悬浮在链接
  • :active 选定的链接,被激活

注意:默认超链接为:蓝色,下划线

2.5 伪元素选择器

:first-letter:为第一个字符的样式

:first-line:为第一行添加样式

:before:在元素内容的最前面添加内容,需要配合content属性使用

:after:在元素内容的最后面添加内容,需要配合content属性使用

案例5 伪类选择器
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
/* 	    a:link{
	    	font-size:12px;
	    	color:black;
	    	text-decoration:none;
	    }
	    a:visited{
	    	font-size:15px;
	    	color:red;
	    }
	    a:hover{
	    	font-size:20px;
	    	color:blue;
	    }
	    a:active{
	    	font-size:40px;
	    	color:green;
	    } */
	    a:link,a:visited{
	    	font-size:13px;
	    	color:#666666;
	    	text-decoration:none;
	    }
	    a:hover,a:active{
	    	color:#ff7300;
	    	text-decoration:underline;
	    }
	    /* 普通标签也可以使用伪类选择器 */
	    p:hover{
            color:red;
	    }
	    p:active{
	    	color:blue;
	    }
	</style>
</head>
<body>
	<a href="02.css应用样式.html">豪华烤冷面</a>
	<p>CSS从入门到精通</p>
</body>
</html>

网页效果:
在这里插入图片描述

案例6 伪元素选择器
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    p:first-letter{
	    	color:red;
	    	font-size:30px;
	    }
	    p:first-line{
	    	background:yellow;
	    }
	    p:before{
	    	content:"嘿嘿";
	    }
	    p:after{
	    	content:"哈哈";
	    }
	</style>
</head>
<body>
	<p>hello world!</p>
	<hr>

	<p>
		hello world!<br>
		welcome to css!
	</p>
</body>
</html>

网页效果:
在这里插入图片描述

3.选择器的优先级
3.1优先级

行内样式>ID选择器>类选择器>标签选择器

原因:首先加载标签选择器,再加载类选择器,然后加载ID选择器,最后加载行内样式,后加载的样式会覆盖先加载的同名样式

3.2内外部样式加载顺序

就近原则

原因:按照书写的顺序依次加载,在同优先级的前提下,后加载的会覆盖先加载的同名样式,所以离得越近越优先

3.3!important

可以使用!important使某个样式有最高的优先级

案例7 选择器的优先级
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	   div{
	   	font-size:20px;
	   	color:red;
	   }
    .hello{
    	font-weight:bold;
    	color:blue;
    }
    #word{
    	text-decoration:underline;
    	color:green;
    }
    p{
    	color:red!important;
    }
	</style>
	<link rel="stylesheet" href="style/world.css">
</head>
<body>
	<div class="hello" id="world" style="color:pink;">CSS从入门到精通</div>
	<p>主讲:</p>
</body>
</html>

网页效果:
在这里插入图片描述

四、常用的CSS属性
1、字体属性

定义字体相关的样式

属性含义说明
font-size大小、尺寸可以使用多种单位
font-weight粗细
font-family字体名称
font-style样式
font
1.1 font-size

取值:

  • inherited继承,默认从父标签继承字体大小(默认值),所有CSS属性的默认值都是inherited

  • px像素 pixel

  • %百分比,相对于父标签字体大小的百分比

  • em倍数,相对于父标签字体大小的倍数

    HTML根元素默认字体大小为16px,也称为基础字体大小

1.2 font-weight

取值:

  • normal普通(默认)
  • bold粗体
  • 自定义400normal 700bold
1.3 font-family

是求系统中要安装指定的字体

一般建议写三种字体,首选、其次、备用、以逗号隔开

1.4 font-style

取值:

  • normal普通
  • italic斜体
1.5 font

简写属性:font:font-style|font-weight|font-size|font-family

必须按此顺序书写

案例8 字体相关的属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    div{
	    	font-size:30px;
	    }

	    p{
	    	/* font-size:20px; */
	    	/* font-size:80%; */
	    	font-size:2em;
	    }
	    span{
	    	font-size:1em;
	    }
	    span.hello{
	    	/* font-size:80%; */
	    	font-size:2em;
	    }
	    body{
	    	font-size:62.5%;
	    }
	    #ddd{
	    	font-size:3em;
	    }
	    ul li{
/* 	    	font-size:30px;
	    	font-weight:500;
	    	font-family: 华文行楷,黑体,宋体;
	    	font-style:normal; */
	    	font:italic normal 20px 黑体,楷体,宋体;
	    } 
	</style>
</head>
<body>
	<p>
	    css从入门到精通
	    <span>主讲:高教培训</span>
	</p>
	<span id="ddd">主讲:hector</span>
	<hr>

	<div>
	    我的div
	    <p>
	        css从入门到精通
	        <span>主讲:高教培训</span>
	    </p>

	</div>
	<hr>

	<span class="hello">主讲:高教培训</span>
    <hr>

    <ul>
    	<li>嘿嘿</li>
    </ul>
</body>
</html>

网页效果:
在这里插入图片描述

2、文本属性
属性含义说明
color颜色
line-height行高行之间的高度
text-align水平对齐方式取值:left、center、right
vertical-align垂直对齐方式取值:top、middle、bottom可用于图片和文字体的对齐方式
text-indent首行缩进
text-decoration文本修饰取值:underline、overline、line-through
text-transform字母大小写取值:lowercase、uppercase、capitalize首字母大写
letter-spacing字符间距
word-spacing单词间距只对英文有效
white-space空白的处理方式文本超出后是否换行显示,取值:nowrap
2.1 color

取值:四种写法

  • 颜色名称:使用英文单词(使用最多)
  • 16进制的RGB取值:#RRGGBB
#FFFFFF--->#FFF 白色
#000000--->#000 黑色
#FF0000--->#F00 红色
#00FF00--->#0F0 绿色
#0000FF--->#00F 蓝色
#CCCCCC--->#CCC 灰色
#FF7300--->无法描写

注意:不区分大小写

  • rgb函数:rgb(red,green,blue)

每个颜色的取值范围:[0,255]

rgb(255,0,0)---->红
rgb(0,255,0)---->绿
rgb(o,o,255)---->蓝
  • rgba函数:rgba(red,green,blue,alpha)

​ 可设置透明度,alpha的取值范围:[0,1]0—表示完全透明 1—完全不透明

rgba(255,0,0,1)------纯红色

rgba(255,0,0,0,5)--------红色的半透明

案例9 文本相关的属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	   p{
	   	color:red;
	   	/* background-color:#0F0; */
	   	/* background-color:rgb(0, 0, 255); */
	   	background-color:#54d3ce;
	   	line-height:50px;
	   	text-align: center;
	   }
	   img{
	   	vertical-align:middle;
	   }
	   div{
	   	text-indent:25px;
	   }
	   span{
	   	text-decoration:line-through;
	   	text-transform:capitalize;
	   	letter-spacing:3px;
	   	word-spacing:8px;
	   }
	   h3{
	   	width:300px;
	   	height:200px;
	   	background-color:#cccccc;
	   	white-space:nowrap;
	   	overflow:hidden;
	   }
	   


	</style>
</head>
<body>
	<p>welcome to CSS!</p>
	<p>welcome to CSS!</p>
	<p>welcome to CSS!</p>
	<p>welcome to CSS!</p>
	<p>welcome to CSS!</p>
	<hr>

	<img src="../images/qq.jpg" alt="">
	html和css很简单
	<hr>


	<div>&nbsp;&nbsp;&nbsp;&nbsp;hahahahahahahahahahahhahahahahah
	ahahahahahahahahaha
	hahahahahahahahah</div>
    <div>hahahahahahahahahahahhahahahahah
	ahahahahahahahahaha
	hahahahahahahahahwelcome to CSS!welcome to CSS!welcome to CSS!
	welcome to CSS!welcome to CSS!</div>
	<hr>


	<span>haha哈哈</span>
	<hr>


	<h3>welwelcome to CSS!welcome to CSS!welcome to CSS!
		welcome to CSS!
		welcome to CSS!welcome to CSS!welcome to CSS!
		welcome to CSS!welcome to CSS!come to CSS!</h3>


</body>
</html>

网页效果:
在这里插入图片描述

3.背景属性
属性含义说明
background-color背景颜色
background-image背景图片
background-repeat背景图片的重复方式
background-position背景图片的显示位置
background-attachment背景图片是否跟随滚动
background简写
3.1background-color

取值:transparent透明

3.2 background-image
  • 必须使用url的方式指定图片的路径

  • 如果是在css这个样式文件中使用相对路径,此时是相对于css文件,不是相对于html文件

    3.3 background-repeat

    取值:repeat(默认)、repeat-x、repeat-y、no-repeat

    3.4 background-position

    默认北京图片显示在左上角

取值:

  • 关键字:top、bottom、left、right、center

  • 坐标:左上角(0,0)坐标,向右为x的正方向,向下为y正方向

    CSS雪碧图,即CSS Sprites,也称为CSS精灵,是一种CSS合并技术

含义:将网页中许多非常小的图片整合到一张图片中,当访问该页面时只需要下载一次

原理:使用background-position进行背景定位,使用坐标精确的定位出背景图片的位置

3.5 background-attachment

取值:scroll默认、fixed固定不变

3.6 background

简写属性:background:background-color|background-image|background-repeat|background-position

以空格隔开,书写顺序没有要求

案例10 背景属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
    <style>
/*         div{
        	color:red;
        	background-color:#cccccc;
        	background-image:url(../images/bg.gif);
        	background-color:transparent;
        	background-image:url(../images/heihei.gif);
        } */
    </style>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<div>
		<p>welcome to css welcome to css welcome to css</p>
		<p>welcome to css welcome to css welcome to css</p>
		<p>welcome to css welcome to css welcome to css</p>
		<p>welcome to css welcome to css welcome to css</p>
		<p>welcome to css welcome to css welcome to css</p>
	</div>
    <hr>

    <p class="icon"></p>
    购物车
</body>
</html>

网页效果:
在这里插入图片描述

案例11 列表属性
4.列表属性
属性含义说明
list-style-type设置列表前的标记
list-style-image将图像作为列表前的标记
list-style-position设置标记的位置取值:outside(默认)、inside
list-style简写
4.1 list-style-type

取值:none、disc、circle、square、decimal

此时不再区分有序列表还是无序列表,只要设置列表前的标记就可以了

4.2 list-style

简写属性:list-style:list-style-type|list-style-image|list-style-position

书写顺序没有要求

案例11 列表属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
/* 	    li{
	    	list-style-type:decimal;
	    } */
	    .first{
	    	list-style-type:decimal;
	    }
	    .second{
	    	/* list-style-type:square; */
	    	list-style-image:url(../images/male.gif);
	    }
	    .third{
	    	list-style-type:circle;
	    	list-style-position:inside;
	    }
	    .fourth{
	    	/* list-style:square url(../images/female.gif) inside; */
	    	list-style:none;
	    }
	    .nav li{
	    	list-style:none;
	    	float:left;
	    	width:50px;
	    }
	    
	    	
	    
	</style>
</head>
<body>
	<ul>
		<li class="first">hello</li>
		<li class="second">hello</li>
		<li class="third">hello</li>
		<li class="fourth">hello</li>
	</ul>
	<hr>

	<nav class="nav">
		<ul>
			<li>新闻</li>
			<li>地图</li>
			<li>视频</li>
			<li>贴吧</li>
		</ul>
	</nav>
</body>
</html>

网页效果:

在这里插入图片描述

5.表格属性

border-collapse:表格中相邻的边框是否合并(折叠)为单一边框
取值:separated(默认) collapse

6.使用LiveReload

可以实现当保存页面文件时实时刷新浏览器

步骤:

1、在Chrome浏览器中安装 LiveReload 扩展程序

将livereload.rar文件解压–>在chrome浏览器的右上角找“…”–>更多工具–>扩展程序–>提示:需要打 开开发者模式–>加载已解压的扩展程序–>注意后项的操作 勾选"允许访问所有网站留存数据"

2、在Sublime中安装LiveRelad插件 将 LiveRelad-sublime.rar 解压放到sublime的插件目录中(packages)中

3、配置sublime中的LiveRelad插入 preference–>packages settings–>LiveReload–>settings - default

"enabled_plugins": [
         "SimpleReloadPlugin" 
         "SimpleRefresh"    
     ]在浏览器中启用LiveReload 先打开要访问的页面,然后点击浏览器地址栏右边的黑色圆圈,当中心的小圆圈变成实心时表示已启 用 

4、在浏览器中启用LiveReload 先打开要访问的页面,然后点击浏览器地址栏右边的黑色圆圈,当中心的小圆圈变成实心时表示已启 用

5、在sublime中启用LiveReload 按ctrl+Shift+P–>搜索livereload,选择enable–>搜索simple reload:选择enable

案例12 表格属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    table{
	    	width:500px;
	    	border:1px solid pink;
	    	border-collapse:collapse;
	    }
	    td{
	    	border:1px solid pink;
	    }
	</style>
</head>
<body>
	<!-- table>(tr>td{td$}*4)*5</-->
	<table  cellpadding="0" cellspacing="0">
		<tr>
			<td>td1</td>
			<td>td2</td>
			<td>td3</td>
			<td>td4</td>
		</tr>
		<tr>
			<td>td1</td>
			<td>td2</td>
			<td>td3</td>
			<td>td4</td>
		</tr>
		<tr>
			<td>td1</td>
			<td>td2</td>
			<td>td3</td>
			<td>td4</td>
		</tr>
		<tr>
			<td>td1</td>
			<td>td2</td>
			<td>td3</td>
			<td>td4</td>
		</tr>
		<tr>
			<td>td1</td>
			<td>td2</td>
			<td>td3</td>
			<td>td4</td>
		</tr>
	</table>
</body>
</html>

网页效果:
在这里插入图片描述

五、 盒子模型
1.简介

解决了网页布局的问题,是网页布局的基础,将页面中所有元素均看作是一个盒子,盒子都包含以下几个属性:

  • width:宽度
  • height:高度
  • berder:边框
  • padding:内边距
  • margin:外边距
2.盒子模型
2.1 border

盒子边框

分为4个方向:

上top、右right、下bottom、左left

border-top、border-right、border-bottom、border-left

每个边框都包含三种样式:

颜色color、粗细width、样式style

border-top-color、border-top-width、border-top-style

border-right-color、border-right-width、border-right-style

border-bottom-color、border-bottom-width、border-bottom-style

border-left-color、border-left-width、border-left-style

样式的取值:

solid实线、dashed虚线、dotted点线、double双线、inset内嵌3D线、outside外嵌3D线

  • 按方向写

    border-top,boder-right,border-bottom,border-left

书写顺序:border-方向:width、style、color

  • 按样式简写

    border-color、border-width、border-style

书写顺序

border-样式:border-top-color,border-right-color,border-bottom-color,border-left-color

必须按顺时针方向书写,同时可以缩写

​ border-width:2px-------------->四个边框宽度均为2px

  • border-width:2px 4px---->上下一样,左右一样
  • border-width:2px 3px 4px

规则:如果省略,则认为上下一样,左右一样

  • 终极简写

​ 如果四个边样式完全相同,border-width style color

2.2 padding

表示盒子的内边距,即内容与边框之间的距离

有四个方向:也可以简写,按顺时针书写

默认:上下一样,左右一样

注:左右冲突以左为准,上下冲突以上为准

2.3 margin

表示盒子的外边距,即盒子与盒子之间的距离

同样也分为四个方向,也可以简写(按顺时针方向,默认上下一样,左右一样)

居中对齐:

/* 元素的水平居中 */
/* 1.块级元素的水平居中 */  
margin:0 auto;  
/* 提示:块级元素必须指定宽度 */      
/* 2.文本的水平居中 */     
text-align:center;     
/* 3.垂直居中,将height和line-height设置为相同 */        height:100px;    
line-height:100px;
其他
3.1 元素所占空间

页面中的元素实际所占空间

  • 宽度=width+左右padding+左右border+左右margin
  • 高度=height+上下padding+上下border+上下margin
案例13 盒子模型
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    p{
	    	width:250px;
	    	background:#ccc;
	    }
	    .first{
/* 	    	border-top-color:red;
	    	border-top-width:1px;
	    	border-top-style:solid;
	    	border-right-color:blue;
	    	border-right-width:2px;
	    	border-right-style:dashed;
	    	border-bottom-color:green;
	    	border-bottom-width:4px;
	    	border-bottom-style:dotted;
	    	border-left-color:gray;
	    	border-left-width:6px;
	    	border-left-style:dotted;
 */
/*             border-color:red yellow blue green;
            border-width:1px 2px 4px 6px;
            border-style:solid dashed dotted double; */
/*             border-color:red green pink;
            border-width:1px 2px 4px 6px;
            border-style:solid dashed dotted double; */

            border:1px dotted red;
	    }
	    .second{
/* 	    	padding-top:15px;
	    	padding-left:10px;
	    	padding-bottom:20px;
	    	padding-right:30px; */
/* 	    	padding:1px 2px 4px 6px; */

	    	padding:5px;
	    }
	</style>
</head>
<body>
	<p class="first">welcome to html</p>
	<p class="second">welcome to css</p>
	<p class="third">welcome to java</p>
</body>
</html>

网页效果:
在这里插入图片描述

3.2 盒子属性的默认值

不同标签的盒子属性默认值可能不同,需要自己进行设置

 body,ul,ol,dl,li,p,h1,h2,h3,h4,h5,h6,form{
   margin:0;
   padding:0;
}
案例14 盒子模型默认值
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    /* body默认margin不为0 */
	    body{
	    	margin:0;
	    	padding:0;
	    }
	    /* p默认的margin也不为0 */
	    p{
	    	margin:0;
	    }
	    /* ul默认的margin和padding都不为0 */
	    ul{
	    	list-style:none;
	    	margin:0;
	    	padding:0;
	    }
	    body,ul,ol,dl,li,p,h1,h2,h3,h4,h5,h6,form{
	    	margin:0;
	    	padding:0;
	    }

	</style>
</head>
<body>
	welcome to css
	<p>hello world</p>
	<!-- ul>li{hello$}*3 -->
	<ul>
		<li>hello1</li>
		<li>hello2</li>
		<li>hello3</li>
	</ul>
</body>
</html>

网页效果:
在这里插入图片描述

3.3 外边距的合并

也成为外边距的折叠,指的是两个块级元素垂直外边距相遇时,它们将合并为一个外边距,合并后的外边距为其中较大的值

两种情况

  • 当一个元素出现在另一个元素的上面时,第一个的下边距与第二个上边距会发生合并
  • 当一个元素包含在另一个元素中时,并且没有内边距或边框把外边距分开时,两个元素的上边距会发生合并

外边距的合并的好处,让排版在视觉上更美观

案例15 外边距的合并
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    div{
	    	width:50px;
	    	height:50px;
	    	background:#ccc;

	    }
	    .div1{
	    	margin-bottom: 20px;
	    }
	    .div2{
	    	margin-top:30px;
	    }
	    .div3{
	    	width:100px;
	    	height:100px;
	    	background:blue;
	    	margin-top:20px;
	    	/* border:1px solid red; */
	    	padding:1px;
	    }
	    .div4{
	    	margin-top:10px;
	    }
	    p{
	    	margin:20px 0;
	    }
	</style>
</head>
<body>
	<div class="div1">div1</div>
	<div class="div2">div2</div>
	<hr>

	<div class="div3">
		<div class="div4"></div>
	</div>
	<hr>

	<!-- p{p$}*8 -->
	<p>p1</p>
	<p>p2</p>
	<p>p3</p>
	<p>p4</p>
	<p>p5</p>
	<p>p6</p>
	<p>p7</p>
	<p>p8</p>
</body>
</html>

网页效果:
在这里插入图片描述

六、定位方式
1.简介

通过position属性实现对元素的定位,有四种定位方式

常用取值:

取值含义说明
static默认值按照常规的文档流进行显示
relative相对定位相对于标签原来的位置进行的定位
absolite绝对定位相对于第一个非static的父标签的定位
fixed固定定位相对于浏览器窗口进行定位

设置定位i方式后,还要设置定位属性(偏移量):top、

2.相对定位

先设置元素的position属性为relative,然后再设置偏移量

3.绝对定位

先设置父标签为非static定位,然后设置元素的position属性为absolute,最后再设置偏移量

  • 一般来说都会将父标签设置为非static定位
  • 如果父标签不是非static,则会相对于浏览器窗口进行定位
  • 如果设置元素位绝对定位后,元素会浮到页面上方
4.固定定位

先设置元素的pposition属性为fixed,然后再设置偏移量

设置元素为固定定位后,元素会浮动在页面上方

5.z-index

设置元素定位方式后,元素会浮在页面上方,此时可以通过 属性设置优先级,控制元素的堆叠顺序,取值为数字,值越大优先级越高,默认为auto(大多数浏览器默认为0)

注意:只能给非static定位的元素设置z-index属性

案例16 定位方式
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    #container{
	    	width:800px;
	    	border:1px solid #000;
	    	position:relative;
	    }
	    .div1,.div2,.div3,.div4{
	    	width:100px;
	    	height:50px;
	    }
	    .div1{
	    	background:red;
	    	/* 默认为static */
	    	/* position:static; */
	    	position:relative;
	    	top:20px;
	    	left:50px;
	    	z-index:-5;
	    }
	    .div2{
	    	background:blue;
	    	position:absolute;
	    	left:100px;
	    	bottom:50px;
	    	z-index:-8;
	    }
	    .div3{
	    	background:green;
	    	position:fixed;
	    	bottom: 50px;
	    	left:100px;
	    }
	    .div4{
	    	background:cyan;
	    }
	</style>
</head>
<body>
	<div id="container">
		<!-- div.div${div$}*4 -->
		<div class="div1">div1</div>
		<div class="div2">div2</div>
		<div class="div3">div3</div>
		<div class="div4">div4</div>
	</div>
</body>
</html>

网页效果:
在这里插入图片描述

七、其他CSS属性
1.浮动和清除
1.1 浮动属性

通过float属性来实现元素的浮动,可以让块级元素脱离常规的文档流,可以向右或者向左进行移动,在同一行显示,如果一行显示不下,则会换行显示

常用取值:

  • left左浮动
  • right右浮动
  • none不浮动,默认值

设置float属性后,元素会浮在页面上层,此时父容器无法计算自己的尺寸,如果我们还想显示父容器通常会在末尾添加一个清除了float属性的空的div来解决

1.2 清除属性

通过clear属性来实现清除,设置元素的哪一侧不允许有浮动元素

常用取值:

  • left左侧不允许出现浮动元素
  • right右侧不允许出现浮动元素
  • both两侧不允许出现浮动元素
  • none允许两侧出现浮动元素,默认

结论:

  • 对于非浮动元素,两边都可以设置清除(常用)

  • 对于浮动元素,向哪边浮动,就只能设置哪边的清除

    案例17 浮动和清除
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
    	<style>
    	    #container{
    	    	/* width:800px; */
    	    	border:1px solid #000;
    	    	
    	    }
    	    .div1,.div2,.div3,.div4{
    	    	width:300px;
    	    	height:50px;
    	    }
    	    .div1{
    	    	background:red;
    	    	float:left;
    
    	    }
    	    .div2{
    	    	background:blue;
    	    	float:left;
    	    	clear:left;
    
    	    }
    	    .div3{
    	    	background:green;
    	    	float:left;
    
    	    }
    	    .div4{
    	    	background:cyan;
    	    	float:left;
    	    }
    	    .clr{
    	    	clear:both;
    	    }
    	</style>
    </head>
    <body>
    	<div id="container">
    		<!-- div.div${div$}*4 -->
    		<div class="div1">div1</div>
    		<div class="div2">div2</div>
    		<div class="div3">div3</div>
    		<div class="div4">div4</div>
    		<div class="clr"><!-- aaa --></div>
    	</div>
    	welcome to CSS
    </body>
    </html>
    

    网页效果:
    在这里插入图片描述

    案例18 练习
    <!DOCTYPE html>
    <html lang="en">
    <head>
     <meta charset="UTF-8">
     <title>Document</title>
     <style>
      body{
       margin:0;
       padding:0;
      }
      #container{
       width:960px;
       margin:0 auto;
       border:2px solid #0f0;
      }
      .div1,.div2,.div3,.div4{
       width:200px;
       height:180px;
       float:left;
       margin:5px;
       border:5px outset #ff7300;
       padding:10px;
      }
      #container img{
       width:100%;
       height:100%;
      }
      #container .clr{
       clear:both;
      }
     </style>
    </head>
    <body>
    <div id="container">
       <div1 class="div1"><img src="../images/adv1.jpg" alt=""></div1>
       <div2 class="div2"><img src="../images/adv2.jpg" alt=""></div2>
       <div3 class="div3"><img src="../images/adv3.jpg" alt=""></div3>
       <div4 class="div4"><img src="../images/adv4.jpg" alt=""></div4>
       <div class="clr"></div>
    </div>
     <p>welcome to css</p >javascript
    </body>
    </html>
    

    网页效果:
    在这里插入图片描述

2.元素的显示和隐藏
2.1 display

通过display属性设置元素是否显示,以及是否独占一行

常用取值:

取值含义说明
none不显示
inline显示为内联元素,行级元素的默认值将块级元素变为行级元素,不再独占一行
block显示为块级元素,块级元素的默认值将行级元素变为块级元素,独占一行
inline-block显示为内联元素,但是可以设置宽和高在inline基础上允许设置宽度和高度

注意:

行级标签是无法设置宽和高的,可以为行级元素display:inline-block,然后就可以设置宽和高了

2.2visibility

也可以通过visibility属性设置元素的显示和隐藏

常用属性:

取值含义说明
visible显示
hidden隐藏
2.3 区别
  • display隐藏时不再占据页面中的空间,后面的元素会占用其位置
  • visibility隐藏时会占用页面中的空间,位置还保留在页面中,只是不显示
案例19 元素的显示和隐藏
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    .div1{
	    	width:100px;
	    	height:100px;
	    	background:blue;
	    	/* display:none; */
	    	display:inline;
	    	/* 将块级元素转换为行级元素进行显示,失去了块级元素独占一行的特点 */
	    }
	    span{
	    	width:100px;
	    	height:100px;
	    	background:yellow;
	    	display:block;
	    	/* 将行级元素转换为块级元素进行显示,独占一行进行显示 */
	    	/* 既对行级元素设置宽和高,还想让他在一行显示 */
	    	/* 方法一:设置浮动 */
	    	/* float:left; */
	    	display:inline-block;

	    }
	    i{
	    	display:inline-block;
	    	width:100px;
	    	height:100px;
	    	background:red;
	    }
	    p{
	    	width:50px;
	    	height:50px;
	    	background:red;
	    }
	    .p1{
	    	display:none;
	    	visibility:hidden;
	    }
	    #login{
	    	display:inline-block;
	    	width:100px;
	    	text-decoration:none;
	    	background:rgba(255, 0, 0, 0.7);
	    	color:#fff;
	    	padding:10px;
	    	text-align: center;
	    	border-radius:8px;
	    }
	    #login:hover{
	    	background:rgba(255, 0, 0, 5);
	    }
	</style>
</head>
<body>
	<div class="div1">dv1</div>
	<span>span1</span>
	<i>呵呵</i>
	<hr>

	<p class="p1">p1</p>
	<p class="p2">p2</p>
	<hr>

	<a href="javascript:alert('哈哈')" id="login">&nbsp;&nbsp;&nbsp;&nbsp;</a>
</body>
</html>

网页效果:
在这里插入图片描述

3.轮廓属性
3.1简介

轮廓outline,用于在元素的周围绘制一个轮廓,位于border的外围,可以突出显示元素

3.2基本用法

常用属性:

  • outline-width:轮廓宽度
  • outline-color:轮廓颜色
  • outline-style:轮廓样式
  • outline简写

在浏览器中,当鼠标单击或使用TAB键让一个表单或链接

优点:可以提高使用表单的用户体验

缺点:有时会影响美观

3.3 outline和border区别
  • border可应用于所有的html元素,而outline主要用于表单元素、超链接元素
  • 当元素获得焦点时会自动出现outline轮廓,当失去焦点会自动消失,这是浏览器的默认行为
  • outline不影响元素的尺寸和位置,但border会影响
案例20 轮廓属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    span{
	    	border:2px solid red;
	    	outline-width:4px;
	    	outline-color:blue;
	    	outline-style: dashed;
	    }
	    .usrname{
	    	border:1px solid red;
	    	outline:none;   /* 用户名文本框不设置轮廓 */
	    	padding-left:3px;
	    	width:80px;
	    }
	    .email{
	    	border:0;
	    	outline:0;
	    	border-bottom: 1px solid black;
	    }
	    .btnsubmit{
	    	border:0;
	    	padding:5px;
	    	width:100px;
	    }
	    .mydiv{
	    	width: 100px;
	    	height: 50px;
	    	background:#ccc;
	    	border:2px solid red;
	    	outline:2px solid red;

	    }
	</style>
</head>
<body>
	<span>welcome to css</span>
	<hr>

	用户名<input type="text" class="usrname">
	<hr>

	<a href="#">CSS</a>
	<hr>

	邮箱<input type="text" class="email">
	<input type="submit" value="提交" class="btnsubmit">
	<hr>

	<div class="mydiv">div</div>
</body>
</html>

网页效果:
在这里插入图片描述

4.其他属性
4.1宽高相关
  • max-width:设置元素的最大宽度
  • max-height:设置元素的最大高度
  • min-width:设置元素的最小宽度
  • min-height:设置元素的最小高度
4.2overflow属性

当元素内容溢出时该如何处理

常用取值:

  • visible溢出时可见,显示在元素外,默认值
  • hidden溢出部分不可见(常用)
  • scroll无论是否出现溢出始终出现滚动条
  • auto溢出时自动出现滚动条
4.3 cursor属性

用于设置光标的形状

常用属性:

  • default默认光标,一般为箭头
  • pointer手形,光标移动超链接上时一般显示为手形
  • move表示可移动
  • text表示文本
  • wait表示程序正忙
  • help表示帮助
案例21 其他属性
!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    p{
	    	border:1px solid red;
	    	min-width:500px;
	    }
	    div{
	    	border:1px solid blue;
	    	width:300px;
	    	height:80px;
	    	overflow:auto;
	    }
	    span{
	    	cursor:help;
	    }
	</style>
</head>
<body>
	<p>
		welcome to css welcome to css welcome to css welcome to css
		welcome to css welcome to css welcome to css welcome to css
		welcome to css welcome to css welcome to css welcome to css
		welcome to css welcome to css welcome to css welcome to css
	</p>
	<hr>


	<div>
	    welcome to css welcome to css welcome to css welcome to css
		welcome to css welcome to css welcome to css welcome to css
		welcome to css welcome to css welcome to css welcome to css
		welcome to css welcome to css welcome to css welcome to css
	</div>
	<hr>

	<span>光标形状</span>
</body>
</html>

网页效果:
在这里插入图片描述

八 页面布局
1.简介

常见的页面布局:

  • 表格布局
  • div布局
2.表格布局
2.1简介

不适用于复杂布局,仅用于简单、有规则的结构

定位相对准确,与浏览器基本无关,适用于简单分隔

2.2 用法

table常用样式的属性

  • border在表格外围设置边框
  • border-spacing设置单元格之间的距离(相当于table标签中的cellspacing属性,即间距)
  • border-collapse表格中相邻边框是否合并,取值:separate、collapse

th/td常用样式属性:

  • border为单元格设置边框
  • padding设置单元格的内边距(相当于table标签中的cellpadding属性,边距)
3.div布局

定位绝对准确,使用非常灵活,适用于复杂的布局方式

案例22 表格属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	    .hello{
	    	/* width: 600px; */
	    	width:80%;
	    	border:1px solid #ccc;
	    	border-spacing:0;
	    	border-collapse:collapse;
	    }
	    .hello th,.hello td{
	    	border:1px solid #ccc;
	    	padding:5px;
	    }
	</style>
</head>
<body>
	<!-- table>(thead>tr>th{th$}*4)+tbody>(tr>td{td$}*4)*6 -->
	<table class="hello">
			<thead>
				<tr>
					<th>th1</th>
					<th>th2</th>
					<th>th3</th>
					<th>th4</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>td1</td>
					<td>td2</td>
					<td>td3</td>
					<td>td4</td>
				</tr>
				<tr>
					<td>td1</td>
					<td>td2</td>
					<td>td3</td>
					<td>td4</td>
				</tr>
				<tr>
					<td>td1</td>
					<td>td2</td>
					<td>td3</td>
					<td>td4</td>
				</tr>
				<tr>
					<td>td1</td>
					<td>td2</td>
					<td>td3</td>
					<td>td4</td>
				</tr>
				<tr>
					<td>td1</td>
					<td>td2</td>
					<td>td3</td>
					<td>td4</td>
				</tr>
				<tr>
					<td>td1</td>
					<td>td2</td>
					<td>td3</td>
					<td>td4</td>
				</tr>
			</tbody>
		</table>	

</body>
</html>

网页效果:
在这里插入图片描述

3.1 简单布局

两种形式:

  • 1:1:1 布局
  • 1:2/3:1布局
案例23 简单布局1
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="css/style1.css">
</head>
<body>
	<div id="container">
		<header class="header">
			header
		</header>
		<article class="main">
			main
		</article>
		<footer class="footer">
			footer
		</footer>
	</div>
</body>
</html>

@charset "utf-8";
body{
	margin:0;
	padding:0;
}
#container{
	width: 980px;
	border:1px solid #ccc;
	margin:0 auto;
}
#container .header{
	width: 100%;
	height: 80px;
	background:red;

}
#container .main{
	width: 100%;
	height: 600px;
	background:blue;
	margin:10px 0;
}
#container .footer{
	width: 100%;
	height: 120px;
	background:green;
}


网页效果:
在这里插入图片描述

案例24 简单布局2
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="css/style2.css">
</head>
<body>
<div id="container">
	<header class="header">
		header
	</header>
	<article class="wrapper">
<!-- 	    <aside>
	    	left aside
	    </aside> -->
	    <section class="main">
	    	main
	    </section>
	    <aside>
	    	right aside
	    </aside>
		
	</article>
	<footer class="footer">
		footer
	</footer>



</div>

</body>
</html>
@charset "utf-8";
body{
	margin:0;
	padding:0;
}
#container{
	width: 980px;
	border:1px solid #ccc;
	margin:0 auto;
}
#container .header{
	width: 100%;
	height: 80px;
	background:red;


}
#container .wrapper{
	width: 100%;
	height: 600px;
	background:blue;
	margin:10px 0;

}
#container .wrapper .main{
	background:cyan;
	width: 760px;
	height: 600px;
	float:left;
	margin:0 10px;
}
#container .wrapper aside{
	background:pink;
	width: 200px;
	height: 400px;
	float:left;
}
#container .footer{
	width: 100%;
	height: 120px;
	background:green;
}

网页效果:
在这里插入图片描述

style3.css
@charset "utf-8";
/* 	    父容器
	    1.溢出时隐藏
	    2.设置容器的内边距padding,用来空出位置,放置左右侧边栏
 */ 
#wrapper{
	overflow:hidden;   /*溢出时隐藏*/
	/* 实现左侧边栏和右侧边栏 */
	padding:0 200px; /* 左右分别空出200px用于放置左、右侧边栏 */
	min-width:300px;
	border:1px solid #ccc;
}
/*
    主体:
    1.宽度自适应
    2.设置一个浮动布局
 */

#wrapper .main{
	width: 100%;
	height: 300px;
	background:green;
	float:left;
}
#wrapper aside{
	width: 190px;
	height: 300px;
	background:blue;
	float:left;
	position:relative;
}
#wrapper .left{
	margin-left:-100%;
	left:-200px;
}
#wrapper .right{
	margin-left: -190px;
	right:-200px;
}

网页效果:
在这里插入图片描述

3.2 圣杯布局

页面结构,两边的边栏宽度固定,中间的主体在一定范围内可自适应,主体应被优先加载,一般防止页面缩放影响浏览,都会为页面设置一个最小宽度

案例26 圣杯布局2
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="css/style4.css">
</head>
<body>
	<div id="container">
		<header class="header">
			header
		</header>
		<article id="wrapper">
			<section class="main">
				main
			</section>
			<aside class="left">
				left
			</aside>
			<aside class="right">
				right
			</aside>
		</article>
		<footer class="footer">
			footer
		</footer>
	</div>
</body>
</html>
style4.css
@charset "utf-8";
body{
	margin:0;
	padding:0;
}
#container{
	margin:0 auto;
}
#container .header{
	width: 100%;
	height: 80px;
	background:red;
}
#container #wrapper{
	overflow:hidden;
	padding:0 200px;
    min-width:300px;
    margin:10px 0;
}
#container .main{
	width: 100%;
	height: 400px;
	background:blue;
	float:left;

}
#container aside{
	float:left;
	width: 190px;
	height: 300px;
	background:cyan;
	position:relative;
}
#container .left{
	margin-left: -100%;
	left:-200px;
}
#container .right{
	margin-left:-190px;
	left:200px;
}
#container .footer{
	width: 100%;
	height: 150px;
	background:green;
}

网页效果:
在这里插入图片描述

3.3双飞翼布局

源自淘宝的团队UED(用户体验设计团队)

双飞翼布局和圣杯布局要实现的效果是相同的,只是思路不同

圣杯布局和双飞翼布局的区别

  • 双飞翼比圣杯布局多创建一个div
  • 双飞翼布局不用设置内边距padding和相对定位,也不用设置偏移量
  • 双飞翼布局使用的margin,圣杯布局使用padding

实际开发中建议使用css3中新增的flex弹性盒子布局,更简单

案例27 双飞翼布局
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="css/style5.css">
</head>
<body>
	<div id="container">
        <header class="header">
        	header
        </header>
        <article class="wrapper">
        	<section class="main">
        		<div class="main-wrapper">
        			main
        		</div>
        	</section>
        	<aside class="left">
        		left
        	</aside>
        	<aside class="right">
        		right
        	</aside>
        </article>
        <footer class="footer">
        	footer
        </footer>
	</div>
</body>
</html>
style5.css
@charset "utf-8";
body{
	margin:0;
	padding:0;
}
#container{
    margin:0 auto;
}
#container .header{
	width: 100%;
	height: 80px;
	background:red;
}
#container .wrapper{
	overflow:hidden;
	min-width:300px;
	margin:10px 0;
}
#container .main{
	width: 100%;
	float:left;
}
#container .main-wrapper{
	background:pink;
	height: 400px;
	margin:0 200px;
}
#container aside{
    float:left;
    width: 190px;
    height: 300px;
    background:cyan;
}
#container .left{
	margin-left: -100%;
}
#container .right{
	margin-left:-190px;
}
#container .footer{
	width: 100%;
	height: 150px;
	background:green;
}

网页效果:
在这里插入图片描述
百度网盘:
链接:https://pan.baidu.com/s/16HzCEEtuRoyr6KRBvysjyw
提取码:j66d

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值