I'm re-building a site using CSS flexbox.
In checking browser compatibility, I see that flexbox is supported by all modern browsers, except that Safari 8 and IE 10 require vendor prefixes.
In checking Google Analytics, I see that 96% of site visitors in the last 6 months use browsers that fully support flexbox. The remaining 4% use browsers that require prefixes or provide no support.
Since we're talking about 4% of users, and the number will keep getting smaller, (and I like to keep my code as clean and simple as possible), I'm considering not using prefixes, and instead asking users to upgrade their browsers.
How can I target older browsers in order to display a message to users asking them to update their browser?
Here's what I have so far:
<!--[if IE]>
<div class="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">
upgrade your browser</a> to improve your experience.</p>
</div>
<![endif]-->
This IE conditional comment covers me for IE versions 6, 7, 8 and 9.
These visitors will get an alert with a link to download a current browser. However, Microsoft discontinued support for conditional comments starting with IE10.
Now I need something similar for:
- IE 10
- Safari 7-8
- Opera Mini < 8
- UC Browser for Android
- Android Browser < 4.4
Is there a simple JS/jQuery script to handle this job? Or another lightweight method?
Solution
Thanks for all the answers. Clearly there are many ways to tackle this problem (Modernizr, PHP, jQuery functions, plain Javascript, CSS, browser-update.org, etc.) Many of these methods will do the job completely and effectively.
I went with the simplest one: CSS (credit @LGSon).
This CSS covers essentially all targeted browsers, except for IE <= 7.
.browserupgrade { display: block; }
_:-ms-fullscreen, :root .browserupgrade { display: none; }
:-o-prefocus, .browserupgrade { display: none; }
@supports (display: flex) { .browserupgrade { display: none; }}
See the answer for details.
And for those relatively few visitors using IE <= 7, a conditional comment in the HTML:
<!--[if lte IE 7]>
<div style=" ... inline styles here ...">
browser upgrade message here
</div>
<![endif]-->
You can also insert prefixes into stylesheet. Check if the browsers style property actually supports the style properties either already present, or dynamically inserted later. Composed it primarily for animations, though could be modified to test for any
css
, style property in the browser. Ifprops
already present instylesheet
, vendor prefixes are inserted intostylesheet
. If props are later dynamically inserted, vendor prefixes are also inserted, or attached onto those props.For example, adding prefixes for
animation
,backface-visibility
,border-radius
,box-shadow
,transform
,transform-style
,transition
; also setting prefix at@keyframes
github https://github.com/guest271314/prefix/blob/master/prefix.1.1.min.js
A more thorough approach.
You can get the user-agent strings for each browser you are interested and create an array of all the ones you want to exclude.
Use
navigator.userAgent
to get the visitor's user-agent and check if it is in your not-supported-browsers array using jQueryinArray()
A simple javascript alert could be thrown for visitors using a browser that is a match.
Here is an example of getting browser related information (example source:w3schools.com)
UPDATE
This is a very simple approach, no 3rd party API involved. Simply use UAparser.js to filter results. All you need to check from your part is browser name and version. Check the example below
You can use modernizr.js for feature detection.
And then write some JavaScript to re-direct the users to the URL above if the feature isn't found.
Yes we have a solution for this...
first of all download a custom build Modernizr from (click here --) https://modernizr.com/ (for flexbox property) then include latest Jquery to your page, add some stylesheet and you are done!
(Note: It is not necessary to download a custom build Modernizr, you can use whole complete build directly from Modernizr CDN but as you need to check Flexbox property only; thats why I told you to download a custom build)
Check this fiddle flexbox support fiddle
Your entire html page (with css, jquery ) will be like this...
Your steps will be,
1. Download custom Modernizr to detect flexbox property
2. create a html page with class="no-js" added in html tag
3. create a div for displaying a message and hide it
4. add jquery and Modernizr to your page
5. show a div, when a browser doesn't support flexbox-property
(for that purpose use 'Modernizr.flexbox' property as shown in above script)
or if you dont want to use Jquery then use following script (thanks to @Shaggy) as,
PREMISE
The JavaScript
style
property returns a complete collection of CSS properties that the browser supports for the specified element, which can be tested using the following Snippet:This is true whether or not a particular property is explicitly set for the specified element. This can be tested by running the following Snippet in Chrome, for example; the first line will return
false
as Chrome does not yet support the unprefixedappearance
property while the second line will returntrue
.However, it should be noted that if an unsupported property is set on an element using JavaScript, via the
style
property then checking for the presence of that property will returntrue
:Therefore we will be using
document.createElement()
to create a temporary element so we can be sure none of the properties we are checking for have been set in this manner. (Thanks to Gothdo for this suggestion, which removed the need for any assumptions to be made.)REQUIREMENTS
Looking at the list of browsers to be targeted:
-ms-
prefix.-webkit-
prefix.-webkit-
prefix.-webkit-
prefix.SOLUTION
We can see from that list that all the browsers to be targeted require prefixing of the flexbox properties so we can target them simply by checking for the presence of any of the unprefixed properties in the temporary element's
style
collection. The following Snippet will returntrue
when run in any of the above browsers:Armed with this knowledge we can now create a working solution to display the browser upgrade message to the necessary browsers.
NOTES
Old Syntax
There was a version of the current flexbox spec that used a slightly different syntax with one of the differences being that the
order
property was namedflex-order
. To the best of my knowledge, the only browser that ever supported that syntax was IE10 which required prefixing so should, therefore, be targeted by the solution above. For the sake of completeness, though, in case there were ever any browsers that supported that syntax without prefixing, you can include an additional check for the presence of theflex-order
property while also checking that theorder
property isn't present in case there are any browsers that support both versions of the syntax.Firefox
Firefox added support for unprefixed flexbox properties in version 22 but did not support either the
flex-flow
orflex-wrap
properties until version 28 so to add v22-27 to our targeting, we should instead check for the presence of one of those properties.Internet Explorer 11
Despite not requiring prefixing, IE11's support for flexbox is still extremely buggy so you might want to consider targeting it as well. There is no way of doing so using any of the flexbox properties, however IE 7-11 (sort of) supported the
image-rendering
property via the non-standard-ms-interpolation-mode
property, with support later dropped in Edge/v12+ so we can instead check for that. There is no guarantee, though, that this property won't be added back in to a future version of Edge so that should be monitored.Opera Mini
Although caniuse.com claims full support for flexbox has existed in Opera Mini since version 5 and is widely considered to be an authoritative source for such information, if you still wish to target it, you can do so by checking for the presence of one of the myriad of properties it doesn't support. To keep things concise, though, I'd suggest replacing the
-ms-interpolation-mode
check with a check fortransform-style
which is not supported by Opera Mini nor by pre-Edge versions of IE. As with the previous check, this one should be monitored as future versions of Opera Mini which you might not want to target may continue not to support thetransform-style
property.UPDATES
13/05/16: Added additional checks to target more browsers after question was updated.
16/05/16: Added more detailed explanations of the proposed solution based on feedback provided in the comments and to make it worthy of the bounty on offer. Removed checks for old flexbox spec as they were redundant.
17/05/16: Added a temporary element to run the checks on, rather than using
document.body
, following Gothdo's suggestion, which led to the removal of suggesting the use ofgetElementsByTagName()
for wider browser support.23/05/16: Added notes with suggested checks for Internet Explorer 11 & Opera Mini.
EDIT:
There are two viable approaches: Modernizr or Feature Queries. Modernizr is the current stable solution, but feature queries
@support
, when fully supported by all browsers, will be the best pure CSS solution for css feature detection.Currently you need to add some prefixes to target those browsers that do not support feature queries, ie. see LGSon's answer.
Modernizr approach:
You can create a flexbox custom Modernizr build pretty easy, as previously recommended: Modernizr Custom Flexbox Build. Reference your custom modernizr javascript file in your website and then add some Html markup and Css classes , something like this:
NOTE: Open the snippet in different browsers to check support.
TESTS:
No flexbox support: Internet Explorer 10
Flexbox support: Chrome 50
FURTHER EXPLANATION:
Modernizr checks if the browser supports your custom feature in this case flexbox. If the browser does not support it, Modernizr adds a class to your
<html>
tag. Like this:<html class="no-flexbox>
. Which you can use at your convenience in your stylesheet.Additionally, you can add a function to detect the user's browser and add it to your update browser message for a better UX. Credit: kennebec