Web Design Programming Chapter 3
Web Design Programming Chapter 3
Each declaration includes a CSS property name and a value, separated by a colon.
A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly
braces.
In the following example all <p> elements will be center-aligned, with a red text color:
Example
p{
color: red;
text-align: center;
}
You can select all <p> elements on a page like this (in this case, all <p> elements will be
center-aligned, with a red text color):
Example
p{
text-align: center;
color: red;
}
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be 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.
The style rule below will be applied to the HTML element with id="para1":
Example
#para1 {
text-align: center;
color: red;
}
To select elements with a specific class, write a period (.) character, followed by the name
of the class.
In the example below, all HTML elements with class="center" will be red and center-aligned:
Example
.center {
text-align: center;
color: red;
}
WEB DESIGN PROGRAMMING: CHAPTER 3
(CSS AND JavaScript) ----JITENDRA SINGH
Grouping Selectors
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
In the example below we have grouped the selectors from the code above:
Example
h1, h2, p {
text-align: center;
color: red;
Each page must include a reference to the external style sheet file inside the
<link> element. The <link> element goes inside the <head> section:
Example
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
An external style sheet can be written in any text editor. The file should not
contain any html tags. The style sheet file must be saved with a .css extension.
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Internal styles are defined within the <style> element, inside the <head>
section of an HTML page:
Example
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
WEB DESIGN PROGRAMMING: CHAPTER 3
(CSS AND JavaScript) ----JITENDRA SINGH
Inline Styles
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style
attribute can contain any CSS property.
The example below shows how to change the color and the left margin of a
<h1> element:
Example
<h1 style="color:blue;margin-left:30px;">This is a heading</h1>
Generally speaking we can say that all the styles will "cascade" into a new
"virtual" style sheet by the following rules, where number one has the highest
priority:
So, an inline style (inside a specific HTML element) has the highest priority,
which means that it will override a style defined inside the <head> tag, or in an
external style sheet, or a browser default value.
WEB DESIGN PROGRAMMING: CHAPTER 3
(CSS AND JavaScript) ----JITENDRA SINGH
3.6 INTRODUCTION
<h2>What Can JavaScript Do?</h2>
TO JAVASCRIPT
JavaScript is one of the 3 languages all web <p>JavaScript can change HTML
developers must learn: attributes.</p>
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages <p>In this case JavaScript changes the src
3. JavaScript to program the behavior of (source) attribute of an image.</p>
web pages
<button
onclick="document.getElementById('myImag
3.7 What can e').src='pic_bulbon.gif'">Turn on the
JAVASCRIPT Do? light</button>
<html>
<body>
Example
<script>
document.getElementById("demo").innerHTML
<h2>What Can JavaScript Do?</h2>
= "My First JavaScript";
</script>
<p id="demo">JavaScript can hide HTML
elements.</p>
JavaScript in <head> or
<button type="button"
onclick="document.getElementById('demo').s <body>
tyle.display='none'">Click Me!</button>
You can place any number of scripts in an
HTML document.
</body>
</html> Scripts can be placed in the <body>, or in the
<head> section of an HTML page, or in both.
JavaScript Can Show
HTML Elements JavaScript in <head>
Showing hidden HTML elements can also be
done by changing the display style: In this example, a JavaScript function is
<!DOCTYPE html> placed in the <head> section of an HTML
<html> page.
<body>
The function is invoked (called) when a button
<h2>What Can JavaScript Do?</h2> is clicked:
External scripts are practical when the same In this example, x, y, and z, are variables:
code is used in many different web pages.
Example
JavaScript files have the file extension .js.
var x = 5;
To use an external script, put the name of the var y = 6;
script file in the src (source) attribute of a var z = x + y;
<script> tag:
From the example above, you can expect:
+= x += y x=x+y
3.10 JavaScript
Operators -= x -= y x=x–y
/ Division Example
txt1 = "John";
txt2 = "Doe";
% Modulus txt3 = txt1 + " " + txt2;
z = "Hello" + 5;
Operator Description
The result of x, y, and z will be:
10
55 && logical and
Hello5
Note: If you add a number and a string, the
|| logical or
result will be a string!
Operators
JavaScript Type
Operator Description Operators
== equal to Operator Description
=== equal value and equal type Typeof Returns the type of a variable
Often this is the case when working with Statement 1 is executed before the loop (the
arrays: code block) starts.
In the following example, the code in the loop Objects are variables too. But objects can
will run, over and over again, as long as a contain many values.
variable (i) is less than 10:
This code assigns many values (Fiat, 500,
Example white) to a variable named car:
while (i < 10) { var car = {type:"Fiat", model:"500",
text += "The number is " + i; color:"white"};
i++;
} The values are written as name:value pairs
(name and value separated by a colon).
or
Object Methods objectName["propertyName"]
<body>
Property Property Value
</p>
age 50
<p>You can use person.property or
person["property"].</p>
eyeColor blue
firstName: "John",
Object Definition
lastName : "Doe",
WEB DESIGN PROGRAMMING: CHAPTER 3
(CSS AND JavaScript) ----JITENDRA SINGH
Every cookie is accessible only within a 1 // retrieve the values of any theme
particular path, which defaults to the current 2 cookies
directory — for example, a cookie set on a page var themes = Cookies.get('theme', true);
at http://example.com/news/ would by default be
accessible from Deleting cookies
http://example.com/news/archives/ but not from A cookie can be deleted using
the home page of the site. The path property the clear function, which takes the cookie name
allows an alternative path to be specified: as a parameter:
1 // create a cookie that is accessible 1 // delete the theme cookie
2 anywhere on the site 2 Cookies.clear('theme');
3 Cookies.set('theme', 'green', {path : '/'}); If the cookie was set for a path or domain other
4 than the current path and domain, these must be
5 // create a cookie that is accessible only passed to the clearfunction through its optional
within the news directory second parameter:
Cookies.set('country', 'uk', {path : 1 // delete the site-wide theme cookie
'/news/'}); 2 Cookies.clear(
Every cookie is accessible only on a particular 3 'theme',
domain, which defaults to the current domain 4 {
and its subdomains — for example, a cookie set 5 path : '/',
on news.example.com would by default be 6 domain : '.example.com'
accessible from archives.news.example.com but 7 });
not from the main domain example.com.
The domain property allows an alternative
domain to be specified:
1 // create a cookie that is accessible on all
2 subdomains of example.com
Cookies.set('theme', 'green', {domain :
'.example.com'});
Finally, the secure property can be set to true to
instruct the browser to send the cookie only over
encrypted connections:
1 // create a cookie that is sent only over
2 encrypted connections
Cookies.set('theme', 'green', {secure :
true});
Retrieving cookies
The value of a cookie can be retrieved using
the get function, which takes the cookie name as
a parameter:
1 // retrieve the value of the theme cookie
2 var theme = Cookies.get('theme');
If there is no cookie with the specified name, the
value undefined is returned. There may be more
than one cookie with the same name if they were
set for different paths or subdomains. In this
case the get function returns the most specific
cookie (the one set for the longest path).
Passing true as a second parameter to
the get function will cause it to return an array
containing the values of all cookies with the
specified name, in order of specificity: