1.CSS初识
如何学习?
1.CSS是什么?
2.CSS如何用?(快速入门)
3.CSS选择器(重难点)
4.美化网页(文字,阴影,超链接,列表,渐变等)
5.盒子模型
6.浮动
7.定位
8.网页动画(特效)
1.1 什么是CSS
CSS:表现(美化网页)字体、颜色、边距、高度、宽度、网页浮动等
1.2 CSS发展史
略
1.3 CSS快速入门
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范,<style> 可以编写css代码,每一个声明最好以分号结尾
语法:
选择器{
声明1;
声明2;
...
}-->
<style>
h1{
color:red;
}
</style>
</head>
<body>
<h1>标题</h1>
</body>
好处:1.内容与表现分离 2.网页结构表现单一,可以实现复用
3.样式丰富 4.建议使用独立于html的css文件 5.利用seo,容易被搜索引擎收录
1.4 CSS的四种导入方式
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--内部样式-->
<style>h1{
color:green;
}</style>
<!--外部样式-->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!--优先级:就近原则,离h1越近的越先执行-->
<!--行内样式:在标签元素中编写一个style属性-->
<h1 style="color:red">标题</h1>
</body>
拓展:外部样式两种写法
- 链接式,属于HTML
<!--外部样式-->
<link rel="stylesheet" href="css/style.css">
- 导入式,属于CSS 2.1特有
<style>
@import url("css/style.css");
</style>
2.选择器
作用:选择页面上的某一个或某一类元素
2.1基本选择器
1.标签选择器(标签{}):选择一类标签
<style>
/*标签选择器:会选择页面上的所有这个标签的元素*/
h1{
color: #c9ff5a;
background: red;
border-radius:24px;
}
p{
font-size:80px ;
}
</style>
2.类选择器 (.class{}):选择所有class属性一致的标签,可以跨标签
<style >
/*类选择器格式 .class的名称{}
好处:可以多个标签归类,是同一个class,可以复用*/
.czx{
color:red;
}
.czx1{
color: beige;
}
</style>
</head>
<body>
<h1 class="czx">还沙虎鲨</h1>
<h1 class="czx1">还沙虎鲨</h1>
<p class="czx">啦啦啦</p>
3.Id选择器(#id{}):全局唯一!
<style >
/*id选择器:id必须保证全局唯一
#id名称{}
不遵循就近原则,优先级:id选择器>class类选择器>标签选择器*/
#czx{
color: #c9ff5a;
}
.style1{
color: aqua;
}
h1{
color: burlywood;
}
</style>
2.2 层次选择器
1.后代选择器:某个元素的后面
/*后代选择器*/
body{
background: red;
}
2.子选择器,一代,儿子
/*子选择器,body之后一代才有,一代之后的就没有*/
body>p{
background: cyan;
}
3.相邻兄弟选择器
/*相邻兄弟选择器,只有一个,相邻(向下)的发挥作用*/
.active + p{
background: forestgreen;
}
4.通用选择器
/*通用兄弟选择器,当前选中元素向下的所有兄弟元素,不包括自己*/
.active~p{
background: indianred;
}
2.3 结构伪类选择器
伪类:带有:的
/*ul的第一个子元素*/
ul li:first-child{
background: indianred;
}
/*ul的最好一个子元素*/
ul li:last-child{
background: red;
}
/*选中p1:定位到父元素,选择当前第一个元素
选择当前p元素的父级元素,选中父级元素中的第一个,并且是当前元素才生效!按照顺序*/
p:nth-child(2){
color: blue;
}
/*选中父元素下的p元素的第二个,按照类型*/
p:nth-of-type(1){
background: yellow;
}
2.4属性选择器(常用)
id+class结合
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: #2700ff;
text-align: center;
color: red;
text-decoration: none;
margin-right:5px ;
font: bold 20px/50px Arial;
}
/*属性名,属性名=属性值(正则)*/
/*存在id属性的元素 a[]{}
=是绝对等于
*=是包含
^=以这个开头
$=以这个结尾* /
*/
/*a[id]{
background: brown;
}*/
/*id=first的元素
a[id=first]{
background: brown;
}*/
/*class中有link的元素
a[class*="links"]{
background: chocolate;
}*/
/*选中href中http开头的元素
a[href^=https]{
background: yellow;
}*/
a[href$=pdf]{
background: yellow;
}
</style>
</head>
<body>
<p class="demo">
<a href="https://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>
<a href="abcd.doc" class="links item last">10</a>
</p>
</body>
3.美化网页元素
3.1 美化的必要
1.有效传递页面信息
2.美化网页,吸引用户
3.凸显网页的主题
4.提高用户的体验
span标签:重点突出的字要用span标签包围起来
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#id{
font-size: 50px;
}
</style>
</head>
<body>
欢迎学习<span id="title1">Java</span>
</span>
</body>
3.2 字体样式
<style>
/*font-family字体
font-size字体大小
font-weight字体粗细
color颜色*/
body{
font-family: "Arial Black",楷体;
color: #ffa836;
}
h1{
font-size: 50px;
}
.p1{
font-weight: lighter;
}
</style>
3.3文本样式
1.颜色 color rgb和rgba
<!--颜色:
单词 RGB 0-F
RGBA A:0-1
text-align:排版,居中
text-indent:首行缩进
height、line-height
行高和块高度一致就可以上下居中-->
<style>
h1{
color:rgba(0,255,255,0.9);
text-align: center;
}
.p1{
text-indent: 2em;
}
.p3{
background: #2700ff;
height: 300px;
line-height: 300px;
}
/*下划线*/
.p1{
text-decoration: underline;
}
/*中划线*/
.p2{
text-decoration:line-through;
}
/*上划线*/
.p3{
text-decoration: overline;
}
/*超链接去下划线*/
a{
text-decoration:none;
}
</style>
2.文本对齐的方式 text-align=center
3.首行缩进 text-indent:2em;
4.行高 line-height; 单行文字上下居中则line-height=height
5.装饰 text-decoration;
6.文本图片水平对齐 vertical-align;
3.4 阴影和超链接伪类
<style>
/*默认颜色*/
a{
text-decoration: none;
color: black;
}
img{
height: 250px;
width: 180px;
}
/*鼠标悬浮的状态(只需要记住hover)*/
a:hover{
color: orange;
font-size: 50px;
}
/*鼠标按住未释放的状态*/
a:active{
color: green;
}
/*text-shadow:阴影颜色 水平位移 垂直偏移 阴影半径*/
#price{
text-shadow: #3cc3ff 10px 10px 2px;
;
}
</style>
3.5 列表
#nav{
width: 300px;
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 35px;
background: red;
}
/*ul li
list-style:
none 去掉原点
sircle 空心圆
decimal 数字
square 正方形
*/
ul{
background: #a0a0a0;
}
ul li{
height: 30px;
list-style: none;
}
a{
text-decoration: none;
font-size: 14px;
color: #000;
}
a:hover{
color: orange;
text-decoration: underline;
}
3.6 背景
背景图片
<style>
div{
width: 1000px;
height: 700px;
border: 1px solid red;
background-image: url("images/a.png");
/*默认是全部平铺的*/
}
.div1{
background-repeat: repeat-x;
}
.div2{
background-repeat: repeat-y;
}
.div3{
background-repeat:no-repeat;
}
</style>
3.7渐变
<style>
body{
background: linear-gradient(19deg,#21D4FD 0%,#B721FF 100%) ;
}
</style>
4.盒子模型
4.1 什么是盒子
margin:外边距
padding:内边距
border:边框
4.2 边框
<style>
/*h1,ul,li,a,body{*/
/* !*body总有一个默认的外边距margin:0*!*/
/* margin: 0px;*/
/* padding: 0px;*/
/* text-decoration: none;*/
/*}*/
/*border:粗细,样式,颜色*/
#box{
width: 300px;
border:1px solid red;
}
h2{
font-size: 16px;
background-color:#3cbda6 ;
line-height: 30px;
color: white;
}
form{
background: #3cbda6;
}
div:nth-of-type(1)> input{
border:3px solid black;
}
div:nth-of-type(2)> input{
border:3px dashed powderblue;
}
div:nth-of-type(3)> input{
border:2px dashed #e65d52;
}
</style>
4.3 内外边距
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--外边距的妙用:居中元素
margin: 0 auto;
-->
<style>
#box{
width: 300px;
border:1px solid red;
margin: 0 auto;
}
/*
顺时针旋转
margin:0
margin:0 1px
margin:0 1px 2px 3px
*/
h2{
font-size: 16px;
background-color:#3cbda6 ;
line-height: 30px;
color: white;
}
form{
background: #3cbda6;
}
input{
border:1px solid black;
}
div:nth-of-type(1){
padding:10px 2px;
}
</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>
盒子的计算方式:这个元素多大?margin+border+padding+内容宽度
4.4圆角边框
4个角
<style>
/*左上 右上 右下 左下,顺时针方向*/
/*圆圈: 圆角=半径*/
div{
width: 100px;
height: 100px;
border: 10px solid red;
border-radius: 50px;
}
</style>
4.5 阴影
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--margin:0 auto:居中
要求:块元素有固定的宽度-->
<style>
/*盒子阴影*/
div{
border-radius: 50px;
box-shadow: 10px 10px 100px blue;
}
</style>
</head>
<body>
<div style="width:500px;display: block;text-align: center">
<img src="../images/a.png" alt="">
</div>
</body>
5.浮动
块级元素:独占一行
h1-h6 p div…
行内元素:不独占一行
span a img strong…
行内元素可以被包含在块级元素中,反之不行
5.1 display
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
block块元素
inline行内元素
inline-block是块元素,但是可以内联在一行-->
<style>
div{
width: 100px;
height: 100px;
border:1px solid red;
display: inline;
}
span{
width: 100px;
height: 100px;
border:1px solid red;
display: inline-block;
}
</style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>
1.这个也是一种实现行内元素排列的方式,但很多情况我们都用float
5.2 float
1.左右浮动 float
div{
margin: 10px;
padding: 5px;
}
#father{
border: 1px #000 solid;
}
.layer01{
border: 1px #F00 dashed;
display: inline-block;
float:right ;
}
.layer02{
border: 1px #00F dashed;
display: inline-block;
float: right;
}
.layer03{
border: 1px #060 dashed;
display: inline-block;
float: left;
}
.layer04{
border: 1px #060 dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
float: left;
clear: both;
}
5.3 父级边框塌陷问题
clear:right;/*右侧不允许有浮动元素*/
clear:left;/*左侧不允许有浮动元素*/
clear:both;/*两侧不允许有浮动元素*/
clear:none
解决方法:1.增加父级元素高度
2.增加一个空的div标签,清除浮动
3.overflow
overflow:hidden/auto /*在父级元素中增加overflow*/
4.父类添加一个伪类:after(避免一个空div的方法,常用)
#father:after{
content: '';
display: block;
clear: both;
}
小结:
- 1.浮动元素后面增加空div
简单,代码中尽量避免空div - 2.设置父元素高度
简单,元素假设有了固定的高度,就会被限制 - 3.overflow
简单,下拉的一些场景避免使用 - 4.父类添加一个伪类:after
写法稍微复杂,无副作用,推荐使用
5.4 对比
- display
方向不可控制 - float
浮动起来的话会脱离标准文档流,所以要解决父级边框塌陷的问题
6. 定位
6.1 相对定位
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--相对定位
相对于自己原来的位置进行偏移~-->
<style>
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border:1px solid red;
}
#first{
border:1px dashed blue;
background-color:#c9ff5a ;
position: relative;/*相对定位*/
top:-20px;
left: 50px;
}
#second{
border:1px dashed green;
background-color:#a0a0a0 ;
}
#third{
border:1px dashed yellow;
background-color: fuchsia;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
相对定位:position和relative
相对于原来的位置,进行指定的偏移,仍然位于标准文档中
top:-20px;
left:20px;
bottom:-10px;
right:20px;
6.2 绝对定位
定位:基于xxx定位,上下左右~
1.没有父级定位的前提下,相对于浏览器定位
2.假设父级元素存在,我们通常相对于父级元素进行偏移
3.在父级元素范围内移动
相对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不存在标准文档流中,原来的位置会被保留
#second{
border:1px dashed green;
background-color:#a0a0a0 ;
position: absolute;
}
6.3 固定定位 fixed
div:nth-of-type(2){/*fixed,固定定位*/
width:50px;
height: 50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
6.4 z-index
图层:
z-index:默认是0,最高无限制
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="content">
<ul>
<li><img src="images/a.png" alt=""></li>
<li class="tipText">学习微服务,找czx</li>
<li class="tipBg"></li>
<li>时间:2099-01-01</li>
<li>地点:火星基地</li>
</ul>
</div>
</body>
</html>
#content{
padding: 0px;
margin: 0px;
overflow: hidden;
font-size: 12px;
line-height: 25px;
border:1px #000 solid;
}
ul,li{
padding: 0px;
margin: 0px;
list-style: none;
}
/*父级元素相对定位*/
#content ul{
position: relative;
}
.tipText,.tipBg{
position: absolute;
width: 380px;
height: 25px;
top:216px;
}
.tipText{
color: white;
z-index: 0;
}
.tipBg{
background: #000;
opacity: 0.5;/*透明度*/
}