0% found this document useful (0 votes)
76 views

Cascading Stylesheet Tutorial SMP2 Material

CSS is a language used to style and lay out web pages. CSS allows you to control the color, layout, and fonts among other aspects of HTML elements. CSS declarations contain a property name and a value separated by a colon within curly braces. CSS rules can target HTML elements by id, class, tag name or other attributes. Styles can be defined internally, inline, or externally in a .css file. When multiple styles conflict, the ones listed later or applied more specifically take precedence due to cascading rules.

Uploaded by

Gopi Nath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Cascading Stylesheet Tutorial SMP2 Material

CSS is a language used to style and lay out web pages. CSS allows you to control the color, layout, and fonts among other aspects of HTML elements. CSS declarations contain a property name and a value separated by a colon within curly braces. CSS rules can target HTML elements by id, class, tag name or other attributes. Styles can be defined internally, inline, or externally in a .css file. When multiple styles conflict, the ones listed later or applied more specifically take precedence due to cascading rules.

Uploaded by

Gopi Nath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Sanjay Sharma

Cascading Stylesheet
CSS is the language we use to style an HTML document.
CSS describes how HTML elements should be displayed.

What is CSS?
• CSS stands for Cascading Style Sheets
• CSS describes how HTML elements are to be displayed on screen, paper,
or in other media
• CSS saves a lot of work. It can control the layout of multiple web pages
all at once

Why Use CSS?


CSS is used to define styles for your web pages, including the design, layout
and variations in display for different devices and screen sizes.

Universal School 1
Sanjay Sharma

CSS Solved a Big Problem


HTML was NEVER intended to contain tags for formatting a web page!

HTML was created to describe the content of a web page, like:

<h1>This is a heading</h1>

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

When tags like <font>, and color attributes were added to the HTML 3.2
specification, it started a nightmare for web developers. Development of large
websites, where fonts and color information were added to every single page,
became a long and expensive process.

To solve this problem, the World Wide Web Consortium (W3C) created CSS.

CSS removed the style formatting from the HTML page!

CSS Saves a Lot of Work!


The style definitions are normally saved in external .css files.

With an external stylesheet file, you can change the look of an entire website by
changing just one file!

CSS Syntax

The selector points to the HTML element you want to style.

Universal School 2
Sanjay Sharma

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
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

Universal School 3
Sanjay Sharma

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.

Universal School 4
Sanjay Sharma

Note: An id name cannot start with a number!

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;
}

Universal School 5
Sanjay Sharma

HTML elements can also refer to more than one class.

Example
In this example 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>

Universal School 6
Sanjay Sharma

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;
}

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):

Universal School 7
Sanjay Sharma

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

h2 {
text-align: center; h1, h2, p {text-align: center; color: red;}
color: red;
}

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.

Example
In this example we have grouped the selectors from the code above:

h1, h2, p {
text-align: center;
color: red;
}

Universal School 8
Sanjay Sharma

Exercise 1:
Change the color of all <p> elements to "red".

Exercise 2:
Change the color of the element with id="para1", to "red".

Exercise 3:
Change the color of all elements with the class "colortext", to "red".

----------------------------------------------------------------------------------

When a browser reads a style sheet, it will format the HTML


document according to the information in the style sheet.

Three Ways to Insert CSS


There are three ways of inserting a style sheet:

• External CSS
• Internal CSS
• Inline CSS

Universal School 9
Sanjay Sharma

Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.

The internal CSS is defined inside the <style> element, inside the head section.

Example
Internal CSS are defined within the <style> element, inside the <head> section
of an HTML page:

Universal School 10
Sanjay Sharma

Inline CSS
An inline CSS 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:

Universal School 11
Sanjay Sharma

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.

Example
External styles are defined within the <link> element, inside the <head>
section of an HTML page:

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.

Here is how the "mystyle.css" file looks:

"mystyle.css"
body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}

Universal School 12
Sanjay Sharma

Note: Do not add a space between the property value and the unit (such as margin-left: 20 px;). The
correct way is: 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;
}

Example
If the internal style is defined after the link to the external style sheet, the
<h1> elements will be "orange":

Universal School 13
Sanjay Sharma

Example
However, if the internal style is defined before the link to the external style
sheet, the <h1> elements will be "navy":

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:

1. Inline style (inside an HTML element)


2. External and internal style sheets (in the head section)
3. Browser default

Universal School 14
Sanjay Sharma

So, an inline style has the highest priority, and will override external and
internal styles and browser defaults.

Exercise:
1. Add an external style sheet with the URL: "mystyle.css".
2. Set "background-color: linen" for the page, using an internal style sheet.
3. Set "background-color: linen" for the page, using an inline style.

Universal School 15
Sanjay Sharma

CSS Comments
1. CSS comments are not displayed in the browser, but they can help document
your source code.
2. Comments are used to explain the code, and may help when you edit the source
code at a later date.
3. Comments are ignored by browsers.
4. A CSS comment is placed inside the <style> element, and starts with /* and ends
with */:

HTML and CSS Comments


In the HTML , you learned that you can add comments to your HTML source by
using the <!--...--> syntax.

In the following example, we use a combination of HTML and CSS comments:

Universal School 16
Sanjay Sharma

CSS Color Names


In CSS, a color can be specified by using a predefined color name:

Universal School 17
Sanjay Sharma

Universal School 18
Sanjay Sharma

CSS Background Color


You can set the background color for HTML elements:

Hello

Universal School 19
Sanjay Sharma

CSS Border Color


You can set the color of borders:

Hello World
Hello World
Hello World
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
----------------------------------------------------------------

CSS Backgrounds
The CSS background properties are used to add background effects for elements.

In this section you will learn about the following CSS background properties:

• background-color
• background-image
• background-repeat
• background-attachment
• background-position
• background (shorthand property)

Universal School 20
Sanjay Sharma

CSS background-color
The background-color property specifies the background color of an element.
Example
The background color of a page is set like this:
body {
background-color: lightblue;
}

You can set the background color for any HTML elements:

Example
Here, the <h1>, <p>, and <div> elements will have different background
colors:

h1 {
background-color: green;
}

div {
background-color: lightblue;
}

p {
background-color: yellow;
}

Opacity / Transparency
The opacity property specifies the opacity/transparency of an element. It can
take a value from 0.0 - 1.0. The lower value, the more transparent:

Example

Universal School 21
Sanjay Sharma

div {
background-color: green;
opacity: 0.3;
}

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("paper.gif");
}

The background image can also be set for specific elements, like the <p>
element:

Example
p {
background-image: url("paper.gif");
}
add background for the body in the below code.

Universal School 22
Sanjay Sharma

Universal School 23
Sanjay Sharma

CSS background-repeat
By default, the background-image property repeats an image both horizontally and
vertically.

Some images should be repeated only horizontally or vertically, or they will look
strange, like this:

Universal School 24
Sanjay Sharma

If the image above is repeated only horizontally (background-repeat: repeat-x;),


the background will look better:

Example
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}

Universal School 25
Sanjay Sharma

CSS background-repeat: no-repeat


Showing the background image only once is also specified by the background-
repeat property:

Example
Show the background image only once:

body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}

CSS background-position
The background-position property is used to specify the position of the
background image.

Example
Position the background image in the top-right corner:

body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}

Universal School 26
Sanjay Sharma

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;
}

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;
}

You can use the shorthand property background:

Example
Use the shorthand property to set the background properties in one declaration:

body {
background: #ffffff url("img_tree.png") no-repeat right top;
}

Universal School 27
Sanjay Sharma

When using the shorthand property the order of the property values is:

• background-color
• background-image
• background-repeat
• background-attachment
• background-position

It does not matter if one of the property values is missing, as long as the other
ones are in this order. Note that we do not use the background-attachment
property in the examples above, as it does not have a value.

Exercise:
1. Set the background color for the page to "linen" and the background color
for <h1> to "lightblue".
2. Set "paper.gif" as the background image of the page
3. Set "gradient_bg_vertical.png" as the background image of the div, and
repeat it vertically only.
4. Specify that the background image for div should be shown once, in the
top right corner.

CSS Border Style


The border-style property specifies what kind of border to display.
The following values are allowed:

• 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
• hidden - Defines a hidden border

The border-style property can have from one to four values (for the top border,
right border, bottom border, and the left border).

Universal School 28
Sanjay Sharma

Example
Demonstration of the different border styles:
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}

Result:

A dotted border.

A dashed border.

A solid border.

A double border.

A groove border. The effect depends on the border-color value.

A ridge border. The effect depends on the border-color value.

An inset border. The effect depends on the border-color value.

An outset border. The effect depends on the border-color value.

No border.
A hidden border.

A mixed border.

CSS Border Width


Universal School 29
Sanjay Sharma

The border-width property specifies the width of the four borders.

p.one {
border-style: solid;
border-width: 5px;
}

p.two {
border-style: solid;
border-width: medium;
}

p.three {
border-style: dotted;
border-width: 2px;
}

p.four {
border-style: dotted;
border-width: thick;
}

Result:

Universal School 30
Sanjay Sharma

The following image shows a web page and its output, all three CSS types are used in the
below example. Write the below code and find out what could be the external CSS.

Universal School 31
Sanjay Sharma

The

Specific Side Widths


The border-width property can have from one to four values (for the top border,
right border, bottom border, and the left border):

Example
p.one {
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides */
}

p.two {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides */
}

p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom
and 35px left */
}

Example
Demonstration of the different border colors:

p.one {
border-style: solid;
border-color: red;
}

p.two {
border-style: solid;
border-color: green;
}

p.three {
border-style: dotted;
border-color: blue;
}

Universal School 32
Sanjay Sharma

Result

Specific Side Colors


The border-color property can have from one to four values (for the top border,
right border, bottom border, and the left border).

Example
p.one {
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue
bottom and yellow left */
}

Universal School 33
Sanjay Sharma

CSS Margins
The CSS margin properties are used to create space around elements, outside of
any defined borders.

With CSS, you have full control over the margins. There are properties for
setting the margin for each side of an element (top, right, bottom, and left).

Margin - Individual Sides


CSS has properties for specifying the margin for each side of an element:

• margin-top
• margin-right
• margin-bottom
• margin-left

Universal School 34
Sanjay Sharma

Example
Use the margin shorthand property with three values:

p {
margin: 25px 50px 75px;
}

The auto Value


You can set the margin property to auto to horizontally center the element
within its container.

The element will then take up the specified width, and the remaining space will
be split equally between the left and right margins.

Example
Use margin: auto:

div {
width: 300px;
margin: auto;
border: 1px solid red;
}

Universal School 35
Sanjay Sharma

Universal School 36

You might also like