Internal linking | XHTML tables | XHTML forms | Frames
Previously we only linked to other pages. Sometimes, though, if a document has many sections, you may wish to link to a section in the same page. Here we will learn how.
Firstly, we must create an anchor element within the page to create a 'hot-spot'. We do this with the syntax:
<a name="somename"></a> or
<a name="somename" />
The anchor is an empty element in this case, so remember to close it using either of the methods shown above
We will create an anchor element and place it at the
end of this page before the closing body tag, and call it demo:
<a name="demo"></a>
Now we make a hyperlink to this 'hot-spot' in a
similar way to normal links, but with an important
difference: the href attribute will
contain the hash sign [#] and the anchor name, like
this:
<a href="#demo">Click here to go to the
end of the page</a>
Click here to go to the end of the page
Notice the url of the link contains the page name,
plus #demo. We can extend this to link
to certain sections of other pages too, for instance,
changing the href to
href="tutorial3.html#background" will
take you to the first CSS tutorial, and the section
on background colours.
XHTML tables look very daunting at first impression, but are easy to get the hang of once you understand the syntax. (It's taken me longer to write out this section than it will for you to learn it, I reckon!). We will start with a simple table.
Which gives:
| Star | Constellation |
|---|---|
| Vega | Lyra |
| Deneb | Cygnus |
| Altair | Aquila |
| Rigel | Orion |
Let's go through this example bit by bit. The first
tag is the table tag, which tells the
browser a table is following. This encloses all the
table data and heading cells.
The next tag is the thead tag, which
stands for table header. Our column headings are
Star and
Constellation. We put these two
headings within the thead tag. Each of
these are further enclosed by a tr tag,
and then by their own th tag. Confused?
It'll all make sense soon, I promise.
The thead tag merely informs the browser
that heading data will follow. It doesn't
actually do anything as such, but if you want the
columns to have headings, it is required for the
table to render correctly. Most browsers render the
contents of thead in a bold font.
Next we have the tr tags. This stands
for table row. So, everything
between the <tr> tags will be on
one row.
Now, each row will have some number of cells. For
simplicity, we have only used two cells per row, i.e.
two columns. So within the tr tags we
will have two th tags. Each
th tag will have the data for the
heading cell within the table, like so:
| Star | Constellation |
|---|
Now that the column headings are done, we will put
some data into the next row's cells. We
start the main body of the table
with the <tbody> tags. Just like
the thead tags, this element merely
informs the browser that the body of the table is
following.
Next we have the tr tags. Again, this
denotes rows, just as before. Within the
tr tags we will insert some table
data. We do this by enclosing the data within
td tags, which stands for table data,
and placing this within one set of tr
tags, to form the first row of our table, as follows:
| Vega | Lyra |
Now we can carry on until we have entered all our
data. Then we end our tbody, and finally
we close the table tag. And that's all
there is to it! Look over the code and the
explanations again if it still doesn't make sense,
and try playing around with the coding in your text
editor - the best way to learn XHTML (or indeed any
other computer language) is to try it out yourself.
Sometimes you may wish to create a table merely for
laying out forms, images etc. In this case you can
omit the thead and tbody
sections. Just use tr and
td within the table tags.
You can also add a table footer at the end
of the table, using <tfoot> and
<th> as above. For instance, you
might use the table to display calculations, in which
case you can use the footer to store the results.
You can create a caption for the table by adding the
caption element after the
<table> tag and before any table
headings or data. Like below:
Gives:
The caption element has caused our table to have the
title 'Some bright stars'. Most browsers place the
caption text at the top of the table, regardless of
wether you place the caption tags at the beginning of
the table or at the end. Notice that we have used the
summary attribute in the
table tag. Like the alt
attribute for images, the summary
attribute provides information about the table, which
improves accessibility. Again, the summary attribute
is read out by screen-readers.
We have also used the width attribute.
This specifies the width of the table. It can either
be expressed in percentages, as above, or in pixels,
in which case we merely write the number of pixels,
e.g. width="400" would give a table 400
pixels wide. When using percentages, the table width
will adjust as you resize the browser window. We have
set the table to span 50% of the screen width as you
can see above. Now we have resized the window, note
how the table size has also changed (in this case the
browser has set the table width to above 50% of the
screen width, even though we have specified 50%,
otherwise the text wouldn't fit the cells):
You can also use the width attribute for
td elements. Remember though, that most
browsers will take the width specified for the first
cell in a column and use this width
for all cells below it.
You can also set the border width using the
border attribute. We will learn a more
customisable way to do this in the next lesson.
Columns and rows can be merged using the
td attributes of rowspan
and colspan. We will first demonstrate the
colspan attribute.
Here we have used the attribute colspan
with a value of 2. This means the cell is set
to span 2 columns (or merge 2 columns together).
This attribute can be used for both td
and th elements. Let's try a more advanced example.
Here we have made 2 th cells, which span
2 columns each. Let us now look at
merging rows.
We have created a cell that spans three rows, and another
cell below it that spans both columns. The table originally
has two columns. Therefore two td cells are
initiated in the first table row. However, since we
have caused the first td to span three rows,
the subsequent 2 rows only have one td element
in each row.
Note that the merged cell in this example counts as part of the first row.
We then added another cell at the end of the first three rows.
This cell is set to span both of the columns, which is why, again
only 1 td element is present. Otherwise, if it
wasn't merged, we would have had to include another td
element to balance the table.
We will take a look at how to create XHTML forms, which can be used for user input. Please note we will only learn how to make the forms, not process them. In order to use the data inputted through a form, we need to use a client-side scripting language, such as JavaScript, or a server-side language - I recommend PHP.
Form elements can take one of the three types:
All forms elements must be within a
<form> tag, for example:
<form name="myform" method="post"
action="somepage.php">
...your form elements...
</form>
The name atribute specifies a name for the form to be
indentifed by. action specifies the
form handler, i.e. the page that will
process the script. If your form is not to be
processed by a server-side form handler then set the
action to action="". method
states the method by which the input values will be
sent to the server. post means the data
will be sent through the http request. The
get method appends the input values to
the url. You may have seen urls such as
www.somepage.com/process.php?user=name&choice=somechoice.
The text following the ? is the data
sent to the server by the get method.
The get method is limited to 1024
characters. If the data sent exceeds this limit, the
method will default to post.