HTML Forms: Elements and Syntax Guide
HTML Forms: Elements and Syntax Guide
HTML Form:
An HTML form is a section of a document which contains controls such as text fields, password
fields, checkboxes, radio buttons, submit button, menus etc.
An HTML form facilitates the user to enter data that is to be sent to the server for processing.
HTML forms are used to collect user information from the webpage and send it to the server.
HTML forms are required if you want to collect some data from of the site visitor.
For example: If a user want to purchase some items on internet, he/she must fill the form such as
shipping address and credit/debit card details so that item can be sent to the given address.
Creating registration forms so that users can sign up with their information and
authenticate further to access the functionalities of the websites/web applications.
Collect data through the different types of surveys, feedback, etc.
Uploading the images, resumes, or any other type of files.
Tag Description
<form> It defines an HTML form to enter inputs by the used side.
<input> It defines an input control.
<textarea> It defines a multi-line input control.
<label> It defines a label for an input element.
<fieldset> It groups the related element in a form.
<legend> It defines a caption for a <fieldset> element.
<select> It defines a drop-down list.
2
Tag Description
<datalist It specifies a list of pre-defined options for input control.
>
<keygen It defines a key-pair generator field for forms.
>
<output> It defines the result of a calculation.
Working with form elements typically involves creating and managing various HTML form
controls, such as text fields, checkboxes, radio buttons, dropdowns, and buttons. Form elements
allow users to input data which can then be submitted to a server or processed on the client side.
Here’s a quick overview of the basic form elements and how you might work with them:
1. Text Fields
Password fields are used for sensitive input, hiding the characters typed.
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<br><br>
3
3. Checkboxes
<label for="gender-male">Male</label>
<input type="radio" id="gender-male" name="gender" value="male">
<br><br>
<label for="gender-female">Female</label>
<input type="radio" id="gender-female" name="gender" value="female">
<br><br>
5. Select Dropdown (Drop-down List)
<label for="country">Country:</label>
<select id="country" name="country">
<option value="us">United States</option>
<option value="ca">Canada</option>
<option value="uk">United Kingdom</option>
</select>
<br><br>
6. Textarea
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50"></textarea>
<br><br>
7. Buttons
<button type="submit">Submit</button>
<button type="reset">Reset</button>
4
<html>
<head>
</head>
<body bgcolor="pink">
<form >
<label for="name">Name:</label>
<label for="password">Password:</label>
<label for="email">Email:</label>
<label for="age">Age:</label>
<label>Gender:</label>
<label for="male">Male</label>
<label for="female">Female</label><br><br>
<label>Hobbies:</label><br>
<label for="reading">Reading</label><br>
<label for="traveling">Traveling</label><br>
<label for="music">Music</label><br><br>
<option value="india">India</option>
<option value="usa">USA</option>
<option value="uk">UK</option>
<option value="canada">Canada</option>
<br>
6
<br>
</form>
</body>
</html>
Output:
Form attributes:
Form attributes are special settings that you can add to the <form> tag to control how the form
behaves. Think of them as the instructions for your form: they tell the browser where to send the
data, how to send it, and how to handle the form. Let's break them down.
7
1. action
o Tells the form where to send the data when it's submitted (usually to a server or a
specific page).
o Example:
<form action="/submit">
o Explanation: When the form is submitted, the data will be sent to /submit.
2. method
o Tells the browser how to send the form data. There are two main methods:
GET: Sends the data as part of the URL (in the browser’s address bar).
POST: Sends the data as part of the request body (not visible in the URL).
o Example:
Explanation: This sends the form data securely to the server at /submit using the
POST method, which is safer for sensitive information.
3. target
o Specifies where to open the response after the form is submitted.
o Common values:
_self: Open in the same tab or window (default).
_blank: Open in a new tab or window.
o Example:
o Explanation: The form data will be sent to /submit, and the response will open in
a new tab.
4. enctype
o Specifies how the form data should be encoded when sending it to the server
(useful for file uploads).
o Example:
5. name
o Provides a name for the form (useful for referencing the form in JavaScript).
o Example:
<form name="contactForm">
8
6. autocomplete
o Controls whether the browser should automatically fill in form fields based on
the user's previous input.
o Common values:
on: Enable autocomplete (default).
off: Disable autocomplete.
o Example:
o Explanation: This form will not show any saved input in the form fields.
7. accept-charset
o Specifies the character encoding to be used when submitting the form data to the
server.
o Example:
o Explanation: The form will use the UTF-8 encoding when sending the data.
note:
note:
Working with IFrames: An HTML iframe is used to display a web page within a web page.
An inline frame is used to embed another document within the current HTML document.
Syntax
<iframe src="url" title="description"></iframe>
Use the height and width attributes to specify the size of the iframe.
Example
<iframe src="demo_iframe.htm" height="200" width="300" title="Iframe Example"></iframe>
Example:
File :[Link]
<html>
<body bgcolor="pink">
<h2> Iframes</h2>
<p>The HTML tag specifies an inline frame. An inline frame is used to embed another
document within the
current HTML document.</p>
<br>
<p>The most common use cases include: Embedding Multimedia Content – Iframe elements are
commonly
used to embed multimedia content like videos, audio files, and animations from platforms such
as YouTube, SoundCloud.</p>
</body>
</html>
File:[Link]
<html>
<body>
10
<h2>HTML Iframes</h2>
<p>You can use the height and width attributes to specify the size of the iframe:</p>
</body>
</html>
OUTPUT:
To build a responsive webpage using HTML, focus on using the viewport meta tag, fluid grids,
and responsive images.
The viewport meta tag is a key component in building responsive websites. It controls how
your webpage is displayed and scaled on different devices, especially mobile phones and tablets.
🔍Explanation of Attributes:
Attribute Description
width=device-width Sets the width of the viewport to the device’s screen width.
initial-scale=1.0 Sets the initial zoom level when the page is loaded.
fluid grids:
A fluid grid layout uses relative units like percentages (%) instead of fixed units like pixels (px)
to create layouts that automatically adjust to the screen size. This is a core principle of
responsive web design.
Responsive Images:
Responsive images automatically resize to fit the screen size or container, making them essential
for mobile-friendly websites.
img {
max-width: 100%;
height: auto;
}
Cascading Style Sheets:
CSS Introduction:
What is CSS?
CSS (Cascading Style Sheets) is the language used to style HTML elements — controlling
layout, colors, fonts, spacing, responsiveness, and more.
CSS Syntax
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded
by curly braces.
Example Explained
p is a selector in CSS (it points to the HTML element you want to style: <p>).
color is a property, and red is the property value
text-align is a property, and center is the property value
<html> OUTPUT:
<head>
<style>
p{
color: red;
text-align: center;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>These paragraphs are styled
with CSS.</p>
</body>
</html>
CSS Comments:
CSS comments are used to add notes or explanations inside your stylesheet. They are ignored
by browsers and do not affect the layout or performance.
13
A CSS comment is placed inside the <style> element, and starts with /* and ends with */
Example
/* This is a single-line comment */
p{
color: red;
}
Example
p{
color: red; /* Set text color to red */
}
Example
p{
color: /*red*/blue;
}
Example
/* This is
a multi-line
comment */
p{
color: red;
}
<html>
<head>
<style>
p{
color: red; /* Set text color to red */
}
14
</style>
</head>
<body>
<h2>My Heading</h2>
</body>
</html>
OUTPUT:
You can apply styles to HTML elements using CSS in three main ways:
1. Inline CSS
The following example sets the text color of the <h1> element to blue, and the text color of
the <p> element to red:
Example
<html>
<body>
15
</body>
</html>
Output:
A Blue Heading
A red paragraph.
2. Internal CSS
It is Written inside a <style> tag within the <head> section of the HTML
The following example sets the text color of ALL the <h1> elements (on that page) to blue, and
the text color of ALL the <p> elements to red. In addition, the page will be displayed with a
"powderblue" background color:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
16
3. External CSS
Stored in a separate .css file and linked to your HTML using <link>.
An external style sheet is used to define the style for many HTML pages.
To use an external style sheet, add a link to it in the <head> section of each HTML page:
[Link]:
<html>
<head>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<h1>Hello from external CSS</h1>
</body>
</html>
[Link]:
h1 {
color: purple;
font-family: Verdana;
}
To run:
Click [Link]
Output:
In CSS (Cascading Style Sheets), style specifications define how HTML elements should be
displayed. There are three main formats for specifying CSS styles:
In CSS, styles are defined by properties and values. However, the syntax of how these
properties and values are written can follow a few specific formats. These formats help
17
determine the type of styling to be applied. Let’s explore the different ways you can define styles
in CSS:
This is the basic and most common format in CSS, where each property is followed by a value.
selector {
property: value;
}
Example:
body {
background-color: #f0f0f0;
color: #333;
}
In this case:
Some CSS properties allow for shorthand notation, combining multiple properties into one line.
font-style: italic;
font-size: 20px;
font-family: Arial, sans-serif;
Selector forms:
In CSS, selectors are patterns used to select and style specific HTML elements. They define to
which HTML elements the styles should be applied to.
18
Here are the main forms of CSS selectors, explained with simple examples:
The element selector selects HTML elements based on the element name. Selects elements by
their tag name.
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
p{
text-align: center;
color: red;
}
<html>
<head>
<style>
p{
text-align: center;
color: red;
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
OUTPUT:
19
The id of an element is unique within a page, so the id selector is used to select one unique
element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element.
Example
The CSS rule below will be applied to the HTML element with id="para1":
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
20
OUTPUT:
Hello World!
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with class="center" will be red and center-aligned:
<html>
<head>
<style>
.center {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
OUTPUT:
21
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
<html>
<head>
<style>
*{
text-align: center;
color: blue;
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>And me!</p>
</body>
</html>
OUTPUT:
22
Hello world!
Me too!
And me!
h1, h2, p {
color: green;
}
CSS Margins:
Margins are used to create space around elements, outside of any defined borders. The
CSS margin properties are used to create space around elements, outside of any defined borders.
With CSS, you have full control over the margins. There are properties for setting the margin for
each side of an element (top, right, bottom, and left).
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
Example
<html>
<head>
23
<style>
div{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
background-color: lightblue;
</style>
</head><body>
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of
100px, and a left margin of 80px.</div>
</body> </html>
Output:
To shorten the code, it is possible to specify all the margin properties in one property.
The margin property is a shorthand property for the following individual margin properties:
margin-top
margin-right
margin-bottom
24
margin-left
Example
p{
margin: 25px 50px 75px 100px;
}
You can set the margin property to auto to horizontally center the element within its container.
The element will then take up the specified width, and the remaining space will be split equally
between the left and right margins.
25
Example
<html>
<head>
<style>
div {
width: 300px;
margin: auto;
</style>
</head>
<body>
<p>You can set the margin property to auto to horizontally center the element within its
container. The element will then take up the specified width, and the remaining space will be
split equally between the left and right margins:</p>
<div>
</div>
</body>
</html>
Output:
26
CSS Padding:
Padding is used to create space around an element's content, inside of any defined borders.
The CSS padding properties are used to generate space around an element's content, inside of
any defined borders.
With CSS, you have full control over the padding. There are properties for setting the padding
for each side of an element (top, right, bottom, and left).
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
Example
<html>
<head>
<style>
div {
27
background-color: lightblue;
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
</style>
</head>
<body>
<div>This div element has a top padding of 50px, a right padding of 30px, a bottom padding of
50px, and a left padding of 80px.</div>
</body>
</html>
Output:
To shorten the code, it is possible to specify all the padding properties in one property.
The padding property is a shorthand property for the following individual padding properties:
28
padding-top
padding-right
padding-bottom
padding-left
Example
div {
padding: 25px 50px 75px 100px;
}
Example
div {
padding: 25px 50px 75px;
}
Example
div {
padding: 25px 50px;
}
padding: 25px;
o all four paddings are 25px
Example
div {
padding: 25px;
}
CSS Height:
If height: auto; the element will automatically adjust its height to allow its content to be
displayed correctly.
If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit
within the specified height, it will overflow. How the container will handle the overflowing
content is defined by the overflow property.
CSS Syntax
height: auto|length|initial|inherit;
Property Values
Value Description
length Defines the height in px, cm, etc. Read about length units
initial Sets this property to its default value. Read about initial
<html>
<head>
<style>
.box1 {
background-color: lightblue;
height: 100px;
width: 200px;
.box2 {
background-color: lightgreen;
height: 50%;
width: 200px;
.container {
background-color: lightgray;
height: 300px;
width: 220px;
31
padding: 10px;
</style>
</head>
<body>
<div class="container">
</div>
</body>
</html>
Output:
CSS Syntax
32
width: auto|value|initial|inherit;
Property Values
Value Description
CSS Text:
CSS Text properties are used to control the appearance and layout of text content on a webpage.
Here are the most commonly used text-related properties in CSS:
🔹 1. color
p{
color: blue;
}
2. text-align
h1 {
text-align: center;
}
33
3. text-decoration
a{
text-decoration: none;
}
4. text-transform
h2 {
text-transform: uppercase;
}
5. letter-spacing
p{
letter-spacing: 2px;
}
6. word-spacing
p{
word-spacing: 5px;
}
7. line-height
p{
line-height: 1.5;
}
34
8. text-indent
p{
text-indent: 50px;
}
9. white-space
pre {
white-space: pre;
}
10. direction
p{
direction: rtl;
}
Example:
<html>
<head>
<style>
.text-example {
</style>
</head>
<body>
this is an example paragraph to demonstrate all css text properties used in a single program. it
shows how
you can control the appearance of text on a web page with color, alignment, spacing, decoration,
and more.
</p>
</body>
</html>
Output:
Property Description
Text alignment in CSS is controlled using the text-align property. It specifies the horizontal
alignment of text inside an element (like a paragraph, heading, div, etc.).
Syntax:
selector {
text-align: value;
}
<html>
<head>
<style>
.left-align {
text-align: left;
.right-align {
text-align: right;
.center-align {
text-align: center;
.justify-align {
text-align: justify;
</style>
</head>
<body>
</body>
38
</html>
Output:
The text-decoration property in CSS is used to add or remove decorations from text, such as
underlines, overlines, line-throughs, or none.
Syntax:
selector {
text-decoration: value;
}
Example:
<html>
<head>
<style>
.underline {
text-decoration: underline;
39
.overline {
text-decoration: overline;
.line-through {
text-decoration: line-through;
.no-decoration {
text-decoration: none;
.combined {
</style>
</head>
<body>
</body>
</html>
Output:
40
The text-transform property in CSS is used to control the capitalization of text. It allows you to
make text appear in uppercase, lowercase, or with capitalized first letters—without changing
the original HTML content.
Syntax:
selector {
text-transform: value;
}
<html>
<head>
<style>
.none {
text-transform: none;
41
.capitalize {
text-transform: capitalize;
.uppercase {
text-transform: uppercase;
.lowercase {
text-transform: lowercase;
</style>
</head>
<body>
</body>
</html>
Output:
In CSS, text spacing controls the amount of space between characters, words, and lines in a
block of text. The main properties are:
1. letter-spacing
p{
letter-spacing: 2px;
}
Value Description
2. word-spacing
p{
word-spacing: 5px;
}
Value Description
3. line-height
p{
line-height: 1.6;
}
43
Value Description
<html>
<head>
<style>
.spacing-example {
</style>
</head>
<body>
<p class="spacing-example">
This paragraph shows how to use letter spacing, word spacing, and line height in CSS. </p>
</body>
</html>
Output:
44
The text-shadow property in CSS adds shadow effects to text. It's used to enhance the
appearance of text by giving it a shadow with custom color, blur, and offset.
Syntax:
selector {
text-shadow: h-offset v-offset blur-radius color;
}
Parameters:
Parameter Description
<html>
<head>
<style>
.shadow-simple {
.shadow-blur {
.shadow-multiple {
</style>
45
</head>
<body>
</body>
</html>
1. .shadow-simple
.shadow-simple {
text-shadow: 2px 2px red;
}
Explanation:
Result: A simple red shadow to the right and bottom of the text.
2. .shadow-blur
.shadow-blur {
text-shadow: 3px 3px 5px gray;
}
Explanation:
Result: A soft gray shadow with a blurry effect around the text.
3. .shadow-multiple
.shadow-multiple {
text-shadow: 2px 2px 2px black, -2px -2px 2px lightgray;
}
46
Explanation:
1. First Shadow:
o 2px 2px 2px black:
➤ Right and down with black color and blur
2. Second Shadow:
o -2px -2px 2px lightgray:
➤ Left and up with light gray color and blur
Result: A layered, 3D effect with black shadow on one side and light gray on the other.
Summary Table:
Class Effect Appearance
CSS has several properties related to fonts that allow you to control the appearance of text.
Below are the main CSS font properties:
font-style
font-variant
font-weight
font-size/line-height
font-family
<html>
<head>
47
<style>
p.a {
font-style: normal;
p.b {
font-style: italic;
p.c {
font-style: oblique;
</style>
</head>
<body>
</body>
</html>
Output:
48
<html>
<head>
<style>
[Link] {
font-variant: normal;
[Link] {
font-variant: small-caps;
</style>
</head>
<body>
</body>
</html>
Output:
h1 {
font-weight: bold;
}
Possible values:
o normal: Regular weight.
o bold: Bold weight.
o Numeric values (100 to 900), where 400 is normal and 700 is bold.
<html>
<head>
<style>
div.a {
line-height: normal;
div.b {
line-height: 1.6;
div.c {
line-height: 80%;
div.d {
line-height: 200%;
</style>
50
</head>
<body>
</div>
</div>
<h2>line-height: 80%:</h2>
</div>
<h2>line-height: 200%:</h2>
</div>
</body>
</html>
Output:
51
<html>
<head>
<style>
p.a {
p.b {
52
font-family: Arial;
</style>
</head>
<body>
</body>
</html>
Output:
CSS List:
In CSS, lists can be styled in various ways to control their appearance. Here’s a clear explanation
of how CSS handles lists:
1. list-style-type
Example:
ul {
list-style-type: square;
}
Common values:
53
disc (default)
circle
square
decimal (for <ol>)
lower-roman
upper-alpha
none (removes bullets/numbers)
2. list-style-position
Defines whether the marker appears inside or outside the list item's content box.
Example:
ul {
list-style-position: inside;
}
Values:
inside
outside (default)
3. list-style-image
Example:
ul {
list-style-image: url('[Link]');
}
4. list-style
Example:
ul {
list-style: square inside url('[Link]');
}
Example:
<html>
<head>
54
<style>
[Link] list {
list-style-type: square;
color: darkblue;
font-size: 18px;
[Link] list {
list-style-type: upper-roman;
color: green;
font-weight: bold;
</style>
</head>
<body>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
<li>Step One</li>
<li>Step Two</li>
<li>Step Three</li>
</ol>
</body>
</html>
Output:
The CSS box model: The CSS box model is essentially a box that wraps around every HTML
element. It consists of: content, padding, borders and margins.
The CSS Box Model is a fundamental concept in web design that describes how elements are
structured and spaced on a webpage. Every HTML element is treated as a box, and this model
defines how the size of that box is calculated.
Program:
<html>
<head>
<style>
.box1 {
width: 300px;
height: 100px;
padding: 20px;
margin: 20px;
background-color: lightblue;
font-family: Arial;
.box2 {
57
padding: 40px;
background-color: lightgreen;
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
</html>
Output:
58
CSS links:
In CSS, links (<a> tags) can be styled to look and behave differently in various states like
normal, hover, visited, etc. This makes your website more interactive and visually appealing.
Selector Description
<html>
59
<head>
<style>
a:link {
color: blue;
text-decoration: none;
a:visited {
color: red;
a:hover {
color: red;
text-decoration: underline;
a:active {
color: green;
</style>
</head>
<body>
</body>
</html>
60
Explanation:
Output:
You get
CSS Position:
The position property specifies the type of positioning method used for an element (static,
relative, absolute, fixed, or sticky).
In CSS, the position property is used to control how elements are placed on the page. It defines
the positioning method and allows elements to be moved precisely using top, right, bottom, and
left properties.
Note:
<html>
<head>
<style>
.box {
width: 150px;
height: 100px;
background-color: lightblue;
margin: 20px;
padding: 10px;
.relative {
position: relative;
top: 20px;
left: 30px;
background-color: orange;
.absolute {
position: absolute;
top: 50px;
left: 200px;
62
background-color: lightgreen;
.fixed {
position: fixed;
top: 10px;
right: 10px;
background-color: red;
color: white;
</style>
</head>
<body>
</body>
</html>
Output:
63
CSS Z-index
The z-index property controls which element appears in front when elements overlap.
Basic Concept:
Syntax:
element {
position: relative;
z-index: 2;
}
<html>
<head>
<style>
.box {
width: 200px;
height: 200px;
position: absolute;
.box1 {
background-color: red;
top: 50px;
left: 50px;
z-index: 1;
.box2 {
background-color: yellow;
top: 100px;
left: 100px;
z-index: 2;
.box3 {
background-color: pink;
top: 150px;
left: 150px;
z-index: 3;
</style>
65
</head>
<body>
</body>
</html>
Output: