Cascading Style Sheet: Chapter Three

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Chapter Three

Cascading Style sheet


What is CSS?
• CSS stands for Cascading Style Sheets and is a simple styling language
which allows attaching style to HTML elements.
• CSS is a style sheet language used to describe the presentation
semantics (the look and formatting) of a document written in a
markup language
• CSS is a language that works with HTML documents to define the way
content is presented.
Con’t….
• CSS is designed primarily to enable the separation of document
content (written in HTML or a similar markup language) from
document presentation, including elements such as the layout, colors,
and fonts.
• This separation can improve content accessibility, provide more
flexibility and control in the specification of presentation
characteristics, enable multiple pages to share formatting, and reduce
complexity and repetition in the structural content
Advantages and Disadvantages
• Advantages:
• Site-wide consistency and page layout controls
• Easier site maintenance
• The style sheet information can be stored separate from your HTML
document, and shared among many HTML files. Change one style sheet file,
and the appearance of the entire site is updated.
• Disadvantages
• Browser compatibility must be the most common difficulty.

4
CSS Syntax
Con’t…
• A CSS rule consists of a selector and a declaration block.
CSS Syntax

• 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.
Con’t…
Example
• In this example all <p> elements will be center-aligned, with a red text color:
p{
  color: red;
  text-align: center;
}

Example Explained
• 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
Con’t….
<html>
<head>
<style>
p{
color: red;
text-align: center;
}
</style>
</head>
<body>
<p>Grade 11!</p>
<p>These paragraphs are styled with CSS.</p>
</body>
</html>
CSS Selectors
• A CSS selectors are used to "find" (or select) the HTML elements you
want to style.
• We can divide CSS selectors into five categories:
• Simple selectors (select elements based on name, id, class)
• Combinator selectors (select elements based on a specific relationship
between them)
• Pseudo-class selectors (select elements based on a certain state)
• Pseudo-elements selectors (select and style a part of an element)
• Attribute selectors (select elements based on an attribute or attribute value)
The CSS element Selector
• The element selector selects HTML elements based on the element
name.
Example
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
• The 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, so the 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;
}

Note: An id name cannot start with a number!


Con’t…
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
The CSS class Selector
• The 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
• In this example all HTML elements with class="center" will be red and center-
aligned: 
.center {
  text-align: center;
  color: red;
}
Note: A class name cannot start with a number!
Con’t…
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph.</p>
</body>
</html>
• You can also specify that only specific HTML elements should be affected by a class.
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
</body>
</html>
HTML elements can also refer to more than one class.
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
p.large {
font-size: 300%;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
<p class="center large">This paragraph will be red, center-aligned, and in a large font-size.</p>
</body>
</html>
The CSS Universal Selector
• The 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;
}
Con’t…
<html>
<head>
<style>
*{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>Every element on the page will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
The CSS Grouping Selector
• The grouping selector selects all the HTML elements with the same style
definitions.
• Look at the following CSS code (the h1, h2, and p elements have the same style
definitions):
Example
• In this example we have grouped the selectors from the code above: 
h1, h2, p {
  text-align: center;
  color: red;
}
• It will be better to group the selectors, to minimize the code.
• To group selectors, separate each selector with a comma.
How To Add CSS
• There are three ways of inserting a style sheet:
• External CSS
• Internal CSS
• Inline CSS
External CSS
• With an external style sheet, you can change the look of an entire
website by changing just one file!
• Each HTML page must include a reference to the external style sheet
file inside the <link> element, inside the head section.
• An 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.
Con’t…
Example
• External styles are defined within the <link> element, inside the <head> section of an HTML page:
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
"mystyle.css"
body {
  background-color: lightblue;
}

h1 {
  color: navy;
  margin-left: 20px;
}
Internal CSS
• An internal style sheet may be used if one single HTML page has a
unique style.
• The internal style is defined inside the <style> element, inside the
head section.
Con’t…
• Example
• Internal styles are defined within the <style> element, inside the <head> section of an HTML page:

<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>
Inline CSS
• An inline style may be used to apply a unique style for a single element.
• To use inline styles, add the style attribute to the relevant element. The style attribute
can contain any CSS property.
Example
• Inline styles are defined within the "style" attribute of the relevant element:
<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>
Cascading Order
• What style will be used when there is more than one style specified
for an HTML element?
• 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:
• Inline style (inside an HTML element)
• External and internal style sheets (in the head section)
• Browser default
• So, an inline style has the highest priority, and will override external
and internal styles and browser defaults.
CSS Backgrounds
• The CSS background properties are used to add background effects
for elements.
• In these section, you will learn about the following CSS background
properties:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
• background (shorthand property)
CSS background-color
• The background-color property specifies the background color of an element.
Example
Here, the<body>, <p>, and <h1>, elements will have different background colors: 
body {
  background-color: lightblue;
}
h1 {
  background-color: green;
}

p {
  background-color: yellow;
}
CSS Background Image
• The background-image property specifies an image to use as the background of an element.
• By default, the image is repeated so it covers the entire element.
Example
• Set the background image for a page: 
body {
  background-image: url(“sunset.jpg");
}
• The background image can also be set for specific elements, like the <p> element:
Example
p {
  background-image: url(" sunset.jpg ");
}
CSS background-repeat
• By default, the background-image property repeats an image both horizontally and vertically.
• To repeat an image horizontally, set background-repeat: repeat-x;
body {
  background-image: url("gradient_bg.png");
  background-repeat: repeat-x;
}
• To repeat an image vertically, set background-repeat: repeat-y;
body {
  background-image: url("gradient_bg.png");
  background-repeat: repeat-x;
}
• Showing the background image only once is also specified by background-repeat: no-repeat
body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
}
Con’t…
• The background-position property is used to specify the position of
the background image.
Example
body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
}
CSS background-attachment
• The background-attachment property specifies whether the background image should scroll or be fixed.
Example
• Specify that the background image should be fixed:
body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
  background-attachment: fixed;
}
Example
• Specify that the background image should scroll with the rest of the page:
body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
  background-attachment: scroll;
}
CSS background - Shorthand property
• To shorten the code, it is also possible to specify all the background properties in one single property.
This is called a shorthand property.
• Instead of writing:
body {
  background-color: #ffffff;
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
}
• Use the shorthand property to set the background properties in one declaration:
body {
  background: #ffffff url("img_tree.png") no-repeat right top;
}
• When using the shorthand property the order of the property values is:
1st background-color
2nd background-image
3rd background-repeat
4th background-attachment
5th background-position

You might also like