CMS

Content

HTML

Images

Monetizing

Home » Content

Aligning Your Content

Saturday, 27 March 2010 · Print This Post Print This Post No Comment

Related Reading

This entry is part 1 of 1 in the series Aligning

Welcome to the next lesson of your free online web design course. If you feel great about your progress so far, then kudos to you – and some more delicious HTML tags and attributes, of course! :)

Centering Your Content The Old Way

The <center> tag is one of the oldest methods for aligning your content. You just put the content you need centered between the <center> and </center> tags and voila! – your content is centered. Let’s center some text:

<html>
<head>

</head>
<body>

<center>
Some text content
</center>

</body>
</html>

When you copy and paste the above code to your notepad and save it as .htm, you should have “Some text content” centered on the screen:

Some text content

Using Divisions

Online web design course continues as we delve deeper into aligning our content. By using the <div> tag along with the “align” attribute, you can align any piece of content to the left, to the right or to the center. Just copy and paste the sample code below to your text editor:

<html>
<head>

</head>
<body>

<div align=”left”>
Text Aligned Left
</div>

<br><br>

<div align=”center”>
Text Aligned Center
</div>

<br><br>

<div align=”right”>
Text Aligned Right
</div>

</body>
</html>

We should have three pieces of text aligned left, center and right respectively:

Text Aligned Left
Text Aligned Center
Text Aligned Right

You can also use the <div> tag and the “align” attribute to justify some text so that it will be aligned both to the left and to the right and look neat and tidy:

<html>
<head>

</head>
<body>

<div align=”justify”>
ONLINE WEB DESIGN COURSE by Onur Özcan is one of the most extensive e-courses about web design on the web. It is extremely easy to understand, yet very comprehensive and full of useful examples. Besides, this great free online web design course teaches you how to get trtaffic to your websites and how to profit from that traffic as well! It’s just awesome.
</div>

</body>
</html>

We’ll get a neat paragraph of shameless self-promotion from me as an output:

ONLINE WEB DESIGN COURSE by Onur Özcan is one of the most extensive e-courses about web design on the web. It is extremely easy to understand, yet very comprehensive and full of useful examples. Besides, this great free online web design course teaches you how to get visitors to your websites as well! It’s just awesome.

As you can see, both sides of this paragraph are justified properly to the left and to the right.

**Before we proceed further in this lesson of our free online web design course, please note that you can use the <div> tag to align any type of content you want (i.e. text, images, links, tables, flash animations, etc.)

Now let’s learn how to align content to the top, middle or bottom of a table cell.

Aligning Inside The Cell

OK, let’s say we have a two-column-single-row table, and let’s assume that this table is 300 pixels wide, with a picture inside one cell and some text inside the other. And let’s also set the “width” attributes of one of the cells to 50%, in order to make the table shared equally by the two cells (the other cell will be automatically set to 100% – 50% = 50% by default by the web browsers, so we do not need to manually set it to 50%, as well):

<html>
<head>

</head>
<body>

<table width=”300″ border=”1″ cellpadding=”5″ cellspacing=”1″>
<tr>
<td width=”50%”>
<img src=”bg.jpg”>
</td>
<td>
Some text
</td>
</tr>
</table>

</body>
</html>

In the output, you should notice that the text inside the second cell automatically gets aligned horizontally to the left and vertically to the middle of the cell:

Some text

Now… We can align the text horizontally the center or the right, if we want to by adding an “align” attribute to the second <td> tag. Let’s align our text to the center:

<html>
<head>

</head>
<body>

<table width=”300″ border=”1″ cellpadding=”5″ cellspacing=”1″>
<tr>
<td width=”50%”>
<img src=”bg.jpg”>
</td>
<td align=”center”>
Some text
</td>
</tr>
</table>

</body>
</html>

Our text should align to the center horizontally:

Some text

If you want to vertically align the contents of a particular cell to the top or bottom, you need to add a “valign” (vertical alignment) attribute to the corresponding <td> tag as well. For example, if we wish to align the text to the top, we add a valign=”top” value to the second <td> tag:

<html>
<head>

</head>
<body>

<table width=”300″ border=”1″ cellpadding=”5″ cellspacing=”1″>
<tr>
<td width=”50%”>
<img src=”bg.jpg”>
</td>
<td align=”center” valign=”top”>
Some text
</td>
</tr>
</table>

</body>
</html>

This should align the text to the top of the cell:

Some text

Similarly, you can align the contents of a particular cell to the bottom by adding a valign=”bottom” value to the corresponding <td> tag.

Sidenote: You can also use the <div> tag along with the “valign” attribute inside the cell (in between the <td> and </td> tags) to get the same results! However, this would be a redundant use of the <div> tag.

Aligning Tables

If you have tables that need to be centered or aligned to the sides, the top or the bottom, you can simply use the same attributes we used with the <td> tags above; the “align” and “valign” tags. Below is a sample code, which contains two tables – one big table, and a smaller table that is “nested” inside the big table:

<html>
<head>

</head>
<body>

<table width=”300″ border=”1″ cellpadding=”5″>
<tr>
<td>

<table width=”200″ border=”1″>
<tr>
<td>
Contents of the nested table
</td>
</tr>
</table>

</td>
</tr>
</table>

</body>
</html>

For your convenience, I’ve bolded out the nested table code above. The code will display in a web browser as shown below:

Contents of the nested table

The nested table is aligned to the left by default. What if we want to align it to the center? Well, we use the align=”center” inside the <table> tag (of the nested table), as shown below:

<html>
<head>

</head>
<body>

<table width=”300″ border=”1″ cellpadding=”5″>
<tr>
<td>

<table width=”200″ border=”1″ align=”center”>
<tr>
<td>
Contents of the nested table
</td>
</tr>
</table>

</td>
</tr>
</table>

</body>
</html>

The above code will get our nested table centered:

Contents of the nested table

What if our outer table is 150 pixels high and we want to align our nested table to the bottom? This is the spot where it gets a little complicated: The <table> tag doesn’t support a “valign” attribute! Therefore, we have to put the “valign” attribute inside the <td> tag of our outer table:

<html>
<head>

</head>
<body>

<table width=”300″ height=”150″ border=”1″ cellpadding=”5″>
<tr>
<td valign=”bottom”>

<table width=”200″ border=”1″ align=”center”>
<tr>
<td>
Contents of the nested table
</td>
</tr>
</table>

</td>
</tr>
</table>

</body>
</html>

The above HTML code will get our nested table align to the center bottom of the 150-pixel-high outer table:

Contents of the nested table

Sidenote: We could have defined the align=”center” expression inside the same <td> tag as well – it would be the same as defining it inside the <table> tag of our nested table.

Aligning Paragraphs

Our free online web design course lesson will continue by teaching you how to align the content of your paragraphs. As we have learned in Lesson 1-03-02, you can use the <p> and </p> tags to create paragraphs. By adding an “align” attribute to your <p> tag, you can actually align your text to the left, the center and to the right or you can justify it. Let’s do an example by aligning some text to the right of the screen:

<html>
<head>

</head>
<body>

<p align=”right”>
Online Web Design Course Newsletter is coming soon! It will be jam-packed with great web design tips and resources, as well as through coverage about search engine optimization and affiliate marketing. Do NOT miss it!
</p>

</body>
</html>

We get a paragraph of text aligned to the right:

Online Web Design Course Newsletter is coming soon! It will be jam-packed with
great web design tips and resources, as well as through coverage about search
engine optimization and affiliate marketing. Do NOT miss it!

You can also justify your paragraphs or align them to the center or by using the “justify” and “center” values with the “align” attribute.

If you enjoyed this lesson, please link to it :)

This lesson is over. Next lesson of your free web design course is going to teach you how to integrate forms into your HTML documents.


Bookmark this Post Subscribe to Blog

Popular Tags

Adsense Background Cascading Style Sheets colors Content font Forms GIF Hex Codes HTML Hyperlinking Images IMeye JavaScript JPEG Keyword Research layout Merging meta PNG SEO Sizing spacer Tables text W3C
Leave a Comment

Threaded commenting powered by Spectacu.la code.