前两天在给一个项目作东西的时候,碰到一个有意思的项目,是须要兼容ie6,有一些碰到而且解决的问题,给你们写下来,方便你们之后碰到相似的问题哈~javascript
能帮到你的话,点个赞呗?css
important某些状况下不能决定最终的样式属性。html
咱们经过对颜色的控制说明这一切~前端
<style type="text/css"> div { width: 100px; height: 100px; border: aliceblue 2px solid; } .frist { background-color: red !important; background-color: blue; } .second { background-color: red !important; } .third { background-color: blue; } .second { background-color: blue; } </style> <div class="frist"></div> <div class="second third"></div> <div class="second"></div>
谷歌 | ie6 |
---|---|
![]() |
![]() |
1.在同一个css代码块中的important没那么强力,会被后续的样式覆盖; 2.不过若是再也不同一个css代码块中写的话,你大爷终究是你大爷~ 3.因此咱们能够利用这个漏洞,写ie6专有的样式了(伪hack)(慎用,不如用ie6的hack写法“_”)
当float和margin同时设置时,就会出现margin双倍的问题java
<style type="text/css"> /** 包裹区 **/ .area { width: 200px; height: 200px; background-color: #00FFFF; } /** 底部指示区 **/ .footer { width: 200px; height: 100px; background-color: royalblue; } /** 测试,margin 的代码块 **/ .testMargin { width: 200px; height: 100px; float: left; margin: 50px; background-color: #0079CC; } /** 50px 指示区 **/ .checkLine { width: 50px; float: left; height: 100px; display: inline-block; background-color: #000; } /** 100px 指示区 **/ .checkLine2 { width: 50px; height: 100px; display: inline-block; background-color: teal; } </style> <div class="area"> <div class="testMargin"></div> </div> <div class="footer"> <!-- 50px 指示块 --> <span class="checkLine"></span> <!-- 100px 指示块 --> <span class="checkLine2"></span> </div>
谷歌 | ie6 |
---|---|
![]() |
![]() |
将div设置display:inlinegit
.testMargin { width: 200px; height: 100px; float: left; display: inline; margin: 50px; background-color: #0079CC; }
编写ie6的hack程序员
.testMargin { width:200px; height:100px; float:left; margin:50px; background-color:#0079CC; _margin: 50px 25px; }
<style type="text/css"> .pngImg { border: #FF0000 1px solid; width: 200px; height: 200px; } .jpgImg { border: black 1px solid; width: 200px; height: 200px; } .pngSpan { border: blue 1px solid; display: inline-block; width: 200px; height: 200px; background: transparent url(https://jhcan333.github.io/can-Share/image/ie6/404.png) no-repeat center top; background-size: cover; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/ie6/404.png", sizingMethod='scale'); /*IE6有效*/ _background: none; /*IE6有效*/ } .jpgSpan { border: brown 1px solid; display: inline-block; width: 200px; height: 200px; background: transparent url(https://jhcan333.github.io/can-Share/image/ie6/404.jpg) no-repeat center top; background-size: contain; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/ie6/404.jpg", sizingMethod='scale'); /*IE6有效*/ _background: none; /*IE6有效*/ } </style>
<span class="pngSpan"></span> <img class="pngImg" src="https://jhcan333.github.io/can-Share/image/ie6/404.png"> <span class="jpgSpan"></span> <img class="jpgImg" src="https://jhcan333.github.io/can-Share/image/ie6/404.jpg">
IE6不支持png背景透明或半透明,因此img标签中的图片会带有背景色,须要借助css滤镜来实现github
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/404.png",sizingMethod='scale');/*IE6有效*/ _background:none;/*IE6有效*/
在这里,首先把background取消,而后使用css滤镜来设置。其中属性前面添加”_”下划线,来表示,只在ie6上使用。web
<script> function correctPNG() { var arVersion = navigator.appVersion.split("MSIE") var version = parseFloat(arVersion[1]) if((version >= 5.5) && (document.body.filters)) { for(var j = 0; j < document.images.length; j++) { var img = document.images[j] var imgName = img.src.toUpperCase() if(imgName.substring(imgName.length - 3, imgName.length) == "PNG") { var imgID = (img.id) ? "id='" + img.id + "' " : "" var imgClass = (img.className) ? "class='" + img.className + "' " : "" var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' " var imgStyle = "display:inline-block;" + img.style.cssText if(img.align == "left") imgStyle = "float:left;" + imgStyle if(img.align == "right") imgStyle = "float:right;" + imgStyle if(img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" img.outerHTML = strNewHTML j = j - 1 } } } } function addHandler (element, type, handler) { if (element.addEventListener) { // DOM2级 事件处理程序,this 指向元素自己。按照添加的顺序正向执行 element.addEventListener(type, handler, false); } else if (element.attachEvent) { // IE 事件处理程序,this 指向 window。按照添加的顺序反向执行 element.attachEvent("on" + type, handler); } else { // DOM0级 事件处理程序。只能绑定一个事件处理程序 element["on" + type] = handler; } } addHandler(window,'load',correctPNG) </script>
将这个js引入项目就能够了浏览器
本是inline的html元素设置为inline-block后,会具备inline-block的特性;
但本是block的html元素设置为inline-block后,会出现不在同行排布的状况;
<style type="text/css"> .span-inline-block { background-color: #7FFFD4; display: inline-block; width: 100px; height: 100px; margin: 5px; } .div-inline-block { background-color: #00ff00; display: inline-block; width: 100px; height: 100px; margin: 5px; } </style> <span class="span-inline-block"></span> <span class="span-inline-block"></span> <span class="span-inline-block"></span> <span class="span-inline-block"></span> <div class="div-inline-block"></div> <div class="div-inline-block"></div> <div class="div-inline-block"></div> <div class="div-inline-block"></div>
1.若无特殊要求,能够把div改成span 2.能够设置float属性。如float为right时,效果以下
<style type="text/css"> .min-div-class { min-width: 200px; min-height: 200px; background-color: #00FF00; } </style> <div class="min-div-class"></div>
直接设置width、height。
原本我把select框的样式给调的美美的,好比这样
结果在ie6上乱了套,源码我就不写了,直接写demo
<style type="text/css"> .selectArea{ position: relative; width:100px; height:24px; line-height:20px; padding-left: 5px; border:1px solid #0079cc; overflow: hidden; } .testSelect{ width:150px; line-height:30px; margin: -3px 0px; border:0px solid transparent; outline: none; background: none; appearance: none; -moz-appearance: none; -webkit-appearance:none; } .dropdown{ position: absolute; right:5px; top:10px; } </style> <div class="selectArea"> <select class="testSelect"> <option>1</option> <option>2</option> <option>3</option> </select> <img class="dropdown" src="https://jhcan333.github.io/can-Share/image/ie6/arrow.png"> </div>
谷歌 | ![]() |
---|---|
ie8 | ![]() |
ie6 | ![]() |
高度~边框 ~ 彻底很差整 ~
Ie6上看起来整齐就行了,不要什么花里胡哨的东西了~hash走起! (T_T)
<style type="text/css"> .selectArea { position: relative; width: 100px; height: 24px; line-height: 20px; padding-left: 5px; border: 1px solid #0079cc; overflow: hidden; _border: 0px #fff solid; _padding: 0px; _overflow: auto; } .testSelect { width: 150px; line-height: 30px; margin: -3px 0px; border: 0px solid transparent; outline: none; background: none; appearance: none; -moz-appearance: none; -webkit-appearance: none; _width: 100px; _margin: 0px; } .dropdown { position: absolute; right: 5px; top: 10px; _display: none; } </style>
差很少是这个效果了吧~(原生的也仍是很整齐的啊)
ie6上的css问题就先整理到这里了,欢迎你们评论讨论 备注:转载注明出处
最近在搞一个和前端程序员相关的公号,除了技术分享以外,也增长了对于职业发展、生活记录之类的文章,欢迎你们关注,一块儿聊天、吐槽,一块儿努力工做,认真生活!