CSS hacks for all browsers: from IE to Opera. Generic CSS hacks for browsers Defining an old browser in css

For every good coder, the question of cross-browser compatibility arises at some point in time. What is cross-browser compatibility? It is the property of a site or site design to appear the same across different browsers. The fact is that when creating browsers, developers do not think much about standardization, this generates a different representation of the same site in their products. The creation of a cross-browser layout / site and allows you to overcome this difference.

For this purpose, so-called hacks (cheats) were invented in the CSS language, which allow you to apply a group of properties only for one browser or several, for example, only Opera or only Chrome / Safari.

This article presents possible working options for the latest versions of browsers, all hacks have been checked for the latest version as of 05/10/2015. If this article helped you or you still have questions, ask in the comments and rate it, this may encourage us to expand the number of hacks for different browsers, and also add examples of use for each.

If the hack is written directly in the table, then to use it, you need to copy it into a .CSS file and adapt it to your style, otherwise, in the name of the link, there is a link with a detailed description of how to do the hack.

Hack compatibility table
Names and hacks themselvesInternet Explorer Google chrome OperaSafariMozilla Firefox
+ - - - -
- + - - -
- - + - -

_ :: - moz-svg-foreign-content,: root .class ()

- - - + -

html / ** / body .class, x: -moz-any-link, x: default (color: # F00;)

- - - - +
- + + + -

@supports (-webkit-appearance: none) (/ * your styles are here as usual * /)

- + + - -

The simplest and most effective option is to create a separate .css file for all versions of IE, and include it along with the main one, but in a special way.

Conditional comments work only in IE under Windows, for other browsers they are normal comments, so they can be used painlessly. The syntax is as follows (in the .php or .html file, where we connect the stylesheet, add the following lines):

where styles.css is the file that will be responsible for IE-only styles.

The only currently known way to write styles only for Google Chrome using JavaScript. At the same time, you do not need to know JS, it is enough to take an example from here and adapt it to your needs.

The bottom line is that we check which browser the site visitor is using and if it is Google Chrome, then we connect an additional stylesheet for it, in which the necessary properties that may overlap in the main stylesheet are selected! Important.

section, add such lines, inside the tag ):

var isChrome = !! window.chrome && !! window.chrome.webstore; if (isChrome) (var $ = document; var cssId = "myCss"; var head = $ .getElementsByTagName ("head"); var link = $ .createElement ("link"); link.id = cssId; link.rel = "stylesheet"; link.type = "text / css"; link.href = "/css/template.css"; // path to the stylesheet only for Chrome link.media = "all"; head.appendChild (link );)

The only currently known way to write Opera-only styles using JavaScript. At the same time, you do not need to know JS, it is enough to take an example from here and adapt it to your needs.

The bottom line is that we check what browser the site visitor is using and if it is Opera, then we connect an additional stylesheet for it, in which the necessary properties that may overlap in the main stylesheet are selected! Important.

The syntax is as follows (in the .php or .html file, where we include the stylesheet, i.e. in section, add such lines , inside the tag ):

var isOpera = !! window.opera || /opera|opr/i.test(navigator.userAgent); if (isOpera) (var $ = document; var cssId = "myCss"; var head = $ .getElementsByTagName ("head"); var link = $ .createElement ("link"); link.id = cssId; link.rel = "stylesheet"; link.type = "text / css"; link.href = "/css/template.css"; // path to the stylesheet only for Opera link.media = "all"; head.appendChild (link );)

The only thing you need to change here is the path to your stylesheet in line 10.

Thank you all for your attention, we are glad for you to try. Again, we are waiting for your comments, suggestions for improvement, etc.

6.5K

Cross-browser compatibility Is the site's ability to display correctly in different browsers... The resource should work the same in all browser versions.

This is especially important in the era of responsive web design, when the ability of the front-end to adapt to wide range various devices while providing optimal quality viewing.

Previously, a designer created a design layout in Photoshop, which was then transferred to HTML and CSS. Today, technological changes are forcing a rethinking of this concept. We can no longer predict which browser, resolution or device will be used to view the site. Gone are the days when most computers used a fixed resolution of 1024 by 768 pixels and it was possible to design sites with static dimensions.

Modern browsers fully support HTML5 and CSS3. But the same HTML / CSS / JavaScript code is interpreted differently in older browsers, resulting in “ cross-browser incompatibility»Site. This is especially true for old Internet versions Explorer.

In this article we will look at the current statistics on web page views, we will provide a list of tools that help to solve various problems compatibility.

1. Current situation

Global statistics for 2015 show that the top 14 screen resolutions in use range from 1920 x 1080 to 320 x 480 pixels.

Although Windows 7 (31.20%) still holds a large market share, mobile platforms are beginning to replace traditional, stationary ones.

Taking a look at the statistics for 2015 on the browsers used, we can see that the first place belongs to Chrome ( all versions - 44.87%), second place - Firefox ( all versions - 10.37%), third Internet Explorer 11 (6.84%).

Accelerated rates of new Google versions Chrome and Firefox allow you to more efficiently develop projects for these platforms. The picture is a little different for the "bad guy" of Internet Explorer, because you can still find old versions of it on the web. And this leads to problems with the cross-browser compatibility of the site.

Microsoft ended support for IE6 on April 8, 2014. And from 2016, only the most recent version of Internet Explorer will be supported and updated. This effectively means that support for IE7 and IE8 was completely dropped in early 2016, regardless of operating system... IE9 will only be supported in Windows Vista IE10 - Windows Server 2012 only, IE11 - Windows 7 and Windows 8.1:



As a result, it is necessary to stop development for IE6 and, as well as for IE7. To justify this strategy, there are several examples of solutions from well-known companies: Google dropped support for IE8 in November 2012 and IE9 in October 2013.

Github no longer supports IE 7 and 8. In addition, some of Twitter functionality does not work in IE8. Finally, the popular framework will not support IE8. from version 4.

However, usage statistics can vary by region, with 6.11% of users in China still browsing through IE8 in 2015. If you take into account that there were about 724.4 million Internet users in China, you can understand that approximately 44.2 million Chinese continue to use IE8 this year.

Therefore, regional markets, specific customers and industry requirements can vary significantly. This is especially true for corporate and government agencies.

2. Analyze your audience

The basic principle here is this: the higher the required cross-browser compatibility, the more time it will take for development, which leads to an increase in the cost of the project. To make an informed, economically sound decision, you need to ask yourself the following questions:

  • What is your target audience?
  • What geographic region should you target?
  • What browsers and devices are your visitors using?
  • Are there technical factors within the company or industry that make you support specific versions of older browsers?
  • What are in terms of ecommerce conversion rates and ROI of different user groups by browser version?

By answering these questions using statistics, for example from Google analytics, you can get an objective picture.

3. Problems in outdated browsers and different approaches to development

Responsive web design relies heavily on media queries to modify CSS for different screen resolutions. In addition, modern sites are characterized by the use of HTML5 semantic elements (for example,

,