CSS学习笔记
观看【狂神学java】CSS3最新教程快速入门教程通俗易懂
-
什么是CSS?
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。
CSS 能够对网页中元素位置的排版进行像素级精确控制,支持几乎所有的字体字号样式,拥有对网页对象和模型样式编辑的能力。
1.了解CSS
字体、颜色、边距、高度、宽度、背景图片、网页定位、网页浮动
1.1 发展史
CSS1.0
CSS2.0 DIV(块)+ CSS,HTML与CSS结构分离的思想,网页变得简单,SEO
CSS2.1 浮动,定位
CSS3.0 圆角,阴影,动画…浏览器兼容性
1.2 快速入门
style
基本入门:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我又来学习了!</title>
<!--style css规范:
可以编写css代码;每一个声明以分号结尾
格式 选择器{
声明1;
声明2;
声明3;
}
-->
<style>
h1{
color: aqua;
}
</style>
</head>
<body>
<h1>你是傻逼!</h1>
</body>
</html>
建议使用这种方法:
== css文件要放在html文件的目录下==
css的优势
- 内容和表现分离
- 网页结构表现统一,可以实现复用
- 样式十分丰富
- 建议使用独立于HTML的css文件
- 利用SEO,容易被搜索引擎收录
1.3 css的三种导入方式
1. 行内样式
<!--行内样式,在标签元素中,编写一个style属性,编写样式即可-->
<h1 style = "color: blue">我喜欢你!</h1>
2. 内部样式
<head>
<meta charset="UTF-8">
<title>我的第二个demon</title>
<!--内部样式表-->
<style>
h1{
color: blueviolet;
}
</style>
</head>
3. 外部样式
在css文件中:
/*外部样式*/
h1{
color: aqua;
}
优先级
<!--优先级: 就近原则 -->
4. 拓展
外部样式两种写法:
-
链接式:
属于:html
<!--外部样式:链接式--> <link rel="stylesheet" href="css/index.css">
-
导入式
属于:CSS2.1特有的 @
<!--!导入式--> <style> @import url("css/index.css"); </style>
2. 选择器
作用:选择页面上的某一个或者某一类元素
2.1 基本选择器
-
标签选择器:选择一类标签
<head> <meta charset="UTF-8"> <title>为你写诗</title> <style> /*标签选择器,会选择页面上所有这个标签的元素*/ h1{ color: darkviolet; /*背景颜色*/ background: bisque; /*背景边框*/ border-radius: 15px; } </style> </head>
-
类选择器 class:选中所有class属性一致的标签,跨标签
<head> <meta charset="UTF-8"> <title>为你写诗</title> <style> <style> /*类选择器的格式,.class的名称{} 好处:可以多个标签归类,是同一个class, 可以重复使用 */ p{ /*设置字体大小*/ font-size: 30px; } /*类选择器*/ .word{ color: #ff1493; } .Title{ color: blue; } </style> </head>
-
id 选择器:全局唯一,不能重复
<head> <meta charset="UTF-8"> <title>为你写诗</title> <style> <style> /*id选择器 ->id必须保证全局唯一! 格式:#id名称{} */ #Two{ color: fuchsia; } </style> </head>
-
全部代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>为你写诗</title> <style> /*标签选择器,会选择页面上所有这个标签的元素*/ h1{ color: darkviolet; /*背景颜色*/ background: bisque; /*背景边框*/ border-radius: 15px; } /*类选择器的格式,.class的名称{} 好处:可以多个标签归类,是同一个class, 可以重复使用 */ p{ /*设置字体大小*/ font-size: 30px; } /*类选择器*/ .word{ color: #ff1493; } .Title{ color: blue; } /*id选择器 ->id必须保证全局唯一! 格式:#id名称{} */ #Two{ color: fuchsia; } /*不遵循就近原则 id选择器 > class选择器 > 标签选择器 */ </style> </head> <body> <h1 class="Title">My love</h1> <p class="word"> 我 不 知 何 谓 逢 时,<br/> 好 似 曾 见 春 色 里。<br/> 喜 你 无 问 何 时 起,<br/> 欢 心 必 常 伴 至 终。<br/> * 家 有 女 初 长 成,<br/> * 生 得 缘 始 相 知,<br/> * 微 一 笑 怎 可 敌?<br/> </p> <p id="Two"> 大 树 鸟 儿 爱,<br/> 花 儿 遍 地 开。<br/> 羽 满 * 作 翼,<br/> 翱 翔 云 雾 北,<br/> * 风 尚 且 *。<br/> </p> </body> </html>
- 优先级
<style> /*不遵循就近原则 id选择器 > class选择器 > 标签选择器 */ </style>
2.2 层次选择器
-
后代选择器:在某个元素后面
/*后代选择器*/ body p{ background: fuchsia; }
-
子选择器:一代
/*子选择器*/ body >p{ background: blue; }
-
相邻兄弟选择器: 同辈
/*相邻选择器:只有一个,相邻(向下)*/ .good + p{ background: bisque; }
-
通用选择器:
/*通用选择器,当前选中的元素向下的所有兄弟元素*/ .good~p{ background: aqua; }
2.3 结构伪类选择器
伪类:
/*ul下的第一个子元素*/
ul li:first-child{
background: antiquewhite;
}
/*ul下的最后一个子元素*/
ul li:last-child{
background: aqua;
border-radius: 15px;
}
/*选中p1:定位到父元素,选择当前的第二个元素
选择当前元素的父级元素,选中父级元素的第二个,
并且是当前元素才生效
*/
p:nth-child(2){
background: blue;
}
/*选中父元素下的p元素的第三个*/
p:nth-of-type(3){
background: yellow;
}
/*悬停*/
a:hover{
background: deeppink;
}
全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>好好学习,天天向上</title>
<!--避免使用class选择器还有id选择器-->
<style>
/*ul下的第一个子元素*/
ul li:first-child{
background: antiquewhite;
}
/*ul下的最后一个子元素*/
ul li:last-child{
background: aqua;
border-radius: 15px;
}
/*选中p1:定位到父元素,选择当前的第二个元素
选择当前元素的父级元素,选中父级元素的第二个,
并且是当前元素才生效
*/
p:nth-child(2){
background: blue;
}
/*选中父元素下的p元素的第三个*/
p:nth-of-type(3){
background: yellow;
}
/*悬停*/
a:hover{
background: deeppink;
}
</style>
</head>
<body>
<a href="">2333333</a>
<p>标题1</p>
<p>标题2</p>
<p>标题3</p>
<ul>
<li><p>标签1</p></li>
<li><p>标签2</p></li>
<li><p>标签3</p></li>
</ul>
</body>
</html>
2.4 属性选择器(重要)
id+class 结合:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: blue;
text-align: center;
color: gainsboro;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;
}
/*属性名,属性名 = 属性值(正则)
= 绝对等于
*= 包含这个元素
^= 以这个开头
$= 以这个结尾
*/
/*存在id属性的元素:a[]{}*/
/*a[id]{!*所有带id的a标签*!
background: yellow;
}*/
/*id=first的元素*/
/*a[id=first]{
background: blue;
}*/
/*class中有links的元素*/
/*a[class*=links]{
background: yellow;
}*/
/*选中href中以http开头的元素*/
/*a[href^=http]{
background: yellow;
}*/
/*选中href中以pdf结尾的*/
a[href$=pdf]{
background: yellow;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://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标签:重点要突出的字,使用span标签套起来
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新的一天学css</title>
<style>
#title1{
background: brown;
font-size: 25px;
}
</style>
</head>
<body>
<h1>
熬夜会<span id ="title1">猝死</span>吗?
</h1>
</body>
</html>
3.2 字体样式
<!--
font-family :字体
font-size :字体大小
font-weight:字体的粗细
color:字体颜色
oblique :斜体
-->
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新的一天学css</title>
<!--
font-family :字体
font-size :字体大小
font-weight:字体的粗细
color:字体颜色
oblique :斜体
-->
<style>
body{
font-family: "Arial Rounded MT Bold",华文楷体;
color: fuchsia;
}
h1{
font-size: 50px;
}
p1{
font:oblique 300 30px "华文楷体";
/* font-size: 30px;*/
font-weight: bolder;
}
p2{
font-weight: bold;
font-size: 20px;
}
p3{
font-size: 50px;
}
p4{
font-size: 30px;
font-weight: bolder;
}
</style>
</head>
<body>
<h1>为你写诗</h1>
<p1 class = "p1">
To: 张小姐<br/>
屏 风 难 叠 云 锦 *,<br/>
湛 南 春 风 尤 未 *。<br/>
十 里 繁 花 倚 绣 *,<br/>
汝 似 仙 女 凡 间 现,<br/>
偷 得 浮 生 半 日 闲。<br/>
才 惊 此 节 移 心 律,<br/>
欲 乘 马 快 相 逢 抵,<br/>
九 重 欢 乐 在 朝 夕。<br/>
</p1>
<br/>
<p2>
爱 你 每 一 天!<br/>
<br/>
梁 先 生<br/>
</p2>
<br/>
<p3> Writing poems for you
</p3>
<br/>
<p4>
is difficult to fold the clouds and brocade, especially the spring breeze in Zhannan. Ten miles of flowers depend on embroidery, and you are like a fairy, and you steal to live for half a day. I was surprised to move my heart rhythm in this section. I want to meet my horse quickly. Nine joys are in the morning and evening.
</p4>
</body>
</html>
3.3 文本样式
- 颜色: color rbg rabg
- 文本对齐方式 : text-align = center
- 首行缩进: text-indent:2em
- 行高: line-height
- 划线: text-decoration: line-through;
- 水平图片对齐: vertical-align: middle;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新的一天学css</title>
<!--
文本对齐:
-->
<style>
body{
font-family: "Arial Rounded MT Bold",华文楷体;
}
h1{
font-size: 50px;
/*颜色:1.单词描述
2.RGB 0~F
3.RGBA A :0~1
*/
color: fuchsia;
background: azure;
border-radius: 15px;
/*排版居中*/
text-align: center;
}
p{
font:oblique 300 30px "华文楷体";
/* font-size: 30px;*/
font-weight: bolder;
color: brown;
text-align: center;
}
.p1{
font-weight: bold;
font-size: 20px;
text-align: center;
color: blue;
/*中划线*/
text-decoration: line-through;
}
.p2{
color: black;
/*下划线*/
text-decoration: underline;
}
.p3{
font-size: 50px;
/*上划线*/
text-decoration: overline;
}
.english{
/*段落首行缩进*/
text-indent: 20px;
font-size: 30px;
font-weight: bolder;
background: cornsilk;
/*行高*/
height: 300px;
line-height: 300px'';
/**/
}
.picture{
text-align: center;
}
/*!*水平对齐*!*/
/*img,span{*/
/* vertical-align: middle;*/
/*}*/
</style>
</head>
<body>
<h1>为你写诗</h1>
<p>
To: 张小姐<br/>
</p>
<p>屏 风 难 叠 云 锦 *,</p>
<p>湛 南 春 风 尤 未 *。</p>
<p>十 里 繁 花 倚 绣 *,</p>
<p>汝 似 仙 女 凡 间 现,</p>
<p>偷 得 浮 生 半 日 闲。</p>
<p>才 惊 此 节 移 心 律,</p>
<p>欲 乘 马 快 相 逢 抵,</p>
<p>九 重 欢 乐 在 朝 夕。</p>
<br/>
<p class="p1">
<!--p标签只有一个,可以给他起类属性-->
爱 你 每 一 天!
</p>
<p class="p2">
梁 先 生!
</p>
<p class="picture">
<img src="../../resource/picture/d.jpg" alt="梁先生的girl">
<br/>
<span>我真的好想你!</span>
</p>
<br/>
<p class="p3" >
Writing poems for you
</p>
<br/>
<p class="english">
is difficult to fold the clouds and brocade, especially the spring breeze in Zhannan. Ten miles of flowers depend on embroidery, and you are like a fairy, and you steal to live for half a day. I was surprised to move my heart rhythm in this section. I want to meet my horse quickly. Nine joys are in the morning and evening.
</p>
</body>
</html>
效果:不给你看图哈哈哈哈!
3.4 阴影
/*阴影:
阴影颜色 水平偏移 竖直偏移 模糊半径
*/
.p4{
text-shadow: #ea2989 10px 10px 10px;
}
/* 第一个参数:表示水平偏移
第二个参数:表示垂直偏移
第三个参数:表示模糊半径
第四个参数:表示颜色
*/
text-shadow:5px 5px 5px 颜色
3.5 超链接伪类
/* 未访问的链接 */
a:link {
color: #FF0000}
/* 已访问的链接,点击之后的状态*/
a:visited {
color: #00FF00}
/* 鼠标移动到链接上,鼠标悬浮的状态*/
a:hover {
color: #FF00FF}
/* 选定的链接,鼠标按住未释放的状态*/
a:active {
color: #0000FF}
3.6 列表
内容列表:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表样式</title>
<link rel="stylesheet" href="css/listdemon.css" 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>
css文件:
#nav{
width: 300px;
background: rgba(169, 169, 169, 0.72);
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 35px;
background: #ff5900;
}
/*ul li
list-style:
none:去掉圆点
circle:空心圆
decimal:数字
spuare:正方形
*/
ul{
background: darkgrey;
}
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
}
a{
text-decoration: none;
font-size: 14px;
color: black;
}
a:hover{
color: #00ffae;
text-decoration: underline;
}
3.7 背景
-
背景颜色: background
-
背景图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 1000px;
height: 700px;
/*边框:
第一个是样式
第二个是实线
第三个是颜色
*/
border: 1px solid red;
/*背景图片*/
background-image: url("images/mygirl.jpg");
/*默认是全部平铺的 repeat*/
}
/*水平平铺*/
.div1{
background-repeat: repeat-x;
}
/*竖直平铺*/
.div2{
background-repeat: repeat-y;
}
/*不平铺*/
.div3{
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</body>
</html>
3.8 渐变
网站:https://www.grabient.com/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--径向渐变,圆形-->
<style>
body{
background-color: #FFFFFF;
background-image: linear-gradient(66deg, #FFFFFF 0%, #6284FF 50%, #FF0000 100%);
}
</style>
</head>
<body>
</body>
</html>
上面的网页情书加了背景颜色好看多了吧!
4.盒子模型
4.1 关于盒子
margin:外边距
padding:内边距
border:边框
4.2 边框
1、边框的粗细
2、边框的样式
3、边框的颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的登录页面</title>
<style>
/*body总有一个默认的外边框margin:0,常见的*/
/*body{
margin: 0;
padding: 0;
text-decoration: none;
}*/
/*border:粗细,样式,颜色*/
#box{
width: 350px;
border: 1px solid #000000;
text-align: center;
}
h2{
font-size: 16px;
background-color: #0defef;
line-height: 30px;
margin: 0px;
}
form{
background: #f000f8;
}
/*黑线边框*/
div:nth-of-type(1) input{
border: 3px solid black;
}
/*虚线边框*/
div:nth-of-type(2) input{
border: 3px dashed yellow;
}
/*虚线边框*/
div:nth-of-type(3) input{
border: 2px dashed #806b00;
}
</style>
</head>
<body>
<!--内容都默认放在div标签下-->
<div id="box">
<h2>会员登录</h2>
<form action="#">
<div>
<span>用户名:</span>
<input type="text">
</div>
<div>
<span>密码:</span>
<input type="password">
</div>
<div>
<span>邮箱:</span>
<input type="text">
</div>
</form>
</div>
</body>
</html>
花里胡哨的!
4.3 内外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--外边距的妙用:居中元素
margin: 0 auto;
-->
<style>
/*border:粗细,样式,颜色*/
#box{
width: 300px;
border: 1px solid red;
margin: 0 auto;
}
/*
顺时针选中
margin: 1px
margin: 1px 2px 3px;
margin: 1px 2px 3px 4px;
*/
h2{
font-size: 16px;
background-color: #0ff5da;
line-height: 30px;
margin: 0px;
}
form{
background: #e33acd;
}
input{
border: 1px solid black;
}
div:nth-of-type(1){
padding: 10px 5px;
}
</style>
</head>
<body>
<div id="box">
<h2>会员登录</h2>
<form action="#">
<div>
<span>用户名:</span>
<input type="text">
</div>
<div>
<span>密码:</span>
<input type="password">
</div>
<div>
<span>邮箱:</span>
<input type="text">
</div>
</form>
</div>
</body>
</html>
盒子的计算方式:你这个元素到底多大?
margin + border + padding + 内容宽度
4.4 圆角边框
圆形:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
左上 右上 右下 左下,顺时针方向
-->
<!--
圆圈: 圆角 = 半径
-->
<style>
div{
width: 100px;
height: 100px;
border: 10px solid red;
border-radius: 100px;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
图形圆形:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#div1{
/*圆形*/
width: 100px;
height: 100px;
border: 10px solid #00ffcc;
border-radius: 100px;
}
#div2{
/*半圆*/
width: 100px;
height: 50px;
border: 10px solid #ff001e;
border-radius: 100px 100px 0 0;
}
#div3{
/*四分之一圆*/
width: 50px;
height: 50px;
border: 10px solid #d000ff;
border-radius: 100px 0 0 0;
}
img{
border-radius: 500px;
}
</style>
</head>
<body>
<div id="div1"></div>
<br/>
<div id="div2"></div>
<br/>
<div id="div3"></div>
<br/>
<img src="../../resource/picture/a.jpg" alt="">
</body>
</html>
效果还行:
4.5 阴影
模板之家: http://www.cssmoban.com/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--margin:0 auto; 居中
要求:块元素,块元素有固定宽度-->
<style>
img{
border-radius: 50px;
box-shadow: 10px 10px 100px #ff00dd;
}
</style>
</head>
<body>
<div>
<div style="width: 500px;display: block;text-align: center ">
<div>
<img src="../../resource/picture/a.jpg" alt="">
</div>
</div>
</div>
</body>
</html>
5.浮动
5.1 标准文档流
块级元素:独占一行
h1-h6 p div 列表…
行内元素:不独占一行
span a img strong
注:行内元素可以被包含在 块级元素中,反之,则不可以
5.2 display
- block:块元素
- inline:行内元素
- inline-block:是块元素,也是行内元素,可以内联,在一行(更多时候用float)
- none:消失
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
block 块元素
inline 行内元素
inline-block inline-block:是块元素,也是行内元素,可以内联,在一行
none 隐藏
-->
<style>
div{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
span{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
</style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>
</html>
5.3 float
左右浮动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="father">
<div class="layer01"><img src="../../resource/picture/a.jpg" alt=""></div>
<div class="layer02"><img src="../../resource/picture/b.jpg" alt=""></div>
<div class="layer03"><img src="../../resource/picture/c.jpg" alt=""></div>
<div class="layer04">
浮动的盒子可以向左浮动,也可以向右浮动,知道它的外边缘碰到包含或另一个浮动盒子为止
</div>
</div>
</body>
</html>
css代码:
div{
margin: 10px;
padding: 5px;
}
#father{
border: 1px #000 solid;
}
.layer01{
border: 1px #F00 dashed;
display: inline-block;
float: left;/*向左浮动*/
clear: both;/*清楚浮动*/
}
.layer02{
border: 1px #00F dashed;
display: inline-block;
float: left;
clear: both;
}
.layer03{
border: 1px #060 dashed;
display: inline-block;
float: left;
clear: both;
}
.layer04{
border: 1px #666 dashed;
font-size: 12px;
line-height: 23px;
float: left;
clear: both;
}
5.4 父级边框塌陷问题
clear
/*
clear:right;右侧不允许有浮动元素
clear:left; 左侧不允许有浮动元素
clear:both; 两侧不允许有浮动元素
clear:none;
*/
.layer04{
border: 1px #666 dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
float: right;
clear: left;
}
解决方案:
-
增加父级元素高度
#father{ border: 1px #000 solid; height: 800px; }
-
增加一个空的div(class=“clear”)标签,清除浮动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="css/style.css" type="text/css"> </head> <body> <div id="father"> <div class="layer01"><img src="../../resource/picture/a.jpg" alt=""></div> <div class="layer02"><img src="../../resource/picture/b.jpg" alt=""></div> <div class="layer03"><img src="../../resource/picture/c.jpg" alt=""></div> <div class="layer04"> 浮动的盒子可以向左浮动,也可以向右浮动,知道它的外边缘碰到包含或另一个浮动盒子为止 </div> <div class="clear"></div> </div> </body> </html>
div{ margin: 10px; padding: 5px; } #father{ border: 1px #000 solid; height: 800px; } .layer01{ border: 1px #F00 dashed; display: inline-block; float: left;/*向左浮动*/ } .layer02{ border: 1px #00F dashed; display: inline-block; float: left; } .layer03{ border: 1px #060 dashed; display: inline-block; float: right; } /* clear:right;右侧不允许有浮动元素 clear:left; 左侧不允许有浮动元素 clear:both; 两侧不允许有浮动元素 clear:none; */ .layer04{ border: 1px #666 dashed; font-size: 12px; line-height: 23px; display: inline-block; float: right; clear: left; } .clear{ clear: both; margin: 0; padding: 0; }
-
overflow
在父级元素中增加一个 overflow: hidden;
overflow: scroll;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #content{ width: 200px; height: 150px; overflow: scroll; } </style> </head> <body> <div id="content"> <img src="../../rescource/picture/a.jpg" alt=""> <p> 忽然觉得从前的自己很天真,你们终会离开。那就在我看得到的地方,平安喜乐,安然无恙! </p> </div> </body> </html>
-
父类添加一个伪类:after
#father:after{ content: ''; display: block; clear: both; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> div{ margin: 10px; padding: 5px; } #father{ border: 1px #000 solid; } /*父类添加一个伪类*/ #father:after{ content: ''; display: block; clear: both; } .layer01{ border: 1px #F00 dashed; display: inline-block; float: left;/*向左浮动*/ } .layer02{ border: 1px #00F dashed; display: inline-block; float: left; } .layer03{ border: 1px #060 dashed; display: inline-block; float: right; } /* clear:right;右侧不允许有浮动元素 clear:left; 左侧不允许有浮动元素 clear:both; 两侧不允许有浮动元素 clear:none; */ .layer04{ border: 1px #666 dashed; font-size: 12px; line-height: 23px; display: inline-block; float: right; } </style> </head> <body> <div id="father"> <div class="layer01"><img src="../../rescource/picture/a.jpg" alt=""></div> <div class="layer02"><img src="../../rescource/picture/b.jpg" alt=""></div> <div class="layer03"><img src="../../rescource/picture/c.jpg" alt=""></div> <div class="layer04"> 浮动的盒子可以向左浮动,也可以向右浮动,直到它的外边缘碰到包含或另一个浮动盒子为止 </div> <div class="clear"></div> </div> </body> </html>
5.5 小结:
-
浮动元后面增加空div
优点:简单
缺点:代码中尽量避免空div
-
设置父元素的高度
优点:简单
缺点:元素假设有了固定的高度,就会被限制
-
overflow
优点:简单
缺点:下拉的一些场景避免使用
-
父类添加一个伪类:after
写法稍微复杂一点,但是没有副作用,推荐使用!
-
5.6.display与float对比
- display:方向不可以控制
- float:浮动起来会脱离标准文档流,所以要解决父级边框塌陷的问题
6. 定位
6.1 相对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 相对定位
相对于自己原来的位置进行偏移~
-->
<style>
body{
padding: 20px;
}
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #666;
}
#first{
background-color: #3cbda6;
border: 1px solid #b27530;
position: relative;/*相对定位 上下左右*/
top: -20px;/*向上偏移20px*/
left: 20px;/*向右偏移20*/
}
#second{
background-color: #0000FF;
border: 1px solid #255066;
}
#third{
background-color: #008800;
border: 1px solid #1c6615;
position: relative;/*相对定位 上下左右*/
bottom: -20px;/*向下偏移20px*/
right: 20px;/*向左偏移20px*/
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
相对定位:position: relative;
相对于原来的位置,进行指定的偏移,相对定位的话,他仍然在标准文档流中,原来的位置会被保留。
练习:
- 使用div和a标签布局页面
- 每个超链接宽度和高度都是100px,背景颜色粉色,鼠标指针移上去变为蓝色
- 使用相对定位改变每个超链接的位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#box{
width: 300px;
height: 300px;
padding: 10px;
border: 2px solid #ff0000;
}
a{
width: 100px;
height: 100px;
text-decoration: none;
background-color: #e616ee;
line-height: 100px;
text-align: center;
color: white;
display: block;
}
a:hover{
background: #10dec7;
}
.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 绝对定位
定位:基于xxx定位。上下左右
-
没有父级元素定位的前提下,相对于浏览器定位
-
假设父级元素存在定位,我们通常会相对于父级元素进行偏移
-
在父级元素范围内
相对于父级或浏览器的位置,进行指定的偏移,相对定位的话,他仍然在标准文档流中,原来的位置不会被保留。
<!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: #3cbda6;
border: 1px solid #b27530;
}
#second{
background-color: #0000FF;
border: 1px solid #255066;
position: absolute;
left: 100px;
top: 20px;
}
#third{
background-color: #008800;
border: 1px solid #1c6615;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
6.3 固定定位 fixed
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
height: 10000px;
}
div:nth-of-type(1){/*绝对定位:相对于浏览器*/
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2){/*fixed:固定定位*/
width: 50px;
height: 50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>
6.4 z-index
层级概念:
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<style></style>
</head>
<body>
<div id="content">
<ul>
<li><img src="images/2020.jpg" alt=""/></li>
<li class="tipText">学习恋爱服务,找梁先生</li>
<li class="tipBg"></li>
<li>时间:forever</li>
<li>地点:我的宇宙</li>
</ul>
</div>
</body>
</html>
css代码:
#content{
width: 380px;
padding: 0px;
margin: 0px;
overflow: hidden;
font-size: 12px;
line-height: 25px;
border: 1px solid yellow;
}
ul,li{
padding: 0px;
margin: 0px;
list-style: none;
}
/*父级元素相对定位*/
#content ul{
position: relative;
}
.tipText,.tipBg{
position: absolute;
width: 380px;
height: 25px;
top:216px
}
.tipText{
color: white;
z-index: 999;
}
.tipBg{
background: orange;
opacity: 0.5;/*背景透明度*/
filter: alpha(opacity=50);
}
7.动画
简单了解:
推荐阅读简书:https://www.jianshu.com/p/e22da5e580ee
推荐网站:https://www.html5tricks.com/tag/html5-canvas/
卡巴斯基 全球实时网络攻击互动地图:https://www.digitaling.com/projects/12573.html?open_source=weibo_search
8.下一个目标
java script 见
谁说CSS一晚有手就行!