Creating rounded corners with HTML tables – 1

0

Posted by touhid | Posted in HTML, Website Design & Development | Posted on 01-10-2009

Creating rounded corners involves a working knowledge of HTML tables and for that you can refer to the in-depth HTML table tutorial. If you are already familiar with the various tags associated with HTML tables and their attributes, you can go ahead and dig right into this tip else, I would recommend that you first read the HTML table tutorial in the Advanced HTML Tutorial.

That said, creating rounded corners in no rocket science. We simply employ HTML tables to create the layout accomplish our task – which is to produce something like the table with rounded corners below.

Rounded Corner: Top-left Rounded Corner: Top-right

This is a Rounded table

You would typically place your content here.

Rounded Corner: Bottom-left Rounded Corner: Bottom-right

Remember, all elements on a web page are rectangular and this includes images! This should come as no surprise because the screen on our monitors is rectangular and so is the browser window and the active space in the browser (the space that displays the web page). Take a look at the image below. Though it might look circular it’s actually a rectangle (a square to be precise) – read more about why images are rectangles.

An image of a circle

The background color of the image above is the same as that of this web page (white – hexadecimal code #FFFFFF) and because of this it seems that the image is only the green colored circle. However, this is not true. The green circle us surrounded by white color which is actually our image background color. If we place the image is a table with a blue background, we can then see the area occupied by the image.

An image of a circle

Now you see the white space that surrounds the green circle – don’t you?
OK! So what does all this have to do with creating tables with rounded corners? A lot, my good visitor! Now that we understand that though images are rectangles, they can be employed to give a semblance of other shapes. The shape that we would use are the four quarters of a circle. But before jumping to all that, let us create our HTML code on which we will work further.

The first step is to understand that a table with rounded corners is created by a table with 3 columns and 3 rows; thus, a total of 9 table cells – like the table below:

Cell 1 Cell 2 Cell 3
Cell 4 Cell 5 Cell 6
Cell 7 Cell 8 Cell 9

Cells 1, 3, 7 and 9 will contain the four quarters of a circle, cells 2, 4, 6 and 8 will form the top, left, right and bottom edges and Cell number 5 will have the actual contents. The four quarters of a circle that we plan to use are four different images. And to create these images you need the assistance of your favorite graphics editing tool. Any graphics tool will let you create a circle. Once you have done so, you need to split it into 4 squares and these will be the four quarters.

For example, in Paint Shop Pro or Adobe Photoshop, start by creating a new image of 28×28 pixels. Draw a circle right in the middle of the image. IMPORTANT note: be sure that you have the anti-aliasing option selected before you draw the circle. Then with the selection tool extract 14×14 pixels from the top left. You can repeat this for other quarters or simply flip and mirror the first quarter that you’ve created. you need to save all the four images with different file names. So starting with an image of a circle, we can get 4 quarters as below:

Circle for rounded corners table
Our starting image
Top-left corner
Top-left corner
Top-right corner
Top-right corner
Bottom-left corner
Bottom-left corner
Bottom-right corner
Bottom-right corner

We will now place the four images in 3×3 table inside the corresponding cells as detailed above, remove the borders, padding to give us our rounded rectangle.

<table width="200" cellpadding="0" cellspacing="0" border="0"
style="background-color: #9C084A">
<tr>
<td width="14"><img src="rounded_corner1.gif" width="14"
height="14" border="0" alt="..." /></td>

<td width="172"></td>

<td width="14"><img src="rounded_corner2.gif" width="14"
height="14" border="0" alt="..." /></td>

</tr>
<tr>

<td></td>

<td align="center">ACTUAL CONTENT</td>

<td></td>

</tr>
<tr>

<td><img src="rounded_corner3.gif" width="14" height="14"
border="0" alt="..." /></td>

<td></td>

<td><img src="rounded_corner4.gif" width="14" height="14"
border="0" alt="..." /></td>

</tr>
</table>

Pay close attention to the code marked in blue.
The width of the HTML table can be anything number that you want, BUT be sure to that the cellpadding, cellspacing and border attributes are set to a value of zero (0). You would also need to give a background color to the table which should be the same color as that of our circle (now split into quarters). I would recommend that you use styles to specify the background color as I have done with style=”background-color: #9C084A;” instead of using the BGCOLOR attribute as its been deprecated.
You will notice that some table cells are empty… you can put a transparent 1×1 pixel gif resized to the correct width and height it you want.
Lastly, DO NOT forget the ALT attribute for the images as this is now a required attribute and your HTML page will not validate correctly if you skip it.
The above code will render a table similar to the one I displayed at the beginning of this article – View the table with rounded corners.

Changing the height and width of the table with rounded corners

As I mentioned above, the width of the table with rounded corners can be anything you want. However, the beauty of this table code is that you dont have to do anything about its height. The height of this table is controlled by the actual contents… the contents placed in cell #5. So depending on the amount of content we place in this cell, the height of the table will increase on its own. Take a look below:

Rounded Corner: Top-left Rounded Corner: Top-right

Scarborough Fair

Are you going to scarborough fair?
Parsley, sage, rosemary and thyme.
Remember me to one who lives there.
She once was a true love of mine.

Tell her to make me a cambric shirt,
(a hill in the deep forest green)
Parsley, sage, rosemary and thyme;
(tracing of sparrow on snow-crested brown)
Without no seams nor needle work,
(blankets and bedclothes the child of the mountain)
Then she’ll be a true love of mine.
(sleeps unaware of the clarion call)

Tell her to find me an acre of land,
(on the side of a hill a sprinkling of leaves)
Parsley, sage, rosemary and thyme;
(washes the grave with silvery tears)
Between the salt water and the sea strand,
(a soldier cleans and polishes a gun)
Then she’ll be a true love of mine.

Tell her to reap it with a sickle of leather,
(war bellows blazing in scarlet battalions)
Parsley, sage, rosemary and thyme;
(generals order their soldiers to kill)
And gather it all in a bunch of heather,
(and to fight for a cause they’ve long ago forgotten)
Then she’ll be a true love of mine.

Rounded Corner: Bottom-left Rounded Corner: Bottom-right

This was the first part of the creating a table with rounded corners. Now let us see how we can create more advanced and complex corners of such tables. This is discussed in the next part – Complex and more advanced rounded corners for HTML tables.

Before we delve on how to create complex rounded corners, let me point out that the roundedness of the table depends on the curves of the image and thus the size of the circle that you started with. So if your circle image was large, it will result in a table with smoother and larger curves and if the circle was small, the rounded curves would be tighter. Take a look at the examples below.

Rounded corner: top-left Rounded corner: top-right

This is a Rounded table

You would typically place your content here.

Rounded corner: bottom-left Rounded corner: bottom-right

Each corner image is 50×50 pixels

Rounded corner: top-left Rounded corner: top-right

This is a Rounded table

You would typically place your content here.

Rounded corner: bottom-left Rounded corner: bottom-right

Each corner image is 8×8 pixels

By now, I guess you would have realized that the secret (if there was any) lies in the images that we have used for the rounded corners. So let us start on creating complex rounded tables.
Here is the simplest of the complex rounded tables that we are going to create.

Rounded corner: top-left Background Rounded corner: top-right
Background

This is a Rounded table

You would typically place your content here.

Background
Rounded corner: bottom-left Background Rounded corner: bottom-right

This rounded table is made using 8 images – 4 for the rounded corners and the rest are employed for the borders of the four sides. Here are the four images used for the corners:

Rounded corner: top-left
16×16 pixels
Corner for top-left
Rounded corner: top-right
16×16 pixels
Corner for top-right
Rounded corner: bottom-left
16×16 pixels
Corner for bottom-left
Rounded corner: bottom-right
16×16 pixels
Corner for bottom-right

The four sides are made by very thin images – The top and bottom edges are created using images that are 1 pixel in width and the left and right edges are of images that are 1 pixel in height. Since each of our corner image is 16 pixels in both width and height, the images for the top and bottom edges are 16 pixels in height (to match the corners) and the ones for the left and right sides are 16 pixels in width. Thus, the images for top and bottom edges are 1 pixel in width and 16 pixels in height and images for left and right edges are 16 pixels in width and 1 pixel in height. Because these images are too small (or thin, if you like) to be displayed at their present dimensions, I have magnified the top-left corner of our table to show you two of the four images – the image used for the top edge and the left edge.

Three images that make the rounded rectangle

The above depicts 3 of the images used in the rounded rectangle – the image for the top-left corner, the image used for the top edge and the image that makes the left border.
Now an important point: The four borders are made up of very thin images and hence these images are employed as backgrounds for their respective table cells. Here is the code:

<table width="200" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="16"><img src="rounded1.gif" width="16" height="16"
border="0" alt="..."></td>
<td width="168" style="background-image: url('back1.gif');">
<img src="blank.gif" width="1" height="16" border="0" alt="..." />
</td>
<td width="16"><img src="rounded2.gif" width="16" height="16"
border="0" alt="..."></td>
</tr>

<tr>
<td style="background-image: url('back4.gif');">
<img src="blank.gif" width="16" height="1" border="0" alt="..." />
</td>
<td>ACTUAL CONTENTS</td>
<td style="background-image: url('back2.gif');">
<img src="blank.gif" width="16" height="1" border="0" alt="..." />
</td>
</tr>

<tr>
<td>
<img src="rounded3.gif" width="16" height="16" border="0" alt="...">
</td>
<td style="background-image: url('back3.gif');">
<img src="blank.gif" width="1" height="16" border="0" alt="..." />
</td>
<td>
<img src="rounded4.gif" width="16" height="16" border="0" alt="...">
</td>
</tr>
</table>

The code in blue is the one I would like you to pay attention to. Here I have employed the background-image style property and have specified the four images as backgrounds of their respective table cells which will then make the top, bottom left and right borders. Note the use of blank.gif image which is a 1×1 pixel transparent gif. This blank image will help you in adjusting the width of table cell without spending time and messing things up.

Before I end this article, here are some nice looking tables with complex rounded corners – remember, it’s all in the images!

Rounded corner: top-left Background Rounded corner: top-right
Background

This is a Rounded table

You would typically place your content here.

Background
Rounded corner: bottom-left Background Rounded corner: bottom-right
Rounded corner: top-left Background Rounded corner: top-right
Background

This is a Rounded table

You would typically place your content here.

Background
Rounded corner: bottom-left Background Rounded corner: bottom-right

Removing space and padding from FORM tag

0

Posted by touhid | Posted in HTML, Website Design & Development | Posted on 01-10-2009

This tip concerns removing the extra padding or space before and after the <FORM> tag. Let us suppose that you have to constrain two text fields and a submit button/image in a limited vertical space. In order to layout the form fields and the button/image, you would place them in table cells (as we do with most other HTML elements). However, you will notice that there is extra space (vertical space) or padding before <FORM> occurs and after the end of the tag.

Now for an example. Below is issue which we are taking about.

Member login
Username:
Password:

Do you notice the extra space after the login button? Good. Now, let us see how we can remove this space. First check the incorrect code. The code for the above form is:

<TABLE WIDTH="200" CELLPADDING="1" CELLSPACING="0" BORDER="0"
BGCOLOR="#878787">
<TR>
<TD>
<TABLE WIDTH="198" CELLPADDING="3" CELLSPACING="0" BORDER="0"
BGCOLOR="#EEEEEE">
   <TR>
   <TD BACKGROUND="greygrad.gif"><B>Member login</B></TD>
   </TR>
   <TR>
   <TD>
   <FORM NAME="spaceform">
   <TABLE WIDTH="192" CELLPADDING="2" CELLSPACING="0" BORDER="0">
      <TR>
      <TD ALIGN="RIGHT">Username:</TD>
      <TD><INPUT TYPE="TEXT" NAME="texte1" ...></TD>
      </TR>
      <TR>
      <TD ALIGN="RIGHT">Password:</TD>
      <TD><INPUT TYPE="PASSWORD" NAME="texte2" ...></TD>
      </TR>
      <TR>
      <TD> </TD>
      <TD><INPUT TYPE="SUBMIT" VALUE="Login" ...>/TD>
      </TR>
   </TABLE>
   </FORM>
   </TD>
   </TR>
</TABLE>
</TD>
</TR>
</TABLE>

Notice the <FORM> tag surrounds the table in which the form fields occur.
In order to remove the extra space/padding, we take the help of Style Sheets. Now CSS (Cascading Style Sheets), allow us to set the margins and padding properties around HTML elements and we employ these two properties to remove the extra space and padding around the <FORM> element. Our code thus becomes:

<TABLE WIDTH="200" CELLPADDING="1" CELLSPACING="0" BORDER="0"
BGCOLOR="#878787">
<TR>
<TD>
<TABLE WIDTH="198" CELLPADDING="3" CELLSPACING="0" BORDER="0"
BGCOLOR="#EEEEEE">
   <TR>
   <TD BACKGROUND="greygrad.gif"><B>Member login</B></TD>
   </TR>
   <TR>
   <TD>
   <FORM NAME="spaceform" STYLE="margin: 0px; padding: 0px;>
   <TABLE WIDTH="192" CELLPADDING="2" CELLSPACING="0" BORDER="0">
      <TR>
      <TD ALIGN="RIGHT">Username:</TD>
      <TD><INPUT TYPE="TEXT" NAME="texte1" ...></TD>
      </TR>
      <TR>
      <TD ALIGN="RIGHT">Password:</TD>
      <TD><INPUT TYPE="PASSWORD" NAME="texte2" ...></TD>
      </TR>
      <TR>
      <TD> </TD>
      <TD><INPUT TYPE="SUBMIT" VALUE="Login" ...>/TD>
      </TR>
   </TABLE>
   </FORM>
   </TD>
   </TR>
</TABLE>
</TD>
</TR>
</TABLE>

With CSS we specify the margin and padding style sheet properties to have a value of zero (0) pixels (px). This results in the removal of any extra space around the HTML form. The result is below:

Member login
Username:
Password:

Don’t go away yet – there is more.
The purpose of the first TABLE (the one that encloses the FORM and the second table) is ONLY to create a thin grey colored border. Most web developers and designers used this technique because it was cross-browser compatible. However, with the important browsers (Internet Explorer, Mozilla, Netscape, Opera…) now supporting style sheets and (imp) with the same results, we can again employ style sheets to create a 1 pixel thin border and do away with our outermost TABLE. Check the code below:

<TABLE WIDTH="198" CELLPADDING="3" CELLSPACING="0" BORDER="0"
STYLE="background-color: #EEEEEE; border: 1px solid #878787">
   <TR>
   <TD STYLE="background-image: url('greygrad.gif');"><B>
   Member login</B></TD>
   </TR>
   <TR>
   <TD>
   <FORM NAME="spaceform" STYLE="margin: 0px; padding: 0px;>
   <TABLE WIDTH="192" CELLPADDING="2" CELLSPACING="0" BORDER="0">
      <TR>
      <TD ALIGN="RIGHT">Username:</TD>
      <TD><INPUT TYPE="TEXT" NAME="texte1" ...></TD>
      </TR>
      <TR>
      <TD ALIGN="RIGHT">Password:</TD>
      <TD><INPUT TYPE="PASSWORD" NAME="texte2" ...></TD>
      </TR>
      <TR>
      <TD> </TD>
      <TD><INPUT TYPE="SUBMIT" VALUE="Login" ...>/TD>
      </TR>
   </TABLE>
   </FORM>
   </TD>
   </TR>
</TABLE>

The code in blue reflects three cascading style sheets properties that I have introduced. These Style Sheet properties are placed inside the STYLE attribute of an HTML element (the <TABLE> and <TD> elements in our case). You will notice that each of the three properties (background-color, border and background-image) is given certain values. Before I detail each of the style sheets properties and their values, let us first check up how our form looks with this new code:

Member login
Username:
Password:

Style sheets can be included in an HTML document in three ways – External Styles, Embedded Styles and Inline Styles. We don’t have to go into those “gory” details for now, but just for your information, we are including styles sheets in our document as Inline Styles – Styles placed inline with the HTML code.

The Style sheets background-color property

The property simply specifies the background color of the HTML element. I am a fan of Hexadecimal color codes, so I have used the hexadecimal code for light grey – #EEEEEE. Note, the value of the style sheet property is separated from its name with a color – :

The Style sheets background-image property

This property specifies the background image to be used for that HTML element. The image is refered by its URL which can be a absolute or a relative URL. I have used a relative URL here.

The Style sheets border property

The way I have used this style sheet property is more complex that the above two. Here I have specified 3 values to it – 1px, solid and #878787. Note, there are no commas separating the three values. So what I have essentially done is to direct the element to have a 1px (one pixel) thin #878787 colored solid border. Taking a step further in our understanding, let us create the same table with a 2 pixel thick, orange (#FF9900) colored border with dashes.

<TABLE WIDTH="198" CELLPADDING="3" CELLSPACING="0" BORDER="0"
STYLE="background-color: #EEEEEE; border: 2px dashed #FF9900">
   <TR>
   <TD STYLE="background-image: url('greygrad.gif');"><B>
   Member login</B></TD>
   </TR>
   <TR>
   <TD>
   <FORM NAME="spaceform" STYLE="margin: 0px; padding: 0px;>
   <TABLE WIDTH="192" CELLPADDING="2" CELLSPACING="0" BORDER="0">
      <TR>
      <TD ALIGN="RIGHT">Username:</TD>
      <TD><INPUT TYPE="TEXT" NAME="texte1" ...></TD>
      </TR>
      <TR>
      <TD ALIGN="RIGHT">Password:</TD>
      <TD><INPUT TYPE="PASSWORD" NAME="texte2" ...></TD>
      </TR>
      <TR>
      <TD> </TD>
      <TD><INPUT TYPE="SUBMIT" VALUE="Login" ...>/TD>
      </TR>
   </TABLE>
   </FORM>
   </TD>
   </TR>
</TABLE>
Member login
Username:
Password:

That was a glimpse of what style sheets can do to your HTML document. Not only can they help you in creating those visuals which we either not possible with HTML or just too painful and complicated to implement, but they also help in segregating design and content which is what all web developers should strive for. Style sheets are the future so eventhough you might feel that their implementation is not the same across different browsers, it is a good idea to get a grasp their essence quickly.

HTML ordered lists – cool HTMl tips

0

Posted by touhid | Posted in HTML, Website Design & Development | Posted on 01-10-2009

Here is a cool HTML tip for ordered lists created using <ol> HTML tag.

The type attribute of <ol> list determines which kind of bullet will be used for listing elements.

We can use three types of bullets for the <ol> lists – Alphabet, Roman numerals and numbers/digits.
Take a look at the code below:

<ol type="1">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

The type attribute has been set to 1, Arabic numerals (digits). This is the default and you don’t need to specify it.
The above code is rendered in a browser as:

  1. Item 1
  2. Item 2
  3. Item 3

To change the bullets to roman numerals, the TYPE attribute needs to be set to i (lowercase alphabet i) or I (uppercase alphabet I). Let us check these out.

<ol type="i">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
</ol>
<ol type="I">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
</ol>
Is rendered as:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4
  5. Item 5
  6. Item 6
  7. Item 7
  8. Item 8
  9. Item 9
  10. Item 10
  11. Item 11
Is rendered as:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4
  5. Item 5
  6. Item 6
  7. Item 7
  8. Item 8
  9. Item 9
  10. Item 10
  11. Item 11

Another attribute, though you may not encounter it often, is START. It takes a number as value and this specifies the number (or alphabet) with which the list would start.

<ol type="1" start="4">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

is rendered as:

  1. Item 1
  2. Item 2
  3. Item 3
<ol type="A" start="10">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

is displayed as:

  1. Item 1
  2. Item 2
  3. Item 3

HTML ordered lists – cool HTMl tips

0

Posted by touhid | Posted in HTML, Website Design & Development | Posted on 01-10-2009

Here is a cool HTML tip for ordered lists created using <ol> HTML tag.

The type attribute of <ol> list determines which kind of bullet will be used for listing elements.

We can use three types of bullets for the <ol> lists – Alphabet, Roman numerals and numbers/digits.
Take a look at the code below:

<ol type="1">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

The type attribute has been set to 1, Arabic numerals (digits). This is the default and you don’t need to specify it.
The above code is rendered in a browser as:

  1. Item 1
  2. Item 2
  3. Item 3

To change the bullets to roman numerals, the TYPE attribute needs to be set to i (lowercase alphabet i) or I (uppercase alphabet I). Let us check these out.

<ol type="i">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
</ol>
<ol type="I">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
</ol>
Is rendered as:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4
  5. Item 5
  6. Item 6
  7. Item 7
  8. Item 8
  9. Item 9
  10. Item 10
  11. Item 11
Is rendered as:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4
  5. Item 5
  6. Item 6
  7. Item 7
  8. Item 8
  9. Item 9
  10. Item 10
  11. Item 11

Another attribute, though you may not encounter it often, is START. It takes a number as value and this specifies the number (or alphabet) with which the list would start.

<ol type="1" start="4">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

is rendered as:

  1. Item 1
  2. Item 2
  3. Item 3
<ol type="A" start="10">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

is displayed as:

  1. Item 1
  2. Item 2
  3. Item 3

Aligning text and objects around images

0

Posted by touhid | Posted in HTML, Website Design & Development | Posted on 01-10-2009

The align attribute of <img> tag helps in aligning other elements like text with respect to the image. It can take any of the following values:

  • top: Aligns the image to the top of adjoining object.
  • middle: Aligns the text baseline to the middle of the image.
  • bottom: Text baseline is aligned to the bottom of the image. This is the default value.
  • left & right: Image is aligned right of left with text wrapping around it.
  • absmiddle, absbottom and baseline: Aligns the image with respect to the middle, bottom or baseline of the text.
  • texttop: Aligns the image top with respect to the ascenders of the text. This attribute value is Netscape specific.
  • center: works same as align=”middle”.

However, a situation might arise when you would like to place only some text, say, a small description, on the left or right side of the image with the rest to flow after the image.

In our example, we want to align “What a fine idea!” to the left of the image with other sentences to be placed after the image.

If you use the following code, you shall not get the desired results.

<img src="bulb.gif" width="128" height="220" alt="Fine idea" bulb
align="right">
What a fine idea!<br>The bulb in this ...

What a fine idea!
The bulb in this image represents an idea that I have cherished over the months but did not get time to implement.

You’ll notice that the next line – “The Bulb in this …” – also lies to the left of the image, while we wanted to place it below the image.

To force the next line to place below the image, we use clear attribute of <br> tag with “all” as value. Thus, placing <br clear=”all”> right after the phrase will force the next line to start at a new line.

<img src="bulb.gif" width="128" height="220" alt="Fine idea"
align="right"><
What a fine idea!<br clear="all">The bulb in this ...

What a fine idea!

bulb

The bulb in this image represents an idea that I have cherished over the months but did not get time to implement.