网站是用什么软件做的/安徽网站设计
浮动
文档流
文档流,是指盒子按照html标签编写的顺序依次从上到下,从左到右排列,块元素占一行,行内元素在一行之内从左到右排列,先写的先排列,后写的排在后面,每个盒子都占据自己的位置。
浮动特性
浮动元素有左浮动(float:left)和右浮动(float:right)两种
浮动的元素会向左或向右浮动,碰到父元素边界、浮动元素、未浮动的元素才停下来
相邻浮动的块元素可以并在一行,超出父级宽度就换行
浮动让行内元素或块元素自动转化为行内块元素
浮动元素后面没有浮动的元素会占据浮动元素的位置,没有浮动的元素内的文字会避开浮动的元素,形成文字饶图的效果
父元素内整体浮动的元素无法撑开父元素,需要清除浮动
浮动元素之间没有垂直margin的合并
浮动示例
1.利用浮动形成左右对峙的两个盒子
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>浮动</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 400px;height: 80px;border: 1px solid black;margin: 50px auto 0;}.d1 div {width: 60px;height: 60px;margin: 10px;}.d1_1 {background-color: red;float: left;}.d1_2 {background-color: blue;float: right;}</style>
</head>
<body>
<div class="d1"><div class="d1_1"></div><div class="d1_2"></div>
</div>
</body>
</html>利用浮动形成左右对峙的两个盒子
对峙盒子效果图
2.上面盒子浮动,下面盒子不浮动
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>文字饶图</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 450px;height: 210px;border: 1px solid black;margin: 50px auto 0;}.pic {width: 80px;height: 80px;background-color: red;float: left;}.text {height: 130px;background-color: blue;}</style>
</head>
<body>
<div class="d1"><div class="pic"></div><div class="text"></div>
</div>
</body>
</html>上面盒子浮动,下面盒子不浮动
上面浮动下面不浮动效果图
本来两个盒子各占一行,我们将上面的红色盒子浮动后,蓝色盒子会和红色盒子重叠,且在最左上角部分,红色覆盖蓝色。
如果未浮动的元素中有文字,我们将背景色去掉,就会形成文字饶图的情况;此外,我们也可以给浮动的块设置margin,控制文字离它的距离。
3.文字饶图示例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>文字饶图</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 450px;height: 210px;border: 1px solid black;margin: 50px auto 0;}.pic {width: 80px;height: 80px;background-color: red;float: left;margin: 10px;}.text {height: 130px;background-color: blue;/*float: left;*/}</style>
</head>
<body>
<div class="d1"><div class="pic"></div><div class="text">哪怕是水龙宗修行水法的看门修士,都无法发现有那一粒粒金光从诸多匾额当中掠出,飘落在地,如萤火攒聚,合拢成为一位高冠博带的少年,大步走入城门,看守城门的水龙宗修士便有些不知所措,这是千年未有的异象,便立即飞剑传讯北宗祖师堂。</div>
</div>
</body>
</html>文字饶图示例
文字饶图效果图
4.用列表制作菜单
首页验收标准大概在3个像素之内,其余页面在5个像素之内即可。
一般菜单都是用ul+li+a来制作的,而不是用div+a制作的;
去掉列表的小圆点:list-style: none;
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>菜单</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.menu {width: 694px;height: 50px;/*background-color: aqua;*//* 去掉无序列表的小圆点 */list-style: none;margin: 50px auto 0;padding: 0;}.menu li {width: 98px;height: 48px;border: 1px solid #000;background-color: gray;/*display: inline-block;*/float: left;margin-left: -1px;}.menu li a {/*background-color: yellow;*/display: block; /* 只需将a链接转换为块标签,再加上hover,就可以实现鼠标移到块上,就整体变色,原来是必须a标签上 */width: 98px;height: 48px;text-align: center; /* 将a标签里面的文字水平居中 */line-height: 48px; /* 将a标签里面的文字垂直居中 */text-decoration: none;font-size: 16px;font-family: "Microsoft YaHei";color: black;}.menu li a:hover {background-color: blue;color: #fff;}</style>
</head>
<body>
<!--ul.menu>(li>a{公司简介})*7-->
<ul class="menu"><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="">公司简介</a></li><li><a href="">公司简介</a></li>
</ul></body>
</html>用列表制作菜单
清除浮动
清除浮动的方法:
父级上增加属性overflow:hidden
在最后一个子元素的后面加一个空的div,给它样式属性 clear:both(不推荐)
使用成熟的清浮动样式类,clearfix
.clearfix:after,.clearfix:before{ content: "";display: table;}
.clearfix:after{ clear:both;}
.clearfix{zoom:1;}
清除浮动的使用方法:
.con2{... overflow:hidden}
或者
<div class="con2 clearfix">
需要清除浮动的场景:1.元素浮动;2.父级元素未设置高度。
在一个div里面,设置边框且不设置高,全部放浮动的元素,会发现上下两条边会合在一起,这是因为浮动的元素撑不起盒子;
.clearfix {zoom: 1;} /* 解决在ie浏览器中可能无法清除浮动的问题 */
清除浮动的方法示例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>清除浮动</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.list {width: 210px;border: 1px solid black;margin: 50px auto 0;list-style: none;padding: 0;/*overflow: hidden; !* 第一种清除浮动的方法,有一定问题 *!*/}.list li {width: 50px;height: 50px;background-color: gray;margin: 10px;float: left;}/*!* 第三种清除浮动的标准方法,合并前 *!*//*.clearfix:after { !* 清除浮动 *!*//*content: "";*//*display: table;*//*clear: both;*//*}*//*.clearfix:before { !* 清除margin-top塌陷 *!*//*content: "";*//*display: table;*//*}*//* 第三种清除浮动的标准方法,给父级元素添加clearfix;类似于第二种方法;我们一般将清除浮动和解决margin-top合在一起 */.clearfix:after, .clearfix:before {content: "";display: table;}.clearfix:after {clear: both;}.clearfix {zoom: 1;} /* 解决在ie浏览器中可能无法清除浮动的问题 */</style>
</head>
<body>
<!--ul.list>li{$}*8-->
<ul class="list clearfix"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><!-- 第二种清除浮动的方法,以前使用的,现在不推荐使用 --><!--<div style="clear: both"></div>-->
</ul></body>
</html>清除浮动的方法示例
定位
关于定位
我们可以使用css的position属性来设置元素的定位类型,postion的设置项如下:
relative 生成相对定位元素,元素所占据的文档流的位置不变,元素本身相对文档流的位置进行偏移
absolute 生成绝对定位元素,元素脱离文档流,不占据文档流的位置,可以理解为漂浮在文档流的上方,相对于上一个设置了相对或者绝对或者固定定位的父级元素来进行定位,如果找不到,则相对于body元素进行定位。
fixed 生成固定定位元素,元素脱离文档流,不占据文档流的位置,可以理解为漂浮在文档流的上方,相对于浏览器窗口进行定位。
static 默认值,没有定位,元素出现在正常的文档流中,相当于取消定位属性或者不设置定位属性
inherit 从父元素继承 position 属性的值
定位元素特性
绝对定位和固定定位的块元素和行内元素会自动转化为行内块元素
定位元素层级
定位元素是浮动的正常的文档流之上的,可以用z-index属性来设置元素的层级;层级最高,就浮动在最上面。
关于层级,后写的标签默认会漂浮在先写的标签的上面,即后写的默认层级最高。
典型定位布局
固定在顶部的菜单
水平垂直居中的弹框
固定的侧边的工具栏
固定在底部的按钮
应注意,相对定位后,元素所占据的文档流的位置不变,这句话可以理解为一个萝卜一个坑,而有一个萝卜飘起来了,但那个坑还是它占着。
相对定位较少使用,一般是和绝对定位配合使用,用作绝对定位的父级定位标签;
要使用绝对定位,先设置父级定位,如果父级不需要偏移,只需要设置一个相对定位 position: relative;即可。
固定定位只相对于浏览器窗口进行定位,不因父级定位了而改变定位的地点。
绝对定位示例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>定位</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 400px;height: 400px;border: 1px solid black;margin: 50px auto 0;}.d1_1, .d1_2 {width: 300px;height: 100px;margin: 10px;}.d1_1 {background-color: red;position: relative;left: 50px;top: 50px;}.d1_2 {background-color: blue;}</style>
</head>
<body>
<div class="d1"><div class="d1_1"></div><div class="d1_2"></div>
</div>
</body>
</html>绝对定位示例
相对定位实例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>定位</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 400px;height: 400px;border: 1px solid black;margin: 50px auto 0;position: relative;}.d1_1, .d1_2 {width: 300px;height: 100px;margin: 10px;}.d1_1 {background-color: red;position: absolute;left: 50px;top: 50px;}.d1_2 {background-color: blue;}</style>
</head>
<body>
<div class="d1"><div class="d1_1"></div><div class="d1_2"></div>
</div>
</body>
</html>
固定定位实例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>定位</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 400px;height: 400px;border: 1px solid black;margin: 50px auto 0;position: relative;}.d1_1, .d1_2 {width: 300px;height: 100px;margin: 10px;}.d1_1 {background-color: red;position: fixed;left: 50px;top: 50px;}.d1_2 {background-color: blue;}</style>
</head>
<body>
<div class="d1"><div class="d1_1"></div><div class="d1_2"></div>
</div>
</body>
</html>
定位元素的层级示例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>定位元素的层级</title><style>.d1 {width: 400px;height: 400px;border: 1px solid black;margin: 50px auto 0;position: relative;}.d1 div {width: 200px;height: 200px;position: absolute;}.d1_1 {background-color: red; left: 20px; top: 20px; z-index: 11}.d1_2 {background-color: blue; left: 40px; top: 40px}.d1_3 {background-color: green; left: 60px; top: 60px}.d1_4 {background-color: yellow; left: 80px; top: 80px}</style>
</head>
<body>
<div class="d1"><div class="d1_1"></div><div class="d1_2"></div><div class="d1_3"></div><div class="d1_4"></div>
</div>
</body>
</html>
消息提示示例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>消息提示</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 100px;height: 100px;margin: 50px auto 0;background-color: yellow;position: relative;border-radius: 14px; /* 将方块盒子设置成方块圆角盒子 */}.d1_1 {width: 28px;height: 28px;background-color: red;color: white;text-align: center;line-height: 28px;position: absolute;left: 86px;top: -14px;border-radius: 14px; /* 将方块盒子设置成圆形盒子 */}</style>
</head>
<body>
<div class="d1"><div class="d1_1">5</div>
</div>
</body>
</html>
02:定位实例-固定在顶部的菜单栏-丐版
让浮动元素居中:用margin的auto没有用,先使用left=50%,再用margin-left= - 浮动元素宽度的一半;
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>菜单置顶</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.menu {position: fixed;width: 960px;height: 80px;background-color: gray;top: 0; /* 让菜单置顶 */left: 50%;margin-left: -480px; /* 让菜单水平居中 */}</style>
</head>
<body>
<div class="d1"><div class="menu">菜单文字</div><p>长文字</p><p>...</p><p>长文字</p>
</div></body>
</html>
弹框-丐版的实现
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>弹框</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1_1 {width: 500px;height: 300px;border: 1px solid black;background-color: gray;color: white;position: fixed;left: 50%;margin-left: -251px; /* 让丐版水平居中 */top: 50%;margin-top: -151px; /* 让丐版垂直居中 */z-index: 999;}.d1_1 h2 {background-color: aqua;color: black;margin: 10px;height: 30px;}.mask { /* 作用是当丐版出现后将丐版以外的内容虚化从而突出丐版 */position: fixed;width: 100%;height: 100%;background-color: black;left: 0;top: 0;opacity: 0.3; /* 设置全屏透明度 */z-index: 998;}.d1 {display: block; /* 控制丐版的出现与否 */}</style>
</head>
<body>
<div class="d1"><div class="d1_1"><h2>弹框标题</h2></div><div class="mask"></div>
</div></body>
</html>
background属性
属性解释
background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性。
它可以分解成如下几个设置项:
background-color 设置背景颜色
background-image 设置背景图片地址
background-repeat 设置背景图片如何重复平铺
background-position 设置背景图片的位置
background-attachment 设置背景图片是固定还是随着页面滚动条滚动
实际应用中,我们可以用background属性将上面所有的设置项放在一起,而且也建议这么做,这样做性能更高,而且兼容性更好,比如:“background: #00FF00 url(bgimage.gif) no-repeat left center fixed”,这里面的“#00ff00”是设置background-color;“url(bgimage.gif)”是设置background-image;“no-repeat”是设置background-repeat;“left center”是设置background-position;“fixed”是设置background-attachment,各个设置项用空格隔开,有的设置项不写也是可以的,它会使用默认值。
背景属性示例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>background属性</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 400px;height: 200px;border: 1px solid;margin: 50px auto 0;background-image: url(../images/bg.jpg); /* 默认直接平铺满整个屏幕 *//* repeat-x 只铺满x轴;repeat-y 只铺满y轴;no-repeat 只平铺一次;repeat 平铺所有;*/background-repeat: no-repeat; /* 铺满x轴 *//* 在no-repeat的情况下,left top:平铺在左上角left center:平铺在左边的中间left bottom:平铺在左边的下面center top:平铺在水平方向中间的顶部;right center:平铺在右边的中间;所以组合起来共九种方式。*//*background-position: center center;*//* 除了用上中下之外,还能用数字类似于定位 */background-position: -20px 10px;background-color: aqua;/*background: url(../images/bg.jpg) -20px 10px no-repeat aqua; !* background参数合并写法,可以只写一两个 *!*/}</style>
</head>
<body>
<div class="d1"><!--<img src="../images/bg.jpg" alt="html5图片">-->html5图片
</div></body>
</html>
大图放小框示例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>大图放小框</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {width: 100px;height: 100px;border: 2px solid black;margin: 50px auto 0;background: url(../images/location_bg.jpg) -103px -150px no-repeat;}</style>
</head>
<body><div class="d1"></div>
</body>
</html>
在调试时,可以通过在浏览器上点击检查,点击右边的css样式,进入调试模式来确定合适的数值;
雪碧图列表背景
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>雪碧图列表的使用</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.list {list-style: none;width: 300px;height: 305px;margin: 50px auto 0;padding: 0;/*background-color: aqua;*/}.list li {height: 60px;border-bottom: 1px dotted black;line-height: 60px;text-indent: 50px;background: url(../images/bg01.png) left 10px no-repeat;}.list .icon02 {background-position: left -71px}.list .icon03 {background-position: left -153px}.list .icon04 {background-position: left -235px}.list .icon05 {background-position: left -317px}</style>
</head>
<body>
<div class="d1"><!--ul.list>li{美人鱼}*5--><ul class="list"><li>美人鱼</li><li class="icon02">美人鱼</li><li class="icon03">美人鱼</li><li class="icon04">美人鱼</li><li class="icon05">美人鱼</li></ul>
</div></body>
</html>
网页背景固定
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>网页背景固定</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.d1 {/*background-attachment: fixed;*/background: url(../images/0022.jpg) no-repeat fixed 50%;}p {text-align: center;color: deeppink;}</style>
</head>
<body>
<div class="d1"><p>文字内容</p><br><br>......<p>文字内容</p><br><br><p>文字内容</p>
</div>
</body>
</html>
最后,给大家推荐一个前端学习进阶内推交流圈子(前端资料分享),不管你在地球哪个方位,
不管你参加工作几年都欢迎你的入驻!(会定期免费提供一些收藏的免费学习书籍资料以及整理好的面试题和答案文档!)
如果您对这个文章有任何异议,那么请在文章评论处写上你的评论。
如果您觉得这个文章有意思,那么请分享并转发,或者也可以关注一下表示您对我们文章的认可与鼓励。
愿大家都能在编程这条路,越走越远。