Solved

CSS IE6 Conditional statement for style sheet not working

Posted on 2009-07-08
16
1,129 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:jay_eire
  • 8
  • 7
16 Comments
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24808664
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

0
 
LVL 7

Author Comment

by:jay_eire
ID: 24808713
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 %> //-->
 
0
 
LVL 7

Author Comment

by:jay_eire
ID: 24808734
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
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24808803
Thats why I questioned your code

the syntax for if is

if (condition){
 statement;
}
0
 
LVL 7

Author Comment

by:jay_eire
ID: 24808841
ok thanks, so how should the if statement look in my code??
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24808862
to you have the
Request.ServerVariables["HTTP_USER_AGENT"];

code already setup?
0
 
LVL 7

Author Comment

by:jay_eire
ID: 24808891
no i dont how do i set that up? im trying to do this inline on the page, im totally lost here :(
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24808972
Are you familiar with C# ?
0
 
LVL 7

Author Comment

by:jay_eire
ID: 24809026
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

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24809101
rename your file with prefix .asp

like
default.asp
or something else
.asp

we will be using asp 1 for this
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24809111
<!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>
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24809113

<!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

0
 
LVL 7

Author Comment

by:jay_eire
ID: 24809144
but the statement needs to work on an  ASPX page on the .net framework 3.5
0
 
LVL 7

Author Comment

by:jay_eire
ID: 24809227
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

0
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
ID: 24810822
You don't need to move it from between the head tags. You just need to move it after the main stylesheet.

You see, declarations in a stylesheet override declarations in a previous stylesheet where there are conflicts. It's the "cascading" part of CSS. And because of that you only need to include the declarations that are different in the IE6 stylesheet instead of having two complete stylesheets.
0
 
LVL 7

Author Closing Comment

by:jay_eire
ID: 31601314
Thank you.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question