I have this layout I am working on. It strongly depends on CSS calc to do the necessary calculations.
width: -webkit-calc(50% - 20px); width: -moz-calc(50% - 20px); width: calc(50% - 20px);
Now, I can't get this to work in Safari. Is there something I'm doing wrong?
And also, is there a way I can introduce a fall-back mechanism for browsers that don't support it? Percentage wont do a good job, since I have to subtract the object in the middle, from the two on the side.
Thanks.
Safari & iOS Safari (both 6 and 7) do not support viewport units (vw, vh, etc) in calc(). So in your case, try not to use viewport units in calc(), since you will have issues in Safari 6 and 7. Usually with calc() you need to also use the -webkit prefix which is required for Safari 6 and Chrome 19-25 per the spec?
Displaying properties in Safari There is a CSS appearance property used to display an element using a platform-native styling based on the users' operating system's theme. To make it work on Safari, we must set the appearance property to its "none" value. Also, use -WebKit- and -Moz- vendor prefixes.
calc() The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length> , <frequency> , <angle> , <time> , <percentage> , <number> , or <integer> is allowed.
CSS Code for Safari Compatibility In the case of Safari web browsers, the media uses minimum resolution and WebKit appearance properties in the recent versions. In the previous Safari versions, it used pixel ratio for a CSS property.
The way I solved this problem, is introducing a pure CSS fall-back, that older browsers that don't support CSS calc would only read.
figure.left { width: 48%; width: -webkit-calc(50% - 20px); width: -moz-calc(50% - 20px); width: calc(50% - 20px); }
The left and right panels, get a width of 48% if the browser cannot read the calc values.
figure.logo { min-width: 40px; width: 4%; width: -webkit-calc(40px); width: -moz-calc(40px); width: calc(40px); }
And the logo, which sits in-between the two panels, gets a width of 4%. And also a minimum width of 40px. And if the calc values can be read by the browser, they are 40px.
You're code looks fine, so I think the problem is browser compatibility. According to Mozilla Documentation, this feature works in Chrome, Firefox, and IE (the newer versions), but is nonexistent in Opera and is buggy in Safari. this question for more compatibility information. A much safer fallback/alternative would be to do your calculations in Javascript:
if ($(document).width() >= 750){ $("#yourElementsID").css("width",$("#yourElementsID").width()*0.5-20) } else if ($(document).width() >= 400){ // do something here for smaller values }
You can use the jQuery .width()
function to detect the width of the document, then set the width of your elements accordingly. But just warning you, try to keep as many of your styles outside of your scripts and inside your stylesheets because otherwise it will be unmaintainable.
If it still doesn't work in some browsers, use Modernizr to detect the browser/version/OS and then redirect them to a different version of the page.
Edit 1: Added responsiveness
Edit 2: Removed Interval
Edit 3: Added mention of Modernizr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With