Stylesheets: Difference between revisions
No edit summary |
mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
= How do I style my web page = |
|||
A style sheet, also referred to as a CSS (Cascading Style Sheet), is simply a way of styling your webpage – for example, colouring, font, spacing and positioning. |
|||
⚫ | |||
⚫ | |||
A style sheet, also referred to as Cascading Style Sheet (CSS), is simply a way of styling your webpage – styling is defining appearance and can be quite complex as the standard for CSS (CSS version 3 was widely adopted in 2014, but that is really just a set of agreed proposals, not a single standard; CSS4 is the version assigned to those proposed standards that started being announced in 2014) allows quite a lot of different descriptors as well as a range of attributes and all their possible values. This article focusses on the simpler attributes; for example, colouring, font, spacing and positioning. |
|||
⚫ | Originally, formatting was placed directly in the HTML of a web-page, but current standard compliance forbids that. Mixing structure and styling in one document becomes unmanageable as your web-site grows. If (for example) you decide to change the colour of your second-level headings you could be editing hundreds of entries throughout your site if each of those second-level headings had its own styling instruction. Even just a few pages is tedious. |
||
The move now is to the use of style sheets and direct formatting in the HTML is frowned up and should be used as little as possible. |
|||
One huge advantage to the style sheet is that making a change to one line in the style will affect your whole site. |
One huge advantage to the style sheet is that making a change to one line in the style will affect your whole site. |
||
Line 9: | Line 10: | ||
There are volumes of information available on CSS writing, both in your local bookshop or library and on the web. However, below is a basic introduction to understanding and writing your own CSS to control your website. |
There are volumes of information available on CSS writing, both in your local bookshop or library and on the web. However, below is a basic introduction to understanding and writing your own CSS to control your website. |
||
⚫ | |||
*Look up the [http://www.w3schools.com/css/ w3school] for an easy follow on to what is described below. |
|||
*Colouring - I have used actual words like 'red' and 'black' for colours here, and modern browsers understand quite a number of the colours defined in similar words in CSS4. A far greater range of colours can be specified using numbers as explained in this [http://en.wikipedia.org/wiki/Web_colors article], and one way of specifying colours using numbers even allow you to include some transparency, allowing a HTML element to be superimposed on another without totally hiding the lower one. |
|||
⚫ | |||
⚫ | |||
(limited HTML knowledge is assumed but we will try to explain as we go) |
(limited HTML knowledge is assumed but we will try to explain as we go) |
||
HTML is the language used to describe how your page will be laid out in the browser. All browsers use HTML as their core language – other technology is used to enhance a web page further (examples: PHP, Java, |
HTML is the language used to describe how your page will be laid out in the browser, the official recommended standard that applied from 2014 is called HTML5, but in 2014 there are already proposals for the next version. All browsers use HTML as their core language – other technology is used to enhance a web page further (examples of script languages: PHP Hypertext Pre-processor, Java, JavaScript) however it is not in the scope of this document to discuss these. |
||
What you need to be clear on is that HTML is a plain text file and everything is enclosed within tags. A tag is contained within < and >. An example of a set of tags would be <nowiki><h1></nowiki> and <nowiki></h1></nowiki>. Note the second of the two tags differs as it has a / symbol. The first tag denotes the start of your content and the second the end. Every tag has a corresponding end tag however with a few exceptions where there is one tag. More on that later. |
What you need to be clear on is that HTML is a plain text file and everything is enclosed within tags. A tag is contained within < and >. An example of a set of tags would be <nowiki><h1></nowiki> and <nowiki></h1></nowiki>. Note the second of the two tags differs as it has a / symbol. The first tag denotes the start of your content and the second the end. Every tag has a corresponding end tag however with a few exceptions where there is one tag. More on that later. |
||
Here is the basic skeleton HTML requirement when writing a web page: |
|||
<pre> |
<pre> |
||
<html> |
<html> |
||
Line 27: | Line 33: | ||
</html> |
</html> |
||
</pre> |
</pre> |
||
A fuller list of what is normally included in a HTML file can be seen [[Customised_templates#HTML_5_-_a_very_quick_guide_to_structure|here]]. Follow up that cross-reference to understand the various approaches for producing customised web pages. |
|||
Notice |
Notice that most HTML elements require both an opening and closing tag (so they can enclose other HTML structure) each tag prefixed with a "<" and suffixed with a ">", and the tags must be closed in the reverse order to when they were opened to preserve that enclosing structure. Some people are lazy and do not close all tags, however this can cause unpredictable results and some browsers are more forgiving than others. If a particular HTML element cannot have something else within it, it will start with "<" and end with ">" (or " />", note the mandatory space before the slash) and not have a separate end tag. |
||
Note indenting the text here is only to ensure the tags are matched and because short lines make reading easier – HTML ignores any such spacing but it can be useful for you when reading the code. I could have written that whole structure as |
|||
<html><head></head><body></body></html> |
<html><head></head><body></body></html> |
||
and it would give the same results. |
and it would give the same results. |
||
Line 40: | Line 47: | ||
Let’s dive straight in a look at some examples; always the best way to learn! (I will use actual extracts for the Cumulus standard web pages). |
Let’s dive straight in a look at some examples; always the best way to learn! (I will use actual extracts for the Cumulus standard web pages). |
||
All the examples are fully functional, simple HTML so you could copy and paste the text into notepad, save as a file with the .html extension and the double click and view in a browser. |
All the examples are fully functional, simple HTML so you could copy and paste the text into notepad, save as a file with the ''.html'' extension and the double click and view in a browser. [Note that standard Cumulus pages use '.htm' as earlier (now obsolete) versions of computer operating systems could only cope with 8 characters before the "." and 3 characters after it]. |
||
=Some simple HTML= |
=== Some simple HTML === |
||
<pre> <html> |
<pre> <html> |
||
Line 51: | Line 58: | ||
<h1>Welcome to my weather station. </h1> |
<h1>Welcome to my weather station. </h1> |
||
<p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The |
<p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The |
||
meteorological day |
meteorological day used at this station ends at 9 am.</p> |
||
<p>All data offered by this website and station is from an amateur weather observation station and should be |
<p>All data offered by this website and station is from an amateur weather observation station and should be |
||
used and accepted at face-value and compared alongside official sources.</p> |
used and accepted at face-value and not expected to be accurate when compared alongside official sources.</p> |
||
</body> |
</body> |
||
</html> |
</html> |
||
Line 61: | Line 68: | ||
We use two sets of formatting tags here…<nowiki><h1></nowiki> and <nowiki><p></nowiki> (and their closing tags too!) |
We use two sets of formatting tags here…<nowiki><h1></nowiki> and <nowiki><p></nowiki> (and their closing tags too!) |
||
<nowiki><h1></nowiki> is a predefined tag in HTML and stands for ‘heading1’. <nowiki><p></nowiki> is a ‘paragraph’. Although we will not worry too much about HTML conventions here, there are conventions that lower case is preferred, that the first heading to appear within the body of HTML should be a first-level one (h1), that at least one heading should precede any paragraphs (p). Depending on your browser, it will have some default styling for each HTML element, for example it may say a first level heading appears with blank space below it, is in a larger font, and is made bold, while a paragraph appears in the standard size font but has some blank space above and below it. |
|||
<nowiki><h1></nowiki> is a predefined tag in HTML and stands for ‘heading1’. <nowiki><p></nowiki> is a ‘paragraph’ |
|||
As described elsewhere in the Wiki, your first attempts to write web pages will probably involve creating customised [[Customised_templates#Changing_the_Standard_Templates|Cumulus Templates]] containing web tags that Cumulus software recognises and [[Customised_templates#What_is_meant_by_.27Cumulus_processes_templates.27|processes]] by creating a web page with values determined by those web tags. To keep the description simple on this Wiki page, all the HTML quoted here is typical of that generated by Cumulus processing, some values have already been inserted where Cumulus web tags would be used in a template. |
|||
=Adding some style= |
== Adding some style == |
||
Basically, each style instruction takes the following structure: |
|||
I want to make heading red rather than black. |
|||
''Descriptor'' { attribute: value; ... } |
|||
⚫ | |||
So the instruction starts with a descriptor defining where, and when, it applies; then enclosed in curly brackets are any number of parameters. Each parameter consists of what styling is being applied before a colon and the associated value (or, space separated, values) for that styling before a semicolon. |
|||
In this first example we want to colour the text of a heading red, rather than whatever colour the browser uses as its default (or the browser inherits from what we have previously defined for an element that encloses the heading). |
|||
⚫ | |||
<pre> |
<pre> |
||
<html> |
<html> |
||
Line 87: | Line 101: | ||
Ok, some new tags here. |
Ok, some new tags here. |
||
The <nowiki><style></nowiki> and <nowiki></style></nowiki> tags in the <nowiki><head></nowiki> section; also note that the opening style tag has some additional information contained within it. |
The <nowiki><style></nowiki> and <nowiki></style></nowiki> tags in the <nowiki><head></nowiki> section; also note that the opening ''style tag'' has some additional information contained within it. The tag tells the browser that what follows next, until the style tag closes, is CSS (style sheet) instructions and the type attribute confirms it is CSS written in plain text. |
||
⚫ | Within this set of tags we have |
||
⚫ | Within this set of tags we have a descriptor that specifies a HTML tag, if the browser finds an <nowiki><h1></nowiki> tag it is to modify the default styling, in this case to make the text colour red. Note the America spelling of ‘color’. Also note that within the style tags we refer to the <nowiki><h1></nowiki> tag without it’s < and > (this is important!). All styling for each tag is enclosed within { and }. As there was only one modifier, the browser accepts that the '}' terminates the value and the semi-colon can be missed off. |
||
If we want to make it red and bold we can combine effects and separate with a semi-colon. |
|||
If we want to make it red and bold we can list both effects within the curly brackets after quoting a descriptor just once; but then we must terminate our values with a semi-colon as in the next example: |
|||
Example |
|||
<pre> |
<pre> |
||
<style type="text/css" media="all"> |
<style type="text/css" media="all"> |
||
h1 { |
h1 { |
||
color: red; |
|||
font-weight: bold; |
|||
} |
|||
</style> |
</style> |
||
</pre> |
</pre> |
||
This could also be combined in one line (and it then more efficient as the browser has fewer characters to read), but using several lines makes it more readable for humans (although where you choose to start a new line, and how much indent you apply, is not defined by any universal standards): |
|||
This could also be combined in one line: |
|||
h1 { color: red; font-weight: bold;} |
h1 { color: red; font-weight: bold;} |
||
=Default styling= |
== Default styling == |
||
Let’s set some defaults |
Let’s set some defaults |
||
Using the predefined <nowiki><body></nowiki> tag we will set a default font of arial and the colour blue. |
Using the predefined <nowiki><body></nowiki> tag we will set a default font of arial, with a couple of alternatives, and the text colour of blue. |
||
<pre> |
<pre> |
||
<html> |
<html> |
||
Line 116: | Line 130: | ||
<style type="text/css" media="all"> |
<style type="text/css" media="all"> |
||
body { color: blue; |
body { color: blue; |
||
font-family: |
font-family: arial "times new roman" courier; |
||
} |
} |
||
h1 { color: red; |
h1 { color: red; |
||
Line 131: | Line 145: | ||
</html> |
</html> |
||
</pre> |
</pre> |
||
Notice the ‘font-family’ has a list of three possible fonts. Typically you would list a number of fonts; that way if the user does not have the first font installed on their computer, it will try the second, and then third, etc. Of course, most people have Arial so we are pretty safe. |
Notice the ‘font-family’ has a list of three possible fonts. Those font names that include spaces have to be enclosed in double quotes, those that are a single word do not use quotes, and spaces separate multiple values. Typically you would list a number of fonts; that way if the user does not have the first font installed on their computer, it will try the second, and then third, etc. Of course, most people have Arial so we are pretty safe. Also note that these 3 fonts are seldom used together, |
||
*''arial'' is a '''sans serif''' font, it does not have small strokes at approximate right angles on the end of the stroke that define the letters; |
|||
*''times new roman'' is a '''serif''' font, there are small strokes on the end of the strokes that define the letters, and this font recognises that different letters use different widths (two words with the same number of letters will have proportionally different widths depending on the actual letters included) and the spacing between letters can vary depending on whether the adjacent strokes are curved or vertical; |
|||
*''courier'' is a ''' monospaced''' font, all letters are allocated the same width (two words with the same number of letters will occupy same width regardless of the actual letters included). |
|||
Normally the alternative fonts will be the same in terms of whether they use 'serif' or not, and whether they use proportional or mono spacing. |
|||
Now every piece of text will be blue and in Arial, unless you decide to style a section in another format. In our example the <nowiki><h1></nowiki> will still be red and bold, but will retain the Arial font |
Now every piece of text will be blue and in Arial, unless you decide to style a section in another format. In our example the <nowiki><h1></nowiki> will still be red and bold, but will retain the Arial font |
||
⚫ | |||
⚫ | |||
Let’s now assume we want the second paragraph in our example to be in italics. How do we achieve this? Well, we know we can restyle the <nowiki><p></nowiki> tag, just as we did with the <nowiki><h1></nowiki> tag however that would restyle both paragraphs. |
Let’s now assume we want the second paragraph in our example to be in italics. How do we achieve this? Well, we know we can restyle the <nowiki><p></nowiki> tag, just as we did with the <nowiki><h1></nowiki> tag however that would restyle both paragraphs. |
||
Line 168: | Line 185: | ||
<nowiki><p class="italics-para"></nowiki> |
<nowiki><p class="italics-para"></nowiki> |
||
We are now telling HTML that this is a paragraph AND it has a class of italics-para. Note that the dot is NOT used when using the class, only when |
We are now telling HTML that this is a paragraph AND it has a class of italics-para. Note that the dot is NOT used when using the class in HTML, only when using it as a descriptor in the style sheet above. |
||
⚫ | So our second paragraph is now formatted with italics (as per our class declaration), however it also has a font of Arial and colour blue even although we did not tell it what font or colour to use. This is where the “C” in CSS comes in. It stands for “Cascading” and simply means that all style sheet rules flow (or cascade) into others with same descriptors or HTML structure implicitly included within that inheritance, if we have not defined another style specifically. |
||
⚫ | So our second paragraph is now formatted with italics (as per our class |
||
By not telling our style what to use it simply assume that it would take the defaults from the <nowiki><body></nowiki> tag as the <nowiki><p></nowiki> tag is contained within the <nowiki><body></nowiki> and <nowiki></body></nowiki>. We could set the font and colour in the “italics-para” class if we wish but as they are the same this is wasteful. |
By not telling our style what to use it simply assume that it would take the defaults from the <nowiki><body></nowiki> tag as the <nowiki><p></nowiki> tag is contained within the <nowiki><body></nowiki> and <nowiki></body></nowiki>. We could set the font and colour in the “italics-para” class if we wish but as they are the same this is wasteful. |
||
Classes are ‘the big thing’ in style sheets and you will used them almost exclusively. Your style sheet will be full of classes. |
Classes are ‘the big thing’ in style sheets and you will used them almost exclusively. Your style sheet will be full of classes. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Let’s introduce a table, used in the Cumulus webpage to present some data. Tables, like everything in HTML have opening and closing tags. We will create a <nowiki><table></nowiki>, create a table body, create a row <nowiki><tr></nowiki>, some headings <nowiki><th></nowiki>, data <nowiki><td></nowiki> and close the tags as required! |
Let’s introduce a table, used in the Cumulus webpage to present some data. Tables, like everything in HTML have opening and closing tags. We will create a <nowiki><table></nowiki>, create a table body, create a row <nowiki><tr></nowiki>, some headings <nowiki><th></nowiki>, data <nowiki><td></nowiki> and close the tags as required! |
||
Line 187: | Line 204: | ||
<tbody> |
<tbody> |
||
<tr> |
<tr> |
||
<th>Dawn:</th> |
<th>Last Dawn:</th> |
||
<td>07:00</td> |
<td>07:00</td> |
||
<th>Sunrise:</th> |
<th>Last Sunrise:</th> |
||
<td>07:25</td> |
<td>07:25</td> |
||
<th>Moonrise:</th> |
<th>Moonrise:</th> |
||
Line 195: | Line 212: | ||
</tr> |
</tr> |
||
<tr> |
<tr> |
||
<th>Dusk:</th> |
<th>Next Dusk:</th> |
||
<td>19:00</td> |
<td>19:00</td> |
||
<th>Sunset:</th> |
<th>Next Sunset:</th> |
||
<td>18:35</td> |
<td>18:35</td> |
||
<th>Moonset:</th> |
<th>Moonset:</th> |
||
<td> |
<td>---</td> |
||
</tr> |
</tr> |
||
</tbody> |
</tbody> |
||
Line 270: | Line 287: | ||
<table class="summary-table"> |
<table class="summary-table"> |
||
<tbody> |
<tbody> |
||
⚫ | |||
<tr> |
|||
⚫ | |||
<td>07:00</td> |
<td>07:00</td> |
||
<th>Sunrise:</th> |
<th>Last Sunrise:</th> |
||
<td>07:25</td> |
<td>07:25</td> |
||
<th>Moonrise:</th> |
<th>Moonrise:</th> |
||
Line 279: | Line 295: | ||
</tr> |
</tr> |
||
<tr> |
<tr> |
||
<th>Dusk:</th> |
<th>Next Dusk:</th> |
||
<td>19:00</td> |
<td>19:00</td> |
||
<th>Sunset:</th> |
<th>Next Sunset:</th> |
||
<td>18:35</td> |
<td>18:35</td> |
||
<th>Moonset:</th> |
<th>Moonset:</th> |
||
<td> |
<td>---</td> |
||
</tr> |
</tr> |
||
</tbody> |
</tbody> |
||
Line 298: | Line 314: | ||
As you can see our style sheet is getting longer, and it will continue to grow. It is far better to have your style sheet in its own file, and refer to it within the HTML. |
As you can see our style sheet is getting longer, and it will continue to grow. It is far better to have your style sheet in its own file, and refer to it within the HTML. |
||
Copy all the Style sheet |
'''Copy''' all the Style sheet instructions between the <style> tags from your latest example, but NOT the <style> tags! '''Paste''' it into notepad and save as “mystyles.css”. Here’s the code |
||
<pre> |
<pre> |
||
body { color: blue; |
body { color: blue; |
||
font-family: "arial","times new roman","courier"; |
font-family: "arial","times new roman","courier"; |
||
} |
|||
h1 { color: red; |
h1 { color: red; |
||
font-weight: bold; |
font-weight: bold; |
||
} |
|||
.italics-para |
.italics-para |
||
Line 312: | Line 330: | ||
border-width: 1px; |
border-width: 1px; |
||
border-color: blue; |
border-color: blue; |
||
color: black; |
color: black; |
||
} |
|||
.summary-table th { color: black; |
.summary-table th { color: black; |
||
Line 320: | Line 339: | ||
</pre> |
</pre> |
||
Now delete the style tags, and all that |
Now return to your HTML, delete the style open and close tags, and all that was within them, and replace with… |
||
<link href="mystyles.css" rel="stylesheet" type="text/css"/> |
<link href="mystyles.css" rel="stylesheet" type="text/css" /> |
||
This tells your browser to read the file mystyles.css as a style sheet and use it as necessary. |
This tells your browser to read the file mystyles.css as a style sheet and use it as necessary. |
||
Remember we mentioned that some |
Remember we mentioned that some HTML elements only have one tag, nothing can be contained within them so they do not have a separate closing tag, well this is one such example, although it is closed in a round about way, notice the very subtle ''space then /'' just before the '>' at the end of the line. |
||
<link href="mystyles.css" rel="stylesheet" type="text/css"></link> |
|||
but most people drop the full closing tag. Do NOT assume that works everywhere – always close your tags unless you know of an exception, like this! |
|||
Line 370: | Line 385: | ||
</html> |
</html> |
||
</pre> |
</pre> |
||
=All change= |
=All change= |
||
Just to prove how quickly you can change the format of a page using style sheets, replace your mystyles.css with |
Just to prove how quickly you can change the format of a page using style sheets, replace the instructions you have in your ''mystyles.css'' with the instructions below and '''reload''' your page. See how the look changes. You did not touch any HTML only changed the formatting in the CSS.!! |
||
<pre> |
<pre> |
||
Line 410: | Line 424: | ||
*check your spelling -- CSS is Americanised so we have color not colour and center not centre! |
*check your spelling -- CSS is Americanised so we have color not colour and center not centre! |
||
*Style sheets are supported in all modern browsers, however with varying degrees of compliance. As a general rule the latest version of |
*Style sheets are supported in all modern browsers, however with varying degrees of compliance. As a general rule the latest version of Chrome, Firefox, Opera, and Safari, are all good at implementing the latest CSS3 recommendations for a standard. Usually either Chrome or Firefox will be quick to pick up CSS4 recommendations, as these browsers are widely used, most developers will consider using newer CSS features. |
||
*When developing a site, test in a variety of browsers, remembering almost anyone may view your site. Anyone using IE may miss out on some features of your design that Microsoft's code ignores, but then those users will not know what they are missing. Those with the more modern browsers can have their experience of visiting your web site enhanced by you using a wider range of the latest CSS features. |
|||
*Internet Explorer can be quite fussy, and those designers writing for where that browser is used exclusively will include separate HTML instructions on what CSS to load for each IE version, making it complicated to understand how the CSS determines the look. The relevant instructions in the HTML are specific to Microsoft, they look like conditional HTML comments, and are treated as comments by non-Microsoft agents. |
|||
*Early versions of Internet Explorer are bad for a variety of reasons. If you are on IE version 5 or 6 you should change to using a modern browser now. If a site asks what browser is being used, IE may even report it is Firefox, but despite that generally older IE will either expect different CSS syntax, or just ignore any standard CSS, so it will not display a page as Firefox would. You have to use the Microsoft conditional comments approach to detect IE and switch CSS accordingly. |
|||
*When developing your site, remember it may be viewed on a range of different devices including PCs, tablets, mobiles; try whatever devices you have, and experiment with any options your browser offers for different widths, default font-sizes and different screen shapes/resolutions. |
|||
*Finally, it is often possible to detect media (although such detections may not always be successful), and apply a different CSS file in each case. For example the page appears shaped best for viewing on a monitor with one CSS file, shaped best for viewing on a mobile with a second file, and shaped best for printing on a sheet of paper with the final CSS file. This is however too complex to describe any further here. |
|||
*Early versions of Internet Explorer are bad for a variety of reasons. If you are on version 5 or 6 you should stop using the internet now, and stick to books!!! |
|||
Line 420: | Line 439: | ||
⚫ | |||
*The best place for all your CSS tags is the [http://www.w3schools.com/css/ w3school] |
|||
*Colouring - I have used actual words like 'red' and 'black' for colours here, however in reality you will want a far greater range. Colour is often written as numbers and explained in this [http://en.wikipedia.org/wiki/Web_colors article] |
|||
=The next article to read ……= |
|||
⚫ | |||
=In the next article……= |
|||
⚫ | |||
Originally written - [[User:Daj|Daj]] 12:25, 25 August 2009 (UTC) |
|||
Updated - [[User:Sfws|Sfws]] ([[User talk:Sfws|talk]]) 14:45, 22 April 2015 (PDT) |
|||
⚫ |
Latest revision as of 21:50, 22 April 2015
How do I style my web page
A style sheet, also referred to as Cascading Style Sheet (CSS), is simply a way of styling your webpage – styling is defining appearance and can be quite complex as the standard for CSS (CSS version 3 was widely adopted in 2014, but that is really just a set of agreed proposals, not a single standard; CSS4 is the version assigned to those proposed standards that started being announced in 2014) allows quite a lot of different descriptors as well as a range of attributes and all their possible values. This article focusses on the simpler attributes; for example, colouring, font, spacing and positioning.
Originally, formatting was placed directly in the HTML of a web-page, but current standard compliance forbids that. Mixing structure and styling in one document becomes unmanageable as your web-site grows. If (for example) you decide to change the colour of your second-level headings you could be editing hundreds of entries throughout your site if each of those second-level headings had its own styling instruction. Even just a few pages is tedious.
One huge advantage to the style sheet is that making a change to one line in the style will affect your whole site.
There are volumes of information available on CSS writing, both in your local bookshop or library and on the web. However, below is a basic introduction to understanding and writing your own CSS to control your website.
Some suggested reading
- Look up the w3school for an easy follow on to what is described below.
- Colouring - I have used actual words like 'red' and 'black' for colours here, and modern browsers understand quite a number of the colours defined in similar words in CSS4. A far greater range of colours can be specified using numbers as explained in this article, and one way of specifying colours using numbers even allow you to include some transparency, allowing a HTML element to be superimposed on another without totally hiding the lower one.
Some basics
(limited HTML knowledge is assumed but we will try to explain as we go)
HTML is the language used to describe how your page will be laid out in the browser, the official recommended standard that applied from 2014 is called HTML5, but in 2014 there are already proposals for the next version. All browsers use HTML as their core language – other technology is used to enhance a web page further (examples of script languages: PHP Hypertext Pre-processor, Java, JavaScript) however it is not in the scope of this document to discuss these.
What you need to be clear on is that HTML is a plain text file and everything is enclosed within tags. A tag is contained within < and >. An example of a set of tags would be <h1> and </h1>. Note the second of the two tags differs as it has a / symbol. The first tag denotes the start of your content and the second the end. Every tag has a corresponding end tag however with a few exceptions where there is one tag. More on that later.
Here is the basic skeleton HTML requirement when writing a web page:
<html> <head> </head> <body> </body> </html>
A fuller list of what is normally included in a HTML file can be seen here. Follow up that cross-reference to understand the various approaches for producing customised web pages.
Notice that most HTML elements require both an opening and closing tag (so they can enclose other HTML structure) each tag prefixed with a "<" and suffixed with a ">", and the tags must be closed in the reverse order to when they were opened to preserve that enclosing structure. Some people are lazy and do not close all tags, however this can cause unpredictable results and some browsers are more forgiving than others. If a particular HTML element cannot have something else within it, it will start with "<" and end with ">" (or " />", note the mandatory space before the slash) and not have a separate end tag.
Note indenting the text here is only to ensure the tags are matched and because short lines make reading easier – HTML ignores any such spacing but it can be useful for you when reading the code. I could have written that whole structure as
<html><head></head><body></body></html>
and it would give the same results.
Most of your work will be done between the <body> and </body> tags – this is the section that controls what is shown in the browser.
When editing an HTML file always use a text editor, not a word processor. Notepad in Windows is fine, however I would heartily recommend you download Notepad++ if using Windows or TextWrangler if using an Apple Mac. Both are free and are excellent for HTML writing as they understand the structure of the HTML page.
Let’s dive straight in a look at some examples; always the best way to learn! (I will use actual extracts for the Cumulus standard web pages).
All the examples are fully functional, simple HTML so you could copy and paste the text into notepad, save as a file with the .html extension and the double click and view in a browser. [Note that standard Cumulus pages use '.htm' as earlier (now obsolete) versions of computer operating systems could only cope with 8 characters before the "." and 3 characters after it].
Some simple HTML
<html> <head> </head> <body> <h1>Welcome to my weather station. </h1> <p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The meteorological day used at this station ends at 9 am.</p> <p>All data offered by this website and station is from an amateur weather observation station and should be used and accepted at face-value and not expected to be accurate when compared alongside official sources.</p> </body> </html>
Go ahead and try this if you wish.
We use two sets of formatting tags here…<h1> and <p> (and their closing tags too!)
<h1> is a predefined tag in HTML and stands for ‘heading1’. <p> is a ‘paragraph’. Although we will not worry too much about HTML conventions here, there are conventions that lower case is preferred, that the first heading to appear within the body of HTML should be a first-level one (h1), that at least one heading should precede any paragraphs (p). Depending on your browser, it will have some default styling for each HTML element, for example it may say a first level heading appears with blank space below it, is in a larger font, and is made bold, while a paragraph appears in the standard size font but has some blank space above and below it.
As described elsewhere in the Wiki, your first attempts to write web pages will probably involve creating customised Cumulus Templates containing web tags that Cumulus software recognises and processes by creating a web page with values determined by those web tags. To keep the description simple on this Wiki page, all the HTML quoted here is typical of that generated by Cumulus processing, some values have already been inserted where Cumulus web tags would be used in a template.
Adding some style
Basically, each style instruction takes the following structure:
Descriptor { attribute: value; ... }
So the instruction starts with a descriptor defining where, and when, it applies; then enclosed in curly brackets are any number of parameters. Each parameter consists of what styling is being applied before a colon and the associated value (or, space separated, values) for that styling before a semicolon.
In this first example we want to colour the text of a heading red, rather than whatever colour the browser uses as its default (or the browser inherits from what we have previously defined for an element that encloses the heading).
This is where we start using style sheets. For the moment we will put this directly into the HTML for simplicity but later we will move the styling into a separate CSS file.
<html> <head> <style type="text/css" media="all"> h1 { color: red } </style> </head> <body> <h1>Welcome to my weather station. </h1> <p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The meteorological day used at this station ends at midnight</p> <p>All data offered by this website and station is from an amateur weather observation station and should be used and accepted at face-value and compared alongside official sources.</p> </body> </html>
Ok, some new tags here.
The <style> and </style> tags in the <head> section; also note that the opening style tag has some additional information contained within it. The tag tells the browser that what follows next, until the style tag closes, is CSS (style sheet) instructions and the type attribute confirms it is CSS written in plain text.
Within this set of tags we have a descriptor that specifies a HTML tag, if the browser finds an <h1> tag it is to modify the default styling, in this case to make the text colour red. Note the America spelling of ‘color’. Also note that within the style tags we refer to the <h1> tag without it’s < and > (this is important!). All styling for each tag is enclosed within { and }. As there was only one modifier, the browser accepts that the '}' terminates the value and the semi-colon can be missed off.
If we want to make it red and bold we can list both effects within the curly brackets after quoting a descriptor just once; but then we must terminate our values with a semi-colon as in the next example:
<style type="text/css" media="all"> h1 { color: red; font-weight: bold; } </style>
This could also be combined in one line (and it then more efficient as the browser has fewer characters to read), but using several lines makes it more readable for humans (although where you choose to start a new line, and how much indent you apply, is not defined by any universal standards):
h1 { color: red; font-weight: bold;}
Default styling
Let’s set some defaults
Using the predefined <body> tag we will set a default font of arial, with a couple of alternatives, and the text colour of blue.
<html> <head> <style type="text/css" media="all"> body { color: blue; font-family: arial "times new roman" courier; } h1 { color: red; font-weight: bold; } </style> </head> <body> <h1>Welcome to my weather station. </h1> <p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The meteorological day used at this station ends at midnight</p> <p>All data offered by this website and station is from an amateur weather observation station and should be used and accepted at face-value and compared alongside official sources.</p> </body> </html>
Notice the ‘font-family’ has a list of three possible fonts. Those font names that include spaces have to be enclosed in double quotes, those that are a single word do not use quotes, and spaces separate multiple values. Typically you would list a number of fonts; that way if the user does not have the first font installed on their computer, it will try the second, and then third, etc. Of course, most people have Arial so we are pretty safe. Also note that these 3 fonts are seldom used together,
- arial is a sans serif font, it does not have small strokes at approximate right angles on the end of the stroke that define the letters;
- times new roman is a serif font, there are small strokes on the end of the strokes that define the letters, and this font recognises that different letters use different widths (two words with the same number of letters will have proportionally different widths depending on the actual letters included) and the spacing between letters can vary depending on whether the adjacent strokes are curved or vertical;
- courier is a monospaced font, all letters are allocated the same width (two words with the same number of letters will occupy same width regardless of the actual letters included).
Normally the alternative fonts will be the same in terms of whether they use 'serif' or not, and whether they use proportional or mono spacing.
Now every piece of text will be blue and in Arial, unless you decide to style a section in another format. In our example the <h1> will still be red and bold, but will retain the Arial font
It's all in the class
Let’s now assume we want the second paragraph in our example to be in italics. How do we achieve this? Well, we know we can restyle the <p> tag, just as we did with the <h1> tag however that would restyle both paragraphs. To achieve this we write our own style tags, referred to as a class. Here’s the code….
<html> <head> <style type="text/css" media="all"> body { color: blue; font-family: "arial","times new roman","courier"; } h1 { color: red; font-weight: bold;} .italics-para { font-style: italic; } </style> </head> <body> <h1>Welcome to my weather station. </h1> <p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The meteorological day used at this station ends at midnight</p> <p class="italics-para">All data offered by this website and station is from an amateur weather observation station and should be used and accepted at face-value and compared alongside official sources.</p> </body> </html>
So what has changed? Firstly, look in the style section and there is a new declaration “.italics-para”. The dot tells the style sheet that we are declaring a class of our own and we are calling it italics-para. (We can call this anything we like) Now look further down the code and note the second paragraph has changed <p class="italics-para">
We are now telling HTML that this is a paragraph AND it has a class of italics-para. Note that the dot is NOT used when using the class in HTML, only when using it as a descriptor in the style sheet above.
So our second paragraph is now formatted with italics (as per our class declaration), however it also has a font of Arial and colour blue even although we did not tell it what font or colour to use. This is where the “C” in CSS comes in. It stands for “Cascading” and simply means that all style sheet rules flow (or cascade) into others with same descriptors or HTML structure implicitly included within that inheritance, if we have not defined another style specifically.
By not telling our style what to use it simply assume that it would take the defaults from the <body> tag as the <p> tag is contained within the <body> and </body>. We could set the font and colour in the “italics-para” class if we wish but as they are the same this is wasteful.
Classes are ‘the big thing’ in style sheets and you will used them almost exclusively. Your style sheet will be full of classes.
More formatting
This is a larger example of a web page being specified by HTML and CSS, but the rules are the same. We are declaring classes in our HTML and referring to them in our CSS.
Let’s introduce a table, used in the Cumulus webpage to present some data. Tables, like everything in HTML have opening and closing tags. We will create a <table>, create a table body, create a row <tr>, some headings <th>, data <td> and close the tags as required!
Firstly, just the table code in order to read it on its own
<table> <tbody> <tr> <th>Last Dawn:</th> <td>07:00</td> <th>Last Sunrise:</th> <td>07:25</td> <th>Moonrise:</th> <td>10:07</td> </tr> <tr> <th>Next Dusk:</th> <td>19:00</td> <th>Next Sunset:</th> <td>18:35</td> <th>Moonset:</th> <td>---</td> </tr> </tbody> </table>
Have a read through the HTML and you should see the table starting, it creating the table body, opening a row, adding a heading, adding some data, another head, another data, heading, data, and then close the row. Repeat another row, then close the body and the table.
Now let’s add some style to the table. We do this, as before, by creating a class and referring to it in the tag.
<table class=”summary-table”> …. …. </table>
And now the extract of the style sheet…
.summary-table { width: 75%; border: solid; border-width: 1px; border-color: blue; color: black;} .summary-table th { color: black; background: lightgrey;} .summary-table td { color: black; }
The first class declaration should be easy; we are creating a class called summary-table, it is to fill 75% of the width of the screen, has a border which is one pixel thick, the border is blue and the text is black.
Next we add a tag to the class “.summary-table th” This simply tells the browser if it finds a <th> tag (table heading) within the table with a class of .summary-table then the font will be black and the background a light grey.
Similarly in the next declaration all the <td> (data) tags will be have a black font colour (otherwise it would inherit it from the blue colour of the <body> -- remember CSS is cascading!)
And the full HTML for you to try….
<html> <head> <style type="text/css" media="all"> body { color: blue; font-family: "arial","times new roman","courier"; } h1 { color: red; font-weight: bold;} .italics-para { font-style: italic; } .summary-table { width: 75%; border: solid; border-width: 1px; border-color: blue; color: black;} .summary-table th { color: black; background: lightgrey;} .summary-table td { color: black; } </style> </head> <body> <h1>Welcome to my weather station. </h1> <p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The meteorological day used at this station ends at midnight</p> <p class="italics-para">All data offered by this website and station is from an amateur weather observation station and should be used and accepted at face-value and compared alongside official sources.</p> <table class="summary-table"> <tbody> <tr><th>Last Dawn:</th> <td>07:00</td> <th>Last Sunrise:</th> <td>07:25</td> <th>Moonrise:</th> <td>10:07</td> </tr> <tr> <th>Next Dusk:</th> <td>19:00</td> <th>Next Sunset:</th> <td>18:35</td> <th>Moonset:</th> <td>---</td> </tr> </tbody> </table> </body> </html>
Splitting the style sheet
As you can see our style sheet is getting longer, and it will continue to grow. It is far better to have your style sheet in its own file, and refer to it within the HTML.
Copy all the Style sheet instructions between the <style> tags from your latest example, but NOT the <style> tags! Paste it into notepad and save as “mystyles.css”. Here’s the code
body { color: blue; font-family: "arial","times new roman","courier"; } h1 { color: red; font-weight: bold; } .italics-para { font-style: italic; } .summary-table { width: 75%; border: solid; border-width: 1px; border-color: blue; color: black; } .summary-table th { color: black; background: lightgrey;} .summary-table td { color: black; }
Now return to your HTML, delete the style open and close tags, and all that was within them, and replace with…
<link href="mystyles.css" rel="stylesheet" type="text/css" />
This tells your browser to read the file mystyles.css as a style sheet and use it as necessary.
Remember we mentioned that some HTML elements only have one tag, nothing can be contained within them so they do not have a separate closing tag, well this is one such example, although it is closed in a round about way, notice the very subtle space then / just before the '>' at the end of the line.
So your full HTML code will be
<html> <head> <link href="mystyles.css" rel="stylesheet" type="text/css"/> </head> <body> <h1>Welcome to my weather station. </h1> <p>The weather station in use is the Fine Offset WH1081, and these pages are updated every 5 minutes. The meteorological day used at this station ends at midnight</p> <p class="italics-para">All data offered by this website and station is from an amateur weather observation station and should be used and accepted at face-value and compared alongside official sources.</p> <table class="summary-table"> <tbody> <tr> <th>Dawn:</th> <td>07:00</td> <th>Sunrise:</th> <td>07:25</td> <th>Moonrise:</th> <td>10:07</td> </tr> <tr> <th>Dusk:</th> <td>19:00</td> <th>Sunset:</th> <td>18:35</td> <th>Moonset:</th> <td>-----</td> </tr> </tbody> </table> </body> </html>
All change
Just to prove how quickly you can change the format of a page using style sheets, replace the instructions you have in your mystyles.css with the instructions below and reload your page. See how the look changes. You did not touch any HTML only changed the formatting in the CSS.!!
body { color: black; font-family: "times new roman","times","courier"; font-size: 16pt; } h1 { color: red; font-weight: bold; background: blue; padding:20px; text-align: center;} .italics-para { font-style: normal; } .summary-table { width: 50%; border: dashed; border-width: 1px; border-color: grey; color: black; font-size:60%; margin-left: 100px;} .summary-table th { color: green;} .summary-table td { color: black;}
Some possible issues
- If your style sheet rules are not being applied, check your coding. Remember each style entry ends with a ; and a group of style entries for one class or tag are enclosed in { and }
- check your spelling -- CSS is Americanised so we have color not colour and center not centre!
- Style sheets are supported in all modern browsers, however with varying degrees of compliance. As a general rule the latest version of Chrome, Firefox, Opera, and Safari, are all good at implementing the latest CSS3 recommendations for a standard. Usually either Chrome or Firefox will be quick to pick up CSS4 recommendations, as these browsers are widely used, most developers will consider using newer CSS features.
- When developing a site, test in a variety of browsers, remembering almost anyone may view your site. Anyone using IE may miss out on some features of your design that Microsoft's code ignores, but then those users will not know what they are missing. Those with the more modern browsers can have their experience of visiting your web site enhanced by you using a wider range of the latest CSS features.
- Internet Explorer can be quite fussy, and those designers writing for where that browser is used exclusively will include separate HTML instructions on what CSS to load for each IE version, making it complicated to understand how the CSS determines the look. The relevant instructions in the HTML are specific to Microsoft, they look like conditional HTML comments, and are treated as comments by non-Microsoft agents.
- Early versions of Internet Explorer are bad for a variety of reasons. If you are on IE version 5 or 6 you should change to using a modern browser now. If a site asks what browser is being used, IE may even report it is Firefox, but despite that generally older IE will either expect different CSS syntax, or just ignore any standard CSS, so it will not display a page as Firefox would. You have to use the Microsoft conditional comments approach to detect IE and switch CSS accordingly.
- When developing your site, remember it may be viewed on a range of different devices including PCs, tablets, mobiles; try whatever devices you have, and experiment with any options your browser offers for different widths, default font-sizes and different screen shapes/resolutions.
- Finally, it is often possible to detect media (although such detections may not always be successful), and apply a different CSS file in each case. For example the page appears shaped best for viewing on a monitor with one CSS file, shaped best for viewing on a mobile with a second file, and shaped best for printing on a sheet of paper with the final CSS file. This is however too complex to describe any further here.
In summary
That is CSS/Style sheets in essence. There is far more to this and lots to read on the subject however what this should do is allow you to read some HTML and CSS and understand how they come together.
The next article to read ……
Learn more about HTML elements like <DIV> and <SPAN> and more about positioning in Webpage layout
Originally written - Daj 12:25, 25 August 2009 (UTC)