CSS
HTML+CSS+JavaScript
结构+表现+交互
知识点梳理:
1.CSS
How to learn ?
1、CSS是什么
2、CSS怎么用(快速入门)
3、CSS选择器(重点、难点)
4、美化网页(文字美化、超链接、列表、渐变)
5、盒子模型
6、浮动
7、定位
8、网页动画(特效)
参考网站:菜鸟教程、W3C
1.1什么是CSS
Cascading Style Sheet 层叠级联样式表
CSS:美化网页
具体表现为:字体、颜色、边距、高度、宽度、网页定位····
1.2发展史
CSS1.0——CSS2.0——CSS2.1——CSS3.0
2.0里:DIV(块)+CSS,提出HTML与CSS结构分离的思想,网页变得简单,SEO(搜索引擎优化)
2.1里:浮动与定位
3.0里:圆角边框、阴影、动画····存在浏览器兼容性
1.3快速入门
样式style
练习格式:CSS文件夹+index.html共存与一个练习文件夹中
-
语法:写在HTML中的
选择器{
声明1;
声明2;
}
每一个声明用分号结尾;
建议写一个HTML再写一个CSS
- CSS的优势
- 内容与表现分离
- 网页结构表现统一、可以实现复用
- 样式十分丰富
- 建议使用独立于HTML的CSS文件
- 利用SEO,容易被搜索引擎收录
1.4css的3种导入方式
-
行内样式:
<h1 style ="color:red"> 标题</h1>
-
内部样式
<style> h1{ color: blue; } </style>
-
外部样式
<link rel="stylesheet" href="css/style.css">
拓展:外部样式两种写法
- 导入式
“}”
<style>
@import url(css/style.css)
</style>
- 链接式
<link rel="stylesheet" href="css/style.css">
优先级:就近原则
2.选择器
作用:选择页面上的某一个或者某一个元素
2.1基本选择器
- 标签选择器:选择一类标签
- 会选择到页面上所有的这个标签的元素
- 类 选择器 class:选择所有class属性一致的标签,跨标签
- 类选择区的格式: .class的名称{}
- 好处:可以多个标签归类,是同一个class,可以复用
- id选择器
- 格式:#id名称{ }
- id保证全局唯一!!
- 不遵循就近原则,固定的
优先级:id选择器>class选择器>标签选择器
基本选择器语法如:
- 选择器 ——————类型——————功能
- *———————通配选择器————选择文档中所有的HTML元素
- E———————元素选择器————选择指定类型的HTML元素
- #id——————ID选择器—————选择指定ID属性值为“id”的任意类型元素
- .class—————类选择器—————选择指定class属性值为“class”的任意类型的任意多个元素
测试代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<style>-->
<!--.lala{-->
<!--color: red;-->
<!--}-->
<!--#aa{-->
<!--color: cyan;-->
<!--}-->
<!--h1{-->
<!--color: black;-->
<!--}-->
<!--</style>-->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1 class="lala">label11</h1>
<h1 id="aa">label22</h1>
<h1>label33</h1>
<h1>label44</h1>
</body>
</html>
style.css
.lala{
color: red;
}
#aa{
color: cyan;
}
h1{
color: black;
}
结果:
2.2高级选择器
层次选择器
祖爷爷 爷爷 爸爸 自己
-
后代选择器:在某个元素的后面
-
格式:
body p{ background:red; }
-
-
子选择器:一代
-
格式:
body>p{ backgroud:cyan; }
-
-
相邻兄弟选择器:同辈一个(向下)
-
格式:
.active + p{ backgroud:brown; }
-
-
通用选择器:当前选中元素中向下的所有兄弟元素
-
格式:
.active~p{ backgroud:green; }
-
结构伪类选择器
伪类:条件过滤
语法:
- 选择器 ——————类型——————功能
- E:hover————用户行为选择器——选择匹配的E元素,且用户鼠标停留在元素E上
- E:first-child———————————作为父元素的第一个子元素的元素E。与E:nth-child(1)等同
- E:last-child———————————作为父元素的最后一个子元素的元素E。与E:nth-last-child(1)等同
- E:nth-of-type(n)—————————选择父元素内具有指定类型的第n个E元素
- E:first-of-type——————————选择父元素内具有指定类型的第一个E元素,与E:nth-of-type(1)等同
ul li:first-child{
backgroud:yellow;
}
ul li:last-child{
backgroud:yellow;
}
选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素中的第一个,并且是当前元素才生效!顺序
p:nth-child(1){
backgroud:red;
}
选中父元素下的p元素的第二个,类型
p:nth-type(1){
backgroud:red;
}
属性选择器(常用)
将id加class结合
语法:
- 选择器 ——————————————功能
- [attribute=value]——————————用于选取带有指定属性和值的元素。
- [attribute*=value]—————————匹配属性值中包含指定值的每个元素。
- [attribute^=value]——————————匹配属性值以指定值开头的每个元素。
- [attribute$=value]——————————匹配属性值以指定值结尾的每个元素。
=绝对等于
*=包含
^=开头等于
$=结尾等于
测试代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: #2700ff;
text-align: center;
color: black;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;
}
a[id=first]{
background: yellow;
}
a[class="links item active"]{
background: gold;
}
a[href*="123"]{
background: red;
}
a[href="abc"]{
background: aqua;
}
a[href$=pdf]{
background: forestgreen;
}
a[href$=doc]{
background: brown;
}
</style>
<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>
<a href="abcd.doc" class="links item last">10</a>
</p>
</body>
</html>
结果如下:
3.美化网页元素
3.1目的:
- 有效传递页面信息
- 页面漂亮才能吸引用户
- 凸显页面主题
- 提高用户体验
span标签:重点要突出的字
3.2字体样式
- font-family字体
- font-size大小
- font-weight粗细
- color颜色
- oblique 斜体
3.3文本样式
- 颜色
- 单词:red yellow
- RGB:0~F
- RGBA:A:0~1(透明度)
- 文本对齐的方式
- text-align:center居中
- 首行缩进
- text-indent:2em(首行缩进两字符)
- 行高
- height:100px
- line-height:100px一行的高度
- 这一行就上下居中了
- 装饰
- text-decoration:underline
- text-decoration:line-through
- text-decoration:overline
- a标签(超链接去下划线)
3.4超链接伪类
<a href="#">超链接</a>
默认的颜色
a{
text-decoration:overline;
color:#000000;
}
鼠标悬浮的颜色
a:hover{
color:gold;
font-size:30px;
}
鼠标按住未释放的状态
a:active{
color:red;
}
默认状态:
鼠标放上去的状态:
鼠标点击时:
3.5阴影
text-shadow :#3cc7f5 10px 20px 2px
分别代表:阴影颜色、水平偏移、垂直偏移、模糊半径
结果如下:
3.6列表
.title{
}
ul li
list-style:
none去掉圆点
circle空心圆
decimal数字
square正方形
ul li{
height:30px;
list-style:none;
text-indent:1em;
}
a{
text-decoration:none;
font-size:14px;
}
a:hover{
color:orange;
text-decoration:underline;
}
进行练习:
<!DOCTYPE html>
<html>
<head lang="en">
<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></li>
<li><a href="#">家居</a> <a href="#">家装</a> <a href="#">厨具</a></li>
<li><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></li>
<li><a href="#">彩票</a> <a href="#">旅行</a> <a href="#">充值</a> <a href="#">票务</a>
</li>
</ul>
</div>
</body>
</html>
style.css:
#nav{
background:rgba(104, 81, 128, 0.52) ;
width: 300px;
}
.title{
height: 50px;
line-height: 50px;
font-size: 30px;
background: coral;
text-indent:1.5em;
}
ul li{
height: 30px;
list-style: none;
}
a{
font-size:16px;
text-decoration: none;
color:black;
}
a:hover{
color:goldenrod;
text-decoration: underline;
}
结果:
3.7背景
背景图片
<style>
div{
width:600px;
height:400px;
border:1px solid red;
background-img:url("images/26-7.jpg");
background-repeat:repeat-x;
}
</style>
background:red url() px px no-repeat
颜色 图片 图片位置 平铺方式
结果如下:
3.8渐变
- 线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向
- 径向渐变(Radial Gradients)- 由它们的中心定义
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
<style>
#grad1 {
height: 200px;
background-color: red;
background-image: linear-gradient(#e66465, #9198e5);
}
</style>
</head>
<body>
<p>从顶部开始的线性渐变。起点是红色,慢慢过渡到蓝色:</p>
<div id="grad1"></div>
</body>
</html>
结果如下:
4.盒子模型
4.1盒子模型参数
margin:外边距
padding:内边距
border:边框
4.2边框
border:1px solid red
- 边框粗细(对应1px)
- 边框样式(实线solid)
- 边框颜色(red)
4.3内外边距
margin:上左下右
margin:0 auto;上下为0,左右自动,使之居中(要求:块元素,块元素有固定的宽度)
margin:0 1px 2px 3px ;顺时针排
padding同理
盒子的计算方式:这个元素到底多大?
margin+border+padding+内容宽度
4.4圆角边框
参数按照:左上 右上 右下 左下,顺时针方向
圆圈:圆角=半径
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>圆角边框</title>
<style>
#g {
width: 100px;
height: 100px;
background-color: red;
background-image: linear-gradient(#e66465, #9198e5);
border-radius: 100px;
}
</style>
</head>
<body>
<div id="g"></div>
</body>
</html>
结果如下:
4.5盒子阴影
box-shadow
<head>
<style>
img{
border-radius:50px;
box-shadow:10px 10px 100px cyan;
}
</style>
</head>
<body>
<div style="width:500px;display:block;text-align:center">
<img src="images/26-7.jpg" alt="">
</div>
</body>
结果如下:
5.浮动Float
5.1标准文档流
- 标准文档流:文档流指的是元素排版布局过程中,元素会自动从左往右,从上往下的流式排列。并最终窗体自上而下分成一行行,并在每行中从左至右的顺序排放元素。
- 存在的现象:
- 空白折叠现象
- 高矮不齐,底边对齐
- 自动换行
- 标准文档流等级
- 块级元素:独自占一行,不和其他元素并列;能设置宽高,不设置宽度会默认为父级元素一样宽
- 行内元素:与其他元素并排,不能设置宽高,默认宽度就是文字的宽度
- 可用display来改变行内,块级元素性质
元素怎样浮动?
- 元素的水平方向浮动,意味着元素只能左右移动而不能上下移动。
- 一个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
块级元素:独占一行
行内元素:不独占一行
行内元素可以被包含在块级元素中,反之不可
彼此相邻的浮动元素 :
- 如果把几个浮动的元素放到一起,如果有空间的话,它们将彼此相邻。
怎样清除浮动?
- 元素浮动之后,周围的元素会重新排列,为了避免这种情况,使用 clear
5.2display
block:块元素
inline:行内元素
inline-block是块元素,但是可以内联,在一行
5.3Float
Float 属性定义元素在哪个方向浮动,浮动元素会生成一个块级框,直到该块级框的外边缘碰到包含框或者其他的浮动框为止。
Float,会使元素向左或向右移动,其周围的元素也会重新排列。
语法:
- 属性——————值————————描述
- float————left/right/none——————指定一个盒子(元素)是否可以浮动。
- clear————left/right/both/none————指定不允许元素左侧/右侧/两侧/无浮动元素。
5.4父级边框塌陷的问题
solution:
1.增加父级元素高度
#father {
border:1px #000 solid;
height: 800px;
}
2.增加一个空的div标签,且清除浮动
<div class="clear"></div>
.clear{
clear: both;
margin: 0;
padding: 0;
}
3.在父级元素中增加一个overflow
overflow:hidden;
4.父类添加一个伪类:after
#father:after{
content: '';
display: block;
clear: both;
}
5.5对比
-
display
方向不可以控制
-
float
浮动起来的话会脱离标准文档流,所以要解决父级边框塌陷的问题~
6.定位Position
定位属性允许用户为一个元素定位。它也可以将一个元素放在另一个元素后面,并指定一个元素的内容太大时,应该发生什么,元素可以使用的顶部,底部,左侧和右侧属性定位.
指定了元素的定位类型,position 属性的五个值:
- static
- HTML 元素的默认值,就是没有定位,遵循正常的文档流对象。
- relative
- 相对定位元素的定位是相对其正常位置
- fixed
- 窗口滚动或不移动,元素的位置相对于浏览器窗口是固定位置。
- absolute
- 绝对定位的元素的位置相对于最近的已定位父元素,如果元素没有已定位的父元素,那么它的位置相对于< html>
6.1相对定位
相对于原来的位置,进行指定的偏移,相对定位的话,它仍然在标准文档流中,原来的位置会被保留
position:relative
进行小练习:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相对定位</title>
<style>
#box{
width: 300px;
height: 300px;
padding: 10px;
border: 2px solid red;
}
a{
width: 100px;
height: 100px;
text-decoration: none;
background: #ffa1f2;
line-height: 100px;
text-align: center;
color: white;
display: block;
}
a:hover{
background: #47a4ff;
}
.a2,.a4{
position: relative;
left: 200px;
top: -100px;
}
.a5{
position: relative;
left: 100px;
top: -300px;
}
</style>
</head>
<body>
<div id="box">
<a class="a1" href="#">链接1</a>
<a class="a2" href="#">链接2</a>
<a class="a3" href="#">链接3</a>
<a class="a4" href="#">链接4</a>
<a class="a5" href="#">链接5</a>
</div>
</body>
</html>
结果如下:
6.2绝对定位
1、没有父级元素定位的前提下,相对于浏览器定位
2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移~
3、在父级元素范围内移动
相对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在在标准文档流中,原来的位置不会被保留
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #666;
padding: 0;
position: relative;
}
#first{
background-color: cyan;
border: 1px dashed #b27530;
}
#second{
background-color: blue;
border: 1px dashed #255066;
position: absolute;
left: 10px;
}
#third{
background-color: orange;
border: 1px dashed #1c6615;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一行</div>
<div id="second">第二行</div>
<div id="third">第三行</div>
</div>
结果如图:
6.3固定定位
元素的位置相对于浏览器窗口是固定位置。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
height: 1000px;
}
div:nth-of-type(1){ /*绝对定位:相对于浏览器*/
width: 100%;
height: 100px;
background: lightblue;
position: absolute;
line-height: 100px;
text-align: center;
right: 0;
top: 0;
}
div:nth-of-type(2){ /*fixed,固定定位*/
width: 100px;
height: 100px;
background: lightseagreen;
position: fixed;
line-height: 100px;
text-align: center;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>导航</div>
<div>返回顶部</div>
</body>
</html>
6.4 z-index
元素的定位与文档流无关,所以它们可以覆盖页面上的其它元素
z-index属性指定了一个元素的堆叠顺序(哪个元素应该放在前面,或后面)
z-index--------设置元素的堆叠顺序--------值:number/auto
z-index:默认是0,最高无限制
小试牛刀:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/css.css">
</head>
<body>
<div id="content">
<ul>
<li><img src="css/images/backg.jpg" alt=""> </li>
<li class="tipText">Welcome to the Moon</li>
<li class="tipBg"></li>
<li>简介:时间为2099-01-01</li>
<li>说明:地点在月球一号基地</li>
</ul>
</div>
</body>
</html>
css:
#content{
width: 973px;
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: 100%;
height: 25px;
top: 460px;
}
.tipText{
color: white;
/*z-index: 0;*/
}
.tipBg{
background: #000;
opacity: 0.2; /*背景透明度*/
filter: Alpha(opacity=50);
}
/*鼠标悬浮在图片会变透明*/
ul li:nth-child(1):hover{
opacity: 0.4;
filter:alpha(opacity=40);
}
结果如下:
Opacity属性值从0.0 - 1.0。值越小,使得元素更加透明。
Alpha(opacity= x)。 x可以采取的值是从0 - 100。较低的值,使得元素更加透明。
opacity的透明度是针对整个元素,一旦设置了透明度,则背景色,文字颜色都有透明度。
而alpha:只是特定的针对某个方面的透明度。