CSS
-
CSS是什么
-
CSS怎么用
-
CSS选择器(重点+难点)
-
美化网页(文字美化,阴影,超链接,列表,渐变....)
-
-
浮动
-
定位
-
网页动画(特效结果)
什么是CSS
-
Cascading Style Sheet 层叠级联样式表
-
CSS:表现(美化网页)
字体,颜色,边距,高度,宽度,背景图片,网页定位,网页浮动...
发展史
CSS1.0
CSS2.0 DIV(块)+CSS,HTML与CSS结构分离的思想,网页变得很简单,SEO
CSS2.1 浮动,定位
CSS3.0 圆角,阴影,动画... 浏览器兼容性~
快速入门
style
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范,<style>可以编写css代码
语法:
选择器{
声明1;
声明2;
声明3;
}
-->
<style>
h1{
font-size: 100px;
color: aquamarine;
}
</style>
</head>
<body>
<h1>我是标题</h1>
</body>
</html>
css的优势
-
内容和表现分离
-
网页结构表现统一,可以实现复用
-
样式十分的丰富
-
建议使用独立于html的css文件
-
利用SEO,容易被搜索引擎收录
CSS的三种导入方式
CSS优先级:就近原则(距离代码进的优先生效)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--内部样式表-->
<style>
h1{
color: green;
}
</style>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!--行内样式:在标签元素中,编写一个style属性,编写样式即可-->
<h1 style="color: red;">我是标题</h1>
</body>
</html>
拓展:外部样式两种写法
-
链接式
html
<link rel="stylesheet" href="css/style.css">
-
导入式
@import是CSS2.1特有的
<!--导入式-->
<style>
@import url("css/style.css");
</style>
选择器
作用:选择页面上的某一个或者某一类元素
基本选择器
-
标签选择器:选择一类标签 标签{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*标签选择器,会选择到页面上所有的标签都会选择到*/
h1{
color: red;
background: green;
border-radius: 25px;
}
p{
font-size: 80px;
}
</style>
</head>
<body>
<h1>你好标题</h1>
<h1>你好标题1</h1>
<p>你好P标签</p>
</body>
</html> -
类选择器 class:选中所有class属性一致的标签,跨标签.类名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*类选择器的格式 .class的名称
*/
.wx{
color: green;
}
.wx1{
color: red;
}
</style>
</head>
<body>
<h1 class="wx">你好标题1</h1>
<h1 class="wx1">你好标题2</h1>
<h1>你好标题3</h1>
<p class="wx">P标签</p>
</body>
</html> -
ID选择器:全局唯一 #id{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*ID选择器,#id名称
不遵循就近原则,固定的
ID选择器>class 选择器>标签选择器
*/
#h1{
color: red;
}
#h2{
color: pink;
}
</style>
</head>
<body>
<h1 id="h1">你好标题1</h1>
<h1 id="h2">你好标题2</h1>
<h1 id="h3">你好标题3</h1>
</body>
</html>
层次选择器
-
后代选择器:在某个元素的后面
/*后代选择器*/
body p{
background: red;
} -
子选择器
/*子选择器*/
body>p{
background: pink;
} -
相邻兄弟选择器 同辈
/*相邻兄弟选择器:只有一个 相邻(向下)*/
.active + p{
background: aqua;
} -
通用选择器
/*通用兄弟选择器,当前选中元素的向下的所有兄弟元素*/
.active~p{
background: burlywood;
}
结构伪类选择器
/*ul的第一个子元素*/
ul li:first-child{
background: red;
}
/*ul的最后一个子元素*/
ul li:last-child{
background: green;
}
/*选中p1:定位到父元素,选择当前的第一个元素*/
/*选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效*/
/*按照顺序*/
p:nth-child(2){
background: blue;
}
/*选中父元素下的p元素的第二个,按照类型选择*/
p:nth-of-type(2){
background: chartreuse;
}
属性选择器(常用)
id+class结合
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>属性选择器</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 25px;
background: aqua;
text-align: center;
color: gray;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Aroal;
}
/*属性名,属性名=属性值(正则)*/
/*
= 绝对等于
*= 包含这个元素
^= 以这个开头
*/
/*存在IP属性的元素 a[]{}*/
/*a[id]{
background: yellow;
}*/
/*id=first 的元素*/
/*a[id=first]{
background: yellow;
}*/
/*class 中有links的元素*/
/* a[class*="links"]{
background: yellow;
}*/
/*选中href中以http开头的元素*/
/* a[href^=http]{
background: yellow;
}*/
/*选中href中以html结尾的元素*/
a[href$=html]{
background: yellow;
}
</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" id="second">2</a>
<a href="image/123.html" class="links item">3</a>
<a href="">4</a>
<a href="">5</a>
<a href="">6</a>
<a href="">7</a>
<a href="">8</a>
<a href="">9</a>
<a href="" class="links item last" >10</a>
</p>
</body>
</html>
美化网页元素
为什么要美化
-
有效的传递网页信息
-
,美化网页,页面漂亮,才能吸引用户
-
凸显页面的主题
-
提高用户的体验
-
span标签:重点要突出的字,使用span套起来
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#title{
color: blue;
}
</style>
</head>
<body>
你好,欢迎学习<span id="title">Java</span>
</body>
</html>
字体样式
<style>
/*
font-family:字体
font-size:大小
font-weight:粗细
color:颜色
*/
body{
font-family: "Adobe Devanagari" 楷体;
color: aqua;
}
h1{
font-size: 50px;
}
.p1{
font-weight: bold;
}
</style>
文本样式
-
颜色 color rgb rgba
-
文本对齐的方式 text-algin=certer
-
首行缩进 text-indent: 2em;
-
行高 line-height
-
装饰 text-decoration
-
图片水平对齐:vertical-align
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
颜色:
单词
RGB
红、绿、蓝 0~F
RGBA A:0~1 透明度
text-align:排版,居中
text-align:2em; 首航缩进
行高和块的高度一致,就可以上下居中了
-->
<style>
h1{
/*color: #b6ffa4;*/
/*color: rgb(0,255,255);*/
color: rgba(0,255,255,0.5);
text-align: center;
}
.p1{
text-indent: 2em;
}
.p2{
background: aqua;
height: 50px;
line-height: 50px;
}
/*下划线*/
.w1{
text-decoration: underline;
}
/*中划线*/
.w2{
text-decoration: line-through;
}
/*下划线*/
.w3{
text-decoration: overline;
}
/*
水平对齐需要参照物
*/
img,span{
vertical-align: middle;
}
</style>
</head>
<body>
<p class="w1">111111111111111111111111</p>
<p class="w2">222222222222222222222222</p>
<p class="w3">333333333333333333333</p>
<h1 >76人官方晒训练照,21+4小将野心十足,22岁就正在朝全明星进发</h1>
<p class="p1">虽然上赛季费城76人再次止步第二轮,被挡在东决大门外,但76人显然也收获一些惊喜,其中球队后卫泰雷兹-马克西在生涯第二年进步神速,不但坐稳了先发位置,而且成为76人内线大将也是绝对核心乔尔-恩比德身边,最为得力的帮手,甚至泰雷兹-马克西有些时候的光芒盖过了三届联盟得分王詹姆斯-哈登,显得更加绚丽多彩。</p>
<p class="p2">要说泰雷兹-马克西的野心着实不小,他并不满足于当角色球员,或者核心身边的副手和绿叶,而是成为队内的王牌球员。76人官方就晒出了泰雷兹-马克西的训练照,看上去即便是休赛期,泰雷兹-马克西也未放松对自己的要求,他没有丝毫懈怠,通过甩绳和登山机等训练,这个休赛期泰雷兹-马克西的身体变得更加强壮结实,显然他已经迫不及待,期待在新赛季再上一个台阶了。</p>
<p>When tomorrow turns in today, yesterday, and someday that no more important in your memory, we suddenly realize that we r pushed forward by time. This is not a train in still in which you may feel forward when another train goes by. It is the truth that we've all grown up. And we become different.</p>
<p class="im1">
<img src="image/hw.jpeg" alt="">
<span>asdasdasdasdasdasds</span>
</p>
</body>
</html>
阴影
/*text-shadow:
阴影颜色
水平偏移
垂直偏移
阴影半径
*/
#price{
text-shadow: lightskyblue 10px 10px 2px;
}
超链接伪类
a,a:hover
/*鼠标悬浮的颜色*/
a:hover{
color: orangered;
font-size: 20px;
}
列表
背景
<style>
div{
width: 1000px;
height: 700px;
border: 1px solid red;
background-image: url("images/shuiwa.jpg");
/*默认是全部平铺的*/
}
.div1{
background-repeat: repeat-x;
}
.div2{
background-repeat: repeat-y;
}
.div3{
background-repeat: no-repeat;
}
</style>
渐变
资源地址:https://www.grabient.com/
盒子模型
margin:外边距
padding:内边距
border:边框
边框
-
边框的粗细
-
边框的样式
-
边框的颜色
<style>
body{
/*body总有一个默认的外边距 margin:0*/
margin: 0;
padding: 0;
text-decoration: none;
}
/*border:粗细,样式,颜色*/
#app{
width: 300px;
border: 1px solid red;
}
h2{
font-size: 16px;
background: gray;
line-height: 30px;
color: blanchedalmond;
/*margin: 0px;*/
}
form{
background: forestgreen;
}
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: 3px dashed yellow;
}
</style>
内外边距
<!--外边距的妙用;居中
margin: 0 auto;左右居中
-->
<style>
body{
/*body总有一个默认的外边距 margin:0*/
margin: 0;
padding: 0;
text-decoration: none;
}
/*border:粗细,样式,颜色*/
#app{
width: 300px;
border: 1px solid red;
margin: 0 auto;
}
/*
顺序:顺时针旋转
margin: 0
margin: 0 1px
margin: 0 1px 2px 3px
*/
h2{
font-size: 16px;
background: gray;
line-height: 30px;
color: blanchedalmond;
/*margin: 0px;*/
}
form{
background: forestgreen;
}
input{
border: 1px solid black;
}
div:nth-of-type(1){
padding: 10px;
}
</style>
盒子的计算方式:
margin+border+padding+内容的宽度
圆角边框
阴影
<style>
/*
左上
右上
右下
左下
顺时针
*/
div{
width: 100px;
height: 30px;
border: 10px solid black;
border-radius: 50px 30px 20px 5px;
box-shadow: 10px 10px 100px yellow;
}
</style>
浮动
标准文档流
块级元素:独占一行
h1~h6 p div.....
行业元素: 不独占一行
span a img .....
行内元素可以被包含,块级元素不可以
display
<!--
inline 行内元素
block 块元素
inline-block 是块元素,可以内联在一行
none 消失
-->
<style>
div{
width: 100px;
height: 100px;
border: 5px solid red;
display: inline;
}
span{
width: 100px;
height: 100px;
border: 5px solid blue;
display: inline-block;
}
</style>
float
左右浮动
父级边框塌陷的问题
clear
解决方案:
1、增加父级元素的告诉
2、增加一个空的div标签,清除浮动
3.overflow
4、父类添加一个伪类:after
对比
定位
相对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
相对定位
相对于自己原来的位置进行偏移
-->
<style>
body{
padding: 20px;
}
div{
margin: 10px;
padding: 5px;
font-size: 16px;
line-height: 25px;
}
#navf{
border: 1px solid blue;
}
#nav1{
border: 1px dashed red;
position: relative;/*相对定位:上下左右*/
top: -20px;
}
#nav2{
border: 1px dashed yellow;
position: relative;/*相对定位:上下左右*/
left: 20px;
}
#nav3{
border: 1px dashed green;
position: relative;/*相对定位:上下左右*/
bottom: -20px;
}
</style>
</head>
<body>
<div id="navf">
<div id="nav1">第一个盒子</div>
<div id="nav2">第二个盒子</div>
<div id="nav3">第三个盒子</div>
</div>
</body>
</html>
相对定位:position: relative;
相对于原来的位置,进行指定的偏移,相对于定位的话,他仍然在标准的文档流中,原来的位置会保留
left: 20px;
right: 20px;
top: -20px;
bottom: -20px;
绝对定位
定位:基于xxx定位,上下左右
-
没有父级元素定位的前提下,相对于浏览器定位
-
假设父级元素存在定位,通常会相对于父级元素进行便宜
-
在
``
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
相对定位
相对于自己原来的位置进行偏移
-->
<style>
body{
padding: 20px;
}
div{
margin: 10px;
padding: 5px;
font-size: 16px;
line-height: 25px;
}
#navf{
border: 1px solid blue;
}
#nav1{
border: 1px dashed red;
position: relative;/*相对定位:上下左右*/
top: -20px;
}
#nav2{
border: 1px dashed yellow;
position: absolute;/*相对定位:上下左右*/
right: 30px;
}
#nav3{
border: 1px dashed green;
position: relative;/*相对定位:上下左右*/
bottom: -20px;
}
</style>
</head>
<body>
<div id="navf">
<div id="nav1">第一个盒子</div>
<div id="nav2">第二个盒子</div>
<div id="nav3">第三个盒子</div>
</div>
</body>
</html>
固定定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
height: 1000px;
}
div:nth-of-type(1){
/*绝对定位:相对于浏览器*/
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2){
/*固定定位:*/
width: 50px;
height: 50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>
z-index
z-index:默认是0
<!DOCTYPE html>
<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/hw.jpeg" alt=""></li>
<li class="tiptext">Java学习之路</li>
<li class="tipbg"></li>
<li>时间:20220927</li>
</ul>
</div>
</body>
</html>
#content{
padding: 0;
margin: 0;
overflow: hidden;
font-size: 16px;
line-height: 25px;
border: 1px solid black;
width: 380px;
}
ul,li{
padding: 0;
margin: 0;
list-style: none;
}
/*父级元素相对定位*/
#content ul{
position: relative;
}
.tiptext,.tipbg{
position: absolute;
width: 380px;
height: 25px;
top: 250px;
}
.tiptext{
color: white;
z-index: 999;
}
.tipbg{
background: black;
opacity: 0.5;/*背景透明度*/
}
动画