Tải bản đầy đủ (.pdf) (39 trang)

HTML5 XP session 09 kho tài liệu bách khoa

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.68 MB, 39 trang )

Session: 9

Creating Tables


how
 to
 create
 and
 format
 tables
 
Ÿ  Describe
 
Ÿ  Explain
 the
 table
 size
 and
 the
 width
 of
 a
 column
 
the
 process
 of
 merging
 table
 cells


 
Ÿ  Explain
 
Ÿ  Explain
 the
 page
 layout
 for
 tables
 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

2
 


A table is made up of rows and columns. The intersection of each row and column is

called as a cell.
A row is made up of a set of cells that are placed horizontally.
A column is made up of set of cells that are placed vertically.
The user can represent the data in a tabular format by using the <table> element in
HTML.
The <tr> element divides the table into rows and the <td> element specifies columns
for each row.
By default, a table does not have a border.
The border attribute of the <table> element specifies a border for making the table
visible in a Web page.
©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

3
 


Ÿ  The
 Code

 Snippet
 demonstrates
 how
 to
 create
 a
 table.
 
<!DOCTYPE HTML>
<html>
<head>
<title>Languages</title>
</head>
<body>

Main Languages


<table border=”1”>
<tr>
<td>English</td>
<td>German</td>
</tr>
<tr>
<td>French</td>
<td>Italian</td>
</tr>
</table>
</body>
</html>

©
 Aptech

 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

4
 


code
 uses
 the
 <table>
 element
 to
 create
 a
 table.
 
 
Ÿ  The
 
border

 aHribute
 of
 <table>
 element
 gives
 a
 border
 to
 the
 table,
 which
 is
 1
 
Ÿ  The
 
pixel
 wide.
 
 
<tr>
 element
 within
 the
 <table>
 element
 creates
 rows.
 
 

Ÿ  The
 
<td>
 element
 creates
 two
 cells
 with
 the
 values
 English
 and
 German
 in
 the
 
Ÿ  The
 
first
 row
 and
 French
 and
 Italian
 in
 the
 second
 row.
 


Ÿ  Following
 figure
 displays
 the
 table
 created.
 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

5
 


user
 can
 specify
 the

 heading
 for
 each
 column
 in
 HTML.
 
 
Ÿ  The
 
specify
 the
 heading
 for
 columns
 in
 a
 table,
 use
 the
 <th>
 element.
 
Ÿ  To
 
Ÿ  The
 text
 included
 within
 the

 <th>
 element
 appears
 in
 bold.
 
 
Ÿ  The
 Code
 Snippet
 demonstrates
 how
 to
 create
 a
 table
 with
 a
 heading.
 
<!DOCTYPE HTML>
<html>
<head>
<title>List of Students </title>
</head>
<body>

List of Students


<table border=”1”>
<tr>
<th>Name</th>

<th>Age</th>
<th>Place</th>
</tr>

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

6
 


<tr>
<td>Mark</td>
<td>17</td>
<td>Madrid</td>
</tr>
<tr>
<td>John</td>
<td>19</td>

<td>London</td>
</tr>
</table>
</body>
</html>

this
 code,
 the
 <table>
 element
 creates
 a
 table
 with
 a
 border
 of
 1
 pixel.
 
Ÿ  In
 
Ÿ  The
 <th>
 element
 provides
 three
 column
 headings

 namely,
 Name,
 Age,
 and
 
Place.
 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

7
 


second
  and
  the
  third

  row
  lists
  the
  details
  of
  the
  students
  in
  the
  three
 
Ÿ  The
 
columns.
 

Ÿ  Following
 figure
 displays
 the
 output
 of
 the
 table
 with
 headings.
 

©
 Aptech

 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

8
 


refers
 to
 a
 process
 of
 extending
 a
 cell
 across
 mul(ple
 rows
 or
 columns.
 

 
Ÿ  Spanning
 
span
 two
 or
 more
 columns,
 use
 the
 colspan
 aHribute
 of
 the
 <td>
 and
 <th>
 
Ÿ  To
 
elements.
 
colspan
 aHribute
 allows
 the
 user
 to
 span
 a

 cell
 along
 a
 horizontal
 row.
 
Ÿ  The
 
Ÿ  The
 value
 of
 the
 colspan
 aHribute
 specifies
 the
 number
 of
 cells
 across
 which
 a
 
specific
 cell
 shall
 be
 expanded.
 
 


Ÿ  The
  Code
  Snippet
  demonstrates
  how
  to
  create
  a
  table
  and
  span
  header
  cells
 
across
 two
 cells
 ver(cally.
 

<!DOCTYPE HTML>
<html>
<head>
<title>Employee Details</title>
</head>
<body>

Employee Details


<table border=”1”>


©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

9
 


<tr>
<th colspan=”2”>IT</th>
<th colspan=”2”>Accounts</th>
</tr>
<tr>
<th>Name</th>
<th>Location</th>
<th>Name</th>
<th>Location</th>
</tr>
<tr>
<td>David</td>

<td>New York</td>
<td>John</td>
<td>London</td>
</tr>

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

10
 


<tr>
<td>Katthy</td>
<td>New Jersey</td>
<td>Peter</td>
<td>Los Angeles</td>
</tr>
</table>

</body>
</html>

code
 creates
 a
 table
 with
 a
 border
 of
 1
 pixel.
 
 
Ÿ  The
 
<th>
 element
 specifies
 two
 column
 headings
 namely,
 IT
 and
 Accounts.
 
 
Ÿ  The

 
Ÿ  Each
  of
  these
  header
  cells
  horizontally
  span
  across
  the
  two
  cells
  by
  seWng
  the
 
colspan
 aHribute
 of
 the
 <th>
 element
 to
 2.
 
 
Ÿ  Each
 of
 these
 headings

 has
 two
 sub-­‐headings
 namely,
 Name
 and
 Loca2on,
 which
 
specify
 the
 name
 and
 loca(on
 of
 employees.
 
 
Ÿ  The
 first
 and
 second
 rows
 display
 the
 details
 of
 the
 employees.
 


©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

11
 


rowspan
 aHribute
 spans
 a
 data
 cell
 across
 two
 or
 more
 rows.

 
 
Ÿ  The
 
allows
 the
 user
 to
 span
 a
 data
 cell
 along
 a
 ver(cal
 column.
 
 
Ÿ  It
 
Ÿ  Like
 the
 colspan
 aHribute,
 the
 rowspan
 aHribute
 can
 be
 used

 within
 the
 <td>
 
and
 <th>
 elements.
 
 
Ÿ  The
 Code
 Snippet
 demonstrates
 how
 to
 span
 a
 cell
 across
 mul(ple
 rows.
 
<!DOCTYPE HTML>
<html>
<head>
<title>Automobile Gallery</title>
</head>
<body>
<table border=”1”>
<tr>

<th>Manufacturer</th>
<th>Model</th>
<th>Price</th>
</tr>
<tr>
<th rowspan=”3”>Audi</th>
<td>A4</td>
<td>34.5</td>
</tr>
©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

12
 


<tr>
<td>A5</td>
<td>42.6</td>

</tr>
<tr>
<td>A6</td>
<td>30.75</td>
</tr>
<tr>
<th rowspan=”2”>BMW</th>
<td>328i</td>
<td>28.25</td>
</tr>
<tr>
<td>530d</td>
<td>47.5</td>
</tr>
</table>
</body>
</html>

Ÿ  The
 code
 creates
 a
 table
 with
 a
 border
 width
 of
 1
 pixel.

 
©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

13
 


three
  <th>
  elements
  within
  the
  <tr>
  element
  specify
  column
  headings
 

Ÿ  The
 
namely,
 Manufacturer,
 Model,
 and
 Price.
 
 

Ÿ  The
  rowspan
  aHribute
  of
  the
  <th>
  element
  combines
  the
  three
  rows
  of
  the
 

Manufacturer
 column
 into
 a
 common

 brand
 namely,
 Audi.
 
 
three
  different
  models
  and
  the
  respec(ve
  prices
  of
  the
  Audi
  brand
  are
 
Ÿ  The
 
displayed
 in
 three
 different
 rows.
 
 
the
 rowspan
 aHribute

 of
 the
 <th>
 element
 combines
 the
 next
 two
 rows
 
Ÿ  Similarly,
 
of
 the
 Manufacturer
 column
 into
 a
 common
 brand
 called
 BMW.
 
Ÿ  Following
 figure
 displays
 the
 rowspan
 aHribute
 effect.

 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

14
 


Ÿ  Alignment
 determines
 the
 representa(on
 of
 text
 along
 the
 le\,
 right,

 or
 center
 
posi(ons.
 
 

Ÿ  In
 HTML,
 by
 default,
 the
 data
 within
 the
 table
 is
 aligned
 on
 the
 le\
 side
 of
 the
 
cell.
 
has
 deprecated
 the

 align
 aHribute.
 
Ÿ  HTML5
 
Ÿ  The
 four
 possible
 values
 for
 seWng
 the
 horizontal
 alignment
 are
 as
 follows:
 
left:
•  Aligns the data within a cell on the left side. This is the default value for
table content.

center:
•  Aligns the data within the cell on the center. This is the default value for
table headings.

right:
•  Aligns the data within the cell on the right side.

justify:

•  Aligns the data within the cell by adjusting the text at the edges.

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

15
 


set
 the
 alignment
 with
 style
 you
 can
 use
 the
 text-­‐align

 aHribute
 to
 specify
 the
 
Ÿ  To
 
horizontal
 alignment.
 
 

Ÿ  The
 Code
 Snippet
 demonstrates
 how
 to
 center
 align
 the
 table
 data.
 
<!DOCTYPE HTML>
<html>
<head>
<title>Automobile Gallery</title>
</head>
<body>

<table border=”1”>
<tr>
<th>Sr.No.</th>
<th>Medicine Name</th>
<th>Price</th>
</tr>
<tr style=”text-align: center;”>
<td>1</td>
<td>Captopril</td>
<td>12.45</td>
</tr>

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

16
 



<tr style=”text-align: center;”>
<td>2</td>
<td>Ceftriaxone</td>
<td>6.94</td>
</tr>
<tr style=”text-align: center;”>
<td>3</td>
<td>Ciprofloxacin</td>
<td>56.21</td>
</tr>
</table>
</body>
</html>

code
 aligns
 the
 data
 within
 the
 row
 using
 a
 style
 within
 the
 <tr>
 element.
 
Ÿ  The

 
table
  content
  is
  center
  aligned
  by
  seWng
  the
  value
  of
  the
  text-align
 
Ÿ  The
 
aHribute
 to
 center.
 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /

 Session
 9
 
 

17
 


Ÿ  Following
 figure
 displays
 the
 horizontal
 alignment.
 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 


18
 


can
  ver(cally
  align
  the
  posi(on
  of
  data
  earlier
  by
  using
  the
  valign
 
Ÿ  Users
 
aHribute.
 

Ÿ  HTML5
 has
 deprecated
 the
 valign
 aHribute.
 

 
Ÿ  The
 possible
 values
 of
 ver(cal
 alignment
 are
 as
 follows:
 
top:
•  Vertically aligns the data within the cell at the top.

middle:
•  Vertically aligns the data within the cell at the center.

bottom:
•  Vertically aligns the data within the cell at the bottom.

Ÿ  To
 set
 the
 alignment
 with
 the
 style,
 you
 can
 use

 the
 text-align
 aHribute
 to
 
specify
 the
 ver(cal
 alignment
 use
 the
 following
 syntax:
 
Syntax:
 
<td style= “text align: center; vertical align: middle”>
Aptech Web site </a>
©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 

 

19
 


style
 can
 also
 be
 applied
 to
 individual
 rows,
 cells,
 or
 to
 the
 en(re
 table.
 
Ÿ  The
 
Ÿ  The
 Code
 Snippet
 demonstrates
 how
 to
 align

 the
 data
 ver(cally
 within
 the
 table
 
using
 the
 style
 aHribute.
 
<!DOCTYPE HTML>
<html>
<head>
<title>CelinaBatteries</title>
</head>
<body>
<table border=”1”>
<tr>
<th>Sr.No.</th>
<th>Product Id</th>
<th>Product Description</th>
</tr>
<tr>
<td style=”text-align: center; vertical-align: middle”>1
</td>
<td style=”text-align: center; vertical-align: middle”>P101
</td>
<td>1.5 Volts AA Ultra Alkaline</td>

</tr>
©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

20
 


<tr>
<td style=”text-align: center; vertical-align: middle”>2
</td>
<td style=”text-align: center; vertical-align: middle”>
M105
</td>
<td>9 Volts pp3 Super Alkaline</td>
</tr>
</table>
</body>
</html>


text-align
 aHribute
 is
 set
 to
 the
 value
 center,
 which
 specifies
 that
 the
 
Ÿ  The
 
data
 within
 the
 rows
 are
 centrally
 aligned.
 
 

Ÿ  The
 vertical-align
 is
 used

 to
 specify
 the
 ver(cal
 alignment
 in
 the
 table.
 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

21
 


Ÿ  Following
 figure

 displays
 the
 ver(cal
 alignment.
 

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

22
 


data
 in
 a
 table
 might
 appear

 cluHered,
 which
 may
 affect
 the
 readability.
 
 
Ÿ  The
 
might
 make
 it
 difficult
 to
 comprehend
 data
 as
 the
 data.
 
 
Ÿ  This
 
Ÿ  To
 overcome
 this
 issue,
 use
 the

 cell
 margin
 aHributes.
 
Ÿ  Cell
 padding
 allows
 the
 user
 to
 control
 the
 look
 of
 the
 content
 on
 a
 page.
 

Ø  Padding
 
 
is
 the
 amount
 of
 space
 between

 the
 content
 and
 its
 outer
 edge.
 
 
Ÿ  Padding
 
tables,
 padding
 is
 referred
 as
 a
 space
 between
 the
 text
 and
 the
 cell
 border.
 
Ÿ  For
 
Ÿ  Suppose,
 if
 the

 user
 wants
 to
 set
 the
 padding
 aHribute
 for
 the
 individual
 cells
 
then
 padding
 aHribute
 can
 be
 used
 in
 a
 style
 as
 follows:
 
<td style=”padding: 4px”>

©
 Aptech
 Ltd.
 

 

Crea(ng
 Tables
 /
 Session
 9
 
 

23
 


specify
 the
 main
 heading
 for
 the
 table,
 use
 the
 <caption>
 element.
 
 
Ÿ  To
 
<caption>

 element
 defines
 a
 cap(on
 for
 the
 table.
 It
 is
 a
 sub-­‐element
 of
 
Ÿ  The
 
the
 <table>
 element.
 
 

must
 be
 present
 immediately
 a\er
 the
 <table>
 tag.
 

Ÿ  It
 
<caption>
 element
 allows
 the
 user
 to
 specify
 a
 (tle
 for
 your
 en(re
 table.
 
 
Ÿ  The
 
Ÿ  There
 can
 be
 only
 one
 cap(on
 for
 a
 table.
 
Ÿ  The

 Code
 Snippet
 demonstrates
 how
 to
 specify
 a
 heading
 for
 a
 table.
 
<!DOCTYPE HTML>
<html>
<head>
<title>Travel Expense Report</title>
</head>
<body>
<table border=”1”>
<caption>Travel Expense Report</caption>
<tr>
<th> </th>
<th>Meals</th>
<th>Hotels</th>
<th>Transport</th>
</tr>

©
 Aptech
 Ltd.

 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

24
 


<tr>
<td>25-Apr</td>
<td>37.74</td>
<td>112.00</td>
<td>45.00</td>
</tr>
<tr>
<td>26-Apr</td>
<td>27.28</td>
<td>112.00</td>
<td>45.00</td>
</tr>
<tr>
<td>Totals</td>
<td>65.02</td>

<td>224.00</td>
<td>90.00</td>
</tr>
</table>
</body>
</html>

©
 Aptech
 Ltd.
 
 

Crea(ng
 Tables
 /
 Session
 9
 
 

25
 


×