Link to home
Start Free TrialLog in
Avatar of jay_eire
jay_eireFlag for United States of America

asked on

CSS IE6 Conditional statement for style sheet not working

Hi Guys,
I have an ASPX page which has two conditional statements, i want to display a different stylesheet if an IE6 user is on my page....my statements are

1)for the style sheet:
<!--[if IE 6]>
<link href="IE6.css" rel="stylesheet" type="text/css" />
<![endif]-->

2) for a paragraph of text
    <!--[if IE 6]>
<h1>This is for IE 6 Only</h1>
<![endif]-->

the paragraph conditional statement works fine in IE 6 but my style sheet statement does not, any ideas what im doing wrong? attached is my code.

Thanks
Jay
********************
My Page Default.aspx
********************
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<!--[if IE 6]>
<link href="IE6.css" rel="stylesheet" type="text/css" />
<![endif]-->
<link href="allbrowsers.css" rel="stylesheet" type="text/css" />
 
<head runat="server">
    <title>CSS Style Sheet Swap</title>
</head>
<body>
<center>    <form id="form1" runat="server">
    <div id ="header">
    
    <h1>All Browsers Can See This</h1>
    
    
    <!--[if IE 6]>
<h1>This is for IE 6 Only</h1>
<![endif]-->
 
    </div>
    </form>
    </center>
 
</body>
</html>
 
 
********************
IE6 Stylesheet
********************
body {
	background-color: #CCFFFF;
}
 
body, td, th {
	color: #333333;
}
 
h1, h2 {
	color: #000033;
}
 
h3, h4, h5, h6 {
	color: #006699;
}
 
a {
  color: #003366;
}
#header
{
    width:800px;
    height:500px;
    background-color:green;
    margin-left:-20px;
}
 
 
********************
All Browsers Stylesheet
********************
body {
	background-color: #003366;
}
 
body {
	color: #DEDECA;
}
 
td, th {
	color: #99CCCC;
}
 
h1 {
	color: #FFFF33;
}
 
h2 {
	color: #FFFF66;
}
 
h3, h4 {
	color: #FFFFCC;
}
 
h5, h6 {
	color: #6699CC;
}
 
a {
	color: #9999cc;
}
#header
{
    width:800px;
    height:50px;
    background-color:Red;
}

Open in new window

Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

use <% %>

is this your actual code?

also, did you want to load the allbrowser css? if so load the IE6 css after to override any elements

=NerdsOfTech
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<!--// <% if IE 6 %> //-->
<link href="IE6.css" rel="stylesheet" type="text/css" />
<!--// <% else %> //-->
<link href="allbrowsers.css" rel="stylesheet" type="text/css" />
<!--// <% endif %> //-->
 
<head runat="server">
    <title>CSS Style Sheet Swap</title>
</head>
<body>
<center>    <form id="form1" runat="server">
    <div id ="header">
    
    <h1>All Browsers Can See This</h1>
    
    
<!--// <% if IE 6 %> //-->
<h1>This is for IE 6 Only</h1>
<!--// <% end if %> //-->
 
    </div>
    </form>
    </center>
 
</body>
</html>

Open in new window

Avatar of jay_eire

ASKER

thanks for the reply, yep this is my code, im just trying to get the switch working for the stylesheet, i copied and pasted your code in but im getting this error back

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1003: Syntax error, '(' expected

Source Error:

 

Line 4:  
Line 5:  <html xmlns="http://www.w3.org/1999/xhtml" >
Line 6:  <!--// <% if IE 6 %> //-->
Line 7:  <link href="IE6.css" rel="stylesheet" type="text/css" />
Line 8:  <!--// <% else %> //-->
 
I want to allow both style sheets, if the user accesses my site using IE6 to use the IE6 stylesheet if they are using any other browser then the page loads the allbrowsers.css
Thats why I questioned your code

the syntax for if is

if (condition){
 statement;
}
ok thanks, so how should the if statement look in my code??
to you have the
Request.ServerVariables["HTTP_USER_AGENT"];

code already setup?
no i dont how do i set that up? im trying to do this inline on the page, im totally lost here :(
Are you familiar with C# ?
no sorry im not, i thought this would be a quick bit of inline code to get the stylesheets switched, i will be using the statement on an ASPX but i cannot even get it working in a html file.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <!--[if IE 6]>
<link href="IE6.css" rel="stylesheet" type="text/css" />
<![endif]-->
<link href="allbrowsers.css" rel="stylesheet" type="text/css" />
</head>
<body>
 <center>
        <div id ="header">
    
    <h1>All Browsers Can See This</h1>
    
    
    <!--[if IE 6]>
<h1>This is for IE 6 Only</h1>
<![endif]-->
 
    </div>
    </center>
</body>
</html>

Open in new window

rename your file with prefix .asp

like
default.asp
or something else
.asp

we will be using asp 1 for this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<%
Dim strBrowser
StrBrowser = Request.ServerVariables("HTTP_USER_AGENT")
If (Left(strBrowser, 4) = "MSIE") Then %>
<link href="IE6.css" rel="stylesheet" type="text/css" />
<% Else %>
<link href="allbrowsers.css" rel="stylesheet" type="text/css" />
<% End If %>
 
<head runat="server">
    <title>CSS Style Sheet Swap</title>
</head>
<body>
<center>    <form id="form1" runat="server">
    <div id ="header">
   
    <h1>All Browsers Can See This</h1>
   

<% If (Left(strBrowser, 4) = "MSIE") Then %>
<h1>This is for IE 6 Only</h1>
<% End If %>
 
    </div>
    </form>
    </center>
 
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<%
Dim strBrowser
StrBrowser = Request.ServerVariables("HTTP_USER_AGENT")
If (Left(strBrowser, 4) = "MSIE") Then %>
<link href="IE6.css" rel="stylesheet" type="text/css" />
<% Else %>
<link href="allbrowsers.css" rel="stylesheet" type="text/css" />
<% End If %>
 
<head runat="server">
    <title>CSS Style Sheet Swap</title>
</head>
<body>
<center>    <form id="form1" runat="server">
    <div id ="header">
   
    <h1>All Browsers Can See This</h1>
   
 
<% If (Left(strBrowser, 4) = "MSIE") Then %>
<h1>This is for IE 6 Only</h1>
<% End If %>
 
    </div>
    </form>
    </center>
 
</body>
</html>

Open in new window

but the statement needs to work on an  ASPX page on the .net framework 3.5
I moved my original CSS conditional statement from between the HEAD tags into the body of my page and it worked.


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<link href="allbrowsers.css" rel="stylesheet" type="text/css" />
 
<head runat="server">
    <title>CSS Style Sheet Swap</title>
</head>
<body>
<!--[if IE 6]>
<link href="IE6.css" rel="stylesheet" type="text/css" />
<![endif]-->
<center>    <form id="form1" runat="server">
    <div id ="header">
    
    <h1>All Browsers Can See This</h1>
    
    
    <!--[if IE 6]>
<h1>This is for IE 6 Only</h1>
<![endif]-->
 
    </div>
    </form>
    </center>
 
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.