how can we detect css support flex-box and flex-wrap using JavaScript.
Create an element and check the style property. If it is supported, it will not return anything, i.e. '' Otherwise it will return undefined .
For example, if you run the snippet below in Chrome, you will get undefined for columns and '' for flex-wrap .
Fragment :
console.log('flex = ' + document.createElement("p").style.flex); console.log('columns = ' + document.createElement("p").style.columns); console.log('flex-wrap = ' + document.createElement("p").style.flexWrap);
Although you only mentioned Javascript in your question, there is also a CSS way to identify features.
The @supports rule, also called CSS Feature Queries .
You provide the CSS declaration as a condition, and the browser executes it to return whether it supports or not. For example, the following CSS will apply a green background color if flex supported.
@supports (display: flex) { div { background-color: #0f0; } }
Browser support is good in all modern browsers, with the exception of IE (as usual). For IE and (Safari <9), you will need to use a fallback when the @supports rule @supports .
Combining the above two, there is also an API around this that you can use in Javascript to detect functions.
var isColumnSupported = CSS.supports('columns', ''); console.log('columns supported: ' + isColumnSupported); var isFlexWrapSupported = CSS.supports('flex-wrap', 'wrap'); console.log('flex-wrap supported: ' + isFlexWrapSupported);
source share