Web UNIT-1
Web UNIT-1
Web UNIT-1
CSS is used along with HTML and JavaScript in most websites to create user interfaces
for web applications and user interfaces for many mobile applications.
CSS Example
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2
specification, it started a nightmare for web developers. Development of large
- 1-
websites, where fonts and color information were added to every single page,
became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
CSS Syntax
Example
In this example all <p> elements will be center-aligned, with a red text color:
p {
color: red;
text-align: center;
}
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
- 2-
1. CSS Element Selector
2. CSS Id Selector
3. CSS Class Selector
4. CSS Universal Selector
5. CSS Group Selector
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p{
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <p>This style will be applied on every paragraph.</p>
13. <p id="para1">Me too!</p>
14. <p>And me!</p>
15. </body>
16. </html>
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element.
An id is always unique within the page so it is chosen to select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. #para1 {
- 3-
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <p id="para1">Hello Javatpoint.com</p>
13. <p>This paragraph will not be affected.</p>
14. </body>
15. </html>
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. .center {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1 class="center">This heading is blue and center-aligned.</h1>
13. <p class="center">This paragraph is blue and center-aligned.</p>
14. </body>
15. </html>
1. <!DOCTYPE html>
- 4-
2. <html>
3. <head>
4. <style>
5. p.center {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1 class="center">This heading is not affected</h1>
13. <p class="center">This paragraph is blue and center-aligned.</p>
14. </body>
15. </html>
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. * {
6. color: green;
7. font-size: 20px;
8. }
9. </style>
10. </head>
11. <body>
12. <h2>This is heading</h2>
13. <p>This style will be applied on every paragraph.</p>
14. <p id="para1">Me too!</p>
15. <p>And me!</p>
16. </body>
17. </html>
1. h1 {
2. text-align: center;
3. color: blue;
4. }
5. h2 {
6. text-align: center;
7. color: blue;
8. }
9. p {
10. text-align: center;
11. color: blue;
12. }
As you can see, you need to define CSS properties for all the elements. It can be
grouped in following ways:
1. h1,h2,p {
2. text-align: center;
3. color: blue;
4. }
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. h1, h2, p {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. <h2>Hello Javatpoint.com (In smaller font)</h2>
14. <p>This is a paragraph.</p>
- 6-
15. </body>
16. </html>
1. Inline CSS :
We can apply CSS in a single element by inline CSS technique.
The inline CSS is also a method to insert style sheets in HTML document. This
method mitigates some advantages of style sheets so it is advised to use this
method sparingly.
If you want to use inline CSS, you should use the style attribute to the relevant tag.
Syntax:
Example:
1. <h2 style="color:red;margin-
left:40px;">Inline CSS is applied on this heading.</h2>
2. <p>This paragraph is not affected.</p>
Output:
2. Internal CSS :
The internal style sheet is used to add a unique style for a single document. It is
defined in <head> section of the HTML page inside the <style> tag.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. background-color: linen;
7. }
8. h1 {
9. color: red;
10. margin-left: 80px;
11. }
12. </style>
13. </head>
14. <body>
15. <h1>The internal style sheet is applied on this heading.</h1>
16. <p>This paragraph will not be affected.</p>
17. </body>
18. </html>
3. External CSS :
The external style sheet is generally used when you want to make changes on
multiple pages. It is ideal for this condition because it facilitates you to change the
look of the entire web site by changing just one file.
It uses the <link> tag on every pages and the <link> tag should be put inside the
head section.
Example:
1. <head>
2. <link rel="stylesheet" type="text/css" href="mystyle.css">
- 8-
3. </head>
The external style sheet may be written in any text editor but must be saved with
a .css extension. This file should not contain HTML elements.
File: mystyle.css
1. body {
2. background-color: lightblue;
3. }
4. h1 {
5. color: navy;
6. margin-left: 20px;
7. }
Note: You should not use a space between the property value and the unit. For
example: It should be margin-left:20px not margin-left:20 px.
Attributes:
- 9-
b.) Target attribute of HTML anchor tag
If we want to open that link to another page then we can use target attribute of <a> tag.
With the help of this link will be open in next page.
Note:
o The target attribute can only use with href attribute in anchor tag.
o If we will not use target attribute then link will open in same page.
Syntax
<a target="_blank|_self|_parent|_top|framename">
Attribute Values
Value Description
_self Opens the linked document in the same frame as it was clicked (this is
default)
_top Opens the linked document in the full body of the window
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title></title>
5. </head>
6. <body>
7. <p>Click on <a href="https://www.javatpoint.com/" target="_blank"> this-
link </a>to go on home page of JavaTpoint.</p>
8. </body>
9. </html>
Example:
- 10-
<a href = "http://www.tutorialspoint.com/xyz.html">
This URL only includes the area after the domain and does not include the complete
website address. It is presumptuous to presume that any links you add will be on the
same website and belong to the same root domain.
The forward slash is the first character in the relative path, which directs the browser
to stay on the current website. A relative URL would look like this −
<a href = "/xyz.html">
All the information needed to locate files Only file names or file names with folder
online is contained in the absolute URL. names are contained in relative URLs.
The browser could not link to the precise We can use this URL form when the file is
line if the four components were missing. on the same server as the original
document.
- 11-
8) <img> tag and its attributes:
HTML img tag is used to display image on the web page. HTML img tag is an empty tag that
contains attributes only, closing tags are not used in HTML image element.
1) src
It is a necessary attribute that describes the source or path of the image. It instructs the
browser where to look for the image on the server.
2) alt
The alt attribute defines an alternate text for the image, if it can't be displayed. The value
of the alt attribute describe the image in words. The alt attribute is considered good for
SEO prospective.
3) width
It is an optional attribute which is used to specify the width to display the image. It is not
recommended now. You should apply CSS in place of width attribute.
4) height
It h3 the height of the image. The HTML height attribute also supports iframe, image and
object elements. It is not recommended now. You should apply CSS in place of height
attribute.
- 12-
Tag Description
Syntax:
1. <form>
2. //Form elements
3. </form>
Attributes
- 13-
autocomplete on Specifies whether a form
off should have autocomplete
on or off
target _blank
_self
_parent
_top
The action attribute of <form> element defines the process to be performed on form when
form is submitted, or it is a URI to process the form information.
The action attribute value defines the web page where information proceed. It can be .php,
.jsp, .asp, etc. or any URL where you want to process your form.
Example:
- 14-
2. <label>User Name:</label><br>
3. <input type="text" name="name"><br><br>
4. <label>User Password</label><br>
5. <input type="password" name="pass"><br><br>
6. <input type="submit">
7. </form>
1. post: We can use the post value of method attribute when we want to process the
sensitive data as it does not display the submitted data in URL.
Example:
2. get: The get value of method attribute is default value while submitting the form. But
this is not secure as it displays data in URL after submitting the form.
Example:
1. _self: If we use _self as an attribute value, then the response will display in current page
only.
Example:
<form action="action.html" method="get" target="_self">
2. _blank: If we use _blank as an attribute it will load the response in a new page.
Example:
<form action="action.html" method="get" target="_blank">
- 15-
The HTML autocomplete attribute is a newly added attribute of HTML5 which enables an
input field to complete automatically. It can have two values "on" and "off" which enables
autocomplete either ON or OFF. The default value of autocomplete attribute is "on".
Example:
<form action="action.html" method="get" autocomplete="on">
Example:
<form action = "action.html" method = "get" novalidate>
Example:
1. <body>
2. <form>
3. Enter your name <br>
4. <input type="text" name="username">
5. </form>
6. </body>
- 16-
password Defines a one-line password input field
button Defines a simple push button, which can be programmed to perform a task on an event.
HTML5 added new types on <input> element. Following is the list of types of elements
of HTML5
datetime-local Defines an input field for entering a date without time zone.
month Defines a control with month and year, without time zone.
week Defines a field to enter the date with week-year, without time zone.
search Defines a single line text field for entering a search string.
- 17-
Following is the description about types of <input> element with examples.
→ <input type="text">:
The type="text" attribute of input tag creates textfield control also known as single line
textfield control. The name attribute is optional, but it is required for the server side
component such as JSP, ASP, PHP etc.
Example:
1. <form>
2. First Name: <input type="text" name="firstname"/> <br/>
3. Last Name: <input type="text" name="lastname"/> <br/>
4. </form>
→ <input type="password">:
The <input> element of type "password" allow a user to enter the password securely in a
webpage. The entered text in password filed converted into "*" or ".", so that it cannot be
read by another user.
Example:
1. <form>
2. <label>Enter User name</label><br>
3. <input type="text" name="firstname"><br>
4. <label>Enter Password</label><br>
5. <input type="Password" name="password"><br>
6. <br><input type="submit" value="submit">
7. </form>
→ <input type="submit">:
The <input> element of type "submit" defines a submit button to submit the form to the
server when the "click" event occurs.
Example:
1. <form action="https://www.javatpoint.com/html-tutorial">
2. <label>Enter User name</label><br>
3. <input type="text" name="firstname"><br>
4. <label>Enter Password</label><br>
5. <input type="Password" name="password"><br>
6. <br><input type="submit" value="submit">
7. </form>
- 18-
→ <input type="reset">:
The <input> type "reset" is also defined as a button but when the user performs a click
event, it by default reset the all inputted values.
Example:
1. <form>
2. <label>User id: </label>
3. <input type="text" name="user-id" value="user">
4. <label>Password: </label>
5. <input type="password" name="pass" value="pass"><br><br>
6. <input type="submit" value="login">
7. <input type="reset" value="Reset">
8. </form>
→ <input type="radio">:
The <input> type "radio" defines the radio buttons, which allow choosing an option
between a set of related options. At a time only one radio button option can be selected at
a time.
Example:
1. <form>
2. <p>Kindly Select your favorite color</p>
3. <input type="radio" name="color" value="red"> Red <br>
4. <input type="radio" name="color" value="blue"> blue <br>
5. <input type="radio" name="color" value="green">green <br>
6. <input type="radio" name="color" value="pink">pink <br>
7. <input type="submit" value="submit">
8. </form>
→ <input type="checkbox">:
The <input> type "checkbox" are displayed as square boxes which can be checked or
unchecked to select the choices from the given options.
Example:
1. <form>
2. <label>Enter your Name:</label>
3. <input type="text" name="name">
4. <p>Kindly Select your favourite sports</p>
- 19-
5. <input type="checkbox" name="sport1" value="cricket">Cricket<br>
6. <input type="checkbox" name="sport2" value="tennis">Tennis<br>
7. <input type="checkbox" name="sport3" value="football">Football<br>
8. <input type="checkbox" name="sport4" value="baseball">Baseball<br>
9. <input type="checkbox" name="sport5" value="badminton">Badminton<br>
<br>
10. <input type="submit" value="submit">
11. </form>
→ <input type="button">:
The <input> type "button" defines a simple push button, which can be programmed to
control a functionally on any event such as, click event.
Example:
1. <form>
2. <input type="button" value="Clcik me " onclick="alert('you are learning HTML
')">
3. </form>
→ <input type="file">:
The <input> element with type "file" is used to select one or more files from user device
storage. Once you select the file, and after submission, this file can be uploaded to the
server with the help of JS code and file API.
Example:
1. <form>
2. <label>Select file to upload:</label>
3. <input type="file" name="newfile">
4. <input type="submit" value="submit">
5. </form>
Example:
→ <input type="image">:
The <input> type "image" is used to represent a submit button in the form of image.
1. <!DOCTYPE html>
2. <html>
3. <body>
- 20-
4. <h2>Input "image" type.</h2>
5. <p>We can create an image as submit button</p>
6. <form>
7. <label>User id:</label><br>
8. <input type="text" name="name"><br><br>
9. <input type="image" alt="Submit" src="login.png" width="100px">
10. </form>
11.
12. </body>
13. </html>
→ <input type=”date">:
The <input> element of type "date" generates an input field, which allows a user to input
the date in a given format. A user can enter the date by text field or by date picker interface.
Example:
1. <form>
2. Select Start and End Date: <br><br>
3. <input type="date" name="Startdate"> Start date:<br><br>
4. <input type="date" name="Enddate"> End date:<br><br>
5. <input type="submit">
6. </form>
→ <input type=”datetime-local">:
The <input> element of type "datetime-local" creates input filed which allow a user to
select the date as well as local time in the hour and minute without time zone information.
Example:
1. <form>
2. <label>
3. Select the meeting schedule: <br><br>
4. Select date & time: <input type="datetime-
local" name="meetingdate"> <br><br>
5. </label>
6. <input type="submit">
7. </form>
→ <input type=”email">:
- 21-
The <input> type "email" creates an input filed which allow a user to enter the e-mail
address with pattern validation. The multiple attributes allow a user to enter more than one
email address.
Example:
1. <form>
2. <label><b>Enter your Email-address</b></label>
3. <input type="email" name="email" required>
4. <input type="submit">
5. <p><strong>Note:</strong>User can also enter multiple email addresses s
eparating by comma or whitespace as following: </p>
6. <label><b>Enter multiple Email-addresses</b></label>
7. <input type="email" name="email" multiple>
8. <input type="submit">
9. </form>
→ <input type=”number">:
The <input> element type number creates input filed which allows a user to enter the
numeric value. You can also restrict to enter a minimum and maximum value using min and
max attribute.
Example:
1. <form>
2. <label>Enter your age: </label>
3. <input type="number" name="num" min="50" max="80">
4. <input type="submit">
5. </form>
→ <input type=”url">:
The <input> element of type "url" creates an input filed which enables user to enter the
URL.
Example:
<form>
1. <label>Enter your website URL: </label>
2. <input type="url" name="website" placeholder="http://example.com"><br>
3. <input type="submit" value="send data">
4. </form>
- 22-
3. HTML <textarea> Element:
The <textarea> tag in HTML is used to insert multiple-line text in a form. The size of
<textarea> can be specify either using "rows" or "cols" attribute or by CSS.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form in HTML</title>
5. </head>
6. <body>
7. <form>
8. Enter your address:<br>
9. <textarea rows="2" cols="20"></textarea>
10. </form>
11. </body>
12. </html>
1. <form>
2. <label for="firstname">First Name: </label> <br/>
3. <input type="text" id="firstname" name="firstname"/> <br/>
4. <label for="lastname">Last Name: </label>
5. <input type="text" id="lastname" name="lastname"/> <br/>
6. </form>
Syntax:
<select>
<option></option>
</select>
- 23-
Attribute:
autofocus autofocus This attribute let automatically focused the drop-down list on page
load.
disabled disabled It is used to disable the control and user cannot interact with the drop-
down list.
form form-id It specifies one or more forms, to which select belong to.
multiple multiple If it sets then a user can select multiple options from the list.
required required If it specified, user must select that field before submitting the form.
Example:
Example
Let's see the simple example of HTML5 datalist tag. If you press A, it will show a list of
cricketers starting with A letter.
1. <label>
2. Enter your favorite cricket player: Press any character<br />
3. <input type="text" id="favCktPlayer" list="CktPlayers">
4. <datalist id="CktPlayers">
5. <option value="Sachin Tendulkar">
6. <option value="Brian Lara">
- 24-
7. <option value="Jacques Kallis">
8. <option value="Ricky Ponting">
9. <option value="Rahul Dravid">
10. <option value="Shane Warne">
11. <option value="Rohit Sharma">
12. <option value="Donald Bradman">
13. <option value="Saurav Ganguly ">
14. <option value="AB diVilliers">
15. <option value="Mahendra Singh Dhoni">
16. <option value="Adam Gilchrist">
17. </datalist>
18. </label>
- 25-
12.) HTML <video> tag:
HTML 5 supports <video> tag also. The HTML video tag is used for streaming video files
such as a movie clip, song clip on the web page.
Currently, there are three video formats supported for HTML video tag:
• mp4
• webM
• ogg
Example
1. <video controls>
2. <source src="movie.mp4" type="video/mp4">
3. Your browser does not support the html video tag.
4. </video>
Attribute Description
controls It defines the video controls which is displayed with play/pause buttons.
poster It specifies the image which is displayed on the screen when the video is not played.
autoplay It specifies that the video will start playing as soon as it is ready.
loop It specifies that the video file will start over again, every time when it is completed.
preload It specifies the author view to upload video file when the page loads.
- 26-
12.) HTML <video> tag:
HTML audio tag is used to define sounds such as music and other audio clips. Currently
there are three supported file format for HTML 5 audio tag.
1. mp3
2. wav
3. ogg
Example
1. <audio controls>
2. <source src="koyal.mp3" type="audio/mpeg">
3. Your browser does not support the html audio tag.
4. </audio>
Attribute Description
controls It defines the audio controls which is displayed with play/pause buttons.
autoplay It specifies that the audio will start playing as soon as it is ready.
loop It specifies that the audio file will start over again, every time when it is completed.
preload It specifies the author view to upload audio file when the page loads.
- 27-