0% found this document useful (0 votes)
21 views30 pages

Cascading Stylesheet

The document provides an overview of Cascading Style Sheets (CSS), explaining its purpose in creating a consistent look and feel across web pages. It covers CSS syntax, selectors, and the different types of style sheets, including inline, internal, and external styles, along with their precedence in case of conflicts. Additionally, it discusses how multiple style sheets can be used and the cascading order of styles applied to HTML elements.

Uploaded by

Alex Kiplagat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views30 pages

Cascading Stylesheet

The document provides an overview of Cascading Style Sheets (CSS), explaining its purpose in creating a consistent look and feel across web pages. It covers CSS syntax, selectors, and the different types of style sheets, including inline, internal, and external styles, along with their precedence in case of conflicts. Additionally, it discusses how multiple style sheets can be used and the cascading order of styles applied to HTML elements.

Uploaded by

Alex Kiplagat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

CSS

OUTLINE
• Using a style sheet to give all the pages of a Web site the same
look and feel.
• CSS syntax for assigning
• Class selector and using the class attribute to apply styles.
• Style sheet types and cascade precedence
• Using style sheets to separate presentation from content.
Introduction
• Cascading Style Sheets (CSS)
• Separation of structure from presentation
• Relationship between the style formatting and the
structure/content is no longer 1:1
• multiple style sheets can be applied to the same Web page
• Same style sheet can be applied to the multiple Web page
CSS Syntax
• A CSS rule-set consists of a selector and a declaration block:

Selector Declaration Declaration

h1 { color: blue; font-size: 12px;}


Property value property value
Syntax explanation
• The selector points to the HTML element you want to style.
• The declaration block contains one or more declarations
separated by semicolons.
• Each declaration includes a CSS property name and a value,
separated by a colon.
• Multiple CSS declarations are separated with semicolons, and
declaration blocks are surrounded by curly braces
Example
p{
color: red;
text-align: center;
}
• p is a selector in CSS (it points to the HTML element you want
to style: <p>).
• color is a property, and red is the property value
• text-align is a property, and center is the property value
CSS Selectors
• Are used to "find" (or select) the HTML elements you want to style.
• There are five categories of selectors namely:
1. Simple selectors (select elements based on name, id, class)
2. Combinator selectors (select elements based on a specific
relationship between them)
3. Pseudo-class selectors (select elements based on a certain state)
4. Pseudo-elements selectors (select and style a part of an element)
5. Attribute selectors (select elements based on an attribute or
attribute value)
Basic CSS selectors
1.The CSS element Selector
• Here, all <p> elements on the page will be center-aligned, with a
red text color:

p{
text-align: center;
color: red;
}
The CSS id Selector

• Uses the id attribute of an HTML element to select a specific


element.
• The id of an element is unique within a page,
• id selector is used to select one unique element!
• To select an element with a specific id, write a hash (#)
character, followed by the id of the element.
Example

• The CSS rule below will be applied to the HTML element with
id="para1":

#para1 {
text-align: center;
color: red;
}
The CSS class Selector

• Selects HTML elements with a specific class attribute.


• To select elements with a specific class, write a period (.)
character, followed by the class name.
Example

• Here all HTML elements with class="center" will be red and


center-aligned:

.center {
text-align: center;
color: red;
}
Example 2
• You can also specify that only specific HTML elements should
be affected by a class.
Example
• In the code below only <p> elements with class="center" will
be center-aligned:

p.center {
text-align: center;
color: red;
}
• HTML elements can also refer to more than one class.
Example
• Here the <p> element will be styled according to class="center"
and to class="large":

<p class="center large">This paragraph refers to


two classes.</p>
The CSS Universal Selector
• Universal selector (*) selects all HTML elements on the page.

Example

• The CSS rule below will affect every HTML element on the
page:
*{
text-align: center;
color: blue;
}
The CSS Grouping Selector

• Selects all the HTML elements with the same style definitions.
Example
h1 {
text-align: center;
color: red;
}

h2 {
text-align: center;
color: red;
}

p{
text-align: center;
color: red;
}
The CSS Grouping Selector
• It will be better to group the selectors, to minimize the code.
• To group selectors, separate each selector with a comma.
Example

h1, h2, p {
text-align: center;
color: red;
}
All CSS Simple Selectors
Multiple Levels of Style
Definition: Ways to Insert CSS
• Cascade Precedence: resolves conflicting style rules

—Inline style sheets

—Embedded sheets

—External style sheets


Inline Styles
• Style information is directly attached to the HTML elements
they affect
• Higher cascade precedence than the other specification methods
• Declaring an individual element’s format:
-Attribute style
-CSS (style) property
-Followed by a colon and a value
Example
<!DOCTYPE html>
<html>
<body>

<h1 style="color:blue;text-align:center;">This is a heading</h1>


<p style="color:red;">This is a paragraph.</p>

</body>
</html>
Internal CSS

• Used when one single HTML page has a unique style.

• It is defined inside the <style> element, inside the <head>

section of an HTML page:


Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}

h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
External CSS

• Changes the entire website by changing just one file!

• A reference link to the external CSS file must be included inside

the <head> <link> </head> section of every HTML page


Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
External style sheet
• Can be written in any text editor, and must be saved with a .css
extension.
• The external .css file should not contain any HTML tags.
Example "mystyle.css"
body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}
Multiple Style Sheets
• If some properties have been defined for the same selector (element) in
different style sheets, the value from the last read style sheet will be used.
• Assume that an external style sheet has the following style for the <h1>
element:
h1 {
color: navy;
}
• Then, assume that an internal style sheet also has the following style for
the <h1> element:
h1 {
color: orange;
}
Multiple Style Sheets
• If the internal style is defined after the link to the external style
sheet, the <h1> elements will be "orange":

<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
Multiple Style Sheets
• However, if the internal style is defined before the link to the
external style sheet, the <h1> elements will be "navy":

<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
Cascading Order

• All the styles in a page will "cascade" into a new "virtual" style
sheet by the following rules, where number one has the highest
priority:
1. Inline style (inside an HTML element)
2. External and internal style sheets (in the head section)
3. Browser default
• So, an inline style has the highest priority, and will override
external and internal styles and browser defaults.

You might also like