HTML+CSS宝典 CSS基础 定位练习
1、二级菜单
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>二级菜单</title> <link rel="stylesheet" href="reset.css"> <link rel="stylesheet" href="index.css"> </head> <body> <header class="header"> <ul class="topnav clearfix"> <li><a href="">Lorem</a></li> <li><a href="">Eos</a></li> <li><a href="">Laboriosam.</a></li> <li> <a href="">客户服务</a> <div class="submenu"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Quia eligendi laudantium maxime beatae </div> </li> <li><a href="">Consectetur</a></li> </ul> </header> <div style="height:1500px;background-color: burlywood;"></div> </body> </html>
index..css
.clearfix::after{ content: ''; display: block; clear: both; } .header{ background: #e3e4e5; color:#999; height: 40px; line-height: 40px; position: fixed; /* 固定定位,位置是相对于视口 */ width: 100%; /* 设置固定定位/绝对定位之后,宽度会成自动,宽度要适应内容,所以要设置宽度100%撑满 */ left: 0; top: 0; } .header .topnav>li{ /* topnav>li 只选择下面一级的子元素,因为下面可能还有li,所以要控制范围,不能影响太多 */ float: left; margin: 0 20px; /* 1.宽度设置 padding: 0 8px; 也可以 2.浮动元素宽度为auto时候适应内容,内容比较多把宽度撑开了, 加上子元素submenu,把li给撑开了,所以宽度还是要用 width 设置 */ width: 150px; height: 38px; text-align: center; border: 1px solid #e3e4e5;/* box-sizing:border-box; */ border-bottom: none; position: relative; } .header .topnav>li:hover{ background-color: #fff; border:1px solid #ccc; border-bottom: none; } /* 字体发居中、行高会继承li元素,所以单独设置子菜单submenu */ .header .topnav>li .submenu{ text-align: left; line-height: 1.5; width: 300px; background-color: #fff; padding: 10px 10px; display: none; /* display表示生成盒子,值为none表示不生成盒子,如果一个元素在页面上没有生成盒子,那个这个元素在页面上是看不见的 */ border:1px solid #ccc; position: absolute; right: -1px; top: 38px; } /* 鼠标移动入到这个li上面的时候,找到这个li下面的submenu */ .header .topnav>li:hover .submenu{ display: block; } /* .header .topnav>li::after 变成 .header .topnav>li:hover::after 找到鼠标移动到的li的时候,才加上伪元素 */ .header .topnav>li:hover::after{ content:''; position: absolute; width: 100%; height: 5px; bottom: -1px; left: 0; background-color: #fff; }
2、弹出层
.modal 表示遮罩层,遮罩层是怎么把窗口刚好铺满呢?
1. 其实就是固定定位 fixed 的元素,
固定定位就不需要设置父元素为 position: relation
固定定位的包含块一定是视口,整个浏览器窗口
2. 设置元素的大小跟视口一样
width:100% 百分之百是相对包含块的宽度,现在包含块是视口,刚好把视口横向占满
height:100% 视口的高度就浏览器窗口的高度,所以大胆使用百分百比作为高度
3. 设置位置 left:0 、 top:0
关于透明效果?每个颜色都具有透明通道
1. 如果用 rgb 来表示就应该是 rgba( 红, 绿, 蓝, alpha ) ,
a 表示 alpha,表示透明通道,取值 0 ~ 1 ,0 是完全透明,1 是完全不透明,中间可以取小数 0.5,css里面小数点可以省略前面的0,比如.5
3. 还可以用 HEX 方式表示 #红绿蓝透明 ,每个颜色都是两个数字表示,透明也是两个数字表示 00 ~ ff
html文件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>弹出层</title> <link rel="stylesheet" href="reset.css"> <link rel="stylesheet" href="index.css"> </head> <body> <div class="main"> <img src="http://www.ruyic.com/blog/uploads/image/202201/164242168139798.jpg" alt=""> </div> <div class="modal"> <div class="container"> 这是弹出框 <div class="close">X</div> </div> </div> </body> </html>
index.css
.main img{ height: 2000px; background-color: #FF8c8c; /* background-image: url(./bg.PNG); */ width: 100%; } .modal{ /* 遮罩层 */ width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); position: fixed; top: 0; left: 0; } .modal .container{ /* container区域在遮罩层里面居中 */ width: 400px; height: 500px; background: #FFFFFF; position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; /* end */ padding: 1em; border-radius: 6px; } .modal .container .close{ width: 30px; height: 30px; font-size: 14px; background-color: #FF4400; border-radius: 50px; line-height: 30px; text-align: center; color: #fff; position: absolute; right:-15px; top: -15px; cursor: pointer; }
3、轮播图
index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="reset.css"/> <link rel="stylesheet" type="text/css" href="index.css"/> <title>轮播图</title> </head> <body> <div class="banner"> <div class="imgs"> <!-- a*3>img[src=$.jpg] --> <a href=""><img src="http://ruyic.com/blog/uploads/image/201505/143071918233392.jpg" alt=""></a> <a href=""><img src="http://ruyic.com/blog/uploads/image/201505/143070453031850.jpg" alt=""></a> <a href=""><img src="http://ruyic.com/blog/uploads/image/201505/143071919486009.jpg" alt=""></a> <div class="left"><</div> <div class="right">></div> <div class="modal"> <div class="title"> <h2>标题标题标题标题</h2> </div> <div class="dots"> <ul> <li></li> <li></li> <li></li> </ul> </div> </div> </div> </div> </body> </html>
index.css
.banner{ width: 520px; height: 304px; /* border: 1px solid; */ margin: 1em auto; overflow: hidden; position: relative; } .banner .imgs{ width: 1560px; height: 304px; /* 高度定死304像素,高度就不会坍塌了,坍塌的高度值为auto */ } .banner .imgs a{ float: left; } .banner .imgs img{ width: 520px; height: 304px; } .banner .left, .banner .right{ position: absolute; width: 50px; height: 50px; color: #FFFFFF; line-height: 50px; text-align: center; font-size: 2em; font-family: arial; top: 0; bottom:0; margin:auto; cursor: pointer; } .banner .left:hover, .banner .right:hover{ background-color: #FFFFFF; color: #f40; border-radius: 50%; } .banner .left{ left: 20px; /* top: 0; bottom:0; margin:auto; 设置垂直居中 */ } .banner .right{ right: 20px; /* top: 0; bottom:0; margin:auto; 设置垂直居中 */ } .banner .modal{ width: 100%; height: 40px; line-height: 40px; background-color: rgba(0,0,0,.3); position: absolute; bottom: 0; left: 0; color: #fff; box-sizing: border-box; padding: 0 20px; } .banner .modal .title{ float: left; color: #fff; font-weight: bold; } .banner .modal .dots{ float: right; } .banner .modal .dots li{ width: 8px; height: 8px; background-color: #ccc; display: inline-block; margin: 0 2px; border-radius: 50%; cursor: pointer; } .banner .modal .dots li:hover{ background-color: #369; }
reset.css
/* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; /* 可以理解为1em */ font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; /* 行高是1 */ } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } /* 自己添加的a元素初始化 */ a{ text-decoration: none; color: inherit; } /* 淘宝网的字体设置 */ body, button, input, select, textarea { font: 14px/1.5 tahoma,arial,'Hiragino Sans GB','\5b8b\4f53',sans-serif; }
Leave a comment
0 Comments.