FEWD Topic 3 - Introduction to CSS
FEWD Topic 3 - Introduction to CSS
Topic 3:
Introduction to CSS
Learning Outcomes
Unit Roadmap
Week Topic Week Topic
Overview of Web JavaScript - II
1 Application Architecture 7
CSS BASICS
Introduction to CSS Topic 3 - 3.6
CSS Syntax
CSS Syntax
p{
color:red;
text-align:center;
}
Introduction to CSS Topic 3 - 3.9
CSS Comment
• Comments are used to explain the code.
• Comments are ignored by browsers.
• A CSS comment begins with /* and ends with */, for example:
/*This is a comment*/
p{
text-align:center;
/*This is another comment*/
color:black;
font-family:arial;
}
Introduction to CSS Topic 3 - 3.10
#para1
{
text-align:center; color:red;
}
Introduction to CSS Topic 3 - 3.11
Class Selector
• Specifies a style for a group of elements.
• It is used on several elements.
• A particular style can be set for multiple HTML elements with the same
class.
• Class selector uses the HTML class attribute, and is defined with a "."
Example 1: all HTML elements with class="center“ will be center-aligned:
.center {text-align:center;}
• A specific set of HTML elements should be affected by a class.
Example 2: all p elements with class="center" will be center-aligned:
p.center {text-align:center;}
Introduction to CSS Topic 3 - 3.12
Quiz - 1
1. What does CSS stand for?
A) Computer Style Sheets
B) Creative Style System
C) Cascading Style Sheets
D) Coding Style Sheets
3. Which of the following is the correct syntax for setting the background color of an element in CSS?
A) background-color: blue;
B) bgcolor: blue;
C) color-background: blue;
D) set-background: blue;
Introduction to CSS Topic 3 - 3.13
WAYS TO INSERT
STYLE SHEETS
Introduction to CSS Topic 3 - 3.14
<head>
<link rel="stylesheet" type="text/css"
href=“samplestyle.css">
</head>
Introduction to CSS Topic 3 - 3.16
Example: hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/background.gif");}
Introduction to CSS Topic 3 - 3.17
Inline Styles
• Style property is used in-line of the HTML tag.
• Can contain any CSS property.
Example: <p style="color:sienna;margin-left:20px;">
This is a paragraph.
</p>
Introduction to CSS Topic 3 - 3.19
Cascading Order
• When multiple styles are used – HTML uses an order of priority.
• The priority order from low to high is:
1. Browser default
2. External style sheet
3. Internal style sheet (in the head section)
4. Inline style (inside an HTML element)
• The inline style (inside an HTML element) has the highest priority and
overrides the styles defined inside the <head> tag, or in an external style
sheet, or in a browser (a default value).
Introduction to CSS Topic 3 - 3.20
Quiz - 2
1. What is an external CSS stylesheet?
A) A CSS file embedded within an HTML document
B) A separate CSS file linked from an HTML document
C) A CSS rule defined in the head section of an HTML document
D) Inline styles applied directly to HTML elements
CSS BACKGROUND
Introduction to CSS Topic 3 - 3.22
CSS Background
• background-color
• background-image
• background-repeat
Introduction to CSS Topic 3 - 3.23
Background Color
background-color property
Example:
body {background-color:#b0c4de;}
Introduction to CSS Topic 3 - 3.24
Background Image
background-image property
Example:
body {background-image:url("paper.gif");}
Introduction to CSS Topic 3 - 3.25
Background Image
• By default, a background image repeats across pages.
• To avoid repetition, use the background-repeat property:
Example:
body
{
background-image:url(“me.png");
background-repeat:no-repeat;
}
Introduction to CSS Topic 3 - 3.26
div {
width: 300px;
padding: 25px;
border: 25px solid navy;
margin: 25px;
}
Introduction to CSS Topic 3 - 3.28
CSS Margins
• Adds space around elements, outside of any defined borders.
CSS Padding
• Creates space around an element's content, inside of any defined borders.
div {
padding-top: 50px;
padding-right: 100px;
padding-bottom: 50px;
padding-left: 100px;
}
Introduction to CSS Topic 3 - 3.33
TEXT FORMATTING
IN CSS
Introduction to CSS Topic 3 - 3.34
Text Color
Text Color
Sets the color of the text.
• Specified by: color: color|initial|inherit;
Text Alignment
h1 {
text-align: center;
}
p.date {
text-align: right;
}
p.main {
text-align: justify;
}
Introduction to CSS Topic 3 - 3.36
Vertical-align Property
img {
vertical-align: text-top;
}
Introduction to CSS Topic 3 - 3.37
Text Decoration
Set or remove decorations from text.
a{
text-decoration: none;
}
h1 {
text-decoration: overline;
}
Introduction to CSS Topic 3 - 3.39
Text Transformation
• Specifies the uppercase and lowercase letters in a text:
text-transform: none|capitalize|uppercase|lowercase|initial|inherit;
p.class1 {
text-transform: uppercase;
}
h1{
text-transform:lowercase;
}
Introduction to CSS Topic 3 - 3.40
Text Indentation
• Specify the indentation of the first line of a text:
text-indent: length|initial|inherit;
Value Descriptions
p{
text-indent: 50px; length Defines a fixed indentation in px, pt, cm, em, etc. Default
} value is 0.
% Defines the indentation in % of the width of the parent
element.
It may be both in
initial Sets this property to its default value.
percentage or in pixels!
inherit Inherits this property from its parent element.
Introduction to CSS Topic 3 - 3.41
Word-spacing Property
• Specify the space between words:
word-spacing: normal|length|initial|inherit;
Value Description
normal Defines normal space between words . This is the
p{ default.
word-spacing: 30px; length Defines an extra space between words in px, pt, cm, em,
etc.
}
Negative values are allowed.
initial Sets this property to its default value.
Letter-spacing Property
Is used to control the space between individual characters.
• Specified by: letter-spacing: normal|length|initial|inherit;
Value Description
h1 {
letter-spacing: 2px; normal No extra space between characters. This is the default.
} length Defines an extra space between characters (negative
values are allowed).
h2 {
letter-spacing: -3px; initial Sets this property to its default value.
} inherit Inherits this property from its parent element.
Introduction to CSS Topic 3 - 3.43
Direction Property
Defines the direction of text within HTML elements.
Value Description
div { ltr The writing direction is left-to-right. This is the
direction: rtl; default.
} rtl The writing direction is right-to-left.
initial Sets this property to its default value.
inherit Inherits this property from its parent element.
Introduction to CSS Topic 3 - 3.44
Text-shadow Property
Is used to apply a shadow effect to text within HTML elements.
• Specified by: text-shadow: h-shadow v-shadow blur-radius
color|none|initial|inherit; Value Description
CSS FONT
AND STYLING
Introduction to CSS Topic 3 - 3.46
CSS Font
p.ex1 {
font: 15px arial, sans-
serif;
}
Introduction to CSS Topic 3 - 3.47
CSS Font
Font-style p.italic {
font-style: italic
font-style: normal|italic|oblique|initial|inherit; }
Font-variant
p.small {
font-variant: normal|small-caps|initial|inherit; font-variant: small-caps;
}
Introduction to CSS Topic 3 - 3.48
CSS Font
Font-weight p.normal {
font-weight: normal;
font-weight: normal|bold|bolder|lighter|number|initial|inherit; }
Font-size h1 {
font-size:medium|xx-small|x-small|small|large|x- font-size: 250%;
}
large|xx large|smaller|larger|length|initial|inherit;
h1 {
font-size: 2.5em; /* 40px/16=2.5em
Note: */
Smaller-Sets the font-size to a smaller size than the parent element
%-Sets the font-size to a percent of the parent element's font size }
p{
font-family: Times New
Font-family Roman;
font-family: font|initial|inherit; }
Introduction to CSS Topic 3 - 3.49
Styling Links
a:link - a normal, unvisited link a:hover - a link when the user’s mouse over it
a:visited - a link the user has visited a:active - a link the moment it is clicked
/* unvisited link */ /* mouse over link */
a:link { a:hover {
color: #FF0000; color: #FF00FF;
text-decoration: none; background-color: #FF704D;
}
}
/* visited link */
a:visited { /* selected link */ a:active {
color: #00FF00; color: #0000FF;
text-decoration: underline; }
}
Notes: a:hover MUST come after a:link and a:visited and
a:active MUST come after a:hover
Introduction to CSS Topic 3 - 3.50
CSS COLORS
Introduction to CSS Topic 3 - 3.51
CSS Colors
<h1 style="color:Tomato;">Tomato Colour</h1>
Colors can be set by: <h1 style="color:DodgerBlue;">Dodger Blue Colour </h1>
<h1 style="color:MediumSeaGreen;"> Medium Sea Green Colour</h1>
• By color names
<h1 style="border:2px solid Tomato;">Hello World</h1>
• By RGB values <h1 style="border:2px solid DodgerBlue;">Hello World</h1>
• By hex values <h1 style="border:2px solid Violet;">Hello World</h1>
• By RGBA <h1 style="background-color:rgb(255, 99,71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
• By HSL <h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
<h1 style="background-color:rgba(255, 99, 71,0.5);">...</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.5);">...</h1>
Introduction to CSS Topic 3 - 3.52
RGB Value
• Colour can be specified as an RGB value:
rgb(red, green, blue)
• Each parameter (red, green, and blue) defines the intensity of
the color between 0 and 255.
For example:
• rgb(255, 0, 0) - displayed as red
• rgb(0,0,0) – displayed as black
• rgb(255,255,255) – displayed as white
Introduction to CSS Topic 3 - 3.53
RGBA Value
HEX Value
• Color can be specified using a hexadecimal value in the form:
#rrggbb
• Where rr (red), gg (green) and bb (blue) are hexadecimal values
between 00 and ff (same as decimal 0-255).
• For example, #ff0000 is displayed as red, because red is set to its
highest value (ff) and the others are set to the lowest value (00).
Introduction to CSS Topic 3 - 3.55
References
CSS Definition:
Mozilla Developer Network (MDN). (n.d.). What is CSS? - Learn web development. Available at: https://developer.mozilla.org/en-
US/docs/Learn/CSS/First_steps/What_is_CSS
CSS Syntax:
Mozilla Developer Network (MDN). (n.d.). CSS: Cascading Style Sheets. Available at: https://developer.mozilla.org/en-
US/docs/Web/CSS
Wikipedia. (n.d.). CSS. Available at: https://en.wikipedia.org/wiki/Cascading_Style_Sheets
CSS Class:
W3Schools. (n.d.). CSS Introduction. Available at: https://www.w3schools.com/css/css_intro.asp
CSS Style Sheets:
Mozilla Developer Network (MDN). (n.d.). CSS: Cascading Style Sheets. Available at: https://developer.mozilla.org/en-
US/docs/Web/CSS
CSS Background:
W3Schools. (n.d.). CSS Backgrounds. Available at: https://www.w3schools.com/css/css_background.asp
CSS Box Model:
Mozilla Developer Network (MDN). (n.d.). Box model. Available at: https://developer.mozilla.org/en-
US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model
Topic 3 – Introduction to CSS
Any Questions?