|
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.
/* CSS2 Compliant browsers (not IE < 6.0) */
#body_wrap {
max-width: 800px;
}
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.
/* IE's expression() allows JavaScript in CSS */
selector {
property: expression('some JavaScript equating to a string value here');
}
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>
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');
}
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:
<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>
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');
}
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');
}
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. :)
|