CSS
css简介
网页分成三个部分:
结构(HTML)
表现(CSS)
行为(JavaScript)
CSS
层叠样式表
网页实际上是一个多层的结构,通过CSS可以分别为网页的每一个层来设置样式
而最终我们能看到只是网页的最上边一层,总之一句话,CSS用来设置网页中元素的样式
🎉第一种方式(内联样式,行内样式):在标签内部通过style属性来设置元素的样式
问题:
使用内联样式,样式只能对一个标签生效,如果希望影响到多个元素必须在每一个元素中都复制一遍并且当样式发生变化时,我们必须要一个一个的修改,非常的不方便
🎉第二种方式(内部样式表):将样式编写到head中的style标签里然后通过CSS的选择器来选中元素并为其设置各种样式
可以同时为多个标签设置样式,并且修改时只需要修改一处即可全部应用
内部样式表更加方便对样式进行复用
问题:
我们的内部样式表只能对一个网页起作用,
它里边的样式不能跨页面进行复用
🎉第三种方式 (外部样式表) 最佳实践:可以将CSS样式编写到一个外部的CSS文件中,然后通过link标签来引入外部的CSS文件
CSS语法
CSS的基本语法:
选择器 声明块
选择器,通过选择器可以选中页面中的指定元素
比如 p 的作用就是选中页面中所有的p元素
声明块,通过声明块来指定要为元素设置的样式
声明块由一个一个的声明组成,一个样式名对应一个样式值,名和值之间以:连接,以;结尾
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
p{
color: red;
font-size: 40px;
}
h1{
color: green;
}
</style>
</head>
<body>
<h1>我是H1</h1>
<p>今天天气真不错!</p>
<p>今天天气真不错!</p>
<p>今天天气真不错!</p>
<p>今天天气真不错!</p>
</body>
</html>
常用选择器
元素选择器
作用:根据标签名来选中指定的元素
语法:标签名{}
例子:p{} h1{} div{}
id选择器
作用:根据元素的id属性值选中一个元素
语法:#id属性值{}
例子:#box{} #red{}
类选择器
作用:根据元素的class属性值选中一组元素
语法:.class属性值
通配选择器
作用:选中页面中的所有元素
语法: *{}
class 是一个标签的属性,它和id类似,不同的是class可以重复使用
可以通过class属性来为元素分组,可以同时为一个元素指定多个class属性
<!DOCTYPE html>
<head>
<title>Document</title>
<style>
p{
color: red;
}
h1{
color: green;
}
#red{
color: red;
}
.blue{
color: blue;
}
.abc{
font-size: 20px;
}
*{
color: red;
}
</style>
</head>
<body>
<h1 class="blue abc">我是标题</h1>
<p>少小离家老大回</p>
<p>乡音无改鬓毛衰</p>
<p id="red">儿童相见不相识</p>
<p>笑问客从何处来</p>
<p class="blue">秋水共长天一色</p>
<p class="blue">落霞与孤鹜齐飞</p>
</body>
</html>
复合选择器
选择器分组(并集选择器)
作用:同时选择多个选择器对应的元素
语法:选择器1,选择器2,选择器3,选择器n{}
#b1,.p1,h1,span,div.red{}
交集选择器
作用:选中同时复合多个条件的元素
语法:选择器1选择器2选择器3选择器n{}
注意点:
交集选择器中如果有元素选择器,必须使用元素选择器开头
<!DOCTYPE html>
<head>
<title>Document</title>
<style>
/* 将class为red的元素设置为红色(字体) */
.red {
color: red;
}
/* 将class为red的div字体大小设置为30px */
div.red {
font-size: 30px;
}
.a.b.c {
color: blue
}
h1,
span {
color: green
}
</style>
</head>
<body>
<div class="red">我是div</div>
<p class="red">我是p元素</p>
<div class="red2 a b c">我是div2</div>
<h1>标题</h1>
<span>哈哈</span>
</body>
</html>
关系选择器
父元素- 直接包含子元素的元素叫做父元素
子元素- 直接被父元素包含的元素是子元素
祖先元素-直接或间接包含后代元素的元素叫做祖先元素,一个元素的父元素也是它的祖先元素
后代元素-直接或间接被祖先元素包含的元素叫做后代元素,子元素也是后代元素
兄弟元素- 拥有相同父元素的元素是兄弟元素
子元素选择器
作用:选中指定父元素的指定子元素
语法:父元素 > 子元素
后代元素选择器:
作用:选中指定元素内的指定后代元素
语法:祖先 后代
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
/* 为div的子元素span设置一个字体颜色红色(为div直接包含的span设置一个字体颜色)*/
div.box>span {
color: orange;
}
div span {
color: skyblue
}
div>p>span {
color: red;
}
/*
选择下一个兄弟
语法:前一个 + 下一个
选择下边所有的兄弟
语法:兄 ~ 弟
*/
p+span {
color: red;
}
p~span {
color: red;
}
</style>
</head>
<body>
<div class="box">
我是一个div
<p>
我是div中的p元素
<span>我是p元素中的span</span>
</p>
<div></div>
<span>我是div中的span元素</span>
<span>我是div中的span元素</span>
<span>我是div中的span元素</span>
<span>我是div中的span元素</span>
</div>
<span>
我是div外的span
</span>
</body>
</html>
属性选择器
[属性名] 选择含有指定属性的元素
[属性名=属性值] 选择含有指定属性和属性值的元素
[属性名^=属性值] 选择属性值以指定值开头的元素
[属性名$=属性值] 选择属性值以指定值结尾的元素
[属性名*=属性值] 选择属性值中含有某值的元素的元素
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
/* p[title]{ */
/* p[title=abc]{ */
/* p[title^=abc]{ */
/* p[title$=abc]{ */
p[title*=e]{
color: orange;
}
</style>
</head>
<body>
<p title="abc">少小离家老大回</p>
<p title="abcdef">乡音无改鬓毛衰</p>
<p title="helloabc">儿童相见不相识</p>
<p>笑问客从何处来</p>
<p>秋水共长天一色</p>
<p>落霞与孤鹜齐飞</p>
</body>
</html>
伪类选择器
伪类(不存在的类,特殊的类)
伪类用来描述一个元素的特殊状态,比如:第一个子元素、被点击的元素、鼠标移入的元素…
伪类一般情况下都是使用:开头
:first-child 第一个子元素
:last-child 最后一个子元素
:nth-child() 选中第n个子元素
特殊值:
n 第n个 n的范围0到正无穷
2n 或 even 表示选中偶数位的元素
2n+1 或 odd 表示选中奇数位的元素
以上这些伪类都是根据所有的子元素进行排序
:first-of-type
:last-of-type
:nth-of-type()
这几个伪类的功能和上述的类似,不通点是他们是在同类型元素中进行排序
:not() 否定伪类,将符合条件的元素从选择器中去除
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
/*将ul里的第一个li设置为红色*/
/* ul > li:first-child{
color: red;
} */
/* ul > li:last-child{
color: red;
} */
/* ul > li:nth-child(2n+1){
color: red;
} */
/* ul > li:nth-child(even){
color: red;
} */
/* ul > li:first-of-type{
color: red;
} */
ul > li:not(:nth-of-type(3)){
color: yellowgreen;
}
</style>
</head>
<body>
<ul>
<span>我是一个span</span>
<li>第〇个</li>
<li>第一个</li>
<li>第二个</li>
<li>第三个</li>
<li>第四个</li>
<li>第五个</li>
</ul>
</body>
</html>
a元素的伪类
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
/* :link 用来表示没访问过的链接(正常的链接)*/
a:link{
color: red;
}
/* :visited 用来表示访问过的链接,由于隐私的原因,所以visited这个伪类只能修改链接的颜色 */
a:visited{
color: orange;
font-size: 50px;
}
/*:hover 用来表示鼠标移入的状态*/
a:hover{
color: aqua;
font-size: 50px;
}
/*:active 用来表示鼠标点击*/
a:active{
color: yellowgreen;
}
</style>
</head>
<body>
<!--
1.没有访问过的链接
2.访问过的链接
-->
<a href="https://www.baidu.com">访问过的链接</a>
<br><br>
<a href="https://www.baidu123.com">没访问过的链接</a>
</body>
</html>
伪元素选择器
伪元素,表示页面中一些特殊的并不真实的存在的元素(特殊的位置)
伪元素使用 :: 开头
::first-letter 表示第一个字母
::first-line 表示第一行
::selection 表示选中的内容
::before 元素的开始
::after 元素的最后
before 和 after 必须结合content属性来使用
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
p::first-letter{
font-size: 50px;
}
p::first-line{
background-color: yellow;
}
p::selection{
background-color: greenyellow;
}
div::before{
content: 'abc';
color: red;
}
div::after{
content: 'haha';
color: blue;
}
</style>
</head>
<body>
<div>Hello Hello How are you</div>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Atque velit modi veniam nisi veritatis tempore laborum nemo ipsa itaque optio. Id odit consequatur mollitia excepturi, minus saepe nostrum vel porro.
</p>
</body>
</html>
样式的继承
样式的继承,我们为一个元素设置的样式同时也会应用到它的后代元素上
继承是发生在祖先后后代之间的
继承的设计是为了方便我们的开发,
利用继承我们可以将一些通用的样式统一设置到共同的祖先元素上,这样只需设置一次即可让所有的元素都具有该样式
注意:并不是所有的样式都会被继承:比如 背景相关的,布局相关等的这些样式都不会被继承。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
p{
color: red;
background-color: orange;
}
div{
color: yellowgreen
}
</style>
</head>
<body>
<p>
我是一个p元素
<span>我是p元素中的span</span>
</p>
<span>我是p元素外的span</span>
<div>
我是div
<span>
我是div中的span
<em>我是span中的em</em>
</span>
</div>
</body>
</html>
选择器的权重
样式的冲突;当我们通过不同的选择器,选中相同的元素,并且为相同的样式设置不同的值时,此时就发生了样式的冲突。
发生样式冲突时,应用哪个样式由选择器的权重(优先级)决定
选择器的权重
内联样式 1,0,0,0
id选择器 0,1,0,0
类和伪类选择器 0,0,1,0
元素选择器 0,0,0,1
通配选择器 0,0,0,0
继承的样式 没有优先级
比较优先级时,需要将所有的选择器的优先级进行相加计算,最后优先级越高,则越优先显示(分组选择器是单独计算的),选择器的累加不会超过其最大的数量级,类选择器在高也不会超过id选择器如果优先级计算后相同,此时则优先使用靠下的样式
可以在某一个样式的后边添加 !important ,则此时该样式会获取到最高的优先级,甚至超过内联样式,
注意:在开发中这个玩意一定要慎用!
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.d1{
background-color: purple !important;
}
.red{
background-color: red;
}
/* div,p,span{
background-color:yellowgreen;
} */
*{
font-size: 50px;
}
div{
font-size: 20px;
}
</style>
</head>
<body>
<div id="box1" class="red d1 d2 d3 d4" style="background-color: chocolate;">我是一个div <span>我是div中的span</span></div>
</body>
</html>
单位
长度单位:
像素
- 屏幕(显示器)实际上是由一个一个的小点点构成的
- 不同屏幕的像素大小是不同的,像素越小的屏幕显示的效果越清晰
- 所以同样的200px在不同的设备下显示效果不一样
百分比
- 也可以将属性值设置为相对于其父元素属性的百分比
- 设置百分比可以使子元素跟随父元素的改变而改变
em
- em是相对于元素的字体大小来计算的
- 1em = 1font-size
- em会根据字体大小的改变而改变
rem
- rem是相对于根元素的字体大小来计算
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
html{
font-size: 30px;
}
.box1{
width:300px;
height: 100px;
background-color: orange;
}
.box2{
width: 50%;
height: 50%;
background-color:aqua;
}
.box3{
font-size: 50px;
/* width: 10em;
height: 10em; */
width: 10rem;
height: 10rem;
background-color: greenyellow;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>
</body>
</html>
颜色
颜色单位:
在CSS中可以直接使用颜色名来设置各种颜色
比如:red、orange、yellow、blue、green … …
但是在css中直接使用颜色名是非常的不方便
RGB值:
RGB通过三种颜色的不同浓度来调配出不同的颜色
R red,G green ,B blue
每一种颜色的范围在 0 - 255 (0% - 100%) 之间
语法:RGB(红色,绿色,蓝色)
RGBA:
就是在rgb的基础上增加了一个a表示不透明度
需要四个值,前三个和rgb一样,第四个表示不透明度
1表示完全不透明 0表示完全透明 .5半透明
十六进制的RGB值:
语法:#红色绿色蓝色
颜色浓度通过 00-ff
如果颜色两位两位重复可以进行简写
#aabbcc --> #abc
HSL值 HSLA值
H 色相(0 - 360)
S 饱和度,颜色的浓度 0% - 100%
L 亮度,颜色的亮度 0% - 100%
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 100px;
height: 100px;
background-color: red;
background-color: rgb(106,153,85);
background-color: rgba(106,153,85,.5);
background-color: #bbffaa;
background-color: #9CDCFE;
background-color: rgb(254, 156, 156);
background-color: hsla(98, 48%, 40%, 0.658);
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
文档流
文档流(normal flow)
网页是一个多层的结构,一层摞着一层
通过CSS可以分别为每一层来设置样式
作为用户来讲只能看到最顶上一层
这些层中,最底下的一层称为文档流,文档流是网页的基础,我们所创建的元素默认都是在文档流中进行排列
对于我们来元素主要有两个状态
在文档流中
不在文档流中(脱离文档流)
元素在文档流中有什么特点:
块元素
块元素会在页面中独占一行(自上向下垂直排列)
默认宽度是父元素的全部(会把父元素撑满)
默认高度是被内容撑开(子元素)
行内元素
行内元素不会独占页面的一行,只占自身的大小
行内元素在页面中左向右水平排列,如果一行之中不能容纳下所有的行内元素
则元素会换到第二行继续自左向右排列(书写习惯一致)
行内元素的默认宽度和高度都是被内容撑开
盒模型
盒模型、盒子模型、框模型(box model)
CSS将页面中的所有元素都设置为了一个矩形的盒子
将元素设置为矩形的盒子后,对页面的布局就变成将不同的盒子摆放到不同的位置
每一个盒子都由一下几个部分组成:
内容区(content)
内边距(padding)
边框(border)
外边距(margin)
内容区(content),元素中的所有的子元素和文本内容都在内容区中排列
内容区的大小由width 和 height两个属性来设置
边框(border),边框属于盒子边缘,边框里边属于盒子内部,出了边框都是盒子的外部
边框的大小会影响到整个盒子的大小
要设置边框,需要至少设置三个样式:
边框的宽度 border-width
边框的颜色 border-color
边框的样式 border-style
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
border-width: 10px;
border-color: red;
border-style: solid;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
边框
边框
框的宽度 border-width
边框的颜色 border-color
边框的样式 border-style
border-width: 10px;
默认值,一般都是 3个像素
border-width可以用来指定四个方向的边框的宽度
值的情况
四个值:上 右 下 左
三个值:上 左右 下
两个值:上下 左右
一个值:上下左右
除了border-width还有一组 border-xxx-width
xxx可以是 top right bottom left
用来单独指定某一个边的宽度
border-color用来指定边框的颜色,同样可以分别指定四个边的边框
规则和border-width一样
border-color也可以省略不写,如果省略了则自动使用color的颜色值
border-style 指定边框的样式
solid 表示实线
dotted 点状虚线
dashed 虚线
double 双线
border-style的默认值是none 表示没有边框
border简写属性,通过该属性可以同时设置边框所有的相关样式,并且没有顺序要求
除了border以外还有四个 border-xxx
border-top
border-right
border-bottom
border-left
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: #bfa;
border-width: 10px;
border-top-width: 10px;
border-left-width: 30px;
border-style: solid dotted dashed double;
border-style: solid;
border: solid 10px orange;
/* border-top: 10px solid red;
border-left: 10px solid red;
border-bottom: 10px solid red; */
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
内边距
内边距(padding)
内容区和边框之间的距离是内边距
一共有四个方向的内边距:
padding-top
padding-right
padding-bottom
padding-left
内边距的设置会影响到盒子的大小
背景颜色会延伸到内边距上
一共盒子的可见框的大小,由内容区 内边距 和 边框共同决定,所以在计算盒子大小时,需要将这三个区域加到一起计算
padding 内边距的简写属性,可以同时指定四个方向的内边距
规则和border-width 一样
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: #bfa;
border: 10px orange solid;
/* padding-top: 100px;
padding-left: 100px;
padding-right: 100px;
padding-bottom: 100px;*/
padding: 10px 20px 30px 40px;
padding: 10px 20px 30px;
padding: 10px 20px;
padding: 10px;
}
.inner {
width: 100%;
height: 100%;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box1">
<div class="inner"></div>
</div>
</body>
</html>
外边距
外边距(margin)
外边距不会影响盒子可见框的大小
但是外边距会影响盒子的位置
一共有四个方向的外边距:
margin-top上外边距,设置一个正值,元素会向下移动
margin-right默认情况下设置margin-right不会产生任何效果
margin-bottom下外边距,设置一个正值,其下边的元素会向下移动
margin-left左外边距,设置一个正值,元素会向右移动
margin也可以设置负值,如果是负值则元素会向相反的方向移动
元素在页面中是按照自左向右的顺序排列的,所以默认情况下如果我们设置的左和上外边距则会移动元素自身,而设置下和右外边距会移动其他元素
margin的简写属性
margin 可以同时设置四个方向的外边距 ,用法和padding一样
margin会影响到盒子实际占用空间
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
border: 10px red solid;
margin-top: 100px;
margin-left: 100px;
margin-bottom: 100px;
margin: 100px;
}
.box2{
width: 220px;
height: 220px;
background-color: yellow
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
水平方向的布局
元素的水平方向的布局:
元素在其父元素中水平方向的位置由以下几个属性共同决定
margin-left
border-left
padding-left
width
padding-right
border-right
margin-right
一个元素在其父元素中,水平布局必须要满足以下的等式
margin-left+border-left+padding-left+width+padding-right+border-right+margin-right = 其父元素内容区的宽度 (必须满足)
以上等式必须满足,如果相加结果使等式不成立,则称为过度约束,则等式会自动调整
调整的情况:如果这七个值中没有为 auto 的情况,则浏览器会自动调整margin-right值以使等式满足
这七个值中有三个值和设置为auto
width
margin-left
maring-right
如果某个值为auto,则会自动调整为auto的那个值以使等式成立
如果将一个宽度和一个外边距设置为auto,则宽度会调整到最大,设置为auto的外边距会自动为0
如果将三个值都设置为auto,则外边距都是0,宽度最大
如果将两个外边距设置为auto,宽度固定值,则会将外边距设置为相同的值
所以我们经常利用这个特点来使一个元素在其父元素中水平居中
示例:
width:xxxpx;
margin:0 auto;
垂直方向的布局
默认情况下父元素的高度被内容撑开
子元素是在父元素的内容区中排列的,如果子元素的大小超过了父元素,则子元素会从父元素中溢出
使用 overflow 属性来设置父元素如何处理溢出的子元素
可选值:
visible,默认值 子元素会从父元素中溢出,在父元素外部的位置显示
hidden 溢出内容将会被裁剪不会显示
scroll 生成两个滚动条,通过滚动条来查看完整的内容
auto 根据需要生成滚动条
overflow-x:
overflow-y:
外边距的折叠
垂直外边距的重叠(折叠):相邻的垂直方向外边距会发生重叠现象
兄弟元素
兄弟元素间的相邻垂直外边距会取两者之间的较大值(两者都是正值)
特殊情况:
如果相邻的外边距一正一负,则取两者的和
如果相邻的外边距都是负值,则取两者中绝对值较大的
兄弟元素之间的外边距的重叠,对于开发是有利的,所以我们不需要进行处理
父子元素
父子元素间相邻外边距,子元素的会传递给父元素(上外边距)
父子外边距的折叠会影响到页面的布局,必须要进行处理
行内元素的盒模型
行内元素的盒模型
行内元素不支持设置宽度和高度
行内元素可以设置padding,但是垂直方向padding不会影响页面的布局
行内元素可以设置border,垂直方向的border不会影响页面的布局
行内元素可以设置margin,垂直方向的margin不会影响布局
display 用来设置元素显示的类型
可选值:
inline 将元素设置为行内元素
block 将元素设置为块元素
inline-block 将元素设置为行内块元素
行内块,既可以设置宽度和高度又不会独占一行
table 将元素设置为一个表格
none 元素不在页面中显示
visibility 用来设置元素的显示状态
可选值:
visible 默认值,元素在页面中正常显示
hidden 元素在页面中隐藏 不显示,但是依然占据页面的位置
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.s1{
background-color: yellow;
margin: 100px;
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
}
a{
display: block;
visibility: hidden;
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<a href="javascript:;">超链接</a>
<a href="javascript:;">超链接</a>
<span class="s1">我是span</span>
<span class="s1">我是span</span>
<div class="box1"></div>
</body>
</html>
盒子的尺寸
默认情况下,盒子可见框的大小由内容区、内边距和边框共同决定
box-sizing 用来设置盒子尺寸的计算方式(设置width和height的作用)
可选值:
ontent-box 默认值,宽度和高度用来设置内容区的大小
border-box 宽度和高度用来设置整个盒子可见框的大小
width 和 height 指的是内容区 和 内边距 和 边框的总大小
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 100px;
height: 100px;
background-color: #bfa;
padding: 10px;
border: 10px red solid;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
轮廓盒圆角
box-shadow 用来设置元素的阴影效果,阴影不会影响页面布局
第一个值 水平偏移量 设置阴影的水平位置 正值向右移动 负值向左移动
第二个值 垂直偏移量 设置阴影的水平位置 正值向下移动 负值向上移动
第三个值 阴影的模糊半径
第四个值 阴影的颜色
outline 用来设置元素的轮廓线,用法和border一模一样
轮廓和边框不同的点,就是轮廓不会影响到可见框的大小
border-radius: 用来设置圆角 圆角设置的圆的半径大小
border-radius 可以分别指定四个角的圆角
四个值 左上 右上 右下 左下
三个值 左上 右上/左下 右下
两个个值 左上/右下 右上/左下
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
box-shadow: 0px 0px 50px rgba(0, 0, 0, .3) ;
}
.box1:hover{
outline: 10px red solid;
}
.box2{
width: 200px;
height: 200px;
background-color: orange;
/* border-top-left-radius: */
/* border-top-right-radius */
/* border-bottom-left-radius: */
/* border-bottom-right-radius: */
/* border-top-left-radius:50px 100px; */
/* 将元素设置为一个圆形 */
border-radius: 50%;
}
</style>
</head>
<body>
<div class="box2"></div>
</body>
</html>
浮动
通过浮动可以使一个元素向其父元素的左侧或右侧移动
使用 float 属性来设置于元素的浮动
可选值:
none 默认值 ,元素不浮动
left 元素向左浮动
right 元素向右浮动
注意,元素设置浮动以后,水平布局的等式便不需要强制成立
元素设置浮动以后,会完全从文档流中脱离,不再占用文档流的位置,
所以元素下边的还在文档流中的元素会自动向上移动
浮动的特点:
1、浮动元素会完全脱离文档流,不再占据文档流中的位置
2、设置浮动以后元素会向父元素的左侧或右侧移动,
3、浮动元素默认不会从父元素中移出
4、浮动元素向左或向右移动时,不会超过它前边的其他浮动元素
5、如果浮动元素的上边是一个没有浮动的块元素,则浮动元素无法上移
6、浮动元素不会超过它上边的浮动的兄弟元素,最多最多就是和它一样高
简单总结:
浮动目前来讲它的主要作用就是让页面中的元素可以水平排列,通过浮动可以制作一些水平方向的布局
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 400px;
height: 200px;
background-color: #bfa;
float: left;
}
.box2{
width: 400px;
height: 200px;
background-color: orange;
float: left;
}
.box3{
width: 200px;
height: 200px;
background-color: yellow;
float: right;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
浮动元素不会盖住文字,文字会自动环绕在浮动元素的周围,所以我们可以利用浮动来设置文字环绕图片的效果
元素设置浮动以后,将会从文档流中脱离,从文档流中脱离后,元素的一些特点也会发生变化
脱离文档流的特点:
块元素:
1、块元素不在独占页面的一行
2、脱离文档流以后,块元素的宽度和高度默认都被内容撑开
行内元素:
行内元素脱离文档流以后会变成块元素,特点和块元素一样
脱离文档流以后,不需要再区分块和行内了
网页的布局
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
header, main, footer{
width: 1000px;
margin: 0 auto;
}
/* 设置头部 */
header{
height: 150px;
background-color: silver;
}
/* 设置主体 */
main{
height: 500px;
background-color: #bfa;
margin: 10px auto;
}
nav, article, aside{
float: left;
height: 100%;
}
/* 设置左侧的导航 */
nav{
width: 200px;
background-color: yellow;
}
/* 设置中间的内容 */
article{
width: 580px;
background-color: orange;
margin: 0 10px;
}
/* 设置右侧的内容 */
aside{
width: 200px;
background-color: pink;
}
/* 设置底部 */
footer{
height: 150px;
background-color: tomato;
}
</style>
</head>
<body>
<!-- 创建头部 -->
<header></header>
<!-- 创建网页的主体 -->
<main>
<!-- 左侧导航 -->
<nav></nav>
<!-- 中间的内容 -->
<article></article>
<!-- 右边的边栏 -->
<aside></aside>
</main>
<!-- 网页的底部 -->
<footer></footer>
</body>
</html>
高度塌陷问题
高度塌陷的问题:
在浮动布局中,父元素的高度默认是被子元素撑开的,
当子元素浮动后,其会完全脱离文档流,子元素从文档流中脱离
将会无法撑起父元素的高度,导致父元素的高度丢失
父元素高度丢失以后,其下的元素会自动上移,导致页面的布局混乱
所以高度塌陷是浮动布局中比较常见的一个问题,这个问题我们必须要进行处理!
BFC(Block Formatting Context) 块级格式化环境
BFC是一个CSS中的一个隐含的属性,可以为一个元素开启BFC,开启BFC该元素会变成一个独立的布局区域
元素开启BFC后的特点:
1.开启BFC的元素不会被浮动元素所覆盖
2.开启BFC的元素子元素和父元素外边距不会重叠
3.开启BFC的元素可以包含浮动的子元素
可以通过一些特殊方式来开启元素的BFC:
1、设置元素的浮动(不推荐)
2、将元素设置为行内块元素(不推荐)
3、将元素的overflow设置为一个非visible的值
常用的方式 为元素设置 overflow:hidden 开启其BFC 以使其可以包含浮动元素
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.outer{
border: 10px red solid;
float: left;
/* display: inline-block; */
overflow: hidden;
}
.inner{
width: 100px;
height: 100px;
background-color: #bfa;
float: left;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
</div>
<div style="width: 200px;height: 200px;background-color:yellow;"></div>
</body>
</html>
clear
clear
作用:清除浮动元素对当前元素所产生的影响
可选值:
left 清除左侧浮动元素对当前元素的影响
right 清除右侧浮动元素对当前元素的影响
both 清除两侧中最大影响的那侧
原理:
置清除浮动以后,浏览器会自动为元素添加一个上外边距,以使其位置不受其他元素的影响
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
div{
font-size: 50px;
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
float: left;
}
.box2{
width: 400px;
height: 150px;
background-color: #ff0;
float: right;
}
.box3{
width: 200px;
height: 200px;
background-color: orange;
/*
由于box1的浮动,导致box3位置上移
也就是box3收到了box1浮动的影响,位置发生了改变
如果我们不希望某个元素因为其他元素浮动的影响而改变位置,
可以通过clear属性来清除浮动元素对当前元素所产生的影响
*/
clear: both;
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3</div>
</body>
</html>
高度塌陷最终解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
border: 10px red solid;
}
.box2{
width: 100px;
height: 100px;
background-color: #bfa;
float: left;
}
.box3{
clear: both;
}
.box1::after{
content: '';
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
<!-- <div class="box3"></div> -->
</div>
</body>
</html>
clearfix
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
}
.box2{
width: 100px;
height: 100px;
background-color: orange;
margin-top: 100px;
}
/* clearfix 这个样式可以同时解决高度塌陷和外边距重叠的问题,当你在遇到这些问题时,直接使用clearfix这个类即可 */
.clearfix::before,
.clearfix::after{
content: '';
display: table;
clear: both;
}
</style>
</head>
<body>
<div class="box1 clearfix">
<div class="box2"></div>
</div>
</body>
</html>
定位简介
定位(position)
定位是一种更加高级的布局手段
通过定位可以将元素摆放到页面的任意位置
使用position属性来设置定位
可选值:
static 默认值,元素是静止的没有开启定位
relative 开启元素的相对定位
absolute 开启元素的绝对定位
fixed 开启元素的固定定位
sticky 开启元素的粘滞定位
相对定位:
当元素的position属性值设置为relative时则开启了元素的相对定位
相对定位的特点:
1.元素开启相对定位以后,如果不设置偏移量元素不会发生任何的变化
2.相对定位是参照于元素在文档流中的位置进行定位的
3.相对定位会提升元素的层级
4.相对定位不会使元素脱离文档流
5.相对定位不会改变元素的性质块还是块,行内还是行内
偏移量(offset)
当元素开启了定位以后,可以通过偏移量来设置元素的位置
top定位元素和定位位置上边的距离
bottom 定位元素和定位位置下边的距离
定位元素垂直方向的位置由top和bottom两个属性来控制
通常情况下我们只会使用其中一
top值越大,定位元素越向下移动
bottom值越大,定位元素越向上移动
left
定位元素和定位位置的左侧距离
right
定位元素和定位位置的右侧距离
定位元素水平方向的位置由left和right两个属性控制
通常情况下只会使用一个
left越大元素越靠右 right越大元素越靠左
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
body{
font-size: 60px;
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
}
.box2{
width: 200px;
height: 200px;
background-color: orange;
position: relative;
left: 100px;
top: -200px;
}
.box3{
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3</div>
</body>
</html>
绝对定位
绝对定位
当元素的position属性值设置为absolute时,则开启了元素的绝对定位
绝对定位的特点:
1.开启绝对定位后,如果不设置偏移量元素的位置不会发生变化
2.开启绝对定位后,元素会从文档流中脱离
3.绝对定位会改变元素的性质,行内变成块,块的宽高被内容撑开
4.绝对定位会使元素提升一个层级
5.绝对定位元素是相对于其包含块进行定位的
包含块( containing block )
正常情况下:
包含块就是离当前元素最近的祖先块元素
绝对定位的包含块:
包含块就是离它最近的开启了定位的祖先元素,
如果所有的祖先元素都没有开启定位则根元素就是它的包含块
html(根元素、初始包含块)
固定定位
固定定位:
将元素的position属性设置为fixed则开启了元素的固定定位
固定定位也是一种绝对定位,所以固定定位的大部分特点都和绝对定位一样
唯一不同的是固定定位永远参照于浏览器的视口进行定位
固定定位的元素不会随网页的滚动条滚动
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
body{
font-size: 60px;
height: 2000px;
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
}
.box2{
width: 200px;
height: 200px;
background-color: orange;
position: fixed;
left: 0;
top: 0;
}
.box3{
width: 200px;
height: 200px;
background-color: yellow;
}
.box4{
width: 400px;
height: 400px;
background-color: tomato;
}
.box5{
width: 300px;
height: 300px;
background-color: aliceblue;
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box4">
4
<div class="box5">
5
<div class="box2">2</div>
</div>
</div>
<div class="box3">3</div>
</body>
</html>
绝对定位元素的布局
水平布局
left + margin-left + border-left + padding-left + width + padding-right + border-right + margin-right + right = 包含块的内容区的宽度
当我们开启了绝对定位后:
水平方向的布局等式就需要添加left 和 right 两个值,此时规则和之前一样只是多添加了两个值:
当发生过度约束:
如果9个值中没有 auto 则自动调整right值以使等式满足
如果有auto,则自动调整auto的值以使等式满足
可设置auto的值
margin width left right
因为left 和 right的值默认是auto,所以如果不指定left和right
则等式不满足时,会自动调整这两个值
垂直方向布局的等式的也必须要满足
top + margin-top/bottom + padding-top/bottom + border-top/bottom + height = 包含块的高度
元素的层级
对于开启了定位元素,可以通过z-index属性来指定元素的层级
z-index需要一个整数作为参数,值越大元素的层级越高
素的层级越高越优先显示
如果元素的层级一样,则优先显示靠下的元素
祖先的元素的层级再高也不会盖住后代元素
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
body {
font-size: 60px;
}
.box1 {
width: 200px;
height: 200px;
background-color: #bfa;
position: absolute;
z-index: 3;
}
.box2 {
width: 200px;
height: 200px;
background-color: rgba(255, 0, 0, .3);
position: absolute;
top: 50px;
left: 50px;
z-index: 1;
}
.box3 {
width: 200px;
height: 200px;
background-color: yellow;
position: absolute;
top: 100px;
left: 100px;
z-index: 3;
}
.box4 {
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3
<div class="box4">4</div>
</div>
</body>
</html>
京东左侧导航条
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>京东的左侧导航</title>
<link rel="stylesheet" href="./css/reset.css">
<style>
/* 设置body */
body{
/* 设置一个网页的背景,以使我们方便查看 */
background-color: #bfa;
}
/* 设置菜单外部容器 */
.left-nav{
/* 设置宽度 */
width: 190px;
/* 设置高度 */
height: 450px;
/* 设置padding */
padding: 10px 0;
/* 设置一个背景颜色 */
background-color: #fff;
margin: 50px auto;
}
/* 设置菜单内部的item */
.left-nav .item{
height: 25px;
/* 要让一个文字在父元素中垂直居中,只需将父元素的line-height设置为一个和父元素height一样的值 */
line-height: 25px;
/* 设置item的右内边距,将文字向内移动 */
padding-left: 18px;
/* 设置字体大小 */
font-size: 12px;
}
/* 设置/的距离 */
.item .line{
padding: 0 2px;
}
/* 为item设置一个鼠标移入的状态 */
.item:hover{
background-color: #d9d9d9;
}
/* 设置超链接的样式 */
.item a{
/* 设置字体大小 */
font-size: 14px;
/* 设置字体的颜色 */
color: #333;
/* 去除下划线 */
text-decoration: none;
}
/* 设置超链接的hover的样式 */
.item a:hover{
color: #c81623;
}
</style>
</head>
<body>
<!-- 创建一个外部的容器 nav(div) div(div) ul(li) -->
<nav class="left-nav">
<div class="item">
<a href="#">家用电器</a>
</div>
<div class="item">
<a href="#">手机</a><span class='line'>/</span><a href="#">运营商</a><span class='line'>/</span><a href="#">数码</a>
</div>
<div class="item">
<a href="#">电脑</a><span class='line'>/</span><a href="#">办公</a>
</div>
<div class="item">
<a href="#">家居</a><span class='line'>/</span><a href="#">家具</a><span class='line'>/</span><a href="#">家装</a><span class='line'>/</span><a href="#">厨具</a>
</div>
<div class="item">
<a href="#">男装</a><span class='line'>/</span><a href="#">女装</a><span class='line'>/</span><a href="#">童装</a><span class='line'>/</span><a href="#">内衣</a>
</div>
<div class="item">
<a href="#">美妆</a><span class='line'>/</span><a href="#">个护清洁</a><span class='line'>/</span><a href="#">宠物</a>
</div>
<div class="item">
<a href="#">女鞋</a><span class='line'>/</span><a href="#">箱包</a><span class='line'>/</span><a href="#">钟表</a><span class='line'>/</span><a href="#">珠宝</a>
</div>
<div class="item">
<a href="#">男鞋</a><span class='line'>/</span><a href="#">运动</a><span class='line'>/</span><a href="#">户外</a>
</div>
<div class="item">
<a href="#">房产</a><span class='line'>/</span><a href="#">汽车</a><span class='line'>/</span><a href="#">汽车用品</a>
</div>
<div class="item">
<a href="#">母婴</a><span class='line'>/</span><a href="#">玩具乐器</a>
</div>
<div class="item">
<a href="#">食品</a><span class='line'>/</span><a href="#">酒类</a><span class='line'>/</span><a href="#">生鲜</a><span class='line'>/</span><a href="#">特产</a>
</div>
<div class="item">
<a href="#">艺术</a><span class='line'>/</span><a href="#">礼品鲜花</a><span class='line'>/</span><a href="#">农资绿植</a>
</div>
<div class="item">
<a href="#">医药保健</a><span class='line'>/</span><a href="#">计生情趣</a>
</div>
<div class="item">
<a href="#">图书</a><span class='line'>/</span><a href="#">文娱</a><span class='line'>/</span><a href="#">电子书</a>
</div>
<div class="item">
<a href="#">机票</a><span class='line'>/</span><a href="#">酒店</a><span class='line'>/</span><a href="#">旅游</a><span class='line'>/</span><a href="#">生活</a>
</div>
<div class="item">
<a href="#">理财</a><span class='line'>/</span><a href="#">众筹</a><span class='line'>/</span><a href="#">白条</a><span class='line'>/</span><a href="#">保险</a>
</div>
<div class="item">
<a href="#">安装</a><span class='line'>/</span><a href="#">维修</a><span class='line'>/</span><a href="#">清洗</a><span class='line'>/</span><a href="#">二手</a>
</div>
<div class="item">
<a href="#">工业品</a>
</div>
</nav>
</body>
</html>
w3导航条
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>导航条</title>
<link rel="stylesheet" href="./css/reset.css">
<style>
/* 设置nav的大小 */
.nav{
/* 设置宽度和高度 */
width: 1210px;
height: 48px;
/* 设置背景颜色 */
background-color: #E8E7E3;
margin:100px auto;
}
/* 设置nav中li */
.nav li{
/* 设置li向左浮动,已使菜单横向排列 */
float: left;
/* 设置li的高度 */
/* height: 48px; */
/* 将文字在父元素中垂直居中 */
line-height: 48px;
}
/* 设置a的样式 */
.nav a{
/* 将a转换为块元素 */
display: block;
/* 去除下划线 */
text-decoration: none;
/* 设置字体颜色 */
color: #777777;
/* 修改字体大小 */
font-size: 18px;
padding: 0 39px;
}
.nav li:last-child a{
padding: 0 42px 0 41px;
}
/* 设置鼠标移入的效果 */
.nav a:hover{
background-color: #3F3F3F;
color: #E8E7E3;
}
</style>
</head>
<body>
<!-- 创建导航条的结构 -->
<ul class="nav">
<li>
<a href="#">HTML/CSS</a>
</li>
<li>
<a href="#">Browser Side</a>
</li>
<li>
<a href="#">Server Side</a>
</li>
<li>
<a href="#">Programming</a>
</li>
<li>
<a href="#">XML</a>
</li>
<li>
<a href="#">Web Building</a>
</li>
<li>
<a href="#">Reference</a>
</li>
</ul>
</body>
</html>
京东的轮播图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./css/reset.css">
<style>
/* 设置图片的容器 */
.img-list{
width: 590px;
height: 470px;
margin: 100px auto;
/*
为ul开启相对定位,目的是使ul中的pointer可以相对于ul定位而不是相对于初始包含块(html)去定位
*/
position: relative;
}
/* 设置li */
.img-list li{
position: absolute;
}
/* 通过修改元素的层级来显示指定的图片 */
.img-list li:nth-child(4){
z-index: 1;
}
/* 设置导航点的样式 */
.pointer{
position: absolute;
z-index: 9999;
bottom: 20px;
left: 40px;
}
.pointer a{
/* 设置元素向左浮动 */
float: left;
width: 10px;
height: 10px;
margin: 0px 2px;
border-radius: 50%;
background-color: rgba(255, 255, 255, .3);
/* 将背景颜色值设置到内容区,边框和内边距不在有背景颜色 */
background-clip: content-box;
border: 2px solid transparent;
}
.pointer a.active,
.pointer a:hover{
background-color: #fff;
border: 2px solid rgba(255, 255, 255, .3);
}
</style>
</head>
<body>
<ul class="img-list">
<li><a href="javascript:;"><img src="./img/05/1.jpg"></a></li>
<li><a href="javascript:;"><img src="./img/05/2.jpg"></a></li>
<li><a href="javascript:;"><img src="./img/05/3.jpg"></a></li>
<li><a href="javascript:;"><img src="./img/05/4.jpg"></a></li>
<li><a href="javascript:;"><img src="./img/05/5.jpg"></a></li>
<li><a href="javascript:;"><img src="./img/05/6.jpg"></a></li>
<li><a href="javascript:;"><img src="./img/05/7.jpg"></a></li>
<li><a href="javascript:;"><img src="./img/05/8.jpg"></a></li>
<div class="pointer">
<a class="active" href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
</div>
</ul>
</body>
</html>
京东顶部导航条
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>京东顶部导航条</title>
<!-- 引入重置样式表 -->
<link rel="stylesheet" href="./css/reset.css">
<!-- 引用图标字体 -->
<link rel="stylesheet" href="./fa/css/all.css">
<style>
.clearfix::before,
.clearfix::after{
content: '';
display: table;
clear: both;
}
body{
/* 设置字体 */
font:12px/1.5 Microsoft YaHei,Heiti SC,tahoma,arial,Hiragino Sans GB,"\5B8B\4F53",sans-serif;
}
/* 设置外部容器的样式 */
.top-bar-wrapper{
/* 设置宽度 */
width: 100%;
/* 设置背景颜色 */
background-color: #E3E4E5;
height: 30px;
/* 设置行高,没有设置高度 使文字垂直居中 */
line-height: 30px;
/* 设置下边框 */
border-bottom: 1px #ddd solid
}
/* 设置内部容器的样式 */
.top-bar{
/* 固定宽度 */
width: 1190px;
/* 设置水平居中 */
margin: 0 auto;
position: relative;
}
/* 设置字体样式 */
.top-bar a ,
.top-bar span,
.top-bar i{
color: #999;
text-decoration: none;
}
.top-bar a:hover,
.top-bar a.highlight{
color: #f10215;
}
/* 设置location */
.location{
float: left;
}
/* 设置location下的小图标 */
.location .fas{
color: #f10215;
}
/* 设置城市列表的效果 */
.location .city-list{
display: none;
width: 320px;
height: 436px;
background-color: white;
border: 1px solid rgb(204, 204, 204);
/* 设置绝对定位,使其不占据页面的位置 */
position: absolute;
top:31px;
z-index: 999;
box-shadow: 0 2px 2px rgba(0, 0, 0, .2)
}
/* 当鼠标移入到location时,让city-list显示 */
.location:hover .city-list{
display: block;
}
.current-city{
padding: 0 10px;
border: 1px solid transparent;
border-bottom: none;
position: relative;
z-index: 9999;
}
/* 设置current-city的移入的效果 */
.location:hover .current-city{
background-color: white;
border: 1px solid rgb(204, 204, 204);
border-bottom: none;
padding-bottom: 1px;
}
/* 设置shortcut */
.shortcut{
float: right;
}
/* 设置分割线 */
.shortcut .line{
width: 1px;
height: 10px;
background-color: rgb(204, 202, 202);
margin: 12px 12px 0;
}
/* 设置li */
.shortcut li{
float: left;
}
</style>
</head>
<body>
<!-- 创建外围的容器 -->
<div class="top-bar-wrapper">
<!-- 创建内部容器 -->
<div class="top-bar clearfix">
<!-- 左侧的菜单 -->
<div class="location">
<div class="current-city">
<i class="fas fa-map-marker-alt"></i>
<a href="javascript:;">北京</a>
</div>
<!-- 放置城市列表的div -->
<div class="city-list">
</div>
</div>
<!-- 右侧的菜单 -->
<ul class="shortcut clearfix">
<li>
<a href="javascript:;">你好,请登录</a>
<a class="highlight" href="javascript:;">免费注册</a>
</li>
<!-- 分割线 -->
<li class="line"></li>
<li><a href="javascript:;">我的订单</a></li>
<li class="line"></li>
<li>
<a href="javascript:;">我的京东</a>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li><a href="javascript:;">京东会员</a></li>
<li class="line"></li>
<li>
<a class="highlight" href="javascript:;">企业采购</a>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li>
<span>客户服务</span>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li>
<span>网站导航</span>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li>
<span>手机京东</span>
</li>
</ul>
</div>
</div>
</body>
</html>
字体
字体相关的样式
color 用来设置字体颜色
font-size 字体的大小
和font-size相关的单位
em 相当于当前元素的一个font-size
rem 相对于根元素的一个font-size
font-family 字体族(字体的格式)
可选值:
serif 衬线字体
sans-serif 非衬线字体
monospace 等宽字体
指定字体的类别,浏览器会自动使用该类别下的字体
font-family 可以同时指定多个字体,多个字体间使用,隔开,字体生效时优先使用第一个,第一个无法使用则使用第二个 以此类推
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
/*
font-face可以将服务器中的字体直接提供给用户去使用
问题:
1.加载速度
2.版权
3.字体格式
*/
@font-face {
/* 指定字体的名字 */
font-family:'myfont' ;
/* 服务器中字体的路径 */
src: url('./font/ZCOOLKuaiLe-Regular.ttf') format("truetype");
}
p{
color: blue;
font-size: 40px;
/* font-family: 'Courier New', Courier, monospace; */
font-family: myfont;
}
</style>
</head>
<body>
<p>
今天天气真不错,Hello Hello How are you!
</p>
</body>
</html>
font 可以设置字体相关的所有属性
语法:
font: 字体大小/行高 字体族
行高 可以省略不写 如果不写使用默认值
font-weight 字重 字体的加粗
可选值:
normal 默认值 不加粗
bold 加粗
100-900 九个级别(没什么用)
font-style 字体的风格
normal 正常的
italic 斜体
图标字体
图标字体(iconfont)
在网页中经常需要使用一些图标,可以通过图片来引入图标,但是图片大小本身比较大,并且非常的不灵活
所以在使用图标时,我们还可以将图标直接设置为字体,然后通过font-face的形式来对字体进行引入
这样我们就可以通过使用字体的形式来使用图标
fontawesome 使用步骤
1.下载 https://fontawesome.com/
2.解压
3.将css和webfonts移动到项目中
4.将all.css引入到网页中
5.使用图标字体,直接通过类名来使用图标字体
class=“fas fa-bell”
class=“fab fa-accessible-icon”
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<link rel="stylesheet" href="./fa/css/all.css">
</head>
<body>
<i class="fas fa-bell" style="font-size:80px; color:red;"></i>
<i class="fas fa-bell-slash"></i>
<i class="fab fa-accessible-icon"></i>
<i class="fas fa-otter" style="font-size: 160px; color:green;"></i>
</body>
</html>
通过伪元素来设置图标字体
1.找到要设置图标的元素通过before或after选中
2.在content中设置字体的编码
3.设置字体的样式
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<link rel="stylesheet" href="./fa/css/all.css">
<style>
li{
list-style: none;
}
li::before{
content: '\f1b0';
font-family: 'Font Awesome 5 Free';
font-weight: 900;
color: blue;
margin-right: 10px;
}
</style>
</head>
<body>
<!-- <i class="fas fa-cat"></i> -->
<ul>
<li>锄禾日当午</li>
<li>汗滴禾下土</li>
<li>谁知盘中餐</li>
<li>粒粒皆辛苦</li>
</ul>
</body>
</html>
通过实体来使用图标字体:
&#x图标的编码;
<span class="fas"></span>
行高
行高(line height)行高指的是文字占有的实际高度
可以通过line-height来设置行高
行高可以直接指定一个大小(px em)
也可以直接为行高设置一个整数,如果是一个整数的话,行高将会是字体的指定的倍数
行高经常还用来设置文字的行间距,行间距 = 行高 - 字体大小
字体框:字体框就是字体存在的格子,设置font-size实际上就是在设置字体框的高度
行高会在字体框的上下平均分配
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
div{
font-size: 50px;
/* 可以将行高设置为和高度一样的值,使单行文字在一个元素中垂直居中 */
line-height: 200px;
border: 1px red solid;
}
</style>
</head>
<body>
<div>今天天气这不错 Hello hello 今天天气这不错 Hello hello 今天天气这不错 Hello hello 今天天气这不错 Hello hello</div>
</body>
</html>
文本样式
text-align 文本的水平对齐
可选值:
left 左侧对齐
right 右对齐
center 居中对齐
justify 两端对齐
vertical-align 设置元素垂直对齐的方式
可选值:
baseline 默认值 基线对齐
top 顶部对齐
bottom 底部对齐
middle 居中对齐
text-decoration 设置文本修饰
可选值:
none 什么都没有
underline 下划线
line-through 删除线
overline 上划线
white-space 设置网页如何处理空白
可选值:
normal 正常
nowrap 不换行
pre 保留空白
背景1
background-image 设置背景图片
可以同时设置背景图片和背景颜色,这样背景颜色将会成为图片的背景色
如果背景的图片小于元素,则背景图片会自动在元素中平铺将元素铺满
如果背景的图片大于元素,将会一个部分背景无法完全显示
如果背景图片和元素一样大,则会直接正常显示
background-repeat 用来设置背景的重复方式
可选值:
repeat 默认值 , 背景会沿着x轴 y轴双方向重复
repeat-x 沿着x轴方向重复
repeat-y 沿着y轴方向重复
no-repeat 背景图片不重复
background-position 用来设置背景图片的位置
设置方式:
通过 top left right bottom center 几个表示方位的词来设置背景图片的位置
使用方位词时必须要同时指定两个值,如果只写一个则第二个默认就是center
通过偏移量来指定背景图片的位置:
水平方向的偏移量 垂直方向变量
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 500px;
height: 500px;
/*background-color 设置背景颜色*/
background-color: #bfa;
background-image: url("./img/1.png");
background-repeat: no-repeat;
background-position: -50px 300px;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
背景2
background-clip
可选值:
border-box 默认值,背景会出现在边框的下边
padding-box 背景不会出现在边框,只出现在内容区和内边距
content-box 背景只会出现在内容区
background-origin 背景图片的偏移量计算的原点
padding-box 默认值,background-position从内边距处开始计算
content-box 背景图片的偏移量从内容区处计算
border-box 背景图片的变量从边框处开始计算
background-size 设置背景图片的大小
第一个值表示宽度
第二个值表示高度
如果只写一个,则第二个值默认是 auto
cover 图片的比例不变,将元素铺满
contain 图片比例不变,将图片在元素中完整显示
background-attachment
背景图片是否跟随元素移动
可选值:
scroll 默认值 背景图片会跟随元素移动
fixed 背景会固定在页面中,不会随元素移动
backgound 背景相关的简写属性,所有背景相关的样式都可以通过该样式来设置,并且该样式没有顺序要求,也没有哪个属性是必须写的
注意:
background-size必须写在background-position的后边,并且使用/隔开 background-position/background-size
background-origin background-clip 两个样式 ,orgin要在clip的前边
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 500px;
height: 500px;
overflow: auto;
background-color: #bfa;
background-image: url("./img/2.jpg");
padding: 10px;
background-origin: border-box;
background-clip: content-box;
background-size: contain;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
背景练习1
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 990px;
height: 32px;
background-image: url(./bg.png);
background-repeat: repeat-x;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
效果图
按钮练习2
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
a:link{
display: block;
width: 93px;
height: 29px;
background-image: url(./link.png);
}
a:hover{
background-image: url(./hover.png);
}
a:active{
background-image: url(./active.png);
}
/*图片属于网页中的外部资源,外部资源都需要浏览器单独发送请求加载,
浏览器加载外部资源时是按需加载的,用则加载,不用则不加载
像我们上边的练习link会首先加载,而hover和active会在指定状态触发时才会加载*/
</style>
</head>
<body>
<a href="javascript:;"></a>
</body>
</html>
雪碧图
解决图片闪烁的问题:
可以将多个小图片统一保存到一个大图片中,然后通过调整background-position来显示的图片
这样图片会同时加载到网页中 就可以有效的避免出现闪烁的问题
这个技术在网页中应用十分广泛,被称为CSS-Sprite,这种图我们称为雪碧图
雪碧图的使用步骤:
1.先确定要使用的图标
2.测量图标的大小
3.根据测量结果创建一个元素
4.将雪碧图设置为元素的背景图片
5.设置一个偏移量以显示正确的图片
雪碧图的特点:
一次性将多个图片加载进页面,降低请求的次数,加快访问速度,提升用户的体验
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
a:link{
display: block;
width: 93px;
height: 29px;
background-image: url(./btn.png);
}
a:hover{
background-position: -93px 0;
}
a:active{
background-position: -186px 0;
}
/*图片属于网页中的外部资源,外部资源都需要浏览器单独发送请求加载,
浏览器加载外部资源时是按需加载的,用则加载,不用则不加载
像我们上边的练习link会首先加载,而hover和active会在指定状态触发时才会加载*/
.box1{
width: 128px;
height: 46px;
background-image: url(./amazon-sprite_.png);
background-position: ;
}
.box2{
width: 42px;
height: 30px;
background-image: url(./amazon-sprite_.png);
background-position: -58px -338px;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<a href="javascript:;"></a>
</body>
</html>
线性渐变
通过渐变可以设置一些复杂的背景颜色,可以实现从一个颜色向其他颜色过渡的效果
!!渐变是图片,需要通过background-image来设置
线性渐变,颜色沿着一条直线发生变化
linear-gradient()
linear-gradient(red,yellow) 红色在开头,黄色在结尾,中间是过渡区域
线性渐变的开头,我们可以指定一个渐变的方向
to left
to right
to bottom
to top
deg deg表示度数
turn 表示圈
渐变可以同时指定多个颜色,多个颜色默认情况下平均分布,也可以手动指定渐变的分布情况
repeating-linear-gradient() 可以平铺的线性渐变
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
/* background-color: red; */
background-image: linear-gradient(to top, blue ,yellow);
/* background-image: linear-gradient(45deg, blue ,yellow); */
/* background-image: linear-gradient(to top left, blue ,yellow); */
/* background-image: linear-gradient(turn, blue ,yellow); */
/* 也可以手动指定渐变的分布情况 指定纯色的起始位置 */
background-image: linear-gradient( blue 50px ,yellow 70px);
/* 可以平铺的线性渐变:重复四次*/
background-image: repeating-linear-gradient(red 50px,yellow 100px);
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
径向渐变
radial-gradient() 径向渐变(放射性的效果)
默认情况下径向渐变的形状根据元素的形状来计算的
正方形 --> 圆形
长方形 --> 椭圆形
我们也可以手动指定径向渐变的大小
circle
ellipse
也可以指定渐变的位置
语法:
radial-gradient(大小 at 位置, 颜色 位置 ,颜色 位置 ,颜色 位置)
大小:
circle 圆形
ellipse 椭圆
closest-side 近边
closest-corner 近角
farthest-side 远边
farthest-corner 远角
位置:
top right left center bottom
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-image: radial-gradient(circle ,red, yellow);
/* 指定渐变位置 */
background-image: radial-gradient(100px 100px at 0 0 ,red, yellow);
/*近边*/
background-image: radial-gradient(closest-side at 50px 100px ,red, yellow);
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
电影卡片练习
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<link rel="stylesheet" href="../exercise/css/reset.css">
<link rel="stylesheet" href="../exercise/fa/css/all.css">
<style>
.outer{
width: 240px;
margin: 0 auto;
/* 设置阴影 */
box-shadow: 0 0 10px rgba(0, 0,0, 3);
}
/* 设置图片 */
.img-wrapper img{
width: 100%;
vertical-align: bottom;
}
.info{
padding: 0 18px;
}
/* 设置标题 */
.info .title{
color: #717171;
font-size: 18px;
margin: 13px 0 15px 0;
}
.info .category{
color: #acaaaa;
font-size: 14px;
}
.info .category i{
margin-left: 4px;
margin-right: 7px;
}
/* 设置简介的样式 */
.info .intro{
color: #acaaaa;
font-size: 14px;
margin-left: 4px;
margin-top: 18px;
line-height: 20px;
margin-bottom: 18px;
}
/* 设置下边的内容*/
.star-wrapper{
height: 46px;
line-height: 46px;
border-top: 1px solid #e9e9e9;
color: #ddd;
padding: 0 16px;
}
/* 设置星星的样式 */
.star{
float: left;
}
.star-wrapper .light{
color: #b9cb41;
}
.star-wrapper .weibo{
float: right;
}
</style>
</head>
<body>
<!-- 创建外部容器 -->
<div class="outer">
<!-- 创建图片容器 -->
<div class="img-wrapper">
<img src="../exercise/img/10/1.jpg" alt="">
</div>
<!-- 创建内容区容器 -->
<div class="info">
<h2 class="title">
Animation films
</h2>
<h3 class="category">
<i class="fas fa-map-marker-alt"></i>
Animation
</h3>
<p class="intro">
Learn ipsum is simply dummy text of the printing & typesetting industry.
</p>
</div>
<!-- 创建评分容器 -->
<div class="star-wrapper">
<!-- 创建星星 -->
<ul class="star">
<li class="fas fa-star light"></li>
<li class="fas fa-star light"></li>
<li class="fas fa-star"></li>
<li class="fas fa-star"></li>
</ul>
<!-- 创建 -->
<ul class="weibo">
<li class="fab fa-weibo"></li>
</ul>
</div>
</div>
</body>
</html>
效果展示:
表格的简介
在table中使用tr表示表格中的一行,有几个tr就有几行
在tr中使用td表示一个单元格,有几个td就有几个单元格
rowspan 纵向的合并单元格
colspan 横向的合并单元格
<!DOCTYPE html>
<html lang="en">
<head>
<style>
table{
width: 500px;
height: 100px;
}
</style>
</head>
<body>
<table border="1" >
<tr>
<td>1</td>
<td>2</td>
<td rowspan="2">3</td>
<td>4</td>
</tr>
<tr>
<td>11</td>
<td>22</td>
<td colspan="2">33</td>
<td>44</td>
</tr>
</table>
</body>
</html>
长表格
可以将一个表格分成三个部分:
头部 thead
主体 tbody
底部 tfoot
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<table border="1" width='50%' align="center">
<thead>
<tr>
<th>日期</th>
<th>收入</th>
<th>支出</th>
<th>合计</th>
</tr>
</thead>
<tbody>
<tr>
<td>2000.1.1</td>
<td>500</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>2000.1.1</td>
<td>500</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>2000.1.1</td>
<td>500</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>2000.1.1</td>
<td>500</td>
<td>200</td>
<td>300</td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td>合计</td>
<td>300</td>
</tr>
</tfoot>
</table>
</body>
</html>
表格的样式
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
table{
width: 50%;
border: 1px solid black;
/*border-spacing:指定边框之间的距离*/
border-spacing: 0px;
/*border-collapse:设置边框的合并 */
border-collapse: collapse;
}
td{
border: 1px solid black;
/* 默认情况下元素在td中是垂直居中的 可以通过 vertical-align 来修改*/
vertical-align: middle;
text-align: center;
}
/*隔行变色*/
/* 如果表格中没有使用tbody而是直接使用tr,那么浏览器会自动创建一个tbody,并且将tr全都放到tbody中,tr不是table的子元素 */
tr:nth-child(odd){
background-color: pink;
}
/* 希望2在1的正中间 */
.box1{
width: 300px;
height: 300px;
background-color:pink;
/* 将元素设置为单元格 */
display: table-cell;
vertical-align: middle;
}
.box2{
width: 100px;
height: 100px;
background-color: aqua;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
<table>
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>地址</td>
</tr>
<tr>
<td>1</td>
<td>孙悟空</td>
<td>男</td>
<td>18</td>
<td>花果山</td>
</tr>
<tr>
<td>2</td>
<td>猪八戒</td>
<td>男</td>
<td>28</td>
<td>高老庄</td>
</tr>
<tr>
<td>3</td>
<td>沙和尚</td>
<td>男</td>
<td>38</td>
<td>流沙河</td>
</tr>
<tr>
<td>4</td>
<td>唐僧</td>
<td>男</td>
<td>16</td>
<td>女儿国</td>
</tr>
</table>
</body>
</html>
表单的简介
表单:
在现实生活中表单用于提交数据
在网页中也可以使用表单,网页中的表单用于将本地的数据提交给远程的服务器
使用form标签来创建一个表单
form的属性
action 表单要提交的服务器的地址
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="../07_HTML/target.html">
<!-- 文本框 注意数据要提交到服务器,必须要指定一个name属性值 -->
文本框<input type="text" name="hello">
<br>
<!-- 密码框 -->
密码框<input type="password" name="密码">
<br>
<br>
<!-- 提交按钮 -->
<input type="submit" value="提交">
<br>
<br>
<!-- 单选按钮:必须有相同的name属性
像这种选择框,必须要指定一个value属性,value属性最终会作为用户的填写的值传递给服务器
- checked 可以将单选按钮设置为默认选中 -->
单选按钮<input type="radio" name="1" value="a">
<input type="radio" name="1" value="b">
<br>
<br>
<!-- 多选框 -->
多选框
<input type="checkbox" name="1" value="a">
<input type="checkbox" name="2" value="b">
<input type="checkbox" name="3" value="c">
<br><br>
<!-- 下拉列表 -->
<select name="4" id="">
<option value="i">选项一</option>
<option value="ii">选项二</option>
<option value="iii">选项三</option>
</select>
</form>
</body>
</html>
表单补充
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="../07_HTML/target.html">
<input type="text" name="username">
<br><br>
<!-- 提交按钮 -->
<input type="submit" value="提交">
<!-- 普通的按钮 -->
<input type="button" value="按钮">
<!-- 重置按钮 -->
<input type="reset" value="重置">
<br><br>
<!-- 另一种写法 -->
<button type="submit">提交</button>
<button type="button">按钮</button>
<button type="reset">重置</button>
<br><br>
<!-- 提交颜色:兼容性不好 -->
<input type="color">
<!-- email-->
<br><br>
<input type="email">
<input type="submit" value="填写你的邮件">
<br><br>
<!--
autocomplete="off" 关闭自动补全
readonly 将表单项设置为只读,数据会提交
disabled 将表单项设置为禁用,数据不会提交
autofocus 设置表单项自动获取焦点 -->
<input type="text" name="username" value="hello" readonly>
<br><br>
<input type="text" name="username" autofocus>
<br><br>
<input type="text" name="b">
</form>
</body>
</html>
项目练习-小米官网顶部导航条
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>小米官网</title>
<!-- 引入重置样式表 -->
<link rel="stylesheet" href="./css/reset.css">
<!-- 引入图标字体-->
<link rel="stylesheet" href="./fa/css/all.css">
<!-- 引入当前页面样式表 -->
<link rel="stylesheet" href="./css/index.css">
<!-- 引入公共样式表 -->
<link rel="stylesheet" href="./css/base.css">
<style>
</style>
</head>
<body>
<!-- 顶部导航条 -->
<!-- 创建顶部导航条外部容器 -->
<div class="topbar-wrapper">
<!-- 创建顶部导航条内部容器 -->
<div class="topbar w clearfix">
<ul class="service">
<li><a href="javascript;:">小米商城</a></li>
<li class="line">|</li>
<li><a href="javascript;:">MIUI</a></li>
<li class="line">|</li>
<li><a href="javascript;:">lOT</a></li>
<li class="line">|</li>
<li><a href="javascript;:">云服务</a></li>
<li class="line">|</li>
<li><a href="javascript;:">金融</a></li>
<li class="line">|</li>
<li><a href="javascript;:">有品</a></li>
<li class="line">|</li>
<li><a href="javascript;:">小爱开放平台</a></li>
<li class="line">|</li>
<li><a href="javascript;:">企业团购</a></li>
<li class="line">|</li>
<li><a href="javascript;:">资质证照</a></li>
<li class="line">|</li>
<li><a href="javascript;:">协议规则</a></li>
<li class="line">|</li>
<li class="app-wrapper"><a class="app" href="javascript;:">
下载APP
<!-- 添加一个弹出层 -->
<div class="qrcode">
<img src="./img/download.png">
<span>小米商城APP</span>
</div>
</a></li>
<li class="line">|</li>
<li><a href="javascript;:">SelectLocation</a></li>
<li class="line">|</li>
</ul>
<!-- 购物车 -->
<ul class="shop-car">
<li><a href="javascript:;">
<i class="fas fa-shopping-cart"></i>
购物车(0)
</a></li>
</ul>
<!-- 用户登录 -->
<ul class="user-info">
<li><a href="javascript:;">登录</a></li>
<li class="line">|</li>
<li><a href="javascript:;">注册</a></li>
<li class="line">|</li>
<li><a href="javascript:;">消息通知</a></li>
<li class="line">|</li>
</ul>
</div>
</div>
<!-- 创建头部的外部容器 -->
<div class="header-wrapper">
<div class="header w clearfix">
<!-- 创建一个logo -->
<h1 class="logo" title="小米">
小米官网
<a class="home" href="/"></a>
<a class="mi" href="/"></a>
</h1>
<!-- 创建中间导航条的容器 -->
<div class="nav-wrapper">
<!-- 创建导航条 -->
<ul class="nav clearfix">
<li class="all-goods"><a href="#">全部商品</a> <li>
<li class="show-goods"><a href="#">小米手机</a></li>
<li class="show-goods"><a href="#">Redmi 红米</a></li>
<li class="show-goods"><a href="#">电视</a></li>
<li class="show-goods"><a href="#">笔记本</a></li>
<li class="show-goods"><a href="#">家电</a></li>
<li class="show-goods"><a href="#">路由器</a></li>
<li class="show-goods"><a href="#">智能硬件</a></li>
<li><a href="#">服务</a></li>
<li><a href="#">社区</a></li>
<!-- 创建一个弹出层 -->
<div class="goods-info">
</div>
</ul>
</div>
<!-- 创建搜索框的容器 -->
<div class="search-wrapper">
<form class="search" action="#">
<input class="search-inp" type="text">
<button class="search-btn">
<i class="fas fa-search"></i>
</button>
</form>
</div>
</div>
</div>
</body>
</html>
index.css
/* 主页index.html样式表 */
/* 顶部导航条的容器 */
.topbar-wrapper{
width: 100%;
height:40px ;
line-height: 40px;
background-color: #333;
}
/* 设置超链接的颜色 */
.topbar a{
font-size: 12px;
color: #b0b0b0;
display: block;
}
/* 设置鼠标移入状态 */
.topbar a:hover{
color: #fff;
}
/* 设置竖线 */
.topbar .line{
font-size: 12px;
color: #424242;
margin: 0 8px;
}
/* 设置左侧导航条 */
.service , .topbar li{
float: left;
}
/* 设置app下的小三角 */
.app-wrapper:hover >.app::after{
width: 0;
height: 0;
content: '';
/* 设置绝对定位 */
position: absolute;
bottom: 0;
left: 0;
right: 0;
margin: auto;
/* 设置四个方向的边框 */
border: 7px solid transparent;
/* 去除上边框 */
border-top: none;
/* 单独设置下边框的颜色 */
border-bottom-color:#fff;
}
/* 设置下载APP的下拉二维码 */
.app{
position: relative;
}
.app .qrcode{
display: none;
position: absolute;
left: -38px;
width: 124px;
height: 148px;
background-color: #fff;
line-height: 1;
text-align: center;
box-shadow: 0 0 10px rgba(0, 0, 0, .3);
}
/* 设置app鼠标移入状态:显示二维码 */
.app-wrapper:hover>.app .qrcode{
display: block;
}
.app .qrcode img{
width: 90px;
margin: 17px;
margin-bottom: 10px;
}
/* 设置二维码中的文字 */
.app .qrcode span{
color: #000;
font-size: 14px;
}
/* 设置右侧导航条 */
.shop-car , .user-info{
float: right;
}
/* 设置购物车 */
.shop-car{
margin-left: 26px;
}
.shop-car a{
width: 120px;
background-color: #424242;
/* 水平居中 */
text-align: center;
}
.shop-car:hover a{
background-color: #fff;
color: #ff6700;
}
.shop-car i{
margin-right: 2px;
}
/* 设置中间的header */
.header-wrapper{
/* background-color: red; */
position: relative;
}
/* 设置中间的header */
.header{
height: 100px;
}
.header-wrapper{
position: relative;
}
/* 设置logo的h1 */
.header .logo{
float: left;
margin-top: 22px;
width: 55px;
height: 55px;
position: relative;
overflow: hidden;
/* 隐藏logo中的文字 常见手段 */
text-indent: -9999px
}
/* 统一设置logo的超链接 */
.header .logo a{
position: absolute;
width: 55px;
height: 55px;
left: 0;
background-color: #FF6700;
background-image: url(../img/mi-logo.png);
background-position: center;
transition: left 0.3s;
}
/* 设置home图片 */
.header .logo .home{
background-image: url(../img/mi-home.png);
left: -55px;
}
/* 设置鼠标移入以后两个图标的位置 */
.header .logo:hover .mi{
left: 55px;
}
.header .logo:hover .home{
left: 0;
}
/* 设置中间的导航条 */
.header .nav-wrapper{
float: left;
list-style: none;
margin-left: 7px;
}
/* 设置导航条整体ul */
.header .nav{
height: 100px;
/* 文字垂直居中 */
line-height: 100px;
padding-left: 58px;
}
/* 设置导航条中的li */
.nav>li{
float: left;
}
/* 设置导航条中字的颜色 */
.nav-wrapper li a{
display: block;
font-size: 16px;
margin-right: 20px;
}
/* 鼠标移入链接状态 */
.nav-wrapper li a:hover{
color: #ff6700;
}
/* 隐藏全部商品 */
.all-goods{
visibility: hidden;
}
.nav .goods-info{
/* 隐藏 */
height: 0;
overflow: hidden;
width: 100%;
background-color:#fff;
/* 边框和阴影在想让它显示的时候设置 */
/* border-top: 1px solid rgb(224, 224, 224);
box-shadow: 0 5px 3px rgba(0, 0, 0, .2); */
position:absolute;
top: 100px;
left: 0;
/* 动画效果 */
transition: height 0.3s;
z-index: 9999;
}
/* 显示 */
.nav .show-goods:hover ~ .goods-info,
.goods-info{
height: 228px;
border-top: 1px solid rgb(224, 224, 224);
box-shadow: 0 5px 3px rgba(0, 0, 0, .2);
}
/* 设置搜索框的容器 */
.search-wrapper{
width: 296px;
height: 50px;
float: right;
margin-top: 25px;
}
.search-wrapper .search-inp{
box-sizing: border-box;
width: 244px;
border: none;
float: left;
padding: 0 10px;
height: 50px;
font-size: 16px;
border: 1px solid rgb(224, 224, 224);
/* 去除轮廓线 */
outline: none;
}
/* 设置input获取焦点后的样式 */
.search-wrapper .search-inp:focus,
.search-wrapper .search-inp:focus + button{
border-color: #FF6700;
}
.search-wrapper .search-btn{
float: left;
height: 50px;
width: 52px;
padding: 0;
border: none;
background-color: #fff;
color: #616161;
font-size: 16px;
border: 1px solid rgb(224, 224, 224);
border-left: none;
}
/* 鼠标移入搜索框的样式 */
.search-wrapper .search-btn:hover{
background-color: #FF6700;
color: white;
border: none;
}
利用CSS写小三角标记
用于为样式设置过渡效果
transition: height 3s;
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.box1{
width: 0px;
height: 0px;
border: 10px red solid;
border-top: none;
border-color: transparent transparent black transparent;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
过渡
过渡(transition)
通过过渡可以指定一个属性发生变化时的切换方式
通过过渡可以创建一些非常好的效果,提升用户的体验
transition-property: 指定要执行过渡的属性
多个属性间使用,隔开
如果所有属性都需要过渡,则使用all关键字
大部分属性都支持过渡效果,注意过渡时必须是从一个有效数值向另外一个有效数值进行过渡
transition-duration: 指定过渡效果的持续时间,时间单位:s 和 ms 1s = 1000ms
transition-timing-function: 过渡的时序函数
指定过渡的执行的方式
可选值:
ease 默认值,慢速开始,先加速,再减速
linear 匀速运动
ease-in 加速运动
ease-out 减速运动
ease-in-out 先加速 后减速
cubic-bezier() 来指定时序函数
工具: https://cubic-bezier.com
steps() 分步执行过渡效果
可以设置一个第二个值:
end , 在时间结束时执行过渡(默认值)
start , 在时间开始时执行过渡
transition-delay: 过渡效果的延迟,等待一段时间后在执行过渡
transition 可以同时设置过渡相关的所有属性,只有一个要求,如果要写延迟,则两个时间中第一个是持续时间,第二个是延迟
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>我的主页</title>
<link rel="stylesheet" href="./icon font/css/all.css">
<style>
/* 清除默认样式 */
* {
margin: 0;
padding: 0;
}
.box1 {
width: 800px;
height: 800px;
background-color: aqua;
}
.box1 div {
width: 100px;
height: 100px;
margin-bottom: 100px;
}
.box2 {
background-color: pink;
/* 设置过渡 */
transition: all 2s;
/* transition-property: width height;
transition-property: all;
transition-duration: 100ms 4s; 这两个同时使用 */
transition-timing-function: ease;
transition-timing-function: cubic-bezier(.24,.95,.82,-0.88);
transition-timing-function: steps(2, start);
transition-delay: 2s;
/* 简写 */
transition:2s margin-left 1s cubic-bezier(.24,.95,.82,-0.88);
}
/* 鼠标移到1之后2的大小发生改变 */
.box1:hover .box2 {
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">
</div>
</div>
</body>
</html>
过渡-米兔练习
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
height: 271px;
width: 132px;
/* 兔子居中 */
margin: 0 auto;
background-image:url(./bigtap-mitu-queue-big.png);
background-position: 0 0;
transition: 0.3s steps(3);
}
.box1:hover{
background-position: -396px 0;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
动画
动画:动画和过渡类似,都是可以实现一些动态的效果,不同的是过渡需要在某个属性发生变化时才会触发,动画可以自动触发动态效果
设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤
animation-direction
指定动画运行的方向
可选值:
normal 默认值 从 from 向 to运行 每次都是这样
reverse 从 to 向 from 运行 每次都是这样
alternate 从 from 向 to运行 重复执行动画时反向执行
alternate-reverse 从 to 向 from运行 重复执行动画时反向执行
animation-fill-mode: 动画的填充模式
可选值:
none 默认值 动画执行完毕元素回到原来位置
forwards 动画执行完毕元素会停止在动画结束的位置
backwards 动画延时等待时,元素就会处于开始位置
both 结合了forwards 和 backwards
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box1 {
width: 800px;
height: 800px;
background-color: silver;
overflow: hidden;
}
.box1 div {
width: 100px;
height: 100px;
margin-bottom: 100px;
margin-left: 10px;
}
.box2 {
background-color: #bfa;
/* 设置2的动画 */
/* animation-name: 要对当前元素生效的关键帧的名字 */
animation-name: text;
/* animation-duration:动画的执行时间 */
animation-duration: 2s;
/* animation-delay:动画的延迟 */
animation-delay: 2s;
/*animation-timing-function:动画执行的效果 */
animation-timing-function: ease-in-out;
/* animation-iteration-count:动画执行次数 infinite-->表示无限执行*/
animation-iteration-count: 3;
/* nimation-direction:指定动画执行的方向 */
animation-direction:normal ;
/*animation-play-state:设置动画执行状态 running 默认值 动画执行 paused 动画暂停 */
animation-play-state: running;
/*animation-fill-mode :动画填充模式*/
animation-fill-mode:none ;
/* 简写 */
animation: test 2s 2 1s alternate;
}
.box1:hover div {
animation-play-state: paused;
}
/* 设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤 */
@keyframes text {
/* 动画的开始位置 也可以使用100%*/
from {
margin-left: 0px;
background-color: #bfa;
}
/* 动画的结束位置 也可以使用0%*/
to {
margin-left: 700px;
background-color: blue;
}
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
动画-奔跑的少年
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 256px;
height: 256px;
margin: 0 auto;
background-image: url(./bg2.png);
animation: run 1s steps(6) infinite;
}
/* 创建关键帧 */
@keyframes run {
from{
background-position: 0 0;
}
to{
background-position: -1536px 0;
}
}
</style>
</head>
<body>
<div class="box1">
</div>
</body>
</html>
关键帧
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.outer {
height: 500px;
border-bottom: 10px black solid;
margin: 50px auto;
overflow: hidden;
}
.box1 {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: pink;
animation: ball 2s forwards ease-in infinite;
}
/* 创建小球下落的动画 落下--弹起--再落下 */
@keyframes ball {
from {
margin-top: 0;
}
33% {
margin-top: 400px;
animation-timing-function: ease-in;
}
66% {
margin-top: 100px;
}
to {
margin-top: 400px;
animation-timing-function: ease-in;
}
/* 也可以这样写 */
/* 33%,66%,to {
margin-top: 400px;
animation-timing-function: ease-in;
} */
}
</style>
</head>
<body>
<div class="outer">
<div class="box1"></div>
</div>
</body>
</html>
平移
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
body{
background-color: silver;
}
.box1 {
width: 200px;
height: 200px;
background-color: pink;
margin: 0 auto;
margin-top: 200px;
/* 平移 */
transform: translateX(100px);
}
.box2 {
width: 200px;
height: 200px;
background-color: orange;
margin: 0 auto;
}
.box3 {
/* 使3居中 */
background-color: aqua;
position: absolute;
/*
这种居中方式,只适用于元素的大小确定
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto; */
/* 当元素被内容撑开时,用这种方法 */
left: 50%;
transform: translateX(-50%);
}
.box4,.box5{
width: 220px;
height: 220px;
background-color:#fff;
float: left;
margin: 0 10px;
transition:all .3s;
}
.box4:hover, .box5:hover{
transform: translateY(-5px);
box-shadow: 0 0 10px rgba(0, 0, 0,.3);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3">11111</div>
<div class="box4"></div>
<div class="box5"></div>
</body>
</html>
Z轴平移
z轴平移,调整元素在z轴的位置,正常情况就是调整元素和人眼之间的距离,距离越大,元素离人越近
z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果,必须要设置网页的视距
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
html{
/* 设置当前网页的视距为800px,人眼距离网页的距离 */
perspective: 800px;
}
body{
border: 1px red solid;
background-color: rgb(241, 241, 241);
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
margin: 200px auto;
transition:2s;
}
body:hover .box1{
transform: translateZ(800px);
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
旋转
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
html{
/* 设置当前网页的视距为800px,人眼距离网页的距离 */
perspective: 800px;
}
body{
border: 1px red solid;
background-color: rgb(241, 241, 241);
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
margin: 200px auto;
transition:2s;
}
body:hover .box1{
/* 通过旋转可以使元素沿着x y 或 z旋转指定的角度
rotateX()
rotateY()
rotateZ() */
/* transform: rotateX(1turn); */
/* transform: rotateZ(45deg); */
/* transform: rotateZ(180deg) translateY(100px); */
/* 是否显示元素的背面 visible hidden*/
backface-visibility: visible;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
鸭子表练习
理论:把针装在一个盒子里,让盒子转
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.sec-wrapper{
width: 500px;
height: 500px;
/* background-color: pink; */
animation: run 60s;
}
.sec{
height: 250px;
width: 4px;
background-color: red;
margin: 0 auto;
animation: run 60s;
}
@keyframes run{
from{
transform: rotateZ(0);
}
to{
transform: rotateZ(360deg);
}
}
</style>
</head>
<body>
<div class="sec-wrapper">
<div class="sec"> </div>
</div>
</body>
</html>
练习
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
/* 里面的所有元素全部居中 */
.clock>div{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
/* 设置表单的样式 */
.clock{
width: 500px;
height: 500px;
margin: 0 auto;
margin-top: 100px;
border-radius: 50%;
/* border: 10px solid black; */
position: relative;
background-image: url(./img/bg3.jpg);
/* 表盘铺满 */
background-size: cover;
}
/* 设置时针 */
.hour-wrapper{
position: absolute;
height: 70%;
width: 70%;
/* background-color: aqua; */
animation: run 7200s linear infinite;
}
.hour{
height: 50%;
width: 6px;
background-color: #000;
margin: 0 auto;
}
/* 设置分针 */
.min-wrapper{
height: 80%;
width: 80%;
/* background-color: red; */
animation: run 600s steps(60) infinite;
}
.min{
height: 50%;
width: 4px;
background-color: #000;
margin: 0 auto;
}
/* 设置秒针 */
.sec-wrapper{
height: 90%;
width: 90%;
animation: run 10s steps(60) infinite;
}
.sec{
height: 50%;
width: 2px;
background-color: white;
margin: 0 auto;
}
/* 旋转的关键帧 */
@keyframes run{
from{
transform: rotateZ(0);
}
to{
transform: rotateZ(360deg);
}
}
</style>
</head>
<body>
<!-- 创建表的容器 -->
<div class="clock">
<!-- 创建时针 -->
<div class="hour-wrapper">
<div class="hour"></div>
</div>
<!-- 创建分针 -->
<div class="min-wrapper">
<div class="min"></div>
</div>
<!-- 创建秒针-->
<div class="sec-wrapper">
<div class="sec"></div>
</div>
</div>
</body>
</html>
复仇者联盟
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
html {
perspective: 800px;
}
.cube {
width: 200px;
height: 200px;
/* background-color: aqua; */
margin: 100px auto;
/* 设置3d变形效果 */
transform-style: preserve-3d;
transform: rotateX(45deg) rotateZ(45deg);
animation: run 40s infinite linear;
}
.cube>div {
width: 200px;
height: 200px;
/* 为元素设置透明效果 */
opacity: 0.7;
position: absolute;
}
img {
/* 去除图片的边 */
vertical-align: top;
}
.box1{
transform: rotateY(90deg) translateZ(100px);
}
.box2{
transform: rotateY(-90deg) translateZ(100px);
}
.box3{
transform: rotateX(90deg) translateZ(100px);
}
.box4{
transform: rotateX(-90deg) translateZ(100px);
}
.box5{
transform: rotateX(180deg) translateZ(100px);
}
.box6{
transform: rotateX(0deg) translateZ(100px);
}
/* 添加动画 */
@keyframes run{
form{
transform: rotateX(0) rotateZ(0);
}
to{
transform: rotateX(1turn) rotateZ(1turn);
}
}
</style>
</head>
<body>
<!-- 创建一个外部容器 -->
<div class="cube">
<!-- 引入图片 -->
<div class="box1">
<img src="./img/1.jpg">
</div>
<div class="box2">
<img src="./img/2.jpg">
</div>
<div class="box3">
<img src="./img/3.jpg">
</div>
<div class="box4">
<img src="./img/4.jpg">
</div>
<div class="box5">
<img src="./img/5.jpg">
</div>
<div class="box6">
<img src="./img/6.jpg">
</div>
</div>
</body>
</html>
缩放
对元素进行缩放的函数:
scaleX() 水平方向缩放
scaleY() 垂直方向缩放
scale() 双方向的缩放
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
.box1{
width: 100px;
height: 100px;
background-color: aqua;
transition: 2s;
margin: 100px auto;
}
.box1:hover{
transform: scaleX(2);
transform: scale(2);
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
less简介
less是一门css的预处理语言
less是一个css的增强版,通过less可以编写更少的代码实现更强大的样式
在less中添加了许多的新特性:像对变量的支持、对mixin的支持… …
less的语法大体上和css语法一致,但是less中增添了许多对css的扩展,
所以浏览器无法直接执行less代码,要执行必须向将less转换为css,然后再由浏览器执行
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
html{
/* css原生也支持变量的设置 */
--color:#ff0;
--length:200px;
}
.box1 {
width: var(--length);
height: 100px;
/* 使用 */
background-color: var(--color);
}
.box2 {
/* 计算函数:calc */
width: calc(200px/2);
height: 100px;
border: 10px solid var(--color);
}
.box3 {
width: 100px;
height: 100px;
border: 10px solid red;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2">111</div>
<div class="box3">aaa</div>
</body>
</html>
less语法
// less中的单行注释,注释中的内容不会被解析到css中
/*css中的注释,内容会被解析到css文件中*/
.box1{
background-color: #bfa;
.box2{
background-color: #ff0;
.box4{
color: red;
}
}
.box3{
background-color: orange;
}
}
//变量,在变量中可以存储一个任意的值
// 并且我们可以在需要时,任意的修改变量中的值
// 变量的语法: @变量名
@a:200px;
@b:#bfa;
@c:box6;
.box5{
//使用变量是,如果是直接使用则以 @变量名 的形式使用即可
width: @a;
color:@b;
}
//作为类名,或者一部分值使用时必须以 @{变量名} 的形式使用
.@{c}{
width: @a;
background-image: url("@{c}/1.png");
}
@d:200px;
@d:300px;
div{
// 变量发生重名时,会优先使用比较近的变量
@d:115px;
width: @d;
height: @e;
}
//可以在变量声明前就使用变量
@e:335px;
div{
width: 300px;
// 新版的语法
// height: $width;
}
父元素和扩展
.box1{
.box2{
color: red;
}
>.box3{
color: red;
&:hover{
color: blue;
}
}
//为box1设置一个hover
//& 就表示外层的父元素
&:hover{
color: orange;
}
div &{
width: 100px;
}
}
.p1{
width: 100px;
height: 200px;
}
//:extend() 对当前选择器扩展指定选择器的样式(选择器分组)
.p2:extend(.p1){
color: red;
}
.p3{
//直接对指定的样式进行引用,这里就相当于将p1的样式在这里进行了复制
//mixin 混合
.p1();
}
// 使用类选择器时可以在选择器后边添加一个括号,这时我们实际上就创建了一个mixins
.p4(){
width: 100px;
height: 100px;
}
.p5{
.p4;
}
//混合函数 在混合函数中可以直接设置变量
.test(@w:100px,@h:200px,@bg-color:red){
width: @w;
height: @h;
border: 1px solid @bg-color;
}
div{
//调用混合函数,按顺序传递参数
// .test(200px,300px,#bfa);
.test(300px);
// .test(@bg-color:red, @h:100px, @w:300px);
}
span{
color: average(red,blue);
}
html{
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
background-color: #bfa;
}
body:hover{
background-color: darken(#bfa,50%);
}
混合函数
.box1{
.box2{
color: red;
}
>.box3{
color: red;
&:hover{
color: blue;
}
}
//为box1设置一个hover
//& 就表示外层的父元素
&:hover{
color: orange;
}
div &{
width: 100px;
}
}
.p1{
width: 100px;
height: 200px;
}
//:extend() 对当前选择器扩展指定选择器的样式(选择器分组)
.p2:extend(.p1){
color: red;
}
.p3{
//直接对指定的样式进行引用,这里就相当于将p1的样式在这里进行了复制
//mixin 混合
.p1();
}
//使用类选择器时可以在选择器后边添加一个括号,这时我们实际上就创建了一个mixins,专门给别人用
.p4(){
width: 100px;
height: 100px;
}
.p5{
.p4;
}
//混合函数 在混合函数中可以直接设置变量
.test(@w:100px,@h:200px,@bg-color:red){
width: @w;
height: @h;
border: 1px solid @bg-color;
}
div{
//调用混合函数,必须按顺序传递参数
// .test(200px,300px,#bfa);
.test(300px);
//或者这样传递参数
//.test(@bg-color:red, @h:100px, @w:300px);
}
span{
color: average(red,blue);
}
html{
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
background-color: #bfa;
}
//鼠标移入,颜色加深
body:hover{
background-color: darken(#bfa,50%);
}
less补充
//import用来将其他的less引入到当前的less
//可以通过import来将其他的less引入到当前的less中
@import "syntax2.less";
.box1{
// 在less中所有的数值都可以直接进行运算
// + - * /
width: 100px + 100px;
height: 100px/2;
background-color: #bfa;
}
弹性盒简介
flex(弹性盒、伸缩盒)
是CSS中的又一种布局手段,它主要用来代替浮动来完成页面的布局
flex可以使元素具有弹性,让元素可以跟随页面的大小的改变而改变
弹性容器
要使用弹性盒,必须先将一个元素设置为弹性容器
我们通过 display 来设置弹性容器
display:flex 设置为块级弹性容器
display:inline-flex 设置为行内的弹性容器
弹性元素
弹性容器的子元素是弹性元素(弹性项)
弹性元素可以同时是弹性容器
弹性容器的属性:
flex-direction 指定容器中弹性元素的排列方式
可选值:
row 默认值,弹性元素在容器中水平排列(左向右),主轴 自左向右
row-reverse 弹性元素在容器中反向水平排列(右向左),主轴 自右向左
column 弹性元素纵向排列(自上向下)
column-reverse 弹性元素方向纵向排列(自下向上)
主轴:
弹性元素的排列方向称为主轴
侧轴:
与主轴垂直方向的称为侧轴
弹性元素的属性:
flex-grow 指定弹性元素的伸展的系数
当父元素有多余空间的时,子元素如何伸展
父元素的剩余空间,会按照比例进行分配
flex-shrink 指定弹性元素的收缩系数
当父元素中的空间不足以容纳所有的子元素时,如果对子元素进行收缩
<!DOCTYPE html>
<html lang="en">
<head>
<title>css</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
ul {
width: 800px;
border: 10px red solid;
/* 将ul设置为弹性容器 */
display: flex;
/* 设置元素的排列方向 */
flex-direction: row-reverse;
}
li {
width: 100px;
height: 100px;
background-color: aquamarine;
font-size: 50px;
text-align: center;
/* 伸展系数 */
flex-grow: 1;
/* 收缩系数 */
/* flex-shrink:1 ; */
}
li:nth-child(2) {
background-color: pink;
}
li:nth-child(3) {
background-color: orange;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
会弹的导航条
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS</title>
<link rel="stylesheet" href="./样式重置表/reset.css">
<style>
.nav{
width: 1210px;
height: 48px;
line-height: 48px;
margin: 50px auto;
background-color: #E8E7E3;
/* 设置为弹性容器 */
display: flex;
}
.nav li{
/* 设置增长系数 */
flex-grow: 1;
}
.nav a{
display: block;
color: #808080;
text-decoration: none;
font-size: 16px;
text-align: center;
}
.nav a:hover{
background-color: #636363;
color: #fff;
}
</style>
</head>
<body>
<ul class="nav">
<li><a href="#">HTML/CSS</a></li>
<li><a href="#">Browser Side</a></li>
<li><a href="#">Server Side</a></li>
<li><a href="#">Programming</a></li>
<li><a href="#">XML</a></li>
<li><a href="#">Web Building</a></li>
<li><a href="#">Reference</a></li>
</ul>
</body>
</html>
弹性容器上的样式
flex-wrap: 设置弹性元素是否在弹性容器中自动换行
可选值:
nowrap 默认值,元素不会自动换行
wrap 元素沿着辅轴方向自动换行
wrap-reverse 元素沿着辅轴反方向换行
flex-flow: wrap 和 direction 的简写属性
flex-flow: row wrap;
justify-content:如何分配主轴上的空白空间(主轴上的元素如何排列)
可选值:
flex-start 元素沿着主轴起边排列
flex-end 元素沿着主轴终边排列
center 元素居中排列
space-around 空白分布到元素两侧
space-between 空白均匀分布到元素间
space-evenly 空白分布到元素的单侧
align-items: 元素在辅轴上如何对齐,元素间的关系
可选值:
stretch 默认值,将元素的长度设置为相同的值
flex-start 元素不会拉伸,沿着辅轴起边对齐
flex-end 沿着辅轴的终边对齐
center 居中对齐
baseline 基线对齐
align-content: 辅轴空白空间的分布
align-content: space-between;
align-self: 用来覆盖当前弹性元素上的align-items
align-self: stretch;
<!DOCTYPE html>
<html lang="en">
<head>
<title>css</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
ul {
width: 800px;
border: 10px red solid;
/* 设置为弹性容器 */
display: flex;
/* 是否可以换行 */
/* flex-wrap: wrap; */
justify-content: space-between;
}
li {
width: 200px;
height: 100px;
background-color: aquamarine;
font-size: 50px;
text-align: center;
}
li:nth-child(2) {
background-color: pink;
}
li:nth-child(3) {
background-color: orange;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
弹性元素上的样式
弹性元素的缩减系数
缩减系数的计算方式比较复杂
缩减多少是根据缩减系数和元素大小来计算
元素基础长度:flex-basis 指定的是元素在主轴上的基础长度
如果主轴是 横向的 则 该值指定的就是元素的宽度
如果主轴是 纵向的 则 该值指定的是就是元素的高度
默认值是 auto,表示参考元素自身的高度或宽度
如果传递了一个具体的数值,则以该值为准
推荐使用:
flex 可以设置弹性元素所有的三个样式
flex 增长 缩减 基础;
initial “flex: 0 1 auto”.
auto “flex: 1 1 auto”
none “flex: 0 0 auto” 弹性元素没有弹性
<!DOCTYPE html>
<html lang="en">
<head>
<title>css</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
ul {
width: 800px;
border: 10px red solid;
/* 将ul设置为弹性容器 */
display: flex;
}
li {
width: 100px;
height: 100px;
background-color: aquamarine;
font-size: 50px;
text-align: center;
/* flex-grow: 1; */
flex-basis: 100px;
/* 简写 */
flex: initial;
}
li:nth-child(2) {
background-color: pink;
/*order 决定弹性元素的排列顺序 */
order: 3;
}
li:nth-child(3) {
background-color: orange;
order: 2;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
淘宝导航–移动端
<!DOCTYPE html>
<html lang="en">
<head>
<title>css</title>
<style>
* {
margin: 0;
padding: 0;
}
.nav{
width: 100%;
}
/* 设置每一行的容器 */
.nav-inner{
/* 设置为弹性容器 */
display: flex;
/* 设置主轴上的空白分布 */
justify-content: space-around;
}
.item{
width: 18%;
text-align: center;
}
/* 设置图片的宽度 */
.item img{
width: 100%;
}
.item a{
color: #333;
text-decoration: none;
font-size: 18px;
}
</style>
</head>
<body>
<!-- 创建一个外层的容器 -->
<nav class="nav">
<div class="nav-inner">
<div class="item">
<a href="#">
<img src="./img/1.png" alt="">
<span>天猫</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/2.png" alt="">
<span>聚划算</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/3.png" alt="">
<span>天猫国际</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/4.png" alt="">
<span>外卖</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/5.png" alt="">
<span>天猫超市</span>
</a>
</div>
</div>
<div class="nav-inner">
<div class="item">
<a href="#">
<img src="./img/6.png" alt="">
<span>充值中心</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/7.png" alt="">
<span>飞猪旅行</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/8.png" alt="">
<span>领金币</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/9.png" alt="">
<span>拍卖</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="./img/10.png" alt="">
<span>分类</span>
</a>
</div>
</div>
</nav>
</body>
</html>
像素
像素:屏幕是由一个一个发光的小点构成,这一个个的小点就是像素
分辨率:1920 x 1080 说的就是屏幕中小点的数量
在前端开发中像素要分成两种情况讨论:CSS像素 和 物理像素
物理像素,上述所说的小点点就属于物理像素
CSS像素,编写网页时,我们所用像素都是CSS像素
浏览器在显示网页时,需要将CSS像素转换为物理像素然后再呈现
一个css像素最终由几个物理像素显示,由浏览器决定。默认情况下在pc端,一个css像素 = 一个物理像素
视口(viewport):视口就是屏幕中用来显示网页的区域
可以通过查看视口的大小,来观察CSS像素和物理像素的比值
默认情况下:
视口宽度 1920px(CSS像素)
1920px(物理像素)
此时,css像素和物理像素的比是 1:1
放大两倍的情况:
视口宽度 960px(CSS像素)
1920px(物理像素)
此时,css像素和物理像素的比是1:2
我们可以通过改变视口的大小,来改变CSS像素和物理像素的比值
手机像素
在不通的屏幕,单位像素的大小是不同的,像素越小屏幕会越清晰
24寸 1920x1080
i6 4.7寸 750 x 1334
智能手机的像素点 远远小于 计算机的像素点
问题:一个宽度为900px的网页在iphone6中要如何显示呢?
默认情况下,移动端的网页都会将视口设置为980像素(css像素),以确保pc端网页可以在移动端正常访问,但是如果网页的宽度超过了980,移动端的浏览器会自动对网页缩放以完整显示网页。所以基本大部分的pc端网站都可以在移动端中正常浏览,但是往往都不会有一个好的体验,为了解决这个问题,大部分网站都会专门为移动端设计网页
完美视口
将网页的视口设置为完美视口
<meta name="viewport" content="width=device-width, initial-scale=1.0">
结论:以后再写移动端的页面,就把上边这个玩意先写上
vw单位
不同的设备完美视口的大小是不一样的
iphone6 – 375
iphone6plus – 414
由于不同设备视口和像素比不同,所以同样的375个像素在不同的设备下意义是不一样,比如在iphone6中 375就是全屏,而到了plus中375就会缺一块
所以在移动端开发时,就不能再使用px来进行布局了
vw 表示的是视口的宽度(viewport width)
100vw = 一个视口的宽度
1vw = 1%视口宽度
vw这个单位永远相当于视口宽度进行计算
设计图的宽度:50px 1125px
使用vw作为单位:100vw
问:创建一个 48px x 35px 大小的元素
100vw = 750px(设计图的像素) 0.1333333333333333vw = 1px
6.4vw = 48px(设计图像素)
4.667vw = 35px
vw适配
<!DOCTYPE html>
<html lang="en">
<head>
<title>css</title>
<style>
* {
margin: 0;
padding: 0;
}
html{
/* 网页中字体大小最小是12px,不能设置一个比12像素还小的字体,如果我们设置了一个小于12px的字体,则字体自动设置为12 5.3333vw = 40px */
font-size: 5.3333vw;
}
/* rem和html中的font-size是等比关系 1rem=1html字体大小 1 rem = 40 px(设计图) */
.box1{
width:18.75rem;
height: 4.667vw;
background-color: silver;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
移动端页面设计
style.less
* {
margin: o;
padding: 0;
}
// 设置变量
@total-width: 750;
// 设置一个公用的类
.w {
width: 693/40rem;
margin: 0 auto;
}
a {
text-decoration: none;
}
// 设置根元素
html {
// 设置rem比值
font-size: 100vm/@total-width*40;
background-color: #eff0f4;
}
// 设置头部
.top-bar:extend(.w) {
// 开启弹性容器
display: flex;
height: (174rem/40);
// 设置对齐方式
justify-content: space-between;
// 设置辅轴的对齐方式
align-items: center;
h1 {
line-height: (174rem/40);
}
// 设置字体
a {
color: #24253d;
font-size: 30px;
i {
color: #999;
font-size: 18px;
}
}
}
// 设置banner
.banner:extend(.w) {
img {
width: 100%;
}
}
// 设置中间的菜单
.menu :extend(.w) {
height: (329rem/40);
//设置弹性元素
display: flex;
// 设置换行
flex-flow: row wrap;
// 设置对齐
justify-content: space-between;
// 设置辅轴方向的对齐方式
align-content: space-evenly;
// 设置框的大小
a {
width: (400rem/40);
height: (104rem/40);
color: white;
border-radius: 5px;
line-height: (104rem/40);
i {
margin: 0 8px 0 8px;
}
}
.course {
background-color: #f97053;
}
.star {
background-color: #cd6efe;
}
.sub {
background-color: #f97053;
}
.download {
background-color: #ff3971;
}
}
// 设置课程列表
.course-list:extend(.w) {
.title {
display: flex;
justify-content: space-between;
align-items: center;
}
h2 {
color: #24253d;
font-size: (45rem/40);
border-left: 1px solid #3a84ff;
padding-left: 10px;
}
a {
font-size: (28rem/40);
color: #656565;
}
}
// 设置课程列表项
.item-list{
display: flex;
overflow: auto;
}
.item {
flex: none;
width: (350rem/40);
height: (400rem/40);
padding: 0 (22rem/40);
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, .3);
border-radius: 5px;
display: flex;
flex-flow: column;
align-content: space-evenly;
margin-right: 30px;
img {
width: 100%;
vertical-align: top;
margin-top: 7px;
}
.course-title {
font-size: (32rem/40);
color: #24253d;
}
.user-info {
display: flex;
// 设置辅轴方向的对齐方式
align-content: space-evenly;
}
.avater {
width: (42rem/40);
height: (42rem/40);
}
.nickname {
font-size: (24rem/40);
color: #969393;
margin-left: 6px;
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>爱学习</title>
<link rel="stylesheet" href="./fa/css/all.min.css">
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<!-- 创建头部的容器 -->
<header class="top-bar">
<div class="menu-btn">
<a href="#">
<i class="fas fa-stream"></i>
</a>
</div>
<h1 class="logo">
<a href="#">
I LEARN
</a>
</h1>
<div class="search-btn">
<a href="#">
<i class="fas fa-search"></i>
</a>
</div>
</header>
<!-- banner -->
<div class="banner">
<a href="#">
<img src="./img/banner.png">
</a>
</div>
<!-- 设置菜单 -->
<nav class="menu">
<a class="course" href="#">
<i class="fas fa-book"></i>
My Courses
</a>
<a class="star" href="#">
<i class="fas fa-star"></i>
Star Teacher
</a>
<a class="sub" href="#">
<i class="fas fa-envelope"></i>
Subscription
</a>
<a class="download" href="#">
<i class="fas fa-globe"></i>
My Download
</a>
</nav>
<!--课程列表 -->
<div class="course-list">
<div class="title">
<h2>Latest course</h2>
<a class="more" href="#">More</a>
</div>
<div class="item-list">
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
</div>
</div>
<div class="course-list">
<div class="title">
<h2>Latest course</h2>
<a class="more" href="#">More</a>
</div>
<div class="item-list">
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
<div class="item">
<!-- 封面 -->
<div class="cover">
<img src="./img/cover.png" alt="">
</div>
<!-- 小标题 -->
<h3 class="course-title">Photography course</h3>
<!-- 用户信息 -->
<div class="user-info">
<div class="avater">
<img src="./img/avatar.png" alt="">
</div>
<div class="nickname">
Annabelle
</div>
</div>
</div>
</div>
</div>
</body>
</html>
效果预览
媒体查询简介
使用媒体查询
语法: @media 查询规则{}
媒体类型:
all 所有设备
print 打印设备
screen 带屏幕的设备
speech 屏幕阅读器
可以使用,连接多个媒体类型,这样它们之间就是一个或的关系
可以在媒体类型前添加一个only,表示只有。only的使用主要是为了兼容一些老版本浏览器
响应式布局
1:网页可以根据不通的设备或窗口大小呈现出不同的效果
2:使用响应式布局,可以使一个网页适用于所有设备
3:响应布局的关键就是 媒体查询
4:通过媒体查询,可以为不通的设备,或设备不同状态来分别设置样式
<style>
@media print,screen{
body{
background-color: #bfa;
}
}
@media only screen {
body{
background-color: #bfa;
}
}
</style>
响应式布局
媒体特性:
width 视口的宽度
height 视口的高度
min-width 视口的最小宽度(视口大于指定宽度时生效)
max-width 视口的最大宽度(视口小于指定宽度时生效)
/*当视口的宽度大于500的时候生效 */
@media (max-width: 500px){
body{
background-color: #bfa;
}
}
样式切换的分界点,我们称其为断点,也就是网页的样式会在这个点时发生变化
一般比较常用的断点
小于768 超小屏幕 max-width=768px
大于768 小屏幕 min-width=768px
大于992 中型屏幕 min-width=992px
大于1200 大屏幕 min-width=1200px
@media only screen and (min-width: 500px) and (max-width:700px){
body{
background-color: #bfa;
}
}
美图-响应式布局
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>meitu</title>
<link rel="stylesheet" href="./css/reset.css">
<link rel="stylesheet" href="./fa/css/all.css">
<link rel="stylesheet" href="./css/style.css">
<style>
li {
list-style: none;
}
</style>
</head>
<body>
<!-- 响应式设计的网页:
① 移动端优先
② 渐进增强-->
<!-- 外部容器 -->
<div class="top-bar-wrapper">
<div class="top-bar">
<!-- 左侧菜单 -->
<div class="left-menu">
<!-- 创建菜单图标 -->
<ul class="menu-icon">
<li></li>
<li></li>
<li></li>
</ul>
<!-- 创建菜单 -->
<ul class="nav">
<li><a href="#">手机</a></li>
<li><a href="#">美容仪器</a></li>
<li><a href="#">配件</a></li>
<li><a href="#">服务支持</a></li>
<li><a href="#">企业网站</a></li>
<li>
<a href="#"><i class="fas fa-search"></i></a>
<span>搜索 Meitu.com</span>
</li>
</ul>
</div>
<!-- logo -->
<h1 class="logo">
<a href="/">meitu美图</a>
</h1>
<!-- 用户信息 -->
<div class="user-info">
<a href="#">
<i class="fas fa-user"></i>
</a>
</div>
</div>
</div>
</body>
</html>
style.less
a {
text-decoration: none;
color: #fff;
&:hover {
color: rgb(197, 196, 196);
}
}
.top-bar-wrapper{
background-color: #000;
}
// 导航条外部容器
.top-bar {
height: 48px;
padding: 0 14px;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
background-color: #000;
}
// 设置左侧导航图标
.left-menu {
// 设置鼠标移入,显示菜单
&:active {
// 显示隐藏的菜单
.nav {
display: block;
}
}
// 设置菜单
.nav {
display: none;
position: absolute;
top: 48px;
left: 0;
bottom: 0;
right: 0;
background-color: #000;
padding-top: 60px;
li {
width: 80%;
margin: 0 auto;
border-bottom: 1px solid #757474;
a {
line-height: 44px;
font-size: 14px;
}
&:last-child a {
display: inline-block;
margin-right: 6px;
}
span {
font-size: 14px;
color: #fff;
}
}
}
// 图标
.menu-icon {
width: 18px;
height: 48px;
position: relative;
// 导航的线
li {
width: 18px;
height: 1px;
background-color: #fff;
position: absolute;
// 修改变形的原点
transform-origin: left center;
// 设置过渡
transition: 0.5s;
}
li:nth-child(1) {
top: 18px;
}
li:nth-child(2) {
top: 24px;
}
li:nth-child(3) {
top: 30px;
}
// 鼠标移入之后的效果
&:active {
li:nth-child(1) {
// 向下旋转
transform: rotateZ(40deg);
}
li:nth-child(2) {
// 隐藏
opacity: 0;
}
li:nth-child(3) {
transform: rotateZ(-40deg);
}
}
}
}
// 设置logo
.logo {
a {
text-indent: -9999px;
display: block;
width: 122px;
height: 32px;
background-image: url(../img/dff63979.sprites-index@2x.png);
background-size: 400px 400px;
}
}
// 设置媒体查询
@media only screen {
// 断点768px
@media (min-width:768px) {
.left-menu {
order: 2;
flex: auto;
// 显示菜单
.nav {
display: flex;
position: static;
padding: 0;
justify-content: space-around;
li {
width: auto;
border-bottom: none;
margin: 0;
a {
line-height: 48px;
}
span{
display: none;
}
}
}
// 隐藏菜单图标
.menu-icon {
display: none;
}
}
.logo {
order: 1;
}
.user-info {
order: 3;
}
}
}