0

To apply a CSS to an existing html page I need to add a link that links to the css file, I am asked to include a link in the webpage that I am building that would link to the same html page but with a different css file, I am thinking I need to create a different css file, then create another .html page by copy the exact content from the first page and only change the link of the css file, but it doesn't seem so efficient and I am assuming there should be a standard method to do this.

thanks

Noona
  • 643
  • 1
  • 14
  • 30
  • Are you using any sort of server side scripting? (i.e. PHP, Perl, ASP.NET, JSP etc...) – Oded Mar 27 '10 at 10:00

2 Answers2

0

you can use a JAVAscript to documet.write a link

Anon
  • 147
  • 1
  • 5
0

If you can use a server-side language like PHP, I would do it something like this:

<?php

   $allowed_stylesheets = array("red", "white", "blue", "green", "mobile");
   $default_stylesheet = "red";

   $stylesheet = 
   (in_array($_GET["stylesheet"]) ? $_GET["stylesheet"] : $default_stylesheet);

?>

<link rel="stylesheet" type="text/css" href="<?php echo $stylesheet; ?>">

you would then be able to call your page, and switch the style sheet like so:

www.example.com/page.php?stylesheet=mobile

note that to make a .html page run in PHP, there is probably some server setup necessary, by default only .php pages are parsed in PHP. Depending on your situation, that may be too much hassle than it's worth. However, I don't know any pure HTML way of switching style sheets reliably.

Also, this way, you will have to repeat the style sheet command every time you call the page.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • No I am not supposed to use a scripting language, does that mean that the only way to do it is to copy the html page content and change the css link? for reference, this is the exact requirement in the assignment" Link to another XHTML page which has the exact same content as your homepage, yet uses a different CSS (this is the copy, which by including this link, has a link to itself). By having these two copies, we would like to examine the separation between content & style", thanks – Noona Mar 27 '10 at 10:16
  • @Noona I suppose you could do this using Javascript but I would regard that a sub-optimal solution because it wouldn't work for people with Javascript turned off. I can not think of a pure HTML solution that does not require your having two different HTML files. I would add the requirement of no server-side languages to the question as an update, maybe somebody comes up with an idea I haven't thought of. – Pekka Mar 27 '10 at 10:31
  • @Noona looks like your assignment pretty much requires you to make another HTML page with a different stylesheet. Sounds like a school project, so just copy & paste. – JKirchartz Mar 27 '10 at 14:40