Table of Contents
•
What is CSS?
•
Styling with Cascading Style sheets (CSS)
•
Selectors and style definitions
•
Linking HTML and CSS
CSS: A New Philosophy
•
Separate content from presentation!
Title
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at pede ut purus malesuada dictum. Donec vitae neque non magna aliquam dictum.
The Resulting Page
Title
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at pede ut purus malesuada dictum. Donec vitae neque non magna aliquam dictum.
• Vestibulum et odio et ipsum
• accumsan accumsan. Morbi at • arcu vel elit ultricies porta. Proin
CSS Introduction
•
Cascading Style Sheets (CSS)
– Used to describe the presentation of documents – Define sizes, spacing, fonts, colors, layout, etc. – Improve content accessibility
– Improve flexibility
CSS Introduction
CSS stands for Cascading Style Sheets
Styles define how to display HTML elements Styles are normally stored in Style Sheets
Styles were added to HTML 4.0 to solve a
problem
External Style Sheets can save you a lot of work External Style Sheets are stored in CSS files
Cont…
•
CSS can be applied to any XML document
Multiple Styles Will Cascade into One
• Style sheets allow style information to be specified in
Cascading Order
Generally speaking we can say that all the styles
will cascade into a new virtual style sheet by
ʺ
ʺ
ʺ
ʺ
the following rules, where number four has the
highest priority:
Browser default
External style sheet
CSS Syntex
The CSS syntax is made up of three parts: a selector, a property and a value:
Selector Property Value
Ex : ‐ selector { property : value } body { color: black }
Style Sheets Syntax
•
More then one property, Separate each
property with semicolon
Grouping
•
Selectors are separated by commas
•
Declarations are separated by semicolons
•
Properties and values are separated by colons
Class selector
p.right {text-align: right} p.center {text-align: center}
HTML Code
<p class="right">
This paragraph will be right-aligned.
</p><p class="center">
Id Selector
You can also define styles for HTML elements
with the id selector. The id selector is defined as
a #.
CSS Comments
•
Comments are used to explain your code, and
may help you when you edit the source code
at a later date. A comment will be ignored by
browsers. A CSS comment begins with /* ,
ʺ ʺ
and ends with */ , like this:
ʺ ʺ
•
Selectors determine which element the rule
applies to:
– All elements of specific type (tag)
– Those that match a specific attribute (id, class)
– Elements may be matched depending on how they
are nested in the document tree (HTML)
•
Examples:
.header a { color: green }
Cont…
Three primary kinds of selectors are :
By tag (type selector):
By element id:
By element class name (only for HTML): Example
Selectors can be combined with commas:
This will match <h1> tags, elements with class link, and element with id top-link
h1 { font-family: verdana,sans-serif; }
#element_id { color: #ff0000; }
.myClass {border: 1px solid red}
Cont…
•
Pseudo-classes define state
– :hover, :visited, :active , :lang
•
Pseudo-elements define element "parts" or are
used to generate content
– :first-line , :before, :after
a:hover { color: red; }
p:first-line { text-transform: uppercase; } .title:before { content: "»"; }
Cont…
•
Match relative to element placement:
This will match all
<a>
tags that are inside of
<p>
•
*
– universal selector (avoid or use with care!):
Universal selectors are used to select any element. The above rule will color all HTML elements on a page blue - regardless of their position in the document tree.
Click Here
•
+
selector – used to match “next sibling”:
This will match all siblings with class name
link
that appear immediately after
<img>
tag
p a {text-decoration: underline}
* {color: blue}
Cont…
• > selector – matches direct child nodes:
This will match all elements with class error, direct children of <p> tag
• [ ] – matches tag attributes by regular expression:
This will match all <img> tags with alt attribute containing the word logo
• .class1.class2 (no space) - matches elements
with both (all) classes applied at the same time
p > .error {font-size: 8px}
Values in the CSS Rules
•
Colors are set in RGB format (decimal or hex):
– Example: #a0a6aa = rgb(160, 166, 170) – Predefined color aliases exist: black, blue, etc.
•
Numeric values are specified in:
– Pixels, ems, e.g. 12px , 1.4em
– Points, inches, centimeters, millimeters
• E.g. 10pt , 1in, 1cm, 1mm
– Percentages, e.g. 50%
Default Browser Styles
•
Browsers have default CSS styles
– Used when there is no CSS information or any other
style information in the document
•
Caution: default styles differ in browsers
– E.g. margins, padding and font sizes differ most
Linking HTML and CSS
•
HTML (content) and CSS (presentation) can be linked
in three ways:
– Inline: the CSS rules in the style attribute
• No selectors are needed
– Embedded: inside the <head> in a <style> tag – External: CSS rules in separate file (best)
• Usually a file with .css extension
• Linked via <link rel="stylesheet" href=…> tag or @import directive in embedded CSS block
•
Using external files is highly recommended
– Simplifies the HTML document
Cont…
1. Inline Styles: Example
2. Embedded Styles: Example
– Embedded in the HTML in the <style> tag:
• The <style> tag is placed in the <head> section of the
document
• type attribute specifies the MIME type
– MIME describes the format of the content
– Other MIME types include text/html, image/gif,
text/javascript …
External Style Sheet
An external style sheet is ideal when the style is
applied to many pages.
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
hr {color: sienna} p {margin-left: 20px}
body {background-image: url("back40.gif")}
Internal Style Sheet
An internal style sheet should be used when a single document has a unique style. You define internal styles in the head section by using the <style> tag, like this:
<head>
<style type="text/css"> hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")} </style>
Inline Styles
An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this method sparingly, such as when a style is to be applied to a single occurrence of an element.
To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:
<p style="color: sienna; margin-left: 20px"> This is a paragraph
Cont…
3. External CSS Styles
– Separate pages can all use a shared style sheet
– Only modify a single file to change the styles across
your entire Web site
link
tag (with a
rel
attribute) :
Example_1
– Specifies a relationship between current document
and another document
– link elements should be in the <head>
Cont…
@import :
Example_2
,
Example_3
– Another way to link external CSS files – Example:
– Ancient browsers do not recognize @import
<style type="text/css">
@import url("styles.css"); /* same as */
CSS - Background
• You can set the following background properties of an element −
• The background-color property is used to set the background color of
an element.
• The background-image property is used to set the background image of
an element.
• The background-repeat property is used to control the repetition of an
small image in the background.
1. Use no repeat value for no repetition.
2. Use repeat-y to repeat background image vertically 3. Use repeat-x to repeat background image horizontally
• The background-position property is used to control the position of an
image in the background.
• The background-attachment property is used to control the scrolling of
Background-image
CSS TEXT
• The CSS text properties allow you to control the appearance
of text.
• It is possible to change the color of a text, increase or decreas
e the space between characters in a text, align a
CSS font
CSS border
• The CSS border properties allow you to specify
the style and color of an element's border.
• In HTML we use tables to create borders around
a text, but with the CSS border
Border style
• dotted - Defines a dotted border• dashed - Defines a dashed border • solid - Defines a solid border
• double - Defines a double border
• groove - Defines a 3D grooved border. The effect depends on the border-color
value
• ridge - Defines a 3D ridged border. The effect depends on the border-color
value
• inset - Defines a 3D inset border. The effect depends on the border-color value • outset - Defines a 3D outset border. The effect depends on the border-color
value
• none - Defines no border
CSS Margin
• The CSS margin properties define the space
around elements. It is possible to use negative values to overlap content. The top, right,
bottom, and left margin can be
changed independently using separate properties.
• A shorthand margin property can also be used to
CSS padding
•
The CSS padding properties define the
space between the element border and
the
element content.
•
Negative values are not allowed. The top,