Browsers have a large number of different versions, and the rendering engines of different types of browsers are also different. Therefore, different browsers may have differences in parsing code, which leads to inconsistent rendering effects on web pages.
Due to browser compatibility issues, different browsers have different default style values for tags. If styles are not initialized, it will cause display differences between different browsers and layout problems. Therefore, it is necessary to initialize styles to achieve consistent layout.
The most direct solution is to use *
to initialize styles, but it will load styles and calculate style priorities for all tags, which may have an impact on performance.
Usually, Normalize.css
is used to normalize the default style differences. Of course, you can also customize your own reset.css
based on the styles.
Before the CSS3
standard was finalized, some browsers had already implemented some features based on the initial drafts. In order to be compatible with the later finalized standards, each browser used its own vendor prefix to distinguish from the standard. After the standard was established, major browsers gradually supported CSS3
new properties without prefixes. Many vendor prefixes can now be omitted. However, in order to be compatible with older versions of browsers, it is still recommended to use vendor prefixes and standard methods for a smooth transition.
Kernel | Representative Browser | Prefix |
---|---|---|
Trident | IE Browser | -ms |
Gecko | Firefox | -moz |
Presto | Opera | -o |
Webkit | Chrome, Safari | -webkit |
The opacity
property used to set the transparency of an element is a property in CSS3
and is supported by modern browsers. For older versions of browsers, it can be supported by adding vendor prefixes. For IE6-IE8
, it can be supported by the filter
property. For IE4-IE9
, compatibility support can be provided through filter syntax.
For browsers that do not support CSS3
media queries below IE9
, respond.js
is usually used as a compatibility solution.
For browsers that do not support new HTML5
tags below IE9
, you can create elements using document.createElement
and set their CSS
styles. Usually, html5shiv.js
is used as a compatibility solution.
placeholder
is a new attribute added in html5
. When input
or textarea
is set with this attribute, the value will be displayed as a gray hint in the text box. The hint text will disappear when the text box gains focus or when content is entered. To ensure compatibility, first check if input
supports placeholder
. If it doesn't, you can achieve the placeholder
effect by using the onfocus
and onblur
event listeners of input
.
Before IE9
, attachEvent
was used instead of the standard method addEventListener
to register element listeners. To handle event compatibility issues, it is usually necessary to encapsulate an adapter method to filter event handler binding and removal.
The recommended way to prevent default behavior by W3C
is to use event.preventDefault()
. This method only prevents the default behavior without stopping the event propagation. For browsers before IE9
, preventing default behavior requires using window.event.returnValue = false
. Returning false
directly in the event handler function can also prevent default behavior, but it is only effective in DOM0
level model. In addition, using return false
in jQuery
will both prevent default behavior and event propagation. It is also common to encapsulate a method to achieve the prevention of default behavior.
The recommended way to stop event propagation by W3C
is to use event.stopPropagation()
. For browsers before IE9
, it is done by using window.event.cancelBubble = true;
. It is also common to encapsulate a method to achieve the prevention of event propagation.
To get the scroll height of the window scrollTop
, a compatibility writing style is needed, even if the <DOCTYPE>
declaration is made, different browsers may handle the document differently.
Use the new Date()
constructor to generate a date and time object. The syntax new Date("2020-06-29")
may output invalid date
in some earlier browsers. This is mainly because early browsers do not support the -
in expressing dates, and /
is widely supported. Therefore, when dealing with compatibility in earlier browsers, -
needs to be replaced with /
.
A syntax specifically provided by IE, which can only be recognized and executed by IE, while other browsers treat it as a comment.