|
There are quite a few very useful CSS2Cascading Style Sheets Standard that is currently the most supported. (CSS Level 2) Properties that IE does not support. One of these is the max-width and min-width properties (along with min-height and max-height).
What is max-width?
The max-width CSS2 property will limit the width of the selected element to a certain value.
max-width example /* CSS2 Compliant browsers (not IE < 6.0) */ #body_wrap { max-width: 800px; }
%2F%2A%20CSS2%20Compliant%20browsers%20%28not%20IE%20%3C%206.0%29%20%2A%2F%0D%0A%23body_wrap%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bmax-width%3A%20800px%3B%0D%0A%7D
The min-width property behaves the same, limiting the selected element to a minimum width specified.
IE support for max-width and min-width
Unfortunately IE does not support the max-width and min-width property via pure CSS. However, IE allows you to define property values via JavaScript inside CSS! This is done via the expression() function.
max-width example /* IE's expression() allows JavaScript in CSS */ selector { property: expression('some JavaScript equating to a string value here'); }
%2F%2A%20IE%27s%20expression%28%29%20allows%20JavaScript%20in%20CSS%20%2A%2F%0D%0Aselector%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bproperty%3A%20expression%28%27some%20JavaScript%20equating%20to%20a%20string%20value%20here%27%29%3B%0D%0A%7D
This can be thought of in JavaScript as:
<script> /* IE's expression() explanation in JS */ selector.style.property = 'some JavaScript equating to a string value here'; </script>
%3Cscript%3E%0D%0A%2F%2A%20IE%27s%20expression%28%29%20explanation%20in%20JS%20%2A%2F%0D%0Aselector.style.property%20%3D%20%27some%20JavaScript%20equating%20to%20a%20string%20value%20here%27%3B%0D%0A%0D%0A%3C%2Fscript%3E
Enabling IE support for max-width and min-width
Using the expression() custom IE function, we can now define a width dynamically in our CSS.
/* CSS2 Compliant browsers (not IE < 6.0) */ #body_wrap { max-width: 800px; } /* Dynamic assignment of width based on widow width. (* html allows only IE selection) */ * html #page_wrap { width: expression(Math.min((document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth) - 20, 800)+'px'); }
%2F%2A%20CSS2%20Compliant%20browsers%20%28not%20IE%20%3C%206.0%29%20%2A%2F%0D%0A%23body_wrap%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bmax-width%3A%20800px%3B%0D%0A%7D%0D%0A%0D%0A%2F%2A%20Dynamic%20assignment%20of%20width%20based%20on%20widow%20width.%20%28%2A%20html%20allows%20only%20IE%20selection%29%20%2A%2F%0D%0A%2A%20html%20%23page_wrap%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bwidth%3A%20expression%28Math.min%28%28document.documentElement%20%3F%20document.documentElement.clientWidth%20%3A%20document.body.clientWidth%29%20-%2020%2C%20800%29%2B%27px%27%29%3B%0D%0A%7D
Math.min() returns the lowest value taken in as parameters.
(document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth) returns the width of the window.
We subtract 20 from the width of the window to account for the vertical scrollbar which is ever-present in the IE browser.
Together they calculate the correct width of the #body_wrap based on the window width.
An explanation in JavaScript:
max-width via JavaScript <script> /* Dynamic assignment of width based on widow width every time the window is resized */ function resize_page_wrap() { var win_width = document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth; document.getElementById('page_wrap').style.width = Math.max(Math.min(win_width - 20, 800), 600)+'px'; } window.onload = resize_page_wrap; // first evaluation when window finished loading window.onresize = resize_page_wrap; // re-evaluate when ever the window resizes </script>
%3Cscript%3E%0D%0A%0D%0A%2F%2A%20Dynamic%20assignment%20of%20width%20based%20on%20widow%20width%20every%20time%20the%20window%20is%20resized%20%2A%2F%0D%0Afunction%20resize_page_wrap%28%29%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bvar%20win_width%20%3D%20document.documentElement%20%3F%20document.documentElement.clientWidth%20%3A%20document.body.clientWidth%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bdocument.getElementById%28%27page_wrap%27%29.style.width%20%3D%20Math.max%28Math.min%28win_width%20-%2020%2C%20800%29%2C%20600%29%2B%27px%27%3B%0D%0A%7D%0D%0A%0D%0Awindow.onload%20%3D%20resize_page_wrap%3B%20%2F%2F%20first%20evaluation%20when%20window%20finished%20loading%0D%0Awindow.onresize%20%3D%20resize_page_wrap%3B%20%2F%2F%20re-evaluate%20when%20ever%20the%20window%20resizes%0D%0A%0D%0A%3C%2Fscript%3E
With JavaScript we have to set event listerners for the window.onload and window.onresize properties. With CSS, the CSS property values are re-calculated when ever the window changes.
We can enable support for min-width just as we did with max-width.
/* CSS2 Compliant browsers (not IE < 6.0) */ #body_wrap { min-width: 600px; } /* Dynamic assignment of width based on widow width. (* html allows only IE selection) */ * html #page_wrap { width: expression(Math.max((document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth) - 20, 600)+'px'); }
%2F%2A%20CSS2%20Compliant%20browsers%20%28not%20IE%20%3C%206.0%29%20%2A%2F%0D%0A%23body_wrap%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bmin-width%3A%20600px%3B%0D%0A%7D%0D%0A%0D%0A%2F%2A%20Dynamic%20assignment%20of%20width%20based%20on%20widow%20width.%20%28%2A%20html%20allows%20only%20IE%20selection%29%20%2A%2F%0D%0A%2A%20html%20%23page_wrap%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bwidth%3A%20expression%28Math.max%28%28document.documentElement%20%3F%20document.documentElement.clientWidth%20%3A%20document.body.clientWidth%29%20-%2020%2C%20600%29%2B%27px%27%29%3B%0D%0A%7D
The solution to IE's min-width max-width problems
We can also combine what we've done to create max-with, min-width emulation for IE.
/** css2 compliant */ #page_wrap { max-width: 800px; min-width: 600px; } /* ie min-width, max-width emulation */ * html #page_wrap { width: 800px; /* in case JS is off, set a fallback width. default is auto */ width: expression(Math.max(Math.min((document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth) - 20, 800), 600)+'px'); }
%2F%2A%2A%20css2%20compliant%20%2A%2F%0D%0A%23page_wrap%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bmax-width%3A%20800px%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bmin-width%3A%20600px%3B%0D%0A%7D%0D%0A%0D%0A%2F%2A%20ie%20min-width%2C%20max-width%20emulation%20%2A%2F%0D%0A%2A%20html%20%23page_wrap%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3Bwidth%3A%20800px%3B%20%2F%2A%20in%20case%20JS%20is%20off%2C%20set%20a%20fallback%20width.%20default%20is%20auto%20%2A%2F%0D%0A%26nbsp%3B%20%26nbsp%3Bwidth%3A%20expression%28Math.max%28Math.min%28%28document.documentElement%20%3F%20document.documentElement.clientWidth%20%3A%20document.body.clientWidth%29%20-%2020%2C%20800%29%2C%20600%29%2B%27px%27%29%3B%0D%0A%7D
All you have to do is change the values 800 and 600 to your min/max-width's and you're set to have it working in IE.
Disadvantages
- Problem is, the expression() function in IE CSS, requires JavaScript to be enabled on the IE browser.
Benefits
- I'd assume its faster than using pure javascript.
- It also keeps all your styles in the CSS file instead of distributing it to JS.
- JS is available on most browsers (95% average on this site).
Notes
- The * html notation in the CSS is a custom IE CSS selector.
- expression() only evaluates to a value. So you can only use a subset of JavaScript that calculates values. It does support conditional statements however via the shorthand syntax:
expression( condition ? 'true value' : 'false value' )
- You can support multiple conditional statements as such:
expression( condition ? 'true value' : (condition ? 'true value' : 'false value') )
Please let me know if you have any suggestions via the comment form. :)
|