├── .bowerrc ├── .editorconfig ├── .gitignore ├── Gruntfile.js ├── LICENSE ├── README.md ├── bower.json ├── calc.js ├── dist ├── calc.js └── calc.min.js ├── package.json └── testcase ├── import.css ├── index.html ├── index.selectivizr.html └── styles.css /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory" : "libs" 3 | } 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = tab 6 | indent_size = 4 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [{package.json,.scss-lint.yml}, *.hbs] 13 | indent_style = space 14 | indent_size = 2 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | libs 3 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 3 | grunt.initConfig({ 4 | pkg: grunt.file.readJSON('package.json'), 5 | uglify: { 6 | options: { 7 | banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n' 8 | }, 9 | dist: { 10 | files: [ 11 | { 12 | expand: true, 13 | cwd: 'dist/', 14 | src: ['**/*.js'], 15 | dest: 'dist/', 16 | ext: '.min.js' 17 | } 18 | ] 19 | } 20 | }, 21 | jshint: { 22 | files: ['Gruntfile.js', 'calc.js'], 23 | options: { 24 | evil: true, 25 | // options here to override JSHint defaults 26 | globals: { 27 | console: true, 28 | module: true, 29 | document: true 30 | } 31 | } 32 | }, 33 | includes: { 34 | options: { 35 | includeRegexp: /^\s*?\/\/\s*import\s+['"]?([^'"]+)['"]?\s*$/, 36 | duplicates: false, 37 | flatten: false 38 | }, 39 | dist: { 40 | files: [ 41 | { 42 | expand: true, 43 | src: ['calc.js', '!libs/**/*.js'], 44 | dest: 'dist/', 45 | ext: '.js' 46 | } 47 | ] 48 | } 49 | }, 50 | watch: { 51 | options: { 52 | spawn: true 53 | }, 54 | js: { 55 | files: ['calc.js', '!libs/**/*.js'], 56 | tasks: ['jshint', 'includes:dist', 'uglify:dist'] 57 | } 58 | }, 59 | 60 | }); 61 | 62 | grunt.loadNpmTasks('grunt-contrib-uglify'); 63 | grunt.loadNpmTasks('grunt-contrib-jshint'); 64 | grunt.loadNpmTasks('grunt-contrib-watch'); 65 | grunt.loadNpmTasks('grunt-includes'); 66 | 67 | grunt.registerTask('default', ['jshint', 'includes:dist', 'uglify:dist']); 68 | 69 | }; 70 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Robert Weber 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Polyfill for CSS3 calc() 2 | 3 | 4 | This is a javascript poly fill for the CSS3 [calc-function](https://developer.mozilla.org/de/docs/Web/CSS/calc). 5 | 6 | Inspired by: 7 | 8 | - [selectivizr](http://selectivizr.com/) 9 | - the original poly fill: [CJKay/PolyCalc](https://github.com/CJKay/PolyCalc) 10 | - more recently by [css.js](https://github.com/jotform/css.js) 11 | 12 | ## Usage 13 | 14 | Simply drop the link like this after you included your CSS: 15 | 16 | 17 | 18 | A test for the support if calc() is integrated based on the [Modernizr test](https://github.com/Modernizr/Modernizr/blob/924c7611c170ef2dc502582e5079507aff61e388/feature-detects/css/calc.js) 19 | 20 | Tested on Internet Explorer 8 and Android 4.0.3 21 | 22 | ## Dependencies 23 | 24 | * support for ``document.querySelectorAll`` 25 | * for media queries ``window.matchMedia`` 26 | 27 | ## Remarks 28 | 29 | ### Specificity 30 | 31 | This polyfill does not take specificity into account when applying styles for calc(). 32 | 33 | For example if you have to rules: 34 | 35 | 36 | .element div { 37 | width: calc(50% - 230px); 38 | } 39 | 40 | div { 41 | width: calc(50% - 100px); 42 | } 43 | 44 | The first rule would apply for the ``
`` element because of higher specificity. The CSS is parsed from top to bottom and therefore the polyfill would apply the styles of the second rule. Just keep that in mind. 45 | 46 | ### Resetting 47 | 48 | This polyfill also does not detect any resetting of calc(): 49 | 50 | 51 | .element div { 52 | width: calc(50% - 230px); 53 | } 54 | 55 | div { 56 | width: 50%; 57 | } 58 | 59 | The polyfill will apply the rules from the first as it is not detecting the resetting of the width in the second. 60 | 61 | ### Inline Styles 62 | 63 | Support for polyfilling inline styles is integrated. However right now there seems no way to get the unparsed contents of a `` 24 | 25 | 26 | 27 | 28 | 29 | 30 |

Polyfill for calc() test case

31 | 32 |
33 |

Element with only px values in calc for width

34 | 35 |
36 | 37 |
38 |

Element with px and percent values in calc for width

39 | 40 |
41 | 42 |
43 |

Element with px and em values in calc for width

44 | 45 |
46 | 47 |
48 |

Element with px and rem values in calc for width

49 | 50 |
51 | 52 |
53 |

Element with only px values in calc for width, but a selector which IE8 does not understand.

54 | 55 |
56 | 57 |
58 |

Element with px and percent values in calc for width in media query

59 | 60 |
61 | 62 |
63 |

Element with decimal percentages and multiplication and division

64 | 65 |
66 | 67 |
68 |

Element with px and em values in calc for width. Inline style

69 |
70 | 71 | 72 | 73 | 126 | 127 | -------------------------------------------------------------------------------- /testcase/index.selectivizr.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Polyfill for calc() test case 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |

Polyfill for calc() test case

33 | 34 |
35 |

Element with only px values in calc for width

36 | 37 |
38 | 39 |
40 |

Element with px and percent values in calc for width

41 | 42 |
43 | 44 |
45 |

Element with px and em values in calc for width

46 | 47 |
48 | 49 |
50 |

Element with px and rem values in calc for width

51 | 52 |
53 | 54 |
55 |

Element with only px values in calc for width, but a selector which IE8 does not understand.

56 | 57 |
58 | 59 |
60 |

Element with px and percent values in calc for width in media query

61 | 62 |
63 | 64 |
65 |

Element with decimal percentages and multiplication and division

66 | 67 |
68 | 69 |
70 |

Element with px and em values in calc for width. Inline style

71 |
72 | 73 | 74 | 75 | 128 | 129 | -------------------------------------------------------------------------------- /testcase/styles.css: -------------------------------------------------------------------------------- 1 | @charset "UTF-8"; 2 | 3 | @import "import.css"; 4 | 5 | @font-face{ 6 | font-family: 'CalligraphyFLFRegular'; 7 | src: url('CalligraphyFLF.eot'); 8 | src: local('CalligraphyFLF Regular'), local('CalligraphyFLF'), url('CalligraphyFLF.ttf') format('truetype'); 9 | } 10 | 11 | 12 | @-webkit-keyframes fade-in { 13 | 0% { opacity: 0; } 14 | 100% { opacity: 1; } 15 | } 16 | @keyframes fade-in { 17 | 0% { opacity: 0; } 18 | 100% { opacity: 1; } 19 | } 20 | 21 | body { 22 | margin: 0; 23 | padding: 0; 24 | font-family: sans-serif; 25 | } 26 | 27 | h2 { 28 | padding: 20px; 29 | color: white; 30 | } 31 | 32 | .element { 33 | height: 200px; 34 | width: 500px; 35 | margin-bottom: 100px; 36 | padding: 0; 37 | text-align: center; 38 | border: 1px solid red; 39 | } 40 | 41 | .element1 { 42 | width: calc(600px - 100px); 43 | background: #ff6347; 44 | } 45 | 46 | .element9, 47 | .element2 { 48 | width: calc(600px - 20%); 49 | background: green; 50 | } 51 | 52 | .element3 { 53 | font-size: 10px; 54 | width: calc(600px - 15em); 55 | background: blue; 56 | } 57 | 58 | .element4 { 59 | width: calc(600px - 15rem); 60 | padding: 20px calc(12px + 2em); 61 | background: purple; 62 | } 63 | 64 | .element6 { 65 | width: calc(200px + 75%); 66 | background: #0f0; 67 | } 68 | 69 | .element7 { 70 | width: calc(50.5% / 2); 71 | padding: 20px calc(2% * 2); 72 | background: purple; 73 | } 74 | 75 | /* .element5 */ 76 | 77 | .element:nth-of-type(5) { 78 | width: calc(500px + 100px); 79 | background: #cc0; 80 | } 81 | 82 | /* Comment for testing */ 83 | 84 | @media screen and (max-width: 780px) { 85 | .element6 { 86 | width: calc(200px + 50%); 87 | background: #0f0; 88 | } 89 | } 90 | 91 | @media screen and (min-width: 840px) { 92 | .element6 { 93 | width: calc(200px + 30%); 94 | background: #0f0; 95 | } 96 | } 97 | --------------------------------------------------------------------------------