1. Unit III: Adding Styles and
Interactivity Using CSS and
Javascript
2. The id Selectors
• In addition to setting a style for a HTML
element, CSS allows you to specify your own
selectors called "id" and "class".
• The id Selector
• The id selector is used to specify a style for a
single, unique element.
• The id selector uses the id attribute of the
HTML element, and is defined with a "#".
3. Example
<style type= “text/css”
#para1{text-align: center; color: red;}
#para2{text-align: left; color: green;}
</style>
<p id= “para1”>Hello World</p>
<p id= “para2”>Hello Philippines</p>
Do NOT start an ID name with a number! It
will not work in Mozilla/Firefox.
4. The class Selector
• The class selector is used to specify a style for
a group of elements. Unlike the id selector, the
class selector is most often used on several
elements.
• This allows you to set a particular style for
many HTML elements with the same class.
• The class selector uses the HTML class
attribute, and is defined with a "."
5. EXAMPLE
<style type= “text/css”
p.a {background-color: red text-align: center;}
p.b {background-color: green text-align: left;}
p.c {background-color: blue text-align: right;}
</style>
<p class= “a”>Hello World</p>
<p class= “b”>Hello World</p>
<p class= “c”>Hello World</p>
6. APPLY THE ID and CLASS SELECTOR
FOR THE FOLLOWING
Use para1 and para2 as ID, then use “h’ and “i” as
class selector.
LAY-OUT
Paragraph 1. (background blue, foreground is red,
font face is Aharoni, font size is 10 pixels)
Paragraph 2. (background green, foreground is
white, font face is Batang, font size is 15 pixels)
CONTENT
1st paragraph: Honesty is the best policy
2nd paragraph: Health and wealth.
7. ANSWER
#para1{background-color: blue; color: red; font-
family: Aharoni; font-size: 10px;}
#para2{background-color: green; color: white;
font-family: Batang; font-size: 15px;}
</style>
<p id= “para1”>Honesty is the best policy</p>
<p id= “para2”>Health is wealth</p>
8. ANSWER
p.h{background-color: blue; color: red; font-
family: Aharoni; font-size: 10px;}
p.i{background-color: green; color: white; font-
family: Batang; font-size: 15px;}
</style>
<p class= “h”>Honesty is the best policy</p>
<p class= “i”>Health is wealth</p>