Home > Article > Web Front-end > A complete list of tips for making div+css compatible with all browsers
CSS技巧
1.div的垂直居中问题 vertical-align:middle; 将行距增加到和整个DIV一样高 line-height:200px; 然后插入文字,就垂直居中了。缺点是要控制内容不要换行?
2. margin加倍的问题?设置为float的div在ie下设置的margin会加倍。这是一个ie6都存在的bug。解决方案是在这个div里面加上display:inline;?例如:?<#div id=”imfloat”>?相应的css为?#IamFloat{?float:left;?margin:5px;/*IE下理解为10px*/?display:inline;/*IE下再理解为5px*/}?
3.浮动ie产生的双倍距离?#box{ float:left; width:100px; margin:0 0 0 100px; //这种情况之下IE会产生200px的距离 display:inline; //使浮动忽略}?这里细说一下block与inline两个元素:block元素的特点是,总是在新行上开始,高度,宽度,行高,边距都可以控制(块元素);Inline元素的特点是,和其他元素在同一行上,不可控制(内嵌元素);?#box{ display:block; //可以为内嵌元素模拟为块元素 display:inline; //实现同一行排列的效果 diplay:table;?
4 IE与宽度和高度的问题 IE 不认得min-这个定义,但实际上它把正常的width和height当作有min的情况来使。这样问题就大了,如果只用宽度和高度,正常的浏览器里这两个值就不会变,如果只用min-width和min-height的话,IE下面根本等于没有设置宽度和高度。?比如要设置背景图片,这个宽度是比较重要的。要解决这个问题,可以这样:?#box{ width: 80px; height: 35px;}html>body #box{ width: auto; height: auto; min-width: 80px; min-height: 35px;}?
5.页面的最小宽度?min -width是个非常方便的CSS命令,它可以指定元素最小也不能小于某个宽度,这样就能保证排版一直正确。但IE不认得这个,而它实际上把width当做最小宽度来使。为了让这一命令在IE上也能用,可以把一个
6.DIV浮动IE文本产生3象素的bug?左边对象浮动,右边采用外补丁的左边距来定位,右边对象内的文本会离左边有3px的间距.?#box{ float:left; width:800px;}?#left{ float:left; width:50%;}?#right{ width:50%;}?*html #left{ margin-right:-3px; //这句是关键}?
7.IE捉迷藏的问题?当div应用复杂的时候每个栏中又有一些链接,DIV等这个时候容易发生捉迷藏的问题。?有些内容显示不出来,当鼠标选择这个区域是发现内容确实在页面。 解决办法:对#layout使用line-height属性 或者给#layout使用固定高和宽。页面结构尽量简单。?
8.float的div闭合;清除浮动;自适应高度;?
① 例如:<#div id=”floatA” ><#div id=”floatB” ><#div id=” NOTfloatC” >这里的NOTfloatC并不希望继续平移,而是希望往下排。(其中floatA、floatB的属性已经设置为 float:left;)?这段代码在IE中毫无问题,问题出在FF。原因是NOTfloatC并非float标签,必须将float标签闭合。在 <#div class=”floatB”> <#div class=”NOTfloatC”>之间加上 < #div class=”clear”>这个div一定要注意位置,而且必须与两个具有float属性的div同级,之间不能存在嵌套关系,否则会产生异常。 并且将clear这种样式定义为为如下即可: .clear{ clear:both;}?
②作为外部 wrapper 的 div 不要定死高度,为了让高度能自动适应,要在wrapper里面加上overflow:hidden; 当包含float的 box的时候,高度自动适应在IE下无效,这时候应该触发IE的layout私有属性(万恶的IE啊!)用zoom:1;可以做到,这样就达到了兼容。?例如某一个wrapper如下定义:?.colwrapper{ overflow:hidden; zoom:1; margin:5px auto;}?
③对于排版,我们用得最多的css描述可能就是float:left.有的时候我们需要在n栏的float div后面做一个统一的背景,譬如:?
④万能float 闭合(非常重要!)?关于 clear float 的原理可参见 [How To Clear Floats Without Structural Markup],将以下代码加入Global CSS 中,给需要闭合的div加上class="clearfix" 即可,屡试不爽.?/* Clear Fix */?.clearfix:after { content:"."; display:block; height:0; clear:both; visibility:hidden; }?.clearfix { display:inline-block; }?/* Hide from IE Mac */?.clearfix {display:block;}?/* End hide from IE Mac */?/* end of clearfix */?或者这样设置:.hackbox{ display:table; //将对象作为块元素级的表格显示}?
11.高度不适应?高度不适应是当内层对象的高度发生变化时外层高度不能自动进行调节,特别是当内层对象使用margin 或paddign 时。?例:?#box {background-color:#eee; }?#box p {margin-top: 20px;margin-bottom: 20px; text-align:center; }?
p对象中的内容
?
12. Why is there a gap under the image under IE6? There are many ways to solve this bug. You can change the layout of the html, or set the img to display:block or set the vertical-align attribute to vertical-align:top | bottom |middle |text-bottom can be solved.
13. How to align text and text input box by adding vertical-align:middle;
14. What is the difference between id and class defined in web standards? 1. Repeating IDs is not allowed in web standards, such as div id="aa"? It is not allowed to be repeated twice, and class defines a class, which can theoretically be repeated infinitely, so that definitions that require multiple references can be used He. 2. Priority issue of attributes. The priority of ID is higher than class. See Example 3 above. It is convenient for client scripts such as JS. If you want to perform script operations on an object in the page, you can define a ID, otherwise you can only find it by traversing the page elements and specifying specific attributes. This is a relative waste of time and resources, and is far less simple than an ID.
15. How to display the content in LI with ellipses after it exceeds the length This method is applicable to IE and OP browsers
16. Why can’t IE set the scroll bar color in web standards? The solution is to replace the body with html < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
17. Why can't I define a container with a height of about 1px? This problem under IE6 is caused by the default line height. How to solve it There are many, such as: overflow:hidden | zoom:0.08 | line-height:1px
18. How to make the layer display on top of FLASH? The solution is to set transparency for FLASH
19. How to vertically center a layer in the browser. Here we use percentage absolute positioning, and use the method of patching negative values. The size of the negative value is divided by its own width and height. With two ?
FF and IE?
1. Div centering problem?div setting margin-left , margin-right is already centered when it is auto. IE does not work. IE needs to set the body to be centered. First define text-algin: center in the parent element; this means that the content within the parent element is centered. ?
2. The border and background of the link (a tag)? For the a link to add a border and background color, you need to set display: block, and set float: left to ensure no line breaks. Referring to menubar, setting the height of a and menubar is to avoid dislocation of the bottom edge display. If height is not set, a space can be inserted in menubar.
3. The problem that the hover style does not appear after the hyperlink is visited. The hyperlink style that is clicked and visited no longer has hover and active. Many people should have encountered this problem. The solution is to change the arrangement of CSS attributes. Sequence: L-V-H-A Code:
4. The cursor finger cursor?cursor: pointer can display the cursor finger shape in IE FF at the same time, hand can only be displayed in IE?
5. The padding and margin?ul tags of UL are defaulted in FF. There is a padding value, but in IE only margin has a value by default, so defining ul{margin:0;padding:0;} first can solve most problems?
6. FORM tag? This tag is in IE , will automatically add some margins, while in FF the margin is 0. Therefore, if you want the display to be consistent, it is best to specify margin and padding in css. In response to the above two problems, I generally use them first in my css. Such a style ul,form{margin:0;padding:0;} is well defined, so you won’t have a headache later.?
7. BOX模型解释不一致问题?在FF和IE 中的BOX模型解释不一致导致相差2px解决方法:div{margin:30px!important;margin:28px;} 注意这两个 margin的顺序一定不能写反, important这个属性IE不能识别,但别的浏览器可以识别。所以在IE下其实解释成这样: div {maring:30px;margin:28px}重复定义的话按照最后一个来执行,所以不可以只写margin:xx px!important;?#box{ width:600px; //for ie6.0- w\idth:500px; //for ff+ie6.0}?#box{ width:600px!important //for ff width:600px; //for ff+ie6.0 width /**/:500px; //for ie6.0-}?
8.属性选择器(这个不能算是兼容,是隐藏css的一个bug)?p[id]{}div[id]{}?这个对于IE6.0和IE6.0以下的版本都隐藏,FF和OPera作用.属性选择器和子选择器还是有区别的,子选择器的范围从形式来说缩小了,属性选择器的范围比较大,如p[id]中,所有p标签中有id的都是同样式的.?
9.最狠的手段 - !important;?如果实在没有办法解决一些细节问题,可以用这个方法.FF对于”!important”会自动优先解析,然而IE则会忽略.如下?.tabd1{?background:url(/res/images/up/tab1.gif) no-repeat 0px 0px !important; /*Style for FF*/?background:url(/res/images/up/tab1.gif) no-repeat 1px 0px; /* Style for IE */}?值得注意的是,一定要将xxxx !important 这句放置在另一句之上,上面已经提过?
10.IE,FF的默认值问题?或许你一直在抱怨为什么要专门为IE和FF写不同的CSS,为什么IE这样让人头疼,然后一边写css,一边咒骂那个可恶的M$ IE.其实对于css的标准支持方面,IE并没有我们想象的那么可恶,关键在于IE和FF的默认值不一样而已,掌握了这个技巧,你会发现写出兼容FF和IE的css并不是那么困难,或许对于简单的css,你完全可以不用”!important”这个东西了。?我们都知道,浏览器在显示网页的时候,都会根据网页的 css样式表来决定如何显示,但是我们在样式表中未必会将所有的元素都进行了具体的描述,当然也没有必要那么做,所以对于那些没有描述的属性,浏览器将采用内置默认的方式来进行显示,譬如文字,如果你没有在css中指定颜色,那么浏览器将采用黑色或者系统颜色来显示,div或者其他元素的背景,如果在 css中没有被指定,浏览器则将其设置为白色或者透明,等等其他未定义的样式均如此。所以有很多东西出现FF和IE显示不一样的根本原因在于它们的默认显示不一样,而这个默认样式该如何显示我知道在w3中有没有对应的标准来进行规定,因此对于这点也就别去怪罪IE了。
11.为什么FF下文本无法撑开容器的高度 标准浏览器中固定高度值的容器是不会象IE6里那样被撑开的,那我又想固定高度,又想能被撑开需要怎样设置呢?办法就是去掉height设置min-height:200px;?这里为了照顾不认识min-height的IE6 可以这样定义: { height:auto!important; height:200px; min-height:200px; }
12.FireFox下如何使连续长字段自动换行 众所周知IE中直接使用 word-wrap:break-word 就可以了, FF中我们使用JS插入 的方法来解决
13.为什么IE6下容器的宽度和FF解释不同呢
IE6, IE7, FF? IE7.0 is out, and there are new problems with CSS support. There are more browsers, and the compatibility of web pages is getting worse. We are still struggling. In order to solve the compatibility problem of IE7.0, I found the following article: Now I mostly use !important to hack, for IE6 and Firefox The test can be displayed normally, but ie7 can correctly interpret !important, which will cause the page not to be displayed as required! The following is a compatibility collection of three browsers.?
The first one is the CSS HACK method?height:20px; /*For Firefox*/?*height:25px; /*For IE7 & IE6*/ ?_height:20px; /*For IE6*/?Pay attention to the order. ?This is also a CSS HACK, but it is not as simple as the above. ?#example { color: #333; } /* Moz */?* html #example { color: #666; } /* IE6 */?*+html #example { color: #999; } /* IE7 */ ?
????????
The third method is the css filter method. The following is a classic translation from a foreign website. .?Create a new css style as follows:?#item {?width: 200px;?height: 200px;?background: red;?}?Create a new div and use the previously defined css style:?