Id and class selector

Preview:

Citation preview

Unit III: Adding Styles and Interactivity Using CSS and

Javascript

USING CASCADING STYLE SHEETSCSS CODES:

The id and class Selector

• 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 "#".

The id Selectors

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.

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 "."

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>

APPLY THE ID and CLASS SELECTOR FOR THE FOLLOWING

Use para1 and para2 as ID, then use “h’ and “i” as class selector.

LAY-OUTParagraph 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)CONTENT1st paragraph: Honesty is the best policy2nd paragraph: Health and wealth.

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>

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>

Recommended