Chapter 3
Chapter 3
CHAPTER 3
CSS(CASCADING STYLE SHEETS)
Quiz one ..Create the ff page.Boxes represent a div element.
What is CSS?
• Every web page is composed of HTML (Hypertext Markup Language)
code that describes the content
• Example:
<p>An<strong>important</strong><font
color=“#FFFF00”>paragraph</font>.</p>
• Displays:
• An important paragraph.
• Repetitive and hard to maintain.
What is CSS?
• Layers of a web page:
• Content:
• Text, images, animation, video, etc.
• Presentation:
• How the content will appear to a human through a web browser, text reader, etc.
• Behavior:
• Real-time user interaction with the page: validation, sorting, drag-n-drop etc.
Style Sheet Languages
• Style sheet languages are used to describe the presentation of
structured documents, like HTML, XML and other markup languages.
What is CSS?
• Cascading Style Sheets
• Contains the rules for the presentation of HTML
=
+
• They were established by the World Wide Web Consortium (W3C). CSS rules
control the look (Style) of web pages or XML files by providing central locations
(Sheets) where HTML or XML tags are interpreted by the browser.
• Cons
• Different browsers may interpret Style Sheets in different ways
• Some styles may not be seen at all on some browsers
General Syntax
• Style Definition:
Selector {
property: value;
}
• Example: body { font-family: Verdana; font-size: 9px; }
style.css
Source of Styles
> Author (Developer) Style Sheets
• External Styles
• Another way to associate a stylesheet file to an HTML page is
through the <style> tag and an inline style rule called @import.
style.css
page1.html
• http://www.webdistortion.com/2009/02/22/useful-online-tools-for-
easier-website-planning-and-prototyping/
Planning and prototyping(using
mindmeister and mockups)
CSS SELECTORS
CSS Selectors
• In CSS, selectors are patterns used to select that element(s) you want
to style.
• Selectors allow us to target elements on a page.
• Selectors: Type/Element/Tag based selector, Identifier Selector, Class
Selector, Grouping Selector, Descendant Selector, Child Selector,
Adjacent Sibling Selector, General Sibling Selector, Attribute Selector,
Universal Selector, Pseudo-classes.
CSS Selectors
> Element Selector
• Also known as type selector and tag
based selector.
• The element selector allows you to
target content on the page based on
the element that contains it.
• Specify the style(s) for a single HTML
element.
• Extremely powerful, but very broad.
Quiz:-Use Element Selector
CSS Selectors
> Class Selector (.)
• Class selectors allow you to
target any element on the
page that has the same class
attribute.
• Class based styles can be
used by multiple HTML
elements.
• They are defined using the
period (.) character together
with a class name.
CSS Selectors
> Id Selector (Identifier Selector)
(#)
• Id selectors work in much the same
way as class selectors in that, they
allow us to target any element on the
page with a specific Id attribute.
• They are defined using the pound (#)
character together with the Id of the
element we want to target.
• In an event where Id and Class selector
conflict with each other, the Id selector
styling will be used in favor of the class
because it is more specific.
CSS Selectors
> Considerations (Id & Class)
• Class and Id attributes extend the meaning of your HTML code.
CSS Selectors
> Considerations (Id & Class)
CSS Selectors
> Class and Id Selectors with the Element
• Below is an example of an element specific selector with class and id.
• Notice that there is no space between the element name and the
class or id selector.
Quiz:-use class & id selectors
CSS Selectors
> Universal Selectors (*)
• Universal selectors are used to select any element.
• The following code sets the text color of all elements in a page to
blue.
CSS Selectors
> Grouping Selectors (,)`
• Often you will find several different elements on the page require the
exact same styling.
• Allows you to specify a single style for multiple elements at one time.
• Multiple selectors can be grouped in a single declaration by using
comma (,)
Quiz:-Group selectors
CSS Selectors
> Descendant Selector (space)
• The most powerful targeting ability
CSS gives us is the ability to
combine selectors together in what
is known as descendant selectors.
• Allow to more precisely target
content based on the relationship
between nested tags and their
parents.
• Descendant selectors are used to
select elements that are
descendants (not necessarily
children) of another element in the
document tree.
CSS Selectors
> Descendant Selector (space)
Quiz:-use a descendant selector to change the font
size of link two
CSS Selectors
> Child Selectors (>)
• A child selector is used to
select an element that is
a direct child of another
element (parent). Child
selectors will not select
all descendants, only
direct children.
CSS Selectors
> Child Selectors (>)
Quiz:-Use Child Selectors to change colors of the
first & third paragraph
CSS Selectors
> Adjacent Selectors (+)
• Also called adjacent sibling
selectors.
• Sibling = has the same parent
• Adjacent = immediately
following
• Allow you to target elements
based on which elements
follow one another in your
code. Essentially adjacent
selector allow you to style an
element based on which
element comes before it in the
document, providing that
both of those elements are
inside the same parent.
CSS Selectors
> Adjacent Selectors (+)
Quiz:- Use Adjacent Selectors To change the last
paragraph color
CSS Selectors
> General Sibling Selectors (~)
• Sibling = has the same
parent element
• General = just following
• Allow you to target elements
based on which elements
follow in your code.
Essentially general sibling
selector allow you to style an
element based on which
element comes before it (not
necessarily immediately) in
the document, providing
that both of those elements
are inside the same parent.
Quiz:-Use General Sibling Selectors
CSS Selectors
> Attribute Selectors
• Attribute selectors selects
elements based upon the
attributes present in the
HTML tags and their value.
• Attributes are specified
using brackets.
• [attribute=“value”] (case
sensitive)
• [attribute] (doesn’t matter
what the value is)
CSS Selectors
> Attribute Selectors
CSS Selectors
> Attribute Selectors
CSS Selectors
> Attribute Selectors
Quiz :- Use Attribute selectors to change the color
of the second link
CSS Selectors
> Attribute Selectors
• Allow us to much patterns as well
• Tilde (~): It basically says go ahead and look for a
white space separated list that includes the word.
CSS Selectors
> Attribute Selectors
• Another pattern matching character
• Caret (^): It basically says go ahead and match any
string that begins with a certain value.
CSS Selectors
> Attribute Selectors
• Another pattern matching character
• Dollar Sign ($): It basically says go ahead and match
any string that ends with a certain value.
CSS Selectors
> Attribute Selectors
• Another pattern matching character
• Asterisk Sign (*): It basically says go ahead and match
any string that contains a certain value.
Quiz:-Use attribute selector & Asterisk Sign