Every topic starts at New. Sign in and your practice counts.
Forms, tables and animation turn plain pages into pages that collect input, organize records and highlight key notes. This topic builds those three skills through the doctor visit example.
This topic teaches how to collect user input with HTML forms, organize data with HTML tables and CSS styling, and add movement with CSS animations.
The form that is used to gather user input is defined within the form tag pair.
A form is defined within the form tag pair. Inside it, the label element gives a clear title for each input field, and the for attribute ties a label to the matching form element. The input element can establish several types of fields such as text fields, checkboxes and buttons, while select builds a drop down list and textarea creates a large box for longer writing.
<form>
<label for="studentName">Student Name:</label>
<input type="text" id="studentName" name="studentName"><br><br>
The label here tells the user to enter the student name, and the input with the matching id provides the text box. A drop down for grade limits the user to the supplied choices only.
<label for="grade">Grade:</label>
<select id="grade" name="grade">
<option value="1">8</option>
<option value="2">9</option>
<option value="3">10</option>
</select><br><br>
Radio buttons are for mutually exclusive choices where only one option can be true at a time. A list field lets users choose one or more items from available options, and it is often used for checkbox groups or drop down menus. All the related radio buttons share the same name, with a different value for each choice. Checkboxes are for preferences where the user may pick more than one item from a group. To create checkboxes, set the input type to checkbox with an id, name and value, and place the matching label beside each box.
<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br><br>
<label for="sports">Sports Participated:</label><br>
<input type="checkbox" id="cricket" name="sports" value="cricket">
<label for="cricket">Cricket</label><br>
<input type="checkbox" id="badminton" name="sports" value="badminton">
<label for="badminton">Badminton</label><br>
<input type="checkbox" id="hockey" name="sports" value="hockey">
<label for="hockey">Hockey</label><br>
<input type="checkbox" id="basketball" name="sports" value="basketball">
<label for="basketball">Basketball</label><br><br>
The submit button is the typical control for transferring information from a form to a server. The reset button clears every field in the form when the user clicks it. The doctor visit form uses action to state where the input data will be submitted and method to state the way of submission, and it collects different sorts of data with matching input types for names, email addresses, phone numbers, dates and times.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doctor's Visit Form</title>
</head>
<body>
<h1>Doctor's Visit Form</h1>
<form action="submit_form.html" method="post"> <p>Patient Information:</p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" required> <br>
<input type="radio" id="male" name="Gender" value="Male">
<label for="male">Male</label>
<input type="radio" id="female" name="Gender" value="Female">
<label for="female">Female</label><br><br>
<input type="checkbox" id="visit1" name="visit1" value="First">
<label for="visit1"> First Visit </label>
<input type="checkbox" id="visit2" name="visit2" value="Second">
<label for="visit2"> Follow up Visit </label>
<input type="checkbox" id="visit3" name="visit3" value="Regular">
<label for="visit3"> Regular Check-up Visit</label><br> <br>
<label for="email">Email Address:</label>
<input type="email" name="email" id="email" required> <br>
<label for="phone">Phone Number:</label>
<input type="tel" name="phone" id="phone" required> <br>
<p>Appointment Details:</p>
<label for="date">Date of Appointment:</label>
<input type="date" name="date" id="date" required> <br>
<label for="time">Time of Appointment:</label>
<input type="time" name="time" id="time" required> <br>
<p>Reason for Visit:</p>
<textarea name="reason" id="reason" rows="5" cols="30" placeholder="
Describe your reason for visit here..."></textarea> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>
The completed form collects patient particulars and appointment details, then shows a submit button and a confirmation message in the output.
Tables organize information on webpages in an easy to read and ordered manner.
A cell is the basic rectangular building block of a table and can hold text or graphics. Cells arranged across one line form a row, while the vertical classification forms a column. A header is a special cell that names or classifies the information in the same column, and header contents are bold and centered to highlight their significance.
The table element marks the beginning and end of a table. Inside it, tr creates a row, th marks a header cell, td marks an ordinary cell with the real data, caption puts a heading on the table, thead, tbody and tfoot divide rows into header, main data and summary parts, and colspan and rowspan let a cell span several columns or rows for complicated layouts.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doctor's Visit Appointments</title>
</head>
<body>
<h1>Doctor's Visit Appointments</h1>
<table>
<thead>
<tr>
<th>Patient Name</th>
<th>Date</th>
<th>Time</th>
<th>Doctor</th>
<th>Reason for Visit</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adil</td>
<td>2024-05-10</td>
<td>10:00 AM</td>
<td>Dr. Shams</td>
<td>Follow-up</td>
</tr>
<tr>
<td>Zaki</td>
<td>2024-05-15</td>
<td>2:00 PM</td>
<td>Dr. Qamar</td>
<td>Check-up</td>
</tr>
</tbody>
</table>
</body>
</html>
The head section places patient name, date, time, doctor and reason for visit as labels on the top row. The tbody section holds the actual appointments, with one tr for each appointment and one td for each piece of information.
A plain table can look congested and hard to read. The link between the borders of neighbouring cells is controlled by border-collapse. The collapse setting combines neighbouring borders into a single line so the table looks neater and more compact, while the separate setting keeps the edges distinct so the table looks larger.
| Setting | Borders | Appearance |
|---|---|---|
| collapse | Combine into a single line | Neater and more compact |
| separate | Stay distinct | Larger with visible gaps |
<h4>Table with border-collapse: collapse</h2>
<table>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</table>
<h4>Table with border-collapse: separate</h2>
<table border="1" border-collapse="separate">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</table>
The caption tag gives an overview of the data by placing a caption on top of the table. Padding is the distance between a cell border and its content, and it can be set for all four sides or for one side only. Cell spacing is the space among the borders of neighbouring cells, and adjusting it makes the table appear lengthened.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doctor's Visit Appointments</title>
<style>
table {
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: gray;
}
</style>
</head>
<body>
<h1>Doctor's Visit Appointments</h1>
<table>
<caption>List of upcoming appointments</caption>
<thead>
<tr>
<th>Patient Name</th>
<th>Date</th>
<th>Time</th>
<th>Doctor</th>
<th>Reason for Visit</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adil</td>
<td>2024-05-10</td>
<td>10:00 AM</td>
<td>Dr. Shams</td>
<td>Follow-up</td>
</tr>
<tr>
<td>Zaki</td>
<td>2024-05-15</td>
<td>2:00 PM</td>
<td>Dr. Qamar</td>
<td>Check-up</td>
</tr>
</tbody>
</table>
</body>
</html>
Background color can be set for a whole table, a row or a single cell. In the refined appointment table, border-collapse removes the extra lines between cells, padding adds space so the text is easier to read, text-align lines the cell text to the left, and a gray background makes the headers prominent. A later style fills the whole breadth of the available area with width set to full, and adds a thin light gray line around each cell so each cell stands out clearly.
<table border="1" style="background-color: yellow;">
<tr>
<td>Cell 1</td>
<td style="background-color: cyan;">Cell 2</td>
<td>Cell 3</td>
</tr>
</table>
<IDOCTYPE html>
<html lang="en">
<head>
<title>Doctor's Visit Appointments</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid lightgray;
}
th {
background-color: gray;
}
</style>
</head>
CSS animation changes the appearance and movement of elements by defining styles at intermediate moments in a sequence.
An animation is built from keyframes that specify the styles of an item at different moments. The style of the element during animation is specified by the keyframes rule, and each keyframe position on the timeline is given a percentage from 0 percent to 100 percent. The div tag acts as a container that groups and styles other components as a single unit for layout purposes. Formatting for HTML elements is defined with the style element, which sits in the head portion and applies its rules to the matching page elements.
A basic animation spins a red box repeatedly. An element with the animated-box id is created with a div, and its size, position and red background are set in the style element. The spin animation runs for two seconds, repeats indefinitely with linear speed, and moves the element from 0 degrees to a full 360 degree turn.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Animation</title>
<style>
#animated-box {
margin-top: 50px;
margin-left: 50px;
width: 100px;
height: 100px;
background-color: red;
animation: spin 2s infinite linear;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div id="animated-box"></div>
</body>
</html>
Another animation changes the color of text. The keyframes rule called colorChange sets the order in which the color change takes place, smoothly moving the text assigned the animated-text id from blue to green every two seconds.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Color Change Animation</title>
<style>
#animated-text {
font-size: 20px;
color: blue;
animation: colorChange 2s infinite alternate;
}
@keyframes colorChange {
from { color: blue; }
to { color: green; }
}
</style>
</head>
<body>
<p id="animated-text">This text will change color!</p>
</body>
</html>
A paragraph with the highlighted-text id starts with a translucent background and is changed to yellow to emphasize the content. The highlight animation repeats endlessly for two seconds, and the keyframes rule outlines the actual animation sequence.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Text Highlighting Animation</title>
<style>
#highlighted-text {
font-size: 20px;
color: black;
background-color: transparent;
animation: highlight 2s ease-in-out alternate infinite;
}
@keyframes highlight {
from { background-color: transparent; }
to { background-color: yellow; }
}
</style>
</head>
<body>
<p id="highlighted-text">This text will be highlighted!</p>
</body>
</html>
The doctor appointment table adds animation style as a footnote. The whole table keeps uniform borders, padding and alignment for a well organized layout. Every alternate row gets a different background through the nth-child odd rule, so all the odd numbered rows stand out. A footnote rule styles the cell with the footnote id for font size and animation, and its highlight keyframes move the background from translucent to yellow and back.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doctor's Visit Appointments</title>
<style>
table {
border: 2px solid lightgray;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: lightblue;
color: white;
}
tr:nth-child(odd) {
background-color: gray;
}
#footnote {
font-size: 12px;
animation: highlight 2s ease-in-out alternate infinite;
}
@keyframes highlight {
from { background-color: transparent; }
to { background-color: yellow; }
}
</style>
</head>
<body>
<h1>Doctor's Visit Appointments</h1>
<table>
<caption>List of upcoming appointments</caption>
<thead>
<tr>
<th>Patient Name</th>
<th>Date</th>
<th>Time</th>
<th>Doctor</th>
<th>Reason for Visit</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adil</td>
<td>2024-05-10</td>
<td>10:00 AM</td>
<td>Dr. Shams</td>
<td>Follow-up</td>
</tr>
<tr>
<td>Zaki</td>
<td>2024-05-15</td>
<td>2:00 PM</td>
<td>Dr. Qamar</td>
<td>Check-up</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" id="footnote">* The
appointments may subject to change.</td>
</tr>
</tfoot>
</table>
</body>
</html>
A single table cell with the footnote id spans all five columns, so the highlighted footnote projects the important note that appointments may be subject to change.
<table border="1"
border-collapse="border-collapse">
<tr>
<td style="padding: 10px">Cell Number 1</td>
<td style="padding: 20px">Cell 2</td>
<td style="padding: 5px">Cell 3</td>
</tr>
</table>
<table border="1" cellspacing="20">
<tr>
<td>Cell Number 1</td>
<td>Cell No. 2</td>
<td>Cell 3</td>
</tr>
</table>
<body>
<h1>Doctor's Visit Appointments</h1>
<table cellspacing="0">
<thead>
<tr>
<th>Patient Name</th>
<th>Date</th>
<th>Time</th>
<th>Doctor</th>
<th>Reason for Visit</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adil</td>
<td>2024-05-10</td>
<td>10:00 AM</td>
<td>Dr. Shams</td>
<td>Follow-up</td>
</tr>
<tr>
<td>Zaki</td>
<td>2024-05-15</td>
<td>2:00 PM</td>
<td>Dr. Qamar</td>
<td>Check-up</td>
</tr>
</tbody>
</table>
</body>
</html>