CSS
1、什么是CSS
Cascading Style Sheet层叠级联样式表
CSS:表现(美化网页)
字体,颜色,边距,高度,宽度,背景图片,网页定位,网页浮动
1.1、发展史
CSS1.0
CSS2.0 DIV(块)+CSS,HTML与CSS结构分离的思想
CSS2.1 浮动,定位
CSS3.0 圆角,阴影,动画…浏览器兼容性
1.2、快速入门
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>第一个css</title>
<!-- 规范,<style> 可以编写css代码,每一个声明,最好使用分号结尾
语法:
选择器{
声明1:
声明2:
声明3:
}
-->
<style>
h1 {
color: red;
}
</style>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>我是标题</h1>
</body>
</html>
推荐写法:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5D5vBZrm-1593351786860)(E:\许浩文件备份\笔记\web\image-20200621151525452.png)]
css的优势:
- 内容和表现分离
- 网页结构表现统一,可以实现复用
- 样式十分的丰富
- 建议使用独立于html的css文件
- 利用SEO,容易被搜索引擎收录
1.3、CSS的3种导入方式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内部样式</title>
<!-- 内部样式 -->
<style type="text/css">
h1{
color: yellow;
}
</style>
<!-- 外部样式 -->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!-- 优先级:就近原则 -->
<!-- 行内样式:在标签元素中,编写一个style属性,编写样式即可 -->
<h1 style="color: red">我是标题</h1>
</body>
</html>
2、选择器
作用:选择页面上的某一个或者某一类元素
2.1、基本选择器
1.标签选择器:选择一类标签 标签{}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
/*标签选择器,会选择到页面上所有的这个标签的元素*/
h1{
color: #a12124;
background: #3cbda6;
border-radius: 24px;
}
p{
font-size:80px;
}
</style>
</head>
<body>
<h1>学css</h1>
<h1>学css</h1>
<p>加油</p>
</body>
</html>
2.类选择器:所有class属性一致的标签,跨标签 .class{}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
/*类选择器格式 .class的名称{}
好处,可以多个标签归类,同一个class,可以复用
*/
.xuhao{
color: #3748ff;
}
.yxx{
color: #a24fff;
}
</style>
</head>
<body>
<h1 class="xuhao">学css</h1>
<h1 class="yxx">学css</h1>
<p class="xuhao">加油</p>
</body>
</html>
3.Id选择器:全局唯一 #id名{}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
/* id选择器 :id必须保证全局唯一!
#id名称{}
优先级:
不遵循就近原则,固定的
id选择器 > class 选择器 > 标签选择器
*/
#xuhao{
color: #3748ff;
}
.yxx{
color: #a24fff;
}
h1{
color: #2d1dc1;
}
</style>
</head>
<body>
<h1 id="xuhao">学css</h1>
<h1 class="yxx">学css</h1>
<h1 class="yxx">加油</h1>
<h1>标题</h1>
</body>
</html>
**优先级:**id > class > 标签
2.2、层次选择器
-
后代选择器:在某个元素的后面 祖爷爷 爷爷 爸爸 你
/*后代选择器*/ body p{ background: red; }
-
子选择器,一代,儿子
/*子选择器*/ body>p{ background: red; }
-
相邻兄弟选择器
/*相邻兄弟选择器*/ .active+p{ background: red; }
-
通用选择器
/*通用兄弟选择器,当前选择元素的向下的所有兄弟元素*/ .active~p{ background: red; }
2.3、结构伪类选择器
/*ul的第一个子元素*/
ul li:first-child {
background: red;
}
/*ul的最后一个子元素*/
ul li:last-child {
background: blue;
}
p:nth-child(1) {
background: yellow;
}
/*选择父元素下的p元素的第二个类型,不会被其他类型所阻碍*/
p:nth-of-type(2) {
background: buttonface;
}
2.4、属性选择器
标签名[属性名=属性值]
=:绝对等于
*=:包含等于
^=:以这个开头
$=:以这个结尾
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: blue;
text-align: center;
color: red;
text-decoration: none;
margin-right: 5px;
line-height: 50px;
}
/*标签名[属性名=属性值
=:绝对等于
*=:包含等于
^=:以这个开头
$=:以这个结尾
]*/
/*a[id]{*/
/*background: green;*/
/*}*/
/*a[id=first]{*/
/*background: fuchsia;*/
/*}*/
/*class中有links的元素*/
/*a[class*="links"]{*/
/*background: yellow;*/
/*}*/
/*选中href中以http开头的元素*/
/*a[href^=http]{*/
/*background: yellow;*/
/*}*/
/*选择以jpg结尾的元素*/
a[href$=jpg]{
background: red;
}
</style>
</head>
<body>
<p class="demo">
<a href="http:www.baidu.com" class="links item first" id="first">1</a>
<a href="" class="links item active" target="_blank" title="test">2</a>
<a href="images/123.html" class="links item">3</a>
<a href="images/123.png" class="links item">4</a>
<a href="images/123.jpg" class="links item">5</a>
<a href="abc" class="links item">6</a>
<a href="/a.pdf" class="links item">7</a>
<a href="/abc.pdf" class="links item">8</a>
<a href="abc.doc" class="links item">9</a>
</p>
</body>
</html>
3、美化网页元素
3.1、为什么要美化网页
- 有效的传递页面信息
- 美化网页,页面漂亮,才能吸引用户
- 凸显页面的主题
- 提高用户的体验
**span标签:**重点要突出的字,使用span套起来
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#title1{
font-size: 50px;
}
</style>
</head>
<body>
欢迎学习<span id="title1">CSS</span>
</body>
</html>
3.2、字体样式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!--
font-family:字体
color:字体颜色
font-size:字体大小
font-weight:字体粗细
-->
<style type="text/css">
body{
font-family: 楷体;
color: red;
}
h1{
font-size: 50px;
}
.p1{
font-weight: bold;
}
</style>
</head>
<body>
<h1>故事结束</h1>
<p class="p1">
《小王子》是法国作家安托万·德·圣·埃克苏佩里于1942年写成的著名儿童文学短篇小说。本书的主人公是来自外星球的小王子。书中以一位飞行员作为故事叙述者,讲述了小王子从自己星球出发前往地球的过程中,所经历的各种历险。作者以小王子的孩子式的眼光,透视出成人的空虚、盲目,愚妄和死板教条,用浅显天真的语言写出了人类的孤独寂寞、没有根基随风流浪的命运。同时,也表达出作者对金钱关系的批判,对真善美的讴歌
</p>
</body>
</html>
3.3、文本样式
- 颜色 color rgb rgba
- 文本对齐方式 text-align=center
- 首行缩进 text-indent:2em
- 行高 line-height:当行文字上下居中!line-height=height
- 装饰 text-decoration
- 文本图片水平对齐 vertical-align:middle
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!--
颜色:
单词
RGB(16进制0~F)
RGBA A:0~1
text-align:排版,居中
text-indent:段落首行缩进
行高 和 块的高度一致,就可以上下居中
-->
<style type="text/css">
h1{
color: rgba(0,255,255,0.9);
text-align: center;
}
.p1{
text-indent: 2em;
}
.p2{
background: yellow;
height: 300px;
line-height: 300px;
}
/*下划线*/
.l1{
text-decoration: underline;
}
/*中划线*/
.l2{
text-decoration: line-through;
}
/*上划线*/
.l3{
text-decoration: overline;
}
/*超链接去下划线*/
a{
text-decoration: none;
}
/*水平参照两物体,a,b*/
img,span{
vertical-align: middle;
}
</style>
</head>
<body>
<p>
<img alt="yxx" src="images/yxx.png">
<span>学习CSS我很开心</span>
</p>
<a href="">123</a>
<h1>故事结束</h1>
<p class="p1">
《小王子》是法国作家安托万·德·圣·埃克苏佩里于1942年写成的著名儿童文学短篇小说。本书的主人公是来自外星球的小王子。书中以一位飞行员作为故事叙述者,讲述了小王子从自己星球出发前往地球的过程中,所经历的各种历险。作者以小王子的孩子式的眼光,透视出成人的空虚、盲目,愚妄和死板教条,用浅显天真的语言写出了人类的孤独寂寞、没有根基随风流浪的命运。同时,也表达出作者对金钱关系的批判,对真善美的讴歌
</p>
<p class="p2">Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 [1] 。</p>
<p class="l1">abcd</p>
<p class="l2">abcd</p>
<p class="l3">abcd</p>
</body>
</html>
3.4、文本阴影
/*text-shadow:阴影颜色,水平偏移,垂直偏移,阴影半径*/
#price{
text-shadow: #3cc7f5 10ox 0px 10px;
}
3.5、超链接伪类
多数用法:a:hover 鼠标悬浮显示不同的颜色
<style type="text/css">
/*默认的颜色*/
a{
text-decoration: none;
color: black;
}
/*鼠标悬浮的颜色(重点)*/
a:hover {
color: orange;
}
a:active {
color: green;
}
a:visited {
color: gray;
}
</style>
3.6、列表
ul li 操作
list-style:
none:去掉原点
circle:空心圆
decimal:数字
square:正方形
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>列表样式</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="nav">
<h2 class="title">全部商品分类</h2>
<ul>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
<li><a href="#">图书</a> <a href="#">音响</a> <a href="#">数字商品</a></li>
</ul>
</div>
</body>
</html>
@charset "UTF-8";
#nav{
width: 300px;
background: #a0a0a0;
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 30px;
background: red;
}
/*ul li*/
/*
list-style:
none:去掉原点
circle:空心圆
decimal:数字
square:正方形
*/
/*ul{
background: #a0a0a0;
}*/
ul li{
height:30px;
list-style: none;
text-indent: 1em;
}
a{
text-decoration: none;
font-size: 14px;
color: #000;
}
a:hover{
color: orange;
text-decoration: underline;
}
3.7、背景
背景图片
背景颜色
<style type="text/css">
div{
width: 1000px;
height: 700px;
border: 1px solid red;
background-image:url("images/yxx.png");
/*默认是全部平铺的*/
}
/*x轴平铺*/
.div1{
background-repeat: repeat-x;
}
/*y轴平铺*/
.div2{
background-repeat: repeat-y;
}
/*无平铺,单个*/
.div3{
background-repeat: no-repeat;
}
</style>
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 30px;
/*颜色 图片 图片位置 平铺方式*/
background: red url("../images/a.png") 250px -5px no-repeat;
}
ul li{
height:30px;
list-style: none;
text-indent: 1em;
background-image: url("../images/a.png");
background-repeat: no-repeat;
background-position: 210px -10px;
}
3.8、渐变
https://www.grabient.com/
background-color: #FAACA8;
background-image: linear-gradient(225deg, #FAACA8 0%, #DDD6F3 100%);
4、盒子模型
4.1、盒子模型
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DSUMNjoH-1593351786864)(E:\许浩文件备份\笔记\web\image-20200624195625205.png)]
margin:外边距
padding:内边距
brder:边框
4.2、圆角边框
- 边框的粗细 border-right-width
- 边框的样式 border-right-style
- 边框的颜色 border-right-color
<style type="text/css">
/*body总有一个默认的外边距*/
body{
margin: 0;
padding: 0;
text-decoration: none;
}
/*border:粗细 样式 颜色*/
#box{
width: 300px;
border: 1px solid red;
}
form{
background: #3cbda6;
}
div:nth-of-type(1) input{
border-right-color: yellow;
border-right-width: 3px;
border-right-style: solid;
}
div:nth-of-type(2) input{
border: 3px solid powderblue;
}
div:nth-of-type(3) input{
border:2px dashed #008c27;
}
</style>
4.3、内外边距
盒子模型计算方式:
元素大小:margin+border+padding+内容宽度
要求:块元素,块元素有固定的宽度
外边距的妙用:
div居中元素:margin: 0 auto;
外边框写法:
顺时针旋转
margin:0 上下左右都为0
margin:0 1px 上下为0,左右为1px
margin:0 1px 1px 1px 上右下左
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 外边距的妙用:居中元素
margin: 0 auto;
-->
<style type="text/css">
/*body总有一个默认的外边距magrin:0*/
body{
margin: 0;
padding: 0;
text-decoration: none;
}
/*border:粗细 样式 颜色*/
h2{
font-size: 16px;
background-color: #3cbda6;
line-height: 30px;
color:white;
/*
顺时针旋转
margin:0 上下左右都为0
margin:0 1px 上下为0,左右为1px
margin:0 1px 1px 1px 上右下左
*/
margin: 0;
}
#box{
width: 300px;
border: 1px solid red;
margin: 0 auto;
}
form{
background: #3cbda6;
}
div:nth-of-type(1) input{
border-right-color: yellow;
border-right-width: 3px;
border-right-style: solid;
}
div:nth-of-type(2) input{
border: 3px solid powderblue;
}
div:nth-of-type(3) input{
border:2px dashed #008c27;
}
input{
border: 1px solid black;
}
</style>
</head>
<body>
<div id="box">
<h2>会员登录</h2>
<form action="#">
<div>
<span>用户名:</span>
<input type="text">
</div>
<div>
<span>密码:</span>
<input type="text">
</div>
<div>
<span>邮箱:</span>
<input type="text">
</div>
</form>
</div>
</body>
</html>
4.4、圆角边框
border-radius
4个角: 左上 右上 右下 左下(顺时针)
<!--
左上 右上 右下 左下(顺时针)
-->
<!--
圆圈:圆角=半径
-->
<style type="text/css">
div{
width: 100px;
height: 100px;
border: 10px solid red;
border-radius: 10px 20px;
}
/*头像圆形*/
img{
border-radius: 25px;
}
</style>
4.5、盒子阴影
box-shadow
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css</title>
<!--
左上 右上 右下 左下(顺时针)
-->
<style type="text/css">
img{
border-radius: 50px;
box-shadow: 10px 10px 100px yellow;
}
</style>
</head>
<body>
<div style="width: 1000px;display: block;text-align: center">
<img alt="tx" src="images/Cache_76a59258fb6533a7..jpg">
</div>
</body>
</html>
5、浮动
5.1、标准文档流
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DtzibW1W-1593351786866)(E:\许浩文件备份\笔记\web\image-20200626190327503.png)]
5.2、display
块级元素:独占一行
h1~H6,p,div,列表
行内元素:不独占一行
span,a,img,strong
行内元素可以包含在块级元素中,反之则不可以
display:
block 块元素
inline 行内元素
inline-block 是块元素,但是可以内联,在一行
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- display
block 块元素
inline 行内元素
inline-block 是块元素,但是可以内联,在一行
none
-->
<style type="text/css">
div{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
span{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline;
}
</style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>
</html>
这也是一种实现行内元素排列的方式,但是我们很多情况是用float
5.3、float浮动
浮动float:
right右浮
left左浮
div{
margin: 10px;
padding: 5px;
}
#father{
border:1px solid red;
}
.layer01{
border: 1px dashed #f00;
display: inline-block;
float: left;
}
.layer02{
border: 1px dashed #f00;
display: inline-block;
float: left;
}
.layer03{
border: 1px dashed #f00;
display: inline-block;
line-height: 20px;
float: left;
}
5.4、父级边框塌陷问题
clear
/*
clear:right 右侧不允许有浮动元素
clear:left 左侧不允许有浮动元素
clear:both 两侧不允许有浮动元素
clear:none 可以浮动
*/
解决方案:
1、增加父级元素高度
/*
clear:right 右侧不允许有浮动元素
clear:left 左侧不允许有浮动元素
clear:both 两侧不允许有浮动元素
clear:none 可以浮动
*/
2、增加一个空的div标签,清除浮动
<div class="clear"></div>
<!-- css -->
.clear{
clear: both;
margin: 0;
padding: 0;
}
3.在父级元素添加一个overflow
#father{
border:1px solid red;
overflow: hidden;
}
4.父级元素添加一个伪类:after
#father:after{
content: '';
display: block;
clear: both;
}
小结:
-
设置父元素的高度
简单,元素假设有了固定的高度,就会被限制
-
浮动元素后面增加空div
简单,但代码中尽量避免空div
-
overflow
简单,下拉的一些场景避免使用
-
父类添加一个伪类
写法复杂一点点,但没有副作用,推荐使用
5.5、对比
-
display
方向不可控制
-
float
浮动起来可能会脱离标准文档流,使用要解决父级边框坍陷问题
6、定位
6.1、相对定位
相对定位:position:relative
相对于原来的位置,进行指定的偏移,相对定位的话,它任然在标准文档流中,原来的位置会被保留
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 相对定位
相对于自己原来的位置进行偏移
-->
<style type="text/css">
body{
padding: 0;
}
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid blue;
padding: 0;
}
#first{
border: 1px dashed red;
background-color: blue;
position: relative;/*相对定位:上下左右*/
top: -20px;
}
#second{
border: 1px dashed black;
background-color: red;
}
#third{
border: 1px dashed yellow;
background-color: green;
position: relative;
bottom: -10px;
right: 20px;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
6.2、绝对定位
定位:基于xxx定位,上下左右
1、没有父级元素定位的前提下,相对于浏览器定位
2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移
3、可以脱离父级元素
相对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 相对定位
相对于自己原来的位置进行偏移
-->
<style type="text/css">
body{
padding: 0;
}
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid blue;
padding: 0;
position: relative;
}
#first{
border: 1px dashed red;
background-color: blue;
}
#second{
border: 1px dashed black;
background-color: red;
position: absolute;
left: 30px;
}
#third{
border: 1px dashed yellow;
background-color: green;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
6.3、固定定位fixed
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 相对定位
相对于自己原来的位置进行偏移
-->
<style type="text/css">
body{
height: 1000px;
}
div:nth-of-type(1) {/*绝对定位:相对于浏览器*/
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0;
bottom: 0;
line-height: 100px;
text-align: center;
}
div:nth-of-type(2) {/*fixed:固定定位*/
width: 50px;
height: 50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
line-height: 50px;
text-align: center;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>
6.4、z-index
图层z-index:默认0,最高无限
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="content">
<ul>
<li><img alt="" src="images/bj.png"></li>
<li class="tipText">好好学习,将来要进大厂</li>
<li class="tipBg"></li>
<li>时间:2020.10.08</li>
<li>地点:湖南城市学院</li>
</ul>
</div>
</body>
</html>
背景透明度:opacity: 数值
@charset "UTF-8";
#content{
margin: 0;
padding: 0;
overflow: hidden;
font-size: 12px;
line-height: 25px;
border: 1px solid black;
width: 850px;
}
ul,li{
margin: 0;
padding: 0;
list-style: none;
}
/*父级元素相对定位*/
#content ul{
position: relative;
}
.tipText, .tipBg{
position: absolute;
width:850px;
height: 25px;
position: absolute;
top:361px;
}
.tipText{
color: white;
/*z-index:0;层级*/
}
.tipBg{
background: black;
/*背景透明度*/
opacity: 0.5;
filter:Alpha(opacity=50);
}
7、动画
用css实现太难了,学js去吧