What is Css Hack_html/css_WEB-ITnose
Jun 24, 2016 am 11:39 AM
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產(chǎn)生的雙倍距離? 10. 為什么FF下文本無法撐開容器的高度? 11. 清除浮動? 左邊對象浮動,右邊采用外補(bǔ)丁的左邊距來定位,右邊對象內(nèi)的文本會離左邊有3px的間距. #box{ float:left; width:800px;}#left{ float:left; width:50%;}#right{ width:50%;}*html #left{ margin-right:-3px; //這句是關(guān)鍵}? 9 屬性選擇器(這個(gè)不能算是兼容,是隱藏css的一個(gè)bug) p[id]{}div[id]{} 10 IE捉迷藏的問題 當(dāng)div應(yīng)用復(fù)雜的時(shí)候每個(gè)欄中又有一些鏈接,DIV等這個(gè)時(shí)候容易發(fā)生捉迷藏的問題。 11 高度不適應(yīng) 高度不適應(yīng)是當(dāng)內(nèi)層對象的高度發(fā)生變化時(shí)外層高度不能自動進(jìn)行調(diào)節(jié),特別是當(dāng)內(nèi)層對象使用 p對象中的內(nèi)容 /*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會產(chǎn)生200px的距離 display:inline; //使浮動忽略}
這里細(xì)說一下block與inline兩個(gè)元素:block元素的特點(diǎn)是,總是在新行上開始,高度,寬度,行高,邊距都可以控制(塊元素);Inline元素的特點(diǎn)是,和其他元素在同一行上,不可控制(內(nèi)嵌元素);
#box{ display:block; //可以為內(nèi)嵌元素模擬為塊元素 display:inline; //實(shí)現(xiàn)同一行排列的效果 diplay:table;
標(biāo)準(zhǔn)瀏覽器中固定高度值的容器是不會象IE6里那樣被撐開的,那我又想固定高度,又想能被撐開需要怎樣設(shè)置呢?辦法就是去掉height設(shè)置min- height:200px; 這里為了照顧不認(rèn)識min-height的IE6 可以這樣定義:
{
height:auto!important;
height:200px;
min-height:200px;
}
.hackbox{ display:table; //將對象作為塊元素級的表格顯示}或者.hackbox{ clear:both;}
或者加入:after(偽對象),設(shè)置在對象后發(fā)生的內(nèi)容,通常和content配合使用,IE不支持此偽對象,非Ie 瀏覽器支持,
所 以并不影響到IE/WIN瀏覽器。這種的最麻煩的......#box:after{ content: "."; display: block; height: 0; clear: both; visibility: hidden;}
8 DIV浮動IE文本產(chǎn)生3象素的bug
HTML代碼
這個(gè)對于IE6.0和IE6.0以下的版本都隱藏,FF和OPera作用
屬性選擇器和子選擇器還是有區(qū)別的,子選擇器的范圍從形式來說縮小了,屬性選擇器的范圍比較大,如p[id]中,所有p標(biāo)簽中有id的都是同樣式的.
有些內(nèi)容顯示不出來,當(dāng)鼠標(biāo)選擇這個(gè)區(qū)域是發(fā)現(xiàn)內(nèi)容確實(shí)在頁面。
解決辦法:對#layout使用line-height屬性 或者給#layout使用固定高和寬。頁面結(jié)構(gòu)盡量簡單。
margin 或paddign 時(shí)。
例:
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 */}

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

To use HTML button elements to achieve clickable buttons, you must first master its basic usage and common precautions. 1. Create buttons with tags and define behaviors through type attributes (such as button, submit, reset), which is submitted by default; 2. Add interactive functions through JavaScript, which can be written inline or bind event listeners through ID to improve maintenance; 3. Use CSS to customize styles, including background color, border, rounded corners and hover/active status effects to enhance user experience; 4. Pay attention to common problems: make sure that the disabled attribute is not enabled, JS events are correctly bound, layout occlusion, and use the help of developer tools to troubleshoot exceptions. Master this

Metadata in HTMLhead is crucial for SEO, social sharing, and browser behavior. 1. Set the page title and description, use and keep it concise and unique; 2. Add OpenGraph and Twitter card information to optimize social sharing effects, pay attention to the image size and use debugging tools to test; 3. Define the character set and viewport settings to ensure multi-language support is adapted to the mobile terminal; 4. Optional tags such as author copyright, robots control and canonical prevent duplicate content should also be configured reasonably.

TolearnHTMLin2025,chooseatutorialthatbalanceshands-onpracticewithmodernstandardsandintegratesCSSandJavaScriptbasics.1.Prioritizehands-onlearningwithstep-by-stepprojectslikebuildingapersonalprofileorbloglayout.2.EnsureitcoversmodernHTMLelementssuchas,

How to make HTML mail templates with good compatibility? First, you need to build a structure with tables to avoid using div flex or grid layout; secondly, all styles must be inlined and cannot rely on external CSS; then the picture should be added with alt description and use a public URL, and the buttons should be simulated with a table or td with background color; finally, you must test and adjust the details on multiple clients.

Using HTML sums allows for intuitive and semantic clarity to add caption text to images or media. 1. Used to wrap independent media content, such as pictures, videos or code blocks; 2. It is placed as its explanatory text, and can be located above or below the media; 3. They not only improve the clarity of the page structure, but also enhance accessibility and SEO effect; 4. When using it, you should pay attention to avoid abuse, and apply to content that needs to be emphasized and accompanied by description, rather than ordinary decorative pictures; 5. The alt attribute that cannot be ignored, which is different from figcaption; 6. The figcaption is flexible and can be placed at the top or bottom of the figure as needed. Using these two tags correctly helps to build semantic and easy to understand web content.

When there is no backend server, HTML form submission can still be processed through front-end technology or third-party services. Specific methods include: 1. Use JavaScript to intercept form submissions to achieve input verification and user feedback, but the data will not be persisted; 2. Use third-party serverless form services such as Formspree to collect data and provide email notification and redirection functions; 3. Use localStorage to store temporary client data, which is suitable for saving user preferences or managing single-page application status, but is not suitable for long-term storage of sensitive information.

class, id, style, data-, and title are the most commonly used global attributes in HTML. class is used to specify one or more class names to facilitate style setting and JavaScript operations; id provides unique identifiers for elements, suitable for anchor jumps and JavaScript control; style allows for inline styles to be added, suitable for temporary debugging but not recommended for large-scale use; data-properties are used to store custom data, which is convenient for front-end and back-end interaction; title is used to add mouseover prompts, but its style and behavior are limited by the browser. Reasonable selection of these attributes can improve development efficiency and user experience.

Native lazy loading is a built-in browser function that enables lazy loading of pictures by adding loading="lazy" attribute to the tag. 1. It does not require JavaScript or third-party libraries, and is used directly in HTML; 2. It is suitable for pictures that are not displayed on the first screen below the page, picture gallery scrolling add-ons and large picture resources; 3. It is not suitable for pictures with first screen or display:none; 4. When using it, a suitable placeholder should be set to avoid layout jitter; 5. It should optimize responsive image loading in combination with srcset and sizes attributes; 6. Compatibility issues need to be considered. Some old browsers do not support it. They can be used through feature detection and combined with JavaScript solutions.
