Cascading Style Sheets
======================
This part of the tutorial will show you how to add in Cascading Style Sheets (CSS) to your Django project/application. It will also go through a few ways to customize the CSS.
If you are not familiar with CSS then visit the W3C website and go through their introduction to CSS tutorial: http://www.w3.org/Style/Examples/011/firstcss.en.html
For further examples, check out: http://www.w3schools.com/css/css_examples.asp
Adding a CSS to Rango
---------------------
To get started you will need to create a css file (called *basic.css*), and reference the css file in your *rango/base.html*.
First create a *css* folder in the *static* folder, and then create another folder in the *css* folder called *basic*. Create a file called *basic.css* in this folder. By putting the css files in the static files folder they will be accessible in the templates via *{{ STATIC_URL}}/css/basic/basic.css*
To the *rango/base.html* template add in the following HTML in the HEADER section:
::
...
Now that the style sheet is referenced you'll be able to add some CSS to control the presentation and layout of the application.
Some Basic CSS Examples
-----------------------
In *basic.css* you can add in and try out the following CSS examples. The basis of CSS is the select and apply pattern.
Changing Background Color
.........................
In the following example, *body* is the selector (meaning that all elements in the html with the body tag will be selected). Then with the curly braces are the actions to be applied, in this case the *background-color* is set to turn green.
::
body
{
background-color: #00FF33;
}
See
http://www.w3schools.com/cssref/css_colors.asp for a color reference, and pick a color that you like.
And for more examples on how to manipulate the background using CSS see http://www.w3schools.com/css/css_examples.asp
Changing Text Color
...................
Update the *basic.css* as follows:
::
body {
background-color: #00FF33;
color:#FFFFFF;
}
p {
color:#000000;
}
Now all the text within body (that is not a link) has been turned white.
However, since there are no
tags, no text has been turned black. Add
tags around the text "Rango Says:" in *rango/index.html*.
Now you will notice that the text "Rango Says:" is now rendered in black, even though the text is within the
tags. This is because CSS tries to apply the most specific application, so in this case, the
At this point it is good idea to make a copy of base.html, to say base-old.html, and then replace the template/html code in base.html with the following line:
::
{% extends 'rango/base-direction.html' %}
Now all your pages that inherit from base.html, will inherit from base-direction.html. This saves you from having to update every template file to inherit from base-direction.html.
Have a look at your application. Wow, impressive, huh?
.. figure:: rango-css-3.png
:align: center
Using a CSS Toolkit
-------------------
The Free CSS templates are great - but most of them are fixed width. So it is better to use a CSS toolkit that is more responsive. A number of CSS Toolkits are available which also include Javascript/JQuery components to provide additional functionality. For example:
* Twitter's Bootstrap CSS Toolkit, http://twitter.github.com/bootstrap/
* Zurb's Foundation CSS Toolkit, http://foundation.zurb.com
* See http://speckyboy.com/2012/08/21/15-more-responsive-css-frameworks-boilerplates-worth-considering/ for many more response CSS frameworks that you can use.
Using Twitter's Bootstrap CSS Toolkit
......................................
Below is an example of how the Bootstrap toolkit can be included into your Django project.
First download the Bootstrap toolkit and save it into your static folder.
Create a template called, base-bootstrap.html in templates/rango. THe following code is based on the Fluid example http://twitter.github.com/bootstrap/getting-started.html#examples:
::
#
Rango
It takes a bit of time to wrangle with the particular CSS Toolkit that you use - but once you've got it up and running it is usually fairly straightforward to customize.
In *rango/index.html* update the sidebar to include the Bootstrap specific classes when presenting categories:
::
{% block sidebar %}
{% endblock %}
Looks pretty neat, eh!?
.. figure:: rango-css-4.png
:align: center
Now reduce the size of the web browser and see how it responds as it gets smaller and smaller, until it stops at the size of a mobile phone screen display.
Also, note that most CSS Toolkits use a grid based layout to position units. The bootstrap toolkit uses classes call span to control the size of elements with the grid layout, see http://twitter.github.com/bootstrap/scaffolding.html#gridSystem for more details.
Exercises
---------
* Since the Free CSS templates follow the same format it is pretty easy to vary the style. Download another Free CSS template, for example, illustrative, and move the folder into the static/css folder. Now, create a new base template called, base-illustrative.html. Use the index.html provided in the illustrative download as a guide to creating base-illustrative.html for your application.