SlideShare a Scribd company logo
CHAPTER 4
INTRODUCTION TO CASCADING
STYLE SHEETS
(CSS) - Part 1
IT 210
Web-Based Design
1
O B J E C T I V E S
By the end of this chapter, you’ll be able to:
- Manage a website's appearance with style sheets.
- Use a style sheet to provide consistency across all website pages.
- Apply styles using the class attribute.
- Customize text properties such as font, size, and color with
precision.
2
OUTLINES FOR PART 1
 Introduction.
 Inline Styles.
 Embedded Style Sheets.
 Linking External Style Sheets.
Selectors in CSS
3
INTRODUCTION
The basic structure of every web page, HTML, is very plain on
its own. The beautiful websites that you see across the
internet are styled with a variety of tools, including CSS.
Cascading Style Sheets 3 (CSS3): is a language that is used in
combination with HTML that customizes how HTML elements
will appear. CSS can define styles and change the layout and
design of a sheet
CSS validated
 iigsaw.w3.org/css-validator/
 This tool can help you make sure that your code is correct
and will work on CSS3-compliant browsers. 4
INTRODUCTION
CSS: A New Philosophy
 Separate content from presentation!
Content
(HTML document)
Presentation
(CSS Document)
Title
Lorem ipsum dolor sit
amet, consectetuer
adipiscing elit.
Suspendisse at pede ut
purus malesuada
dictum. Donec
viaccumsan. Morbi at
• arcu vel elit ultricies
porta. Proin
tortor purus, luctus
non, aliquam nec,
interdum vel, ia
molestie. Praesent
augue tortor, convallis
eget, euismod
nonummy, lacinia ut,
risus.
Bold
Italics
Indent
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
The Resulting
Page
Nice web done by CSS: http://seanhalpin.io/
WRITING CSS CODE IN HTML FILE
You can declare CSS codes in the HTML5, by:
1. INLINE STYLES:
CSS styles can be directly added to HTML elements by using the
style attribute in the element’s opening tag. Each style declaration
is ended with a semicolon. Styles added in this manner are known
as inline styles.
2. EMBEDDED STYLE: CSS code can be written in an HTML file by
enclosing the code in <style> tags. Code surrounded by <style>
tags will be interpreted as CSS syntax.
6
<h2 style="text-align: center;"> Centered text </h2>
<p style="color: blue; font-size:
18px;">Blue,text</p>
<head> <style> h1 { color : blue ; } </style> </head>
INLINE STYLES (CONT.) EXAMPLE (1)
7
INLINE STYLES
In Example (1) applies inline styles to p elements to alter their font
size and color.
 Each CSS property is followed by a colon (:) and the value of the
attribute and
 Multiple property declarations are separated by a semicolon (;)
8
<p style = "font-size: 20pt ; color:
deepskyblue ;">
it’s important to know that inline styles are a
quick way of directly styling an HTML element.
Fig. 4.1 | Using inline styles
INLINE STYLES (CONT.)
EXAMPLE (1) ON THE BROWSER
9
inline styles are a fast way of
styling HTML, but they also
have limitations. If you
wanted to style, for
example, multiple <h1>
elements, you would have
to add inline styling to each
element manually. In
addition, you would also
have to maintain the HTML
code when additional <h1>
elements are added.
https://colorhunt.co/ is a web site to help Designers and Artists to choose a
beautiful Color Palettes
INLINE STYLES (CONT.)
COLOR PROPERTY SETS TEXT COLOR
Color names and hexadecimal codes may be used as the color property value.
10
EMBEDDED (internal) STYLE SHEETS
 A second technique for using style sheets is embedded style
sheets, which enable you to embed a CSS3 codes in an
HTML5 document’s head section.
11
EMBEDDED STYLE SHEETS
EXAMPLE (2)
In this example create
an embedded style
sheet containing four
styles.
12
EMBEDDED STYLE SHEETS
EXAMPLE (2) CONT..
13
Fig. 4.3 | Embedded style sheet.
EMBEDDED STYLE SHEETS
EXAMPLE (2) OUTPUT
14
EMBEDDED STYLE SHEETS
EXAMPLE (2): EXPLAINING LINE 11 TO 18
Line 11: Styles placed in the head apply to matching elements wherever
they appear in the body. The style element’s type attribute specifies the
MIME (Multi purpose Internet Mail Extensions) type that describes the
style element’s content.
lines 12–17: declares the CSS rules for the style sheet.
15
Default in
HTML 5
STYLE SHEETS SYNTAX
 A style sheet consists of a set of rules.
 Each rule consists of one or more selectors and a declaration block.
 Selectors are separated by commas
 Declarations are separated by semicolons
 Properties and values are separated by colons
16
h1,h2,h3 { color: green; font-weight: bold; }
WRITING CSS CODE IN SEPARATE FILES
CSS code can be written in its own files to keep it separate from
the HTML code. The extension for CSS files is .css. These can be
linked to an HTML file using a <link> tag in the <head> section.
That called as External Style (highly recommended )
CSS rules in separate file .css extension linked via
<link rel="stylesheet" href=…> tag
17
WRITING CSS CODE IN SEPARATE FILES
<LINK> TAG
To link an external style sheet in html documents we use <link> tag.
Note: Link tag is void. (No need to close it)
o link tag has many elements some of them are:
18
Element
(Attribut
e)
Description
rel specify a relationship between two
documents
type specifies the MIME type of the related
document
href provides the URL for the document
WRITING CSS CODE IN SEPARATE FILES
 External style sheets are separate documents that contain
only CSS rules.
 Help create a uniform look for a website:
oSeparate pages can all use the same styles.
oModifying a single style-sheet file makes changes to styles
across an entire website (or to a portion of one).
 When changes to the styles are required, you need to
modify only a single CSS file to make style changes across
all the pages that use those styles. This concept is
sometimes known as skinning.
19
body { background-color:
#E6E6FA;
color: #000000;}
h2 { color: #003366; }
LINKING EXTERNAL STYLE SHEETS
Multiple web pages can associate with the same external
style sheet file.
20
site.css index.html
clients.html
about.html
Etc…
WHAT IS THE PREFERRED WAY OF INCLUDING CSS WITHIN A
PROJECT? HOW DO I KNOW WHEN TO USE INLINE STYLES, THE
TAGS, OR A .CSS FILE?
As a web developer you will eventually come across HTML documents
which include CSS either inline with style attributes or within the <style>
tags at the head of the document. Hence, it is important to be aware of
all the various ways to include CSS in a project.
That having been said, mashing HTML and CSS together is not a great
habit to get into. As developers we want to “separate our concerns”
because this usually creates codebases that are more flexible, readable,
and maintainable. As such, keeping our CSS contained within a separate
.css file is the preferred way of including CSS within most projects.
21
SELECTORS IN CSS
1. CSS Type Selectors
 used to match all elements of a given type or tag name
 For example, in HTML, the tag for a paragraph element
is <p>. The CSS syntax for selecting <p> elements is:
p {
…..
}
22
SELECTORS IN CSS
2. CSS class selectors
 it matches elements based on the contents of their class
attribute. a . needs to be prepended.
23
For example, consider the following HTML:
<p class="brand">Sole Shoe Company</p>
The paragraph element in the example above has a class
attribute within the <p> tag.
The class attribute is set to "brand". To select this element
using CSS, we could use the following CSS selector:
.brand {
}
SELECTORS IN CSS
3. CSS ID selectorsIt matches elements based on the contents of their id
attribute. The values of id attribute should be unique.
24
Explain:
If an HTML element needs to be styled uniquely (no
matter what classes are applied to the element), we
can add an ID to the element to the tag:
<h1 id="large-title"> ... </h1>
Then, CSS can select HTML elements by their id
attribute. To select an id element, CSS prepends the
id name with a hashtag (#). For instance, if we wanted
to select the HTML element in the example above, it
would look like this:
#large-title {
SELECTORS IN CSS
4. Groups of CSS Selectors
 Match multiple selectors to the same CSS rule, using a
comma-separated list. In this example, the text for both h1
and h2 is set to red.
Example:
h1, h2 {
color: red; }
25
SELECTORS IN CSS
5. Chaining Selectors
 that rule sets apply only to elements that match all criteria.
Example:
26
/* Select h3 elements with the section-heading class
*/
h3.section-heading {
color: blue; }
/* Select elements with the section-heading and
button class */
.section-heading .button {
cursor: pointer; }
MULTIPLE CLASSES
For instance, perhaps there’s a heading element that needs to be green and
bold. You could write two CSS rules like so:
.green { color: green; }
.bold { font-weight: bold; }
Then, you could include both of these classes on one HTML element like this:
<h1 class="green bold"> ... </h1>
We can add multiple classes to an HTML element’s class attribute by separating
them with a space. This enables us to mix and match CSS classes to create
many unique styles without writing a custom class for every style combination
needed. 27
CLASS VS ID
classes are meant to be used many times
ID is meant to style only one element.
IDs override the styles of tags and classes.
ID attribute should be unique
28
SELECTOR SPECIFICITY
The most specific selector type is the ID selector, followed by
class selectors, followed by type selectors.
In this example, only color: blue will be implemented as it has
an ID selector whereas color: red has a type selector
Specificity is a ranking system that is used when there are
multiple conflicting property values that point to the same
element. When determining which rule to apply, the selector
with the highest specificity wins out.
29
h1 #header { color: blue; } /*implemented*/
h1 { color: red; } /*not implemented*/
CSS !IMPORTANT RULE
The CSS !important rule is used on declarations to override
any other declarations for a property and ignore selector
specificity.
!important rules will ensure that a specific declaration always
applies to the matched elements.
However, generally it is good to avoid using !important as
bad practice.
30
#column-one {
width: 200px !important; }
.post-title {
color: blue !important; }
Review CSS Selectors
•CSS can change the look of HTML elements. In order to do this, CSS must select HTML
elements.
•CSS can select HTML elements by tag, class, or ID.
•Multiple CSS classes can be applied to one HTML element.
•Classes can be reusable, while IDs can only be used once.
•IDs are more specific than classes, and classes are more specific than tags.
•The !important flag will override any style, however it should almost never be used, as it
is extremely difficult to override.
•Multiple unrelated selectors can receive the same styles by separating the selector
names with commas.
31
SOME OF CSS FONT PROPERTY
font-family Property
font-family property specifies the name of the font to use.
32
Fig. 4.5 | Generic font families.
SOME OF CSS FONT PROPERTY
font-size Property
 font-size property: specifies the size used to render the font.
 You can specify a point size such(12pt) or a relative value such as (xx-
small, x-small, small, smaller, medium, large, larger, x-large and xx-
large).
 Relative font-size values are preferred over points, because an author
does not know the specific measurements of each client’s display.
 Relative values permit more flexible viewing of web pages.
 For example, users can change font sizes the browser displays for
readability.
33
SOME OF CSS FONT PROPERTY
font-weight property
 font-weight property : specifies the “boldness” of
text. Possible values are:
o bold
o normal (the default)
o bolder (bolder than bold text)
o lighter (lighter than normal text)
34
SOME OF CSS FONT PROPERTY
text-align property
 The text-align property places text in the left, right,
or center of its parent container.
color property :
 defines the color of the text.
35
VALUES IN THE CSS RULES
 Colors are set in RGB format (decimal or hex):
o Example: #a0a6aa = rgb(160, 166, 170)
o Predefined color aliases exist: black, blue, etc.
 Numeric values are specified in:
o Pixels, ems, e.g. 12px , 1em
o Points, inches, centimeters, millimeters
• E.g. 10pt , 1in, 1cm, 1mm
o Percentages, e.g. 50%
o Zero can be used with no unit: border: 0; 36

More Related Content

PPTX
Lecture 3CSS part 1.pptx
PPTX
Introduction to CSS
PPT
CSS Training in Bangalore
PPTX
Cascading Style Sheet
PPT
Css week11 2020 2021 for g10 by eng.osama ghandour
PDF
4. Web Technology CSS Basics-1
PPTX
Web Development - Lecture 5
PDF
ch-hguygureehuvnvdfduyertiuhrnhduuyfjh3.pdf
Lecture 3CSS part 1.pptx
Introduction to CSS
CSS Training in Bangalore
Cascading Style Sheet
Css week11 2020 2021 for g10 by eng.osama ghandour
4. Web Technology CSS Basics-1
Web Development - Lecture 5
ch-hguygureehuvnvdfduyertiuhrnhduuyfjh3.pdf

Similar to Lecture 9 CSS part 1.pptxType Classification (20)

PPT
Css week11 2019 2020 for g10 by eng.osama ghandour
PPT
Unit 2-CSS & Bootstrap.ppt
PPT
Chapter 4a cascade style sheet css
PPTX
PPT
IP - Lecture 6, 7 Chapter-3 (3).ppt
PPTX
CSS Fundamentals: selectors and Properties
PPTX
Introduction to CSS.pptx web for web web
PPTX
PPT
ch04-css-basics_final.ppt
PDF
Intro to HTML and CSS - Class 2 Slides
PPTX
Web technologies-course 03.pptx
PPTX
Cascading style sheet, CSS Box model, Table in CSS
PPT
Shyam sunder Rajasthan Computer
PPTX
Cascading style sheet an introduction
PDF
Cascading Style Sheets
PPTX
Cascading Styling Sheets(CSS) simple design language intended to transform th...
PPTX
LIS3353 SP12 Week 13
Css week11 2019 2020 for g10 by eng.osama ghandour
Unit 2-CSS & Bootstrap.ppt
Chapter 4a cascade style sheet css
IP - Lecture 6, 7 Chapter-3 (3).ppt
CSS Fundamentals: selectors and Properties
Introduction to CSS.pptx web for web web
ch04-css-basics_final.ppt
Intro to HTML and CSS - Class 2 Slides
Web technologies-course 03.pptx
Cascading style sheet, CSS Box model, Table in CSS
Shyam sunder Rajasthan Computer
Cascading style sheet an introduction
Cascading Style Sheets
Cascading Styling Sheets(CSS) simple design language intended to transform th...
LIS3353 SP12 Week 13

More from ZahouAmel1 (18)

PPTX
2- lec_2.pptxDesigning with Type, SpacingDesigning with Type, SpacingDesignin...
PPTX
1-Lect_1.pptxLecture 5 array in PHP.pptx
PPTX
Lecture 8 PHP and MYSQL part 2.ppType Classificationtx
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PPTX
Lecture 2 HTML part 1.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PPTX
Lecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PPTX
Lec 1 Introduction to Computer and Information Technology #1.pptx
PPTX
DB-Lec1.pptxUpdatedpython.pptxUpdatedpython.pptx
PPTX
DB- lec2.pptxUpdatedpython.pptxUpdatedpy
PPTX
Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx
PPTX
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
PPTX
5-LEC- 5.pptxTransport Layer. Transport Layer Protocols
PPTX
6-LEC- 6.pptx Network Layer. Addressing Subnetting Mask (default and subnet) ...
PPTX
7-Lect_7 .pptxNetwork Layer. Addressing Subnetting Mask (default and subnet) ...
PPTX
7-Lect_7 .pptxNetwork LayerNetwork Layer
PPTX
8-Lect_8 Addressing the Network.tcp.pptx
PPTX
9-Lect_9-1.pptx9-Lect_9-1.pptx9-Lect_9-1.pptx
PPTX
9-Lect_9-2.pptx DataLink Layer DataLink Layer
2- lec_2.pptxDesigning with Type, SpacingDesigning with Type, SpacingDesignin...
1-Lect_1.pptxLecture 5 array in PHP.pptx
Lecture 8 PHP and MYSQL part 2.ppType Classificationtx
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 2 HTML part 1.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lec 1 Introduction to Computer and Information Technology #1.pptx
DB-Lec1.pptxUpdatedpython.pptxUpdatedpython.pptx
DB- lec2.pptxUpdatedpython.pptxUpdatedpy
Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx
4-Lect_4-2.pptx4-Lect_4-2.pptx4-Lect_4-2.pptx
5-LEC- 5.pptxTransport Layer. Transport Layer Protocols
6-LEC- 6.pptx Network Layer. Addressing Subnetting Mask (default and subnet) ...
7-Lect_7 .pptxNetwork Layer. Addressing Subnetting Mask (default and subnet) ...
7-Lect_7 .pptxNetwork LayerNetwork Layer
8-Lect_8 Addressing the Network.tcp.pptx
9-Lect_9-1.pptx9-Lect_9-1.pptx9-Lect_9-1.pptx
9-Lect_9-2.pptx DataLink Layer DataLink Layer

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Approach and Philosophy of On baking technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
“AI and Expert System Decision Support & Business Intelligence Systems”
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectroscopy.pptx food analysis technology
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
sap open course for s4hana steps from ECC to s4
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...

Lecture 9 CSS part 1.pptxType Classification

  • 1. CHAPTER 4 INTRODUCTION TO CASCADING STYLE SHEETS (CSS) - Part 1 IT 210 Web-Based Design 1
  • 2. O B J E C T I V E S By the end of this chapter, you’ll be able to: - Manage a website's appearance with style sheets. - Use a style sheet to provide consistency across all website pages. - Apply styles using the class attribute. - Customize text properties such as font, size, and color with precision. 2
  • 3. OUTLINES FOR PART 1  Introduction.  Inline Styles.  Embedded Style Sheets.  Linking External Style Sheets. Selectors in CSS 3
  • 4. INTRODUCTION The basic structure of every web page, HTML, is very plain on its own. The beautiful websites that you see across the internet are styled with a variety of tools, including CSS. Cascading Style Sheets 3 (CSS3): is a language that is used in combination with HTML that customizes how HTML elements will appear. CSS can define styles and change the layout and design of a sheet CSS validated  iigsaw.w3.org/css-validator/  This tool can help you make sure that your code is correct and will work on CSS3-compliant browsers. 4
  • 5. INTRODUCTION CSS: A New Philosophy  Separate content from presentation! Content (HTML document) Presentation (CSS Document) Title Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at pede ut purus malesuada dictum. Donec viaccumsan. Morbi at • arcu vel elit ultricies porta. Proin tortor purus, luctus non, aliquam nec, interdum vel, ia molestie. Praesent augue tortor, convallis eget, euismod nonummy, lacinia ut, risus. Bold Italics Indent 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 The Resulting Page Nice web done by CSS: http://seanhalpin.io/
  • 6. WRITING CSS CODE IN HTML FILE You can declare CSS codes in the HTML5, by: 1. INLINE STYLES: CSS styles can be directly added to HTML elements by using the style attribute in the element’s opening tag. Each style declaration is ended with a semicolon. Styles added in this manner are known as inline styles. 2. EMBEDDED STYLE: CSS code can be written in an HTML file by enclosing the code in <style> tags. Code surrounded by <style> tags will be interpreted as CSS syntax. 6 <h2 style="text-align: center;"> Centered text </h2> <p style="color: blue; font-size: 18px;">Blue,text</p> <head> <style> h1 { color : blue ; } </style> </head>
  • 7. INLINE STYLES (CONT.) EXAMPLE (1) 7
  • 8. INLINE STYLES In Example (1) applies inline styles to p elements to alter their font size and color.  Each CSS property is followed by a colon (:) and the value of the attribute and  Multiple property declarations are separated by a semicolon (;) 8 <p style = "font-size: 20pt ; color: deepskyblue ;"> it’s important to know that inline styles are a quick way of directly styling an HTML element.
  • 9. Fig. 4.1 | Using inline styles INLINE STYLES (CONT.) EXAMPLE (1) ON THE BROWSER 9 inline styles are a fast way of styling HTML, but they also have limitations. If you wanted to style, for example, multiple <h1> elements, you would have to add inline styling to each element manually. In addition, you would also have to maintain the HTML code when additional <h1> elements are added.
  • 10. https://colorhunt.co/ is a web site to help Designers and Artists to choose a beautiful Color Palettes INLINE STYLES (CONT.) COLOR PROPERTY SETS TEXT COLOR Color names and hexadecimal codes may be used as the color property value. 10
  • 11. EMBEDDED (internal) STYLE SHEETS  A second technique for using style sheets is embedded style sheets, which enable you to embed a CSS3 codes in an HTML5 document’s head section. 11
  • 12. EMBEDDED STYLE SHEETS EXAMPLE (2) In this example create an embedded style sheet containing four styles. 12
  • 14. Fig. 4.3 | Embedded style sheet. EMBEDDED STYLE SHEETS EXAMPLE (2) OUTPUT 14
  • 15. EMBEDDED STYLE SHEETS EXAMPLE (2): EXPLAINING LINE 11 TO 18 Line 11: Styles placed in the head apply to matching elements wherever they appear in the body. The style element’s type attribute specifies the MIME (Multi purpose Internet Mail Extensions) type that describes the style element’s content. lines 12–17: declares the CSS rules for the style sheet. 15 Default in HTML 5
  • 16. STYLE SHEETS SYNTAX  A style sheet consists of a set of rules.  Each rule consists of one or more selectors and a declaration block.  Selectors are separated by commas  Declarations are separated by semicolons  Properties and values are separated by colons 16 h1,h2,h3 { color: green; font-weight: bold; }
  • 17. WRITING CSS CODE IN SEPARATE FILES CSS code can be written in its own files to keep it separate from the HTML code. The extension for CSS files is .css. These can be linked to an HTML file using a <link> tag in the <head> section. That called as External Style (highly recommended ) CSS rules in separate file .css extension linked via <link rel="stylesheet" href=…> tag 17
  • 18. WRITING CSS CODE IN SEPARATE FILES <LINK> TAG To link an external style sheet in html documents we use <link> tag. Note: Link tag is void. (No need to close it) o link tag has many elements some of them are: 18 Element (Attribut e) Description rel specify a relationship between two documents type specifies the MIME type of the related document href provides the URL for the document
  • 19. WRITING CSS CODE IN SEPARATE FILES  External style sheets are separate documents that contain only CSS rules.  Help create a uniform look for a website: oSeparate pages can all use the same styles. oModifying a single style-sheet file makes changes to styles across an entire website (or to a portion of one).  When changes to the styles are required, you need to modify only a single CSS file to make style changes across all the pages that use those styles. This concept is sometimes known as skinning. 19
  • 20. body { background-color: #E6E6FA; color: #000000;} h2 { color: #003366; } LINKING EXTERNAL STYLE SHEETS Multiple web pages can associate with the same external style sheet file. 20 site.css index.html clients.html about.html Etc…
  • 21. WHAT IS THE PREFERRED WAY OF INCLUDING CSS WITHIN A PROJECT? HOW DO I KNOW WHEN TO USE INLINE STYLES, THE TAGS, OR A .CSS FILE? As a web developer you will eventually come across HTML documents which include CSS either inline with style attributes or within the <style> tags at the head of the document. Hence, it is important to be aware of all the various ways to include CSS in a project. That having been said, mashing HTML and CSS together is not a great habit to get into. As developers we want to “separate our concerns” because this usually creates codebases that are more flexible, readable, and maintainable. As such, keeping our CSS contained within a separate .css file is the preferred way of including CSS within most projects. 21
  • 22. SELECTORS IN CSS 1. CSS Type Selectors  used to match all elements of a given type or tag name  For example, in HTML, the tag for a paragraph element is <p>. The CSS syntax for selecting <p> elements is: p { ….. } 22
  • 23. SELECTORS IN CSS 2. CSS class selectors  it matches elements based on the contents of their class attribute. a . needs to be prepended. 23 For example, consider the following HTML: <p class="brand">Sole Shoe Company</p> The paragraph element in the example above has a class attribute within the <p> tag. The class attribute is set to "brand". To select this element using CSS, we could use the following CSS selector: .brand { }
  • 24. SELECTORS IN CSS 3. CSS ID selectorsIt matches elements based on the contents of their id attribute. The values of id attribute should be unique. 24 Explain: If an HTML element needs to be styled uniquely (no matter what classes are applied to the element), we can add an ID to the element to the tag: <h1 id="large-title"> ... </h1> Then, CSS can select HTML elements by their id attribute. To select an id element, CSS prepends the id name with a hashtag (#). For instance, if we wanted to select the HTML element in the example above, it would look like this: #large-title {
  • 25. SELECTORS IN CSS 4. Groups of CSS Selectors  Match multiple selectors to the same CSS rule, using a comma-separated list. In this example, the text for both h1 and h2 is set to red. Example: h1, h2 { color: red; } 25
  • 26. SELECTORS IN CSS 5. Chaining Selectors  that rule sets apply only to elements that match all criteria. Example: 26 /* Select h3 elements with the section-heading class */ h3.section-heading { color: blue; } /* Select elements with the section-heading and button class */ .section-heading .button { cursor: pointer; }
  • 27. MULTIPLE CLASSES For instance, perhaps there’s a heading element that needs to be green and bold. You could write two CSS rules like so: .green { color: green; } .bold { font-weight: bold; } Then, you could include both of these classes on one HTML element like this: <h1 class="green bold"> ... </h1> We can add multiple classes to an HTML element’s class attribute by separating them with a space. This enables us to mix and match CSS classes to create many unique styles without writing a custom class for every style combination needed. 27
  • 28. CLASS VS ID classes are meant to be used many times ID is meant to style only one element. IDs override the styles of tags and classes. ID attribute should be unique 28
  • 29. SELECTOR SPECIFICITY The most specific selector type is the ID selector, followed by class selectors, followed by type selectors. In this example, only color: blue will be implemented as it has an ID selector whereas color: red has a type selector Specificity is a ranking system that is used when there are multiple conflicting property values that point to the same element. When determining which rule to apply, the selector with the highest specificity wins out. 29 h1 #header { color: blue; } /*implemented*/ h1 { color: red; } /*not implemented*/
  • 30. CSS !IMPORTANT RULE The CSS !important rule is used on declarations to override any other declarations for a property and ignore selector specificity. !important rules will ensure that a specific declaration always applies to the matched elements. However, generally it is good to avoid using !important as bad practice. 30 #column-one { width: 200px !important; } .post-title { color: blue !important; }
  • 31. Review CSS Selectors •CSS can change the look of HTML elements. In order to do this, CSS must select HTML elements. •CSS can select HTML elements by tag, class, or ID. •Multiple CSS classes can be applied to one HTML element. •Classes can be reusable, while IDs can only be used once. •IDs are more specific than classes, and classes are more specific than tags. •The !important flag will override any style, however it should almost never be used, as it is extremely difficult to override. •Multiple unrelated selectors can receive the same styles by separating the selector names with commas. 31
  • 32. SOME OF CSS FONT PROPERTY font-family Property font-family property specifies the name of the font to use. 32 Fig. 4.5 | Generic font families.
  • 33. SOME OF CSS FONT PROPERTY font-size Property  font-size property: specifies the size used to render the font.  You can specify a point size such(12pt) or a relative value such as (xx- small, x-small, small, smaller, medium, large, larger, x-large and xx- large).  Relative font-size values are preferred over points, because an author does not know the specific measurements of each client’s display.  Relative values permit more flexible viewing of web pages.  For example, users can change font sizes the browser displays for readability. 33
  • 34. SOME OF CSS FONT PROPERTY font-weight property  font-weight property : specifies the “boldness” of text. Possible values are: o bold o normal (the default) o bolder (bolder than bold text) o lighter (lighter than normal text) 34
  • 35. SOME OF CSS FONT PROPERTY text-align property  The text-align property places text in the left, right, or center of its parent container. color property :  defines the color of the text. 35
  • 36. VALUES IN THE CSS RULES  Colors are set in RGB format (decimal or hex): o Example: #a0a6aa = rgb(160, 166, 170) o Predefined color aliases exist: black, blue, etc.  Numeric values are specified in: o Pixels, ems, e.g. 12px , 1em o Points, inches, centimeters, millimeters • E.g. 10pt , 1in, 1cm, 1mm o Percentages, e.g. 50% o Zero can be used with no unit: border: 0; 36