CSS常见问题
1 (IE6,7)H5标签兼容
解决方法1:(只显示核心代码)
1<script>
3 document.createElement("header");
4 document.createElement("section");
5 document.createElement("footer");
6 </script>
7
8 <style>
9 //由于它默认的是不识别的,所以把这个标签理解为自定义标签,自定义标签默认就是内联元素,内联元素不支持宽高,所以我们要将其转换成块元素——display:block
10 header{width:200px;height:200;display:block;background:red;}
11 section{width:400px;height:400;display:block;background:green;}
12 footer{width:200px;height:200;display:block;background:red;}
13 </style>
14
15 <body>
16 <header>header</header>
17 <section>section</section>
18 <footer>footer</footer>
19 </body>
解决方法2:引入html5shiv.js插件
2 元素浮动之后,能设置宽度的话就给元素加宽度,如果需要宽度是内容撑开,就给它里边的块元素加上浮动
解决方案:需要谁去浮动,就把浮动加给谁
<style>
width:400;
border:1px soild black;
overflow:hidden;//清浮动,仅为了演示,不是最好的方法
}
.left{
float:left;
background-color:red;
}
.right{
float:right;
background-color:green;
}
h2{
float:left; //解决方案:需要谁去浮动,就把浮动加给谁
margin:0;
height:30px;
}
</style>
<body>
<div class="box">
<div class="left>
<h2>left</h2>
</div>
<div class="right>
<h2>right</h2>
</div>
</div>
3 第一块元素浮动,第二块元素加margin值等于第一块元素 在IE6下会有间隙问题
解决方案:1 不建议这么写 2 如非要这样写,建议用浮动解决,不用margin
body{margin:0;}
.box{width:300px;}
.left{
width:200px;
height:200px;
backgrong-color:red;
float:left;
}
.right{
width:200px;
height:200px;
backgrong-color:blue;
// margin-left:200px;
float:left;//解决方案:用浮动解决
}
<body>
<div class="box>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
4 IE6下子元素超出父级宽高,会把父级的宽高撑开
解决方案:不要让子元素的宽高超过父级
.box{
width:200px;
height:200px;
border:10px soild #000;
}
//解决方案,不要让子元素的宽高超过父级,即content的width<box的width
.content{
width:400px;
height:400px;
background-color:red;
}
</style>
<body>
<div class="box">
<div class="content"></div>
</div>
</body>
5 p 包含块元素嵌套规则
注意:<p>,< td >,<H1-H6>这三个标签不能嵌套块元素
CSS兼容性问题
1 margin兼容性问题
解决方案:
margin-top传递——解决:触发BFC,haslayout上下margin叠压——解决:尽量使用同一方向的margin,比如都设置top,buttom
<style>
background-color:green;
overflow:hidden;
zoom:1;//触发haslayout
}
.item{
height:50px;
background-color:red;
//margin:50px;
margin-top:50px;//尽量使用同一方向的margin
}
.mt100{
margin-top:100px;
}
</style>
<!--
1 margin-top传递——解决:触发BFC,haslayout
2 上下margin叠压——解决:尽量使用同一方向的margin,比如都设置top,buttom
-->
<body>
<div class="box">
<div class="item"></div>
<div class="item mt100"></div>
</div>
</body>
2 IE6不支持display:inline-block
解决方案:
针对ie6,7添加*display:inlline;
*zoom:1;
div{
width:100px;
height:100px;
background-color:red;
display:inline-block;
//触发haslayout
*display:inline;
*zoom:1;
}
</style>
<body>
<div>div</div>
<div>div</div>
<div>div</div>
</body>
3 IE6最小高度问题( IE6下最小高度19px)
解决方案:
针对ie6,7添加overflow:hidden;
<style>
height:1px;//IE6最小高度19px,相差甚远,无法忽视
background-color:red;
overflow:hidden;//解决方案
}
</style>
<body>
<div>div</div>
</body>
4 IE6,7 双边距
当元素浮动后再设置margin,那么就会产生双倍边距
解决方案:
针对ie6,7添加*display:inline;
body{
margin:0;
}
.box{
width:750px;
border:1px solid #000;
overflow:hidden;//解决浮动,只是方便样式,不是最好方案
}
.item{
width:200px;
height:200px;
background-color:red;
margin-left:50px;
float:left;
*display:inline;//解决方案
</style>
<body>
<div class="box">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</body>
5 li里元素都浮动 li在IE6,7下方会产生4px间隙问题
解决方案:
针对ie6,7添加*vertical-align:top
.list{
margin:0;
padding:0;
list-style:none;
width:300px;
}
.list li{
height:30px;
border:1px solid red;
line-height:30px;
*vertical-align:top;//解决方案
}
.list li a{
float:left;
}
.list li span{
float:right;
}
</style>
<body>
<ul class="list">
<li>
<a href="">left</a>
<span>right</span>
</li>
<li>
<a href="">left</a>
<span>right</span>
</li>
<li>
<a href="">left</a>
<span>right</span>
</li>
</ul>
</body>
6 浮动元素之间注释,导致多复制一个文字问题 (小尾巴)
两个浮动元素中间有注释或者内联元素并且和父级宽度和相差不超过3px
解决方案:
1 两个浮动元素中间避免出现内联元素或注释
2 与父级宽度相处3px或以上
.wrap{
width:400px;
}
.left{
float:left;
}
.right{
width:397px;//与父级宽度相差3px或以上
float:right;
}
</style>
<body>
<div class="wrap">
<div class="left"></div>
//两个浮动元素中间避免出现内联元素或注释
<span></span>
<!-- IE下文字溢出BUG -->
<div class="right"></div>
</div>
</body>
7 IE6,7父级元素的overflow:hidden是包不住子级的relative
解决方案:
针对ie6,7给父级元素添加相对定位,让父级和子级处于同一环境下
<style>
width:400px;
height:400px;
background-color:red;
border:1px solid black;
overflow:hidden;
*position:relative;//解决方案
}
.content{
width:600px;
height:600px;
background-color:blue;
position:relative;
}
</style>
<body>
<div class="box">
<div class="content"></div>
</div>
</body>
8 IE6下绝对定位元素父级宽高是奇数,绝对定位元素的right和buttom值会有1px的偏差
解决方案:
避免父级宽高出现奇数
.box{
width:307px;
height:307px;
background-color:red;
border:1px solid black;
position:relative/absolute;
}
.content{
width:100px;
height:100px;
background-color:blue;
position:absolute;
right:0;
bottom:0;
}
</style>
<body>
<div class="box">
<div class="content"></div>
</div>
</body>
9 IE6下绝对定位元素和浮动元素并列绝对定位元素消失
解决方案:
避免他们绝对定位元素和浮动元素同级并列
.box{
width:300px;
height:300px;
border:1px solid black;
position:relative;
}
.item{
width:200px;
height:200px;
background-color:blue;
float:left;
margin-left:50px;
*display:inline;
}
.box span{
width:100px;
height:100px;
background-color:black;
position:absolute;
right:-10px;
top:-10px;
}
</style>
<body>
<div class="box">
//避免
<div class="item"></div>
<span></span>
</div>
</body>
10 IE6下input 的空隙
解决方案:
给input元素添加float
<style>
width:300px;
background-color:blue;
border:1px solid black;
}
.box input{
width:300px;
height:30px;
background-color:blue;
border:0;
margin:0px;
*float:left;//解决方案
}
</style>
<body>
<div class="box">
<input type="text"/>
</div>
</body>
11 IE6下输入类型表单控件背景问题
解决方案:
针对ie6,7设置background-attachment:fixed;
input{
background:url("img/img.jpg") no-repeat fixed;
}
</style>
<body>
<input type="text"/>
</body>
CSS hack
针对不同的浏览器写不同的css样式的过程,就叫css hack
div{
width:200px;
height:200px;
background-color:red;
background-color:blue9;//9 IE10以及IE10以下版本的
*background-color:red; // * IE7以及IE7以下版本的
_background-color:blue; // _ IE6以及IE6以下版本的
}
<body>
<div></div>
</body>
PNG24兼容性问题
IE6不支持png24图片
解决方案 :
JS插件(问题:不能处理body之上png24)
DD_belatedPNG.lix('xxx');
<script src="js/DD_belatedPNG_0.0.Ba.js></script>
<script>
DD_belatedPNG.fix("img,div");//选择器,需要对哪个元素进行处理就选择哪个,多个用,隔开
</script>
<style>
body{
background-color:red;
}
div{
300px;
height:300px;
background:url("img/png.png") no-repeat;
}
</style>
<body>
<div></div>
<img src="img/png.png" alt=""/>
</body>
原生滤镜
_background-image:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="XX.png",sizingMethod="crop");
//不能处理body之上png24
<script>
DD_belatedPNG.fix("body");
</script>
<style>
body{
300px;
height:300px;
background:red url("img/png.png") no-repeat;
_background-image:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="img/png.png",sizingMethod="crop");
}
</style>
<body>
</body>