Forum Moderators: not2easy

Message Too Old, No Replies

conditional css

can one use if clauses based on url to set css attribute values

         

stumped

2:37 am on Jun 23, 2009 (gmt 0)

10+ Year Member



Is there a way to make css styling conditional based on URL parameters so that attribute values are set one way for a particular filename and another way for a different filename. I have seen the conditional syntax for if you need to style differently for IE 6 or so but I am looking for how to code css conditionally based on URL parameters. Can it be done? Inside a .css file or outside of it with javascript?

badbadmonkey

8:25 am on Jun 23, 2009 (gmt 0)

10+ Year Member



You will need to generate the CSS with server side code. You then reference the dynamic script instead of a static .css file.

swa66

11:40 am on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Most of us tackle the problem differently:

Set a class or id on your body, indicating the section the page is part of.
Use that identifyer to style things differently)

e.g.:
a page news.html and a page aboutus.html, the news should have a yellow background with red titles and the aboutus should have a blue background with white letters.

news.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>test</title>
<link rel="stylesheet" type="text/css" href="/style.css" />
</head>
<body id="news">
<h1>NEWS</h1>
<p>Hello world</p>
</body>
</html>

aboutus.html:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>test</title>
<link rel="stylesheet" type="text/css" href="/style.css" />
</head>
<body id="about">
<h1>ABOUT US</h1>
<p>Hello world</p>
</body>
</html>

style.css:


* {
margin:0;
padding:0;
}
#news h1 {
color: red;
}
#news {
background-color: yellow;
}
#about
background-color: blue;
color: white;
}

code itself is untested, but I use something very similar in production use