8,postion 重点掌握1)position: fixed,2)父position:relative + 子position: absolute fixed: style="position: fixed" 固定在某个位置,可以设置离开上下左右(top,bottom,left,right)的距离,滚动滑轮时该位置不会变化 设置为fixed后,相当于float了,可以完成头部菜单和内容的分离 absolute: style="position: absolute" 单独存在场景非常少,一般和relative结合使用 固定在某个位置,可以设置离开上下左右(top,bottom,left,right)的距离,滚动滑轮时该位置会变化 relative: <div style="position: relative;"> <div style="pisition: absolute;"> xxx </div> </div> 单独relative存在时没有任何意义,relative + absolute 可以实现子标签根据夫标签定位 样例一,结合JavaScript可以实现返回顶部的例子: <div onclick="GoTop()" style="position:fixed; bottom:20px; right:20px"> <script> function GoTop(){ document.body.scrollTop = 0; } </script>
样例二,position:fixed实现头部和内容的分离 <div style="height: 48px; position: fixed; top: 0; left:0; right:0">头部</div> <div style="margin-top: 50px; height: 3000px;">内容</div>
9,分层 z-index:层级顺序,越大越在上面。 opasity:明度,0~1 用position:fixed去覆盖底层,想要居中靠margin:auto 0;会失效,可以通过距离边缘百分比和px搞定,参考下面样例的顶层 样例:实现三层模态对话框功能: 顶层-中间遮罩层-底层,上面2层需要有position:fixed确保光标不管移动到哪都能跳出来,另外默认需要display:none,点击按钮后才触发呈现 <div style="z-index: 10; position: fixed; height:400px; width:500px; background-color: white; top: 50%; left:50%; margin-left: -250px; margin-top: -200px;" ></div> <div style="z-index: 9; position: fixed; background-color: black; top:0; right:0; left:0; bottom: 0; opacity: 0.5;"></div> <div style="height:5000px; background-color: green">asdf</div>
10,图片超出父级标签的范围时,overflow hidden :多的部分隐藏 auto:出现滚动条 样例,图片如果超出200px+200px,超出部分会被隐藏掉: <div style="height:200px; width:200px; overflow:hidden" > <img src="1.jpg"> </div>
11,hover 鼠标移动当前标签上式,hover样式才会生效 .pg-header .menu : hover{ background-color: blue} 样例,头顶部分离,居中菜单,应用hover <head> <style> .pg-header{ position: fixed; right: 0; left:0; top:0; bottom: 0; height: 48px; background-color: #2459a2; line-height: 48px; /*让头部菜单内容上下居中*/ } .pg-body{ margin-top: 50px; } .w{ width: 980px; margin: 0 auto; /*让头部菜单内容左右各自留空些,如果是想居中,可以直接在.header里text-align: center;*/ } .pg-header .menu{ display: inline-block; /*行内标签页可以设置一些块级标签的属性*/ padding: 0 20px; /*上下留0px,左右留10px*/ } .pg-header .menu:hover{ /*鼠标移动到当前标签上时,以下CSS属性才生效*/ background-color: blue; } </style> </head> <body> <div class="pg-header"> <div class="w"> <a class="LOGO"> LOGO </a> <a class="menu"> 全部 </a> <a class="menu"> 42区 </a> <a class="menu"> 段子 </a> <a class="menu"> 笑话 </a> </div> </div> <div class="pg-body"> <div class="w"> 内容 </div> </div> </body>
12,Background,背景图片 background-image: url('image/x.gif'); # 如果div大,则图片会重复堆叠放 Background-repeat: no repeat; # 可以让图片不再重复堆叠 Background-position-x: 3px # 只横着加 Background-position-y: 4px # 只竖着加 等效于:background-position: 3px,4px 简写方法:background: url() 3px 4px no-repeat; # 直接在background属性下加值 样例一,一幅竖着很长的图片,实现选中不同位置的小图标呈现(大网站图标都是靠这种方式实现,减少请求次数) <div style="background-image: url('image/x.jpeg'); background-repeat: no repeat; <!--默认会堆叠放,加上no-repeat后就不会堆叠放了--> height:80px; height: 20px; width: 20px; Background-position-x: 0px; <!--默认就是0px,可以省略--> Background-position-y: -30px; "> <!--靠移动y轴改变图片,2句可以合并为background-position: 0px,-30px --> </div>
样例二,input登陆框边上加上个小图标,用到了background,position,padding,display <div style="height: 35px; width: 400px; position: relative"> <input type="text" style="height: 35px; width: 370px; padding-right: 30px"> <span style="background-image: url(x.jpg); right:5px; top:10px; height: 20px; width: 20px; display: inline-block; position: absolute"></span> </div>
13,其他 自适应:media IE中,默认img标签,有一个1px的边框,如果不需要,可以<style>img{border:0;}</style> CSS需要掌握的属性: position:位置,absolute+relative,fixed background-color:背景色 text-align: margin padding font-size z-index over-flow :hover opacity float clear:both line-height border color display height:高度
转载于:https://www.cnblogs.com/guxh/p/9695073.html
相关资源:JAVA上百实例源码以及开源项目