Cit 303-Book 3 4
Cit 303-Book 3 4
Chapter No. 03
HTML and CSS
At the end of this chapter students will be able to:
HTML stands for Hypertext Markup Language, and it is the most widely used
language to write Web Pages. HTML is the standard markup language for
creating Web pages. It describes the structure of a Web page. It consists of a
series of elements. These elements tell the browser how to display the
content. Hypertext refers to the way in which Web pages (HTML documents)
are linked together. Thus, the link available on a webpage is called Hypertext.
HTML is a Markup Language which means you use HTML to simply "mark-up"
a text document with tags that tell a Web browser how to structure it to
display.
Originally, HTML was developed with the intent of defining the structure of
documents like headings, paragraphs, lists, etc.
An HTML document has two main parts: the head and the body. But firstly
every HTML document should start by declaring that it is an HTML document.
All HTML documents must start with a document type declaration:
<!DOCTYPEhtml>.
The HTML document itself begins with <html> and ends with </html>.
The visible part of the HTML document is between <body> and </body>.
Head of an HTML Document:
The head element contains Meta information about the HTML page. The
HEAD of an HTML document is where information (which might be ignored
32 | P a g e
Chapter # 3 HTML and CSS
by some Web browsers), such as the document's title, can be placed. The
HEAD of a document should be declared using the following HTML tags:
<head> Should appear after the <html> definition. </head> should appear at
the end of head.
A typical entry in the HEAD section of a document would be:
<title> Title of the document </title>. The title appears on the title bar of
your web browser.
Body of an HTML Document
The BODY of an HTML document is where all the information you wish to
view in your web browser. The text must be carefully marked-up, paragraphs
must begin with the <p> tag and the end of each paragraph must be clearly
marked using the HTML tag </p>. If the body text doesn't contain paragraph
breaks then the text will be viewed as one long paragraph! The BODY of a
document should be declared using the following HTML tags:
<body> tag should appear after the </head> tag. </body> tag should appear
after the document's text but before the </html> tag.
A Simple HTML Document (Example):
Write the following text in your html editor (notepad, NetBeans or any other
editor). You can use both lowercase and uppercase, as HTML is not case
sensitive.
<!DOCTYPE html>
<html>
<head>
<title> A Simple HTML Document </title>
</head>
<body>
<h1>Heading is added here</h1>
<p>This is a very simple HTML document</p>
<p>It only has two paragraphs</p>
</body>
</html>
Save this file as “first.html” here first is the name of your html document and
html is the extension that tells that the file you saved is an html file. The page
will look like as given in the picture below:
33 | P a g e
Chapter # 3 HTML and CSS
34 | P a g e
Chapter # 3 HTML and CSS
35 | P a g e
Chapter # 3 HTML and CSS
36 | P a g e
Chapter # 3 HTML and CSS
37 | P a g e
Chapter # 3 HTML and CSS
List Tags: To add list of values both ordered and unordered lists we use list
tags: Few tags are:-
Tag Description Example
<ul> Defines an unordered list <ul style="list-style-type:circle;
padding-
left:0px"><li>Islamabad</li><li
>Karachi</li><li>Lahore</li><li
>Peshawar</li><li>Quetta</li>
</ul>
<ol> Defines an ordered list <ol style="list-style-
type:lower-
roman"><li>Islamabad</li><li>
Karachi</li><li>Lahore</li><li>
Peshawar</li><li>Quetta</li><
/ol>
<li> Defines a list item "><li>Islamabad</li>
38 | P a g e
Chapter # 3 HTML and CSS
Meta Info:
Tag Description Example
<head> Defines information <head>
about the document. <link rel="stylesheet"
The contents inside head href="E:\CIT\Web Development with
39 | P a g e
Chapter # 3 HTML and CSS
Attributes:
An attribute indicates the characteristics of an object. For example, size and
color of a line or text are the attributes. Attributes are specified with opening
tag to provide additional information about an element or object of the
document.
Example:
<h1 align=”center”>Web Programming </h1>
<hr size=50 color=red>
Some important attributes are: background-color, Background, Text, Link,
Vlink, Alink, href, src, width, alt, style, etc.
3.2 DESCRIBE CSS STYLES
CSS stands for Cascading Style Sheets. CSS describes how HTML elements are
to be displayed on screen, paper, or in other media. CSS saves a lot of work.
It can control the layout of multiple web pages all at once.
CSS can be added to HTML elements in 3 ways:
Inline - by using the style attribute in HTML elements
Internal - by using a <style> element in the <head> section
External - by using an external CSS file
Benefits of using CSS:
A style sheet gives the user several benefits:
better control over layout
better control over text display
40 | P a g e
Chapter # 3 HTML and CSS
Example:
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
3.2.1 Inline CSS
An inline CSS is used to apply a unique style to a single HTML element. An
inline CSS uses the style attribute of an HTML element. This example sets the
text color of the <h1> element to blue:
Example:
<h1 style="color:blue;">This is a Blue Heading</h1>
41 | P a g e
Chapter # 3 HTML and CSS
color: blue;
}
p{
color: red;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
3.2.3 External CSS
An external style sheet is used to define the style for many HTML pages. With
an external style sheet, you can change the look of an entire web site, by
changing one file. To use an external style sheet, add it as a link to in the
<head> section of the HTML page:
Example:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head><body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
An external style sheet can be written in any text editor. The file must not
contain any HTML code, and must be saved with a .css extension.
Here is how the "styles.css" looks:
body {
background-color: powderblue;
}
h1 {
color: blue;
}
42 | P a g e
Chapter # 3 HTML and CSS
p{
color: red;
}
CSS Fonts: The CSS color property defines the text color to be used. The
CSS font-family property defines the font to be used. The CSS font-size
property defines the text size to be used.
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p{
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Border: The CSS border property defines a border around an HTML
element:
Example
p{
border: 1px solid powderblue;
}
43 | P a g e
Chapter # 3 HTML and CSS
CSS Padding: The CSS padding property defines a padding (space) between
the text and the border:
Example
p{
border: 1px solid powderblue;
padding: 30px;
}
External References :
External style sheets can be referenced with a full URL or with a path relative
to the current web page. This example uses a full URL to link to a style sheet:
Example
<link rel="stylesheet" href="https://www.w3schools.com/html/styles. css">
Each table row is defined with the <tr> tag. A table header is defined with the
<th> tag. By default, table headings are bold and centered. A table data/cell
is defined with the <td> tag.
44 | P a g e
Chapter # 3 HTML and CSS
Colspan and Rowspan Attributes: You will use colspan attribute if you want
to merge two or more columns into a single column. Similar way you will use
rowspan if you want to merge two or more rows.
45 | P a g e
Chapter # 3 HTML and CSS
</tr>
<tr>
<td>102</td>
<td>DAE</td>
<td>990</td>
<td>PBTE Lahore</td>
</tr>
</table>
There are various form elements available like text fields, textarea fields,
dropdown menus, radio buttons, checkboxes, etc. Form Example:
First name:
Last name:
SUBMIT
The <form> Element: The HTML <form> element defines a form that is used
to collect user input:
<form> … </form>
An HTML form contains form elements.
Form elements are different types of input elements, like text fields,
checkboxes, radio buttons, submit buttons, and more.
The <input> Element:
The <input> element is the most important form element.
The <input> element can be displayed in several ways, depending on the
type attribute.
Here are some examples:
Type Description
46 | P a g e
Chapter # 3 HTML and CSS
47 | P a g e
Chapter # 3 HTML and CSS
48 | P a g e
Chapter # 3 HTML and CSS
<title>Page Layout</title>
<style>
.head1 {
font-size:40px;
color:#009900;
font-weight:bold;
}
.head2 {
font-size:17px;
margin-left:10px;
margin-bottom:15px;
}
body {
margin: 0 auto;
background-position:center;
background-size: contain;
}
.menu {
position: sticky;
top: 0;
background-color: #009900;
padding:10px 0px 10px 0px;
color:white;
margin: 0 auto;
overflow: hidden;
}
.menu a {
float: left;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 20px;
}
.menu-log {
right: auto;
49 | P a g e
Chapter # 3 HTML and CSS
float: right;
}
footer {
width: 100%;
bottom: 0px;
background-color: #000;
color: #fff;
position: absolute;
padding-top:20px;
padding-bottom:50px;
text-align:center;
font-size:30px;
font-weight:bold;
}
.body_sec {
margin-left:20px;
}
</style>
</head>
<body>
<!-- Header Section -->
<header>
<div class="head1">Computer Science</div>
<div class="head2">A computer science web Page</div>
</header>
<!-- Menu Navigation Bar -->
<nav>
<div class="menu">
<a href="#home">HOME</a>
<a href="#news">NEWS</a>
<a href="#notification">NOTIFICATIONS</a>
<div class="menu-log">
<a href="#login">LOGIN</a>
</div>
</div>
</nav>
50 | P a g e
Chapter # 3 HTML and CSS
51 | P a g e
Chapter # 3 HTML and CSS
EXERCISE
PART I: SHORT QUESTIONS
13. What is html tag and give one example?
14. Write any five html tags?
15. What is CSS?
16. What is an Internal CSS?
17. Explain external CSS
18. Why we use CSS?
19. Describe cellpadding and cellspacing attributes.
20. How can you add a header in an html page?
52 | P a g e
Chapter # 3 HTML and CSS
1 2 3 4 5 6 7 8 9 10
B D B B A B A A B C
53 | P a g e
Chapter # 3 HTML and CSS
Reference:
HTML Tutorial. (n.d.). Retrieved from https://www.w3schools.com:
https://www.w3schools.com/html/html_intro.asp
54 | P a g e
Chapter # 4 Javascript
Chapter. No. 04
JAVASCRIPT
4.1 Introduction to Javascript
4.2 Hiding and Showing Elements
4.3 Styling Elements
4.4 Using Jquery
4.5 Jquery Selectors
4.6 Validating Forms
4.1 What is JavScript?
JavaScript is a lightweight, interpreted programming language. It is designed
for creating network-centric applications. It is complimentary to and
integrated with Java. JavaScript is very easy to implement because it is
integrated with HTML. It is open and cross-platform.
<html>
<body>
<script language = "javascript" type = "text/javascript">
55 | P a g e
Chapter # 4 Javascript
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>
4.1.1 JavaScript Can Change HTML Content
This example uses the method to "find" an HTML element (with id="demo")
and changes the element content (innerHTML) to "Hello JavaScript":
Example
document.getElementById("demo").style.display="none";
or
document.getElementById('demo').style.display='none';
4.1.3 JavaScript Can Show HTML Elements
Showing hidden HTML elements can also be done by changing the display style
Example:
document.getElementById("demo").style.display="block";
or
document.getElementById('demo').style.display='block';
4.1.4 JavaScript Can Change HTML Styles (CSS)
Changing the style of an HTML element, is a variant of changing an HTML attribute:
Example:
56 | P a g e
Chapter # 4 Javascript
document.getElementById("demo").style.fontSize="35px";
or
document.getElementById('demo').style.fontSize='35px';
JavaScript Functions?
Function myFunction(p1,p2){
return p1 * p2; // The function returns the product of p1andp2
}
The HTML DOM allows JavaScript to change the style of HTML elements.
<script>
document.getElementById("p2").style.color = "blue";
</script>
</body>
</html>
Using Events
The HTML DOM allows you to execute code when an event occurs. Events
are generated by the browser when "things happen" to HTML elements:
57 | P a g e
Chapter # 4 Javascript
An element is clicked on
The page has loaded
Input fields are changed
This example changes the style of the HTML element with id="id1", when the
user clicks a button
Example:
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button>
</body>
</html>
58 | P a g e
Chapter # 4 Javascript
With jQuery, you can hide and show HTML elements with the hide() and
show() methods:
$("#hide").click(function(){
$("p").hide();
});
59 | P a g e
Chapter # 4 Javascript
$("#show").click(function(){
$("p").show();
});
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
}):
An id should be unique within a page, so you should use the #id selector
when you want to find a single, unique element. To find an element with a
specific id, write a hash character, followed by the id of the HTML element:
60 | P a g e
Chapter # 4 Javascript
$("#test")
Example
When a user clicks on a button, the element with id="test" will be hidden:
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
Example
When a user clicks on a button, the elements with class="test" will be
hidden:
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
<html>
<body>
61 | P a g e
Chapter # 4 Javascript
<h2>JavaScript Validation</h2>
<input id="numb">
<p id="demo"></p>
<script>
functionmyFunction() {
let x = document.getElementById("numb").value;
</body>
</html>
62 | P a g e
Chapter # 4 Javascript
EXERCISE
PART I: SHORT QUESTIONS
1. What is JavaScript?
2. What can JavaScript Do?
3. Write aexample of Hide HTML Element through javaScript?
4. Explain JavaScript Functions?
5. Write the syntax of JavaScript Functions.
6. Define jQuery Selector?
7. Define the #id Selector?
8. Define the .class Selector?
9. Write any two Examples of jQuery Selectors with Description?
10. Explain jQuery Hide and Show?
11. How to create sliding effects on HTML elements with jQuery?
63 | P a g e
Chapter # 4 Javascript
64 | P a g e
Chapter # 4 Javascript
Answers
1 2 3 4 5 6 7 8 9 10
A A A B A C B B D C
11 12
A C
65 | P a g e
Chapter # 4 Javascript
REFRENCES
1. https://www.tutorialspoint.com/javascript/index.html
2. https://www.w3schools.com/jquery/jquery_syntax.asp
66 | P a g e