Home > Article > Web Front-end > What is Css Hack_html/css_WEB-ITnose
What is a CSS hack? Since different browsers, such as Internet Explorer 6, Internet Explorer 7, Mozilla Firefox, etc., have different understandings of CSS parsing, the generated page effects will be different and the page effects we need will not be obtained.
At this time we need to write different CSS for different browsers so that it can be compatible with different browsers at the same time and get the page effects we want in different browsers.
This process of writing different CSS codes for different browsers is called CSS hack, also called writing CSS hack.
What is the principle of CSS Hack?
Because different browsers have different support and parsing results for CSS, and also because of the priority relationship in CSS. We can use this to write different CSS for different browsers.
For example, IE6 can recognize the underscore "_" and the asterisk "*", IE7 can recognize the asterisk "*", but cannot recognize the underscore "_", and Firefox cannot recognize both. Wait
The writing order is generally to write the CSS of browsers with strong recognition capabilities at the end. How to write is explained in more detail below.
How to write CSS Hack
For example, to distinguish between IE6 and firefox browsers, you can write like this:
I see it as red in IE6 and green in firefox.
Explain:
The above css is in Firefox. It cannot recognize what the asterisked stuff behind it is, so it filters it out and ignores it. The result of the analysis is: div {background:green}, so of course the background of this div is green.
In IE6, both backgrounds can be recognized, and the result of its analysis is: div{background:green;background:red;}, so according to the priority level, the red at the back has a higher priority. , so of course the background color of this div is red.
CSS hack: Distinguish between IE6, IE7, firefox
Distinguish between different browsers, CSS hack writing:
Distinguish between IE6 and FF:
background:orange;*background:blue;
Distinguish between IE6 and FF IE7:
background:green !important; background:blue;
Difference between IE7 and FF:
background:orange; *background:green;
Difference between FF, IE7, IE6:
background: orange;*background:green;_background:blue;
background:orange;*background:green !important;*background:blue;
Note: IE can recognize *; standard browsers (such as FF) cannot recognize *;
IE6 can recognize *, but not !important,
IE7 can recognize * and also !important;
FF cannot recognize *, but can recognize !important;
IE6 IE7 FF " #demo {width:100px;} "For example;
#demo {width:100px;} /*Executed by FIREFOX, IE6, IE7.*/
* html #demo {width:120px;} /* Will be executed by IE6, the previous definition will be overwritten by the later one, so the width of #demo is 120px in IE6; */
* html #demo {width:130px;} /*Will be executed by IE7*/
---------------
So in the end, the width of #demo is interpreted in the three browsers as:
FIREFOX:100px;
ie6:120px;
ie7:130px;
======================Gorgeous dividing line============ ==========
!important in CSS is a very important attribute and sometimes plays a very important role. 52CSS.com does not have much knowledge in this area. Let’s read the following article to get a comparative understanding of it. .
When I was writing some CSS code a few days ago, it was difficult for me again, because the damn IE6’s support for CSS is so poor. I haven’t noticed it before, because the things I make are basically based on IE, but The CSS I wrote for my blog this time needs to support more than one browser: IE. The unfortunate thing is that I installed Windows 7, which comes with IE8 browser. I thought there would be no problem, but when I open IE6, misalignment still occurs. , so I decided to see what was going on with IE6.
I wrapped all CSS blocks with borders, and found that the distance between two divs in IE is obviously wider than in other browsers. For example, if you write a div with a margin attribute of 20px, then in IE It seems to be 40px in IE, which is why the accuracy calculation is just right, but it is misaligned in IE.
Later I saw the !important attribute. This attribute is actually included in the CSS specification. As a result, IE6 did not support it. It was precisely because it did not support that many CSSers found a solution. Generally speaking, in CSS, if you write two same attributes in the same CSS block, then the bottom one is actually executed, for example:
view plaincopy to clipboardprint?
.home{
margin-left:20px;
margin-left:40px;
}
.home{
margin-left:20px;
margin-left:40px;
}
Then when executed, it is actually executed according to 40px. The appearance of !important is to allow users to set the priority of the executed statement. If you change the above statement to:
view plaincopy to clipboardprint?
.home{
margin-left:20px!important;
margin-left:40px;
}
.home{
margin-left:20px!important;
margin-left:40px;
}
Then in Firefox, Google browser and IE7 or above, it will be as follows 20px to execute, but under IE6 it still executes as 40px, because IE6 does not support the !important specification, we can follow this rule to meet the design needs of IE6, when we find that the display effects of IE6 and other browsers are different, then Just set two, add the !important tag to the upper one, and do not need to add the lower sentence, so that IE6 will execute it as follows.
====================== Gorgeous dividing line================== ====
It is a commonplace issue that CSS is compatible with various browsers. There are tutorials all over the Internet. The following content is not too novel and is purely a personal summary. I hope it can be helpful to beginners. Certain help.
1. CSS HACK
HACK concept:
Different browsers, such as Internet Explorer 6, Internet Explorer 7, Mozilla Firefox, etc., have different understandings of CSS parsing, so This will cause the generated page effects to be different, and we will not get the page effects we need. At this time, we need to write different CSS for different browsers so that it can be compatible with different browsers at the same time and get the page effects we want in different browsers. This process of writing different CSS codes for different browsers is called CSS hack,
HACK rules:
IE can recognize it*; standard browsers (such as FF) cannot recognize it* ;
IE6 can recognize *, but not !important,
IE7 can recognize *, but it can also recognize !important;
FF cannot recognize *, but can recognize !important;
IE6 IE7 FF
* √ √ ×
!important × √ √
Example: Take: " #demo {width:100px;} " as an example; #demo {width:100px;} FIREFOX:100px; The following two methods can solve almost all HACKs today. 1, !important With IE7’s support for !important, the !important method is now only for IE6’s HACK. (Pay attention to the writing. Remember that the declaration position needs to be in advance.) 2. Universal float closure For the principle of clear float, please refer to [How To Clear Floats Without Structural Markup] 3. Other compatibility tips 2. Centering problem. 3. The BOX interpretation of IE5 and IE6 is inconsistent 4. Advance declaration of FORM tag and ul tag 5. 6. 7. 8. 浮动ie产生的双倍距离 10. 为什么FF下文本无法撑开容器的高度 11. 清除浮动 左边对象浮动,右边采用外补丁的左边距来定位,右边对象内的文本会离左边有3px的间距. #box{ float:left; width:800px;}#left{ float:left; width:50%;}#right{ width:50%;}*html #left{ margin-right:-3px; //这句是关键} 9 属性选择器(这个不能算是兼容,是隐藏css的一个bug) p[id]{}div[id]{} 10 IE捉迷藏的问题 当div应用复杂的时候每个栏中又有一些链接,DIV等这个时候容易发生捉迷藏的问题。 11 高度不适应 高度不适应是当内层对象的高度发生变化时外层高度不能自动进行调节,特别是当内层对象使用 p对象中的内容 /*CSS compatibility list between IE and Firefox*/ 2.FF: div is already centered when margin-left and margin-right are set to auto , IE does not work 3.FF: When setting text-align on the body, the div needs to set margin: auto (mainly margin-left, margin-right) to be centered 4.FF: After setting the padding, the div will increase the height and width, but IE will not, so you need to use !important to set an additional height and width 5.FF: !important is supported, but IE ignores it, you can use !important as FF Specially set the vertical centering problem of the style 6.div: vertical-align:middle; Increase the line spacing to the same height as the entire DIV line-height:200px; Then insert text and it will be vertically centered. The disadvantage is that the content must be controlled without wrapping 7.cursor: pointer can display the cursor finger shape in IE FF at the same time, hand can only be used by IE 8.FF: link with border and background color, You need to set display: block and 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. 9. The BOX model interpretation in mozilla firefox and IE is inconsistent, resulting in a 2px difference. Solution: 10. The BOX interpretation of IE5 and IE6 is inconsistent 11.ul tag has padding value by default in Mozilla, but in IE only margin has value, so define it first Notes: 1. The float div must be closed. For example: (the attributes of floatA and floatB have been set to float:left;) 2. Double the margin question. The margin set for a div set to float under IE will be doubled. This is a bug that exists in ie6. <#div id="imfloat">#div> #IamFloat{ 3. Regarding the inclusive relationship of the container Many times, especially when there is a parallel layout in the container, such as two or three float divs, the width is very easy Something went wrong. In IE, the width of the outer layer will be squeezed by the wider inner div. Be sure to use Photoshop or Firework to measure with pixel-level accuracy. 4. Questions about height If content is added dynamically, it is best not to define the height. Browsers can automatically scale, but if it is static content, it is best to set the height. (It seems that sometimes it won’t open automatically, I don’t know what’s going on) 5. The most ruthless method - !important; If there is really no way to solve some detailed problems, You can use this method. FF will automatically parse "!important" first, but IE will ignore it. As follows
Browser priority: FF
* html #demo {width:120px;}
* html #demo {width:130px;}
---------------
So in the end, the width of #demo is The explanations of the three browsers are:
ie6:120px;
ie7:130px;
Note:
* html HACK for IE7 must ensure that there is the following statement at the top of the HTML:
Add the following code to Global CSS and add class="clearfix" to the div that needs to be closed. It works every time.
1. The
ul tag has a padding value by default in Mozilla, but in IE only margin has a value. So defining ul{margin:0;padding:0;} first can solve most problems. You can also use !important to solve
1). Vertically centered. Set line-height to the same height as the current div, and then pass vertical-align: middle. (Be careful not to wrap the content .)
2). Horizontally centered. margin: 0 auto;(Of course not universal)
You can place any item within the body of the html, and the item will automatically obtain equal left and right boundary values to ensure Displayed in the center. However, this is still a problem in browsers before IE6 and will not be centered, so it must be modified as follows:
body {
text-align: center;
}
#content {
text-align: left;
width: 700px;
margin: 0 auto;
}
Setting the body will cause the main content to be centered, but even all the text will be centered. This is probably not the effect you want. For this reason, the div of #content also needs to specify a value: text-align: left
div{width: under IE5 300px;margin:0 10px 0 10px;}
The width of the div will be interpreted as 300px-10px (right padding)-10px (left padding). The final width of the div is 280px,
while in IE6 and other browsers The top width is calculated as 300px 10px (right padding) 10px (left padding) = 320px.
At this time we can make the following modifications to div{width:300px!important;width:340px;margin:0 10px 0 10px}
I don’t quite understand what this is, I only know that both IE5 and firefox support it. IE6 does not support it. If anyone understands it, please let me know. Thanks! :)
These two tags will automatically have some margins in IE, while in FF the margin is 0, so if you want The display is consistent, so it is best to specify margin and padding in css. In response to the above two problems, I generally use this style ul first in my css, and form{margin:0;padding:0;} is defined, so You won’t have a headache later.
cursor: pointer can display the cursor finger shape in IE FF at the same time, hand can only be displayed in IE
If If the text is too long, change the long part into an ellipsis and display it: IE5, FF is invalid, but it can be hidden, IE6 is valid
Fixed-width Chinese character truncation: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; (However, it can only handle text truncation on one line, not multiple lines.) ( IE5 and above) FF cannot, it only hides.
IE does not recognize the definition of min-, but in fact it treats normal width and height as if there is min. This is a big problem. If you only use width and height, these two values will not change in a normal browser. If you only use min-width and min-height, the width and height are not set at all under IE.
For example, if you want to set a background image, this width is more important. To solve this problem, you can do this:
#box{ width: 80px; height: 35px;}html>body #box{ width: auto; height: auto; min-width: 80px; min-height: 35px;}
#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;
标准浏览器中固定高度值的容器是不会象IE6里那样被撑开的,那我又想固定高度,又想能被撑开需要怎样设置呢?办法就是去掉height设置min- height:200px; 这里为了照顾不认识min-height的IE6 可以这样定义:
{
height:auto!important;
height:200px;
min-height:200px;
}
.hackbox{ display:table; //将对象作为块元素级的表格显示}或者.hackbox{ clear:both;}
或者加入:after(伪对象),设置在对象后发生的内容,通常和content配合使用,IE不支持此伪对象,非Ie 浏览器支持,
所 以并不影响到IE/WIN浏览器。这种的最麻烦的......#box:after{ content: "."; display: block; height: 0; clear: both; visibility: hidden;}
8 DIV浮动IE文本产生3象素的bug
HTML代码
这个对于IE6.0和IE6.0以下的版本都隐藏,FF和OPera作用
属性选择器和子选择器还是有区别的,子选择器的范围从形式来说缩小了,属性选择器的范围比较大,如p[id]中,所有p标签中有id的都是同样式的.
有些内容显示不出来,当鼠标选择这个区域是发现内容确实在页面。
解决办法:对#layout使用line-height属性 或者给#layout使用固定高和宽。页面结构尽量简单。
margin 或paddign 时。
例:
CSS: #box {background-color:#eee; }
#box p {margin-top: 20px; margin-bottom: 20px; text-align:center; }
Solution: in Add two empty div objects above and below the P object. CSS code: .1{height:0px;overflow:hidden;} or add the border attribute to the DIV.
1.DOCTYPE affects CSS processing
div{margin:30px!important;margin:28px;}
Pay attention to these two margins The order must not be reversed. According to Ajie, the !important attribute cannot be recognized by IE, but other browsers can. So it is actually interpreted like this under IE:
div{maring:30px;margin:28px}
If you repeat the definition, it will be executed according to the last one, so you cannot just write margin:XXpx!important;
Under IE5
div{width:300px;margin:0 10px 0 10px;}
The width of div will be interpreted as 300px-10px (right padding)- The width of the final div of 10px (left padding) is 280px, but on IE6 and other browsers, the width is calculated as 300px 10px (right padding) 10px (left padding) = 320px. At this time we can make the following modifications
div{width:300px!important;width /**/:340px;margin:0 10px 0 10px}
About this/**/ I don’t quite understand what it is. I only know that IE5 and firefox support it but IE6 does not. If anyone understands If so, please tell me, thanks! :)
ul{margin:0;padding:0;}
It can solve most problems
<#div id="floatA" >#div>
<#div id="floatB" >#div>
<#div id="NOTfloatC" >#div>
NOTfloatC here does not want to continue to pan, but wants to go down Row.
This code has no problem in IE, the problem lies in FF. The reason is that NOTfloatC is not a float label, and the float label must be closed.
Add between
<#div class="floatB">#div>
<#div class="NOTfloatC">#div>
<#div class="clear">#div>
This div must pay attention to the declaration position, must be placed in the most appropriate place, and must be combined with two divs with float attributes There cannot be a nested relationship between divs at the same level, otherwise an exception will occur.
And define the clear style as follows:
.clear{
clear:both;}
In addition, in order to allow the height to automatically adapt, add overflow in the wrapper: hidden;
When a box containing a float is used, the height automatic adaptation is invalid under IE. At this time, the layout private attribute of IE should be triggered (the evil IE!) You can use zoom:1; to achieve this. compatible.
For example, a wrapper is defined as follows:
.colwrapper{
overflow:hidden;
zoom:1;
margin:5px auto;}
The solution is to add display:inline;
For example:
The corresponding css is
float:left;
margin:5px;/*under IE, it is understood as 10px*/
display:inline;/*under IE Then understand it as 5px*/}
.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 */}