Solved

CSS IE6 Conditional statement for style sheet not working

Posted on 2009-07-08
16
1,132 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 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