Resources/Using HTML CSS and Javascript

Using HTML, CSS, and Javascript on Your Team Wiki

On this page you will find: HTML versus MediaWiki markup, how to use CSS and Javascript in templates, how to use link and script tags to load CSS and Javascript, how to use inline CSS and Javascript .

All the iGEM web sites are built on top of MediaWiki, which allows groups of people to create and edit pages easily.

HTML versus MediaWiki markup

All the iGEM web sites are built on top of MediaWiki ⇗, which allows groups of people to create and edit pages easily.

MediaWiki has its own markup ⇗ for formatting pages, creating links, etc. However, using HTML allows more creativity and flexibility. iGEM's websites allow any HTML tags. Simply enclose your HTML inside <html> tags within the page text.

You can mix wiki markup and HTML on the same page. Just make sure all the HTML is inside <html> tags, and all the wiki markup is outside <html> tags.

Example:

This example uses makes text bold and italic, first using html and then wiki markup.

<html>
<p> <b> <i> Example text </i> </b> </p>
</html>

''''' Example text '''''

Note

Using HTML in MediaWiki pages will cause the final page (as fully rendered by MediaWiki) to be noncompliant with strict HTML formatting specifications. This is basically inevitable, and is an acceptable trade-off for the added flexibility in wiki design that you get by using HTML. Most modern web browsers will interpret pages in "quirks" mode, which tries to "do the right thing" with HTML that is not strictly compliant.

How to use templates including CSS and Javascript

If you want to use the same styles or scripts on several different pages, it is useful to write them in one place and then "include" them on your individual pages. One way you can do this is to use MediaWiki's Template function.

You can include any HTML content this way, not just CSS and Javascript. This is useful for creating things like a common header, menu bar, and footer for the pages on your wiki.

If you edit a page, there will be a list of which templates are used on that page, below the Save/Preview buttons. You can also see which pages use your template. Go to the template, and then look in the top menu for "Wiki Tools" → "What Links Here". This is useful if you want to change or delete a template -- you can check if any important pages are using it.

First, create a page whose name starts with "Template:TeamName". Inside <html> tags, put the material you want to include. Here are two examples:

2020.igem.org/Template:TeamName/CSS

<style>
/*Make text red and bold*/
.red_text {
color: #f42434;
font-weight:bold;
}
</style>

2020.igem.org/Template:TeamName/JS

<script>
$(document).ready(function() {

$("#alert").addClass("red_text");

});
</script>

Then, use MediaWiki's syntax to include the template on another page, outside the <html> tags:

2020.igem.org/Team:TeamName/Project

{{TeamName/CSS}}
<html>
<h2> Our Project </h2>
<p> Beginning of the page about our project.</p>
</html>
{{TeamName/JS}}

Note

If you make a template containing Javascript, you cannot use the && operator. When the page is included via the {{ ... }} syntax, any & will be encoded to &amp;. For short scripts, you can get around this problem by using nested if statements instead of &&. However, you should use <script> tags to include any substantial scripts.

Using <link> and <script> tags to include CSS and Javascript

To use this method, first, create a page in your team name space. In this example we will use: "Template:TeamName/CSS" for style and "Template:TeamName/JS" for code. Below are examples of css styling and javascript code.

2020.igem.org/Template:TeamName/CSS

/*Make text red and bold*/
.red_text {
color: #f42434;
font-weight:bold;
}

2020.igem.org/Template:TeamName/JS

$(document).ready(function() {

$("#alert").addClass("red_text");

});

Make sure to enter your enter your CSS or Javascript code without any surrounding <html> tags.

This method for calling code is also used on the iGEM websites, for example in: 2020.igem.org/Sitemap. The style can be found in: HQ:Sitemap.css and the javascript is in: HQ:Sitemap.js

Once the pages are ready, include these pages using <link> tags for CSS, and <script> tags for Javascript:

<html>

<link rel="stylesheet" type="text/css" href="https://2020.igem.org/wiki/index.php?title=Template:TeamName/CSS&action=raw&ctype=text/css" />

<script type="text/javascript" src="https://2020.igem.org/wiki/index.php?title=Template:TeamName/JS&action=raw&ctype=text/javascript"</script>

</html>

Strictly speaking, this is an incorrect use of the <link> tag. But as stated previously, using HTML on MediaWiki will already result in not-quite-compliant pages that still render correctly in most browsers. It is not really worthwhile to worry about this.

How to make "inline" CSS and Javascript

You can include CSS or Javascript within <style> or <script> tags in your HTML. It is customary to put CSS styling at the beginning of your page (before the main content), and Javascript code at the beginning or the end.

Note

If you use inline Javascript, you cannot use the && operator. When the page is rendered, any & will be encoded to &amp;. For short scripts, you can get around this problem by using nested if statements instead of &&. However, you should use <script> tags to include any substantial scripts.

<html>
<style type="text/css">
/*Make text red and bold*/
.red_text {
color: #f42434;
font-weight:bold;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#alert").addClass("red_text");
});
</script>
</html>