ASP

Dynamic CSS

12.13.07

Example

The stylesheet for this page is actually an .asp file with rules dynamically generated depending on the detected browser.

http://www.leodominguez.com/style/

Code

The debate about CSS hacks wages on and on. I don't think anyone actually likes using them, but sometimes there's just no way around it. For example, on this page the <pre> elements should obey an overflow: auto; property, but they don't in IE6. Instead of scrolling they stretch horizontally! Adding a CSS hack to fix this results in invalid CSS. How to get around this with ASP? Dynamically generate your CSS file depending on the browser and version.

<%
'make sure your .asp file is treated like a .css file
response.contenttype="text/css"

' create an instance of the Browser Capabilities component
set browserdetect = server.createobject("mswc.browsertype")

' find some properties of the browser being used to view this page
gotbrowser = trim(browserdetect.Browser)
gotversion = trim(browserdetect.Version)
gotmajorver = trim(browserdetect.Majorver)
gotminorver = trim(browserdetect.Minorver)
gotplatform = trim(browserdetect.Platform)
gotframes = trim(browserdetect.Frames)
gottables = trim(browserdetect.Tables)
gotcookies = trim(browserdetect.Cookies)
gotjavascript = trim(browserdetect.JavaScript)
%>

In this paticular scenario, <pre> elements "need" a hack in order to display as intended in IE6.

pre
{
	background: #FFF7D7;
	border: 1px solid #666;
	color: black;
	padding: 1em;
	margin-bottom:1.2em;
	<%if gotbrowser <> "IE" and gotmajorver <> "6" then%>overflow: auto;<%end if%>
}

<%if gotbrowser = "IE" and gotmajorver = "6" then%>
/**************************************************************/
/* PRE overflow:auto IE hack- HTML CSS, unfortunately we have to... */
/* http://sebastienlorion.blogspot.com/2005/08/fun-with-css-overflow-and-ie.html */
pre {overflow-x: auto; width: 100%;}

html>body pre {overflow: auto;}
/**************************************************************/
<%end if%>

Credits

Fun with CSS overflow and IE

No More CSS Hacks

Problem loading stylesheet - not loaded because its MIME type, "text/html", is not "text/css"

[SOLVED] Stylesheet error in Firefox

ASP ContentType Property