Lecture 3 - Cascading Style Sheets (CSS)
Lecture 3 - Cascading Style Sheets (CSS)
Eman Karim
Cascading Style Sheets (CSS) is used to format the layout of a webpage. With
CSS, you can control the color, font, and size of text, the spacing between
elements, how elements are positioned and laid out, what background images
or background colors are to be used, different displays for different devices and
screen sizes, and much more!
The word cascading means that a style applied to a parent element will also
apply to all children elements within the parent. So, if you set the color of the
body text to "blue", all headings, paragraphs, and other text elements within the
body will also get the same color.
CSS can be added to HTML documents in 3 ways:
• Inline - by using the style attribute inside HTML elements.
• Internal - by using a <style> element in the <head> section.
• External - by using a <link> element to link to an external CSS file.
The most common way to add CSS, is to keep the styles in external CSS files.
CSS Syntax
To use an external style sheet, add a link to it in the <head> section of each
HTML page.
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
The element selector selects HTML elements based on the element name.
In the following example, all <p> elements on the page will be center-aligned,
with a red text color.
2
Second Year - 1st Semester Web Programming Assist. Lect. Eman Karim
In the following example the CSS rule below will be applied to the HTML
element with id="id1".
In the following example all HTML elements with class="center" will be red
and center-aligned.
3
Second Year - 1st Semester Web Programming Assist. Lect. Eman Karim
The universal selector (*) selects all HTML elements on the page.
In the following example the CSS rule below will affect every HTML element on
the page.
4
Second Year - 1st Semester Web Programming Assist. Lect. Eman Karim
CSS Declaration
5
Second Year - 1st Semester Web Programming Assist. Lect. Eman Karim
The CSS margin property defines a margin (space) outside the border.
The CSS background-color property defines the background color.
The CSS background-image property specifies an image to use as the background.
Aqua
BurlyWood
CadetBlue
Chocolate
color
color: Blue; DarkKhaki
DarkSalmon
DarkSeaGreen
IndianRed
PaleGreen
PaleVioletRed
rgb(255, 0, 0)
rgb(0, 255, 0)
color: rgb(255, 0, 0); rgb(0, 0, 255)
rgb(0, 0, 0)
rgb(255, 255, 255)
font-family font-family: cambria; algerian
Forte
Cambria
Verdana
MV Boli
accent-color accent-color: red; Use it with checkbox and radio
font-size font-size: 250%; You can use any size
text-align text-align: right; center
right
left
text-transform text-transform: uppercase; uppercase
lowercase
capitalize
font- weight font-weight: bold; bold
6
Second Year - 1st Semester Web Programming Assist. Lect. Eman Karim
Note : In HTML, a color can be specified as an RGB value, using this formula:
Each parameter (red, green, and blue) defines the intensity of the color with a
value between 0 and 255.
This means that there are 256 x 256 x 256 = 16777216 possible colors!
7
Second Year - 1st Semester Web Programming Assist. Lect. Eman Karim
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest
value (255), and the other two (green and blue) are set to 0.
Another example, rgb(0, 255, 0) is displayed as green, because green is set to its
highest value (255), and the other two (red and blue) are set to 0.
To display black, set all color parameters to 0, like this: rgb(0, 0, 0).
To display white, set all color parameters to 255, like this: rgb(255, 255, 255).