CSS Stands For " Ascading Tyle Heets": Cascading
CSS Stands For " Ascading Tyle Heets": Cascading
Selector {declaration;}
Declaration = {property: value;}
Property: what aspect you want to change
ex: color, font, margins, etc.
Value: the exact setting for that aspect.
ex: red, italic, 40px, etc.
Selector
– Definition: identifies the HTML elements that
the rule will be applied to, identified by the
actual element name, e.g. <body>, or by other
means such as class attribute values.
– Example:
– Example:
• Each declaration or definition contains:
– A property
– A colon
– A value
– A semicolon to separate two or more values
– Can include one or more values
• h1 {color:blue; font-size:12px}
Different ways of Style
1. Inline styles
2. Embedded styles / Internal styles
3. External style sheet
Inline Style Sheet
• Inline styles
– Add styles to each tag within the HTML file
– Use it when you need to format just a single
section in a web page.
• Syntax:
– <h1 style=“color:red; font-family: sans-serif”>
Welcome to CSS</h1>
• Example: inline.html
Embedded / Internal Style Sheets
• Embedded or internal styles
– A style is applied to the entire HTML file
– Use it when you need to modify all instances
of particular element (e.g., h1) in a web page.
• Syntax:
<style>
p {color:green; font-size:30px}
</style>
• Example: embedded.html
Creating an Embedded Style Sheet
<head>
<title>Embedded Example</title>
<style type=“text/css”>
Style declarations
</style>
</head>
• A style declaration:
– Selector {attribute1:value1; attribute2:value2;
…}
– Selector = an element in a document (e.g., a
header or paragraph)
External Style Sheets
• External style sheets
– An external style sheet is a text file containing the
style definition (declaration)
– Use it when you need to control the style for an entire
web site.
• Syntax:
– h1, h2, p {color:red; font-family:sans-serif}
• Example: external.html
Creating an External Style Sheet
• Open a new blank document in Notepad
• Type style declarations
– h1 {color:red; font-family:sans-serif;}
• Do not include <style> tags
• Save the document as filename.css
Linking to Style Sheets
• Open an HTML file
• Between <head> and </head> add
– <link href=URL rel=“relation_type” type=“link_type”>
• URL is the file.css
• Relation_type=“stylesheet”
• Link_type=“text/css”
• Save this file and the .css file in the same web
server directory
An example of an external style
sheet with an original html file
<head> h1{color:red; font-
<title>External Style family:sans-serief;}
Sheet</title>
<link href=“external.css” h2{color:green;}
rel=“stylesheet”
type=“text/css” /> p{font-size:20px;
</head> color:blue;}
• Examples:
conflicts.html conflicts1.html conflicts2.html
Style Sheet Strategies
• Wherever possible, place your styles in
external style sheets
• Take advantage of the power of CSS to
have control over an entire Web site.
• At the top level of your web site: define a
default global.css style sheet
• Refine styles at sublevels with a
section.css style sheet
• Try to avoid using styles in tags
Attribute Selectors
• Create an attribute selector to select
an element based on the element’s
attributes.
Using IDs and Classes
• Use an id to distinguish something,
like a paragraph, from the others in a
document.
– For example, to identify a paragraph as
“head”, use the code:
Example: classes.html
Working With Classes
• To create a class, enter the following in the
HTML tag:
– <tag CLASS=class_name>
– <h1 CLASS=FirstHeader>IU</h1>
– class_name is a name to identify this class of
tags
• To apply a style to a class of tags, use:
– tag.class_name {style attributes} or
– .class_name {style attributes}
Working With Classes and Ids
• The difference between the Class
property and the ID property is that the
value of the ID property must be unique:
– you can’t have more than one tag with the
same ID value
– You can apply the same Class value to
multiple document tags
Positioning – what is it?
Positioning refers to the layout of the items
on your page.
• Example: dimensions.html
The CSS Box Model
• Every CSS box is divided into regions,
consisting of:
1.Content
2.Padding
3.Border
4.Margins
Introducing the <div> tag:
• The <div> tag is our basic building block when
laying out a page design. By defining the height
and width of the <div>, we are "reserving" that
amount of space on the screen for whatever
content we wish to place there.
<div class="box300">
This is outside the box.
This is a 300 by 300 pixel box
with a 1px border.
</div> By establishing the box
This is outside the box. dimensions, we can leave it there
as a placeholder until we have our
content ready. In the meantime,
the rest of the page can be built out
with our other content.
Adding padding:
<head> This is a 300 by 300 pixel box
<style type="text/css"> with a 1px border and 10px
.box300 { padding.
width:300px;
height:300px;
border:1px solid black;
padding:10px;
}
</style>
</head>
Example: boxmodel.html
Text Flow
• Floating
– Move an element to one side of the screen
• Example: floating.html
CSS media types
• CSS media types allow a programmer to decide what a
page should look like depending on the kind of media being
used to display the page.
• The most common media type for a web page is the screen
media type, which is a standard computer screen. Other
media types in CSS 2 include handheld, Braille, aural and
print.
• The handheld medium is designed for mobile Internet
devices, while Braille is for machines that can read or print web
pages in Braille. aural styles allow the programmer to give a
speech-synthesizing web browser more information about the
content of the web page.
• The print media type affects a web page’s appearance when it
is printed.
• Example: mediatypes.html
CSS Drop-Down Menu
• Drop-down menus are a good way to provide navigation links
on a website without using a lot of screen space.
• In this section, we take a second look at the :hover pseudoclass
and introduce the display property to create a drop-down menu
using CSS and XHTML.
• We will use this feature in a more advanced way to cause a
menu to appear when the mouse hovers over a menu button.
• The other important property we need is the display property.
• This allows a programmer to decide whether an element is
rendered on the page or not. Possible values include block,
inline and none.
• The block and inline values display the element as a block
element or an inline element, while none stops the element
from being rendered.
• Example: dropdown.html
User Style Sheets
• Format pages based on preferences
User Style Sheets