I am trying to learn by doing. Here is the first problem that I have solved. I have actually not done it perfectly. The table header should cover both the text and the image but it is only above text. If you can help me out with the design I will be thankful.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table>
<th ><h3>Food you love to eat</h3></th>
<tr>
<td>I love eating food . All types of food are good for health. I like fruits in them. Fruits are good for health.</td>
<td width="170"> <img src ="http://www.htmliseasy.com/exercises/fruitbowl.gif" /> </td>
</tr>
</table>
</body>
</html>
4 Answers 4
The HTML you submitted was invalid. I changed the following things so your html would be valid:
Don't put
<h3>
tags inside a table. Header tags are used for headings only. Don't use headings to make text BIG or bold. Use CSS for positioning and font sizing.In a table, the
<tr>
tag is the first tag that should appear.In the
<img>
tag, the attributealt
is required. Thealt
attribute specifies alternate text for an image if the image cannot be displayed.
Your HTML validated:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h3>Food you love to eat</h3>
<table>
<tr>
<td>I love eating food . All types of food are good for health. I like fruits in them. Fruits are good for health.</td>
<td width="170"> <img src ="http://www.htmliseasy.com/exercises/fruitbowl.gif" alt="Fruit Bowl" /> </td>
</tr>
</table>
</body>
</html>
-
2\$\begingroup\$ Now that I think of it, I believe I know why you put the <h3> tags in the table. This is how you go about doing what you were wanting:
<td colspan="2" align="center"><font size="5"><b><i>Food you love to eat</i></b></font></td>
\$\endgroup\$Eric Anderson– Eric Anderson2011年06月29日 21:03:51 +00:00Commented Jun 29, 2011 at 21:03 -
2\$\begingroup\$ use td or th? th tag would be better for it I personally think \$\endgroup\$user1211– user12112011年06月29日 22:09:36 +00:00Commented Jun 29, 2011 at 22:09
-
2\$\begingroup\$ If you would like to use <th> tags, there's no problem with that, just don't put header tags within them. \$\endgroup\$Eric Anderson– Eric Anderson2011年06月30日 01:11:39 +00:00Commented Jun 30, 2011 at 1:11
-
\$\begingroup\$ @Eric: Yes, you got it right. I wanted to do the same as you mentioned. Thanks :) \$\endgroup\$user1211– user12112011年06月30日 14:17:03 +00:00Commented Jun 30, 2011 at 14:17
-
1\$\begingroup\$ @Fahad: Cool, I'm glad thing's are working out for you. Good luck learning HTML. :) \$\endgroup\$Eric Anderson– Eric Anderson2011年06月30日 18:28:52 +00:00Commented Jun 30, 2011 at 18:28
Something that wasn't mentioned was the Use of the Table header tags
If you want the Table headers it should look something like this
<tr>
<th colspan="2">Food you love to eat</th>
</tr>
<tr>
<td>
I love eating food . All types of food are good for health. I like fruits in them. Fruits are good for health.
</td>
<td width="170">
<img src ="http://www.htmliseasy.com/exercises/fruitbowl.gif" alt="Fruit Bowl" />
</td>
</tr>
The Table headers need to be in a row.
I added a colspan
property because I don't see that you are going to need two separate table headers for each column. if you have more than 2 columns and you want the header to stretch across all of them then replace "2"
with the appropriate number of columns. you can mix and match as well, creating a th
that spans the last two and not the first one, or however you need it.
Just because snippets are cool
table {
border: 3px solid red;
}
th {
border: 3px double green;
}
td {
border: 3px dashed blue;
<table width="500px">
<tr>
<th colspan="2">Food you love to eat</th>
</tr>
<tr>
<td>
I love eating food . All types of food are good for health. I like fruits in them. Fruits are good for health.
</td>
<td width="170">
<img src ="http://www.htmliseasy.com/exercises/fruitbowl.gif" alt="Fruit Bowl" />
</td>
</tr>
</table
If you are listing multiple items that you "love to eat" then this would be a very good use of a table structure.
-
1\$\begingroup\$ Actually, this is mentioned in a comment on Eric's answer. \$\endgroup\$Simon Forsberg– Simon Forsberg2014年02月13日 14:49:24 +00:00Commented Feb 13, 2014 at 14:49
-
\$\begingroup\$ @SimonAndréForsberg, not how to properly use
th
tags and that they are a special type oftd
tag that needs to be nested inside of atr
\$\endgroup\$Malachi– Malachi2014年02月13日 14:51:37 +00:00Commented Feb 13, 2014 at 14:51
This is a great start, but I would recommend finding resources that are more up to date. Nowadays tables are largely reserved for containing data, rather than structuring pages or most content.
I highly suggest going to Youtube, and finding some starter videos containing HTML5. Table tutorials won't get you anywhere but 1996. Keep up the good work.
-
\$\begingroup\$ Thanks. By this time I am almost done with html and css. Now PHP, Python and .NET on the way :) \$\endgroup\$user1211– user12112012年09月08日 05:37:10 +00:00Commented Sep 8, 2012 at 5:37
-
\$\begingroup\$ Glad to hear. Sounds like you're moving along quickly! \$\endgroup\$danchet– danchet2012年09月10日 15:54:15 +00:00Commented Sep 10, 2012 at 15:54
-
2\$\begingroup\$ where is the Review? \$\endgroup\$Malachi– Malachi2014年02月13日 14:32:37 +00:00Commented Feb 13, 2014 at 14:32
-
\$\begingroup\$ this does contain Data, this is actually a good use for a Table, especially if you want to add items dynamically. \$\endgroup\$Malachi– Malachi2014年02月13日 14:45:22 +00:00Commented Feb 13, 2014 at 14:45
-
1\$\begingroup\$ This is such an over-discussed topic, I won't continue it here. Take a look at this thread. stackoverflow.com/questions/83073/… \$\endgroup\$danchet– danchet2014年02月13日 18:33:20 +00:00Commented Feb 13, 2014 at 18:33
You could change your doctype to HTML5:
<!DOCTYPE html>
You could also use a validator — http://validator.w3.org/ , which will highlight any syntax problems such as missing alt attributes, missing tr element around a td element, etc.
And since you have evolved since, you will find this resource useful: http://developers.whatwg.org/