Lect 03-SET 372 - Introduction To CSS
Lect 03-SET 372 - Introduction To CSS
03 Introduction to CSS
Introduction
CSS Syntax
CSS Selector
Summary
2-Mar-24
3
Introduction
2-Mar-24
4
Cascading Style Sheets (CSS)
2-Mar-24
5
Why Use CSS?
Layout and variations in display for different devices and screen sizes.
With an external stylesheet file, you can change the look of an entire website by
changing just one file
2-Mar-24
6
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.
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 (unless you specify something else)!
2-Mar-24
7
Why Use CSS?
For example, if you don’t like the default look of the <h1>, <h2>, and other
heading tags,
You can assign new styles to override the default settings for many properties.
2-Mar-24
8
CSS Syntax
2-Mar-24
9
CSS Syntax
Each declaration includes a CSS property name and a value, separated by a colon.
2-Mar-24
10
CSS Syntax: Example
p is a selector that points to the HTML element you want to style: <p>).
2-Mar-24
11
Importing a Stylesheet
2-Mar-24
12
Importing a Stylesheet
External CSS:
You can also include a stylesheet with the HTML <link> tag:
You can also use as many <link> elements as you like in your HTML.
The rel attribute specifies the relationship between the current document and the
linked document. Only used if the href attribute is present.
3-Mar-24
13
Importing a Stylesheet
External CSS:
An external style sheet is used to define the style <!DOCTYPE html>
<html>
for many HTML pages. <head>
<link rel="stylesheet" href="styles.css">
</head>
To use an external style sheet, add a link to it in <body>
<html>
<head>
<link rel='stylesheet' href='mystyles.css'>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html> HTML
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
mystyles.css
2-Mar-24
15
Importing a Stylesheet
Internal CSS:
<head>
<style>
body {
background-color: lightblue;
}
</style>
</head> CSS
2-Mar-24
Importing a Stylesheet: Example 2 16
<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> HTML
2-Mar-24
17
Importing a Stylesheet
Inline CSS:
2-Mar-24
Importing a Stylesheet: Example 3 18
<html>
<head>
</head>
<body>
<h1 style="color:blue;text-align:center;">This
is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html> HTML
2-Mar-24
19
CSS Selector
2-Mar-24
20
CSS Selector
2-Mar-24
21
Type Selector
Here, all <p> elements on the page will be center-aligned, with a red
text color:
p {
color: red;
text-align: center;
} CSS
2-Mar-24
22
Type Selector: Example 4
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>Every paragraph will be affected by the
style.</p>
<p>And me!</p>
</body>
</html>
HTML
2-Mar-24
23
Descendant Selector
Let you apply styles to elements that are contained within other
elements.
Example: sets all text within <b>...</b> tags to red, but only if those tags occur
within <p>...</p> tags.
p b {
color: red;
} CSS
2-Mar-24
24
Descendant Selector: Example 5
<html>
<head>
<style>
ol ol { list-style-type:lower-alpha; }
</style>
</head>
<body>
<ol>
<li>One</li> <li>Two</li>
<li>Three
<ol>
<li>One</li> <li>Two</li>
<li>Three</li>
</ol>
</li>
</ol>
</body>
</html>
2-Mar-24
HTML
<!DOCTYPE html>
<html>
25
<head>
<style>
ul.a { list-style-type: circle; }
ul.b { list-style-type: square; }
ol.c { list-style-type: upper-roman; }
ol.d { list-style-type: lower-alpha; }
</style>
</head>
<body>
<h2>The list-style-type Property</h2>
<p>Example of unordered lists:</p>
<ul class="a">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="b">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<p>Example of ordered lists:</p>
<ol class="c">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ol class="d">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li> The Output
</ol>
</body> 3-Mar-24
</html> HTML
26
id Selector
2-Mar-24
27
id Selector: Example 6
<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>
HTML
2-Mar-24
28
class Selector
.center {
color: red;
text-align: center;
} CSS
2-Mar-24
29
class Selector: Example 7
<html>
<head>
<style>
.center{
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center“ > Red and center heading</h1>
You can also specify that only specific HTML elements should be
affected by a class.
2-Mar-24
31
class Selector
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.
h1 { h1 {
color: navy; color: orange;
} CSS } CSS
2-Mar-24
HTML Color Values 32
Selects all the HTML elements with the same style definitions.
p { CSS
color: red;
text-align: center;
} CSS
2-Mar-24
34
Cascading Order
What style will be used when there is more than one style is used?
So, an inline style has the highest priority, and will override external
and internal styles and browser defaults.
2-Mar-24
35
Cascading Order: Example 8
<html>
<head>
<link rel="stylesheet" type="text/css"
href="mystyle.css">
<style>
body {background-color: linen;}
</style>
</head>
<body style="background-color: lavender">
<h1>Multiple Styles Will Cascade into One</h1>
Type attribute default value is "text/css", which indicates that the content is CSS. For now, the only
3-Mar-24
supported value is "text/css"
36
Fonts
font-family
font-style,
font-size,
font-weight.
2-Mar-24
37
font-family
2-Mar-24
38
font-style
2-Mar-24
39
font-size
The solution that works in all browsers, is to set a default font-size in percent for the
<body> element: i.e. body { font-size: 100%; } 3-Mar-24
40
font-size
To allow users to resize the text (in the browser menu), many developers use
em instead of pixels.
3-Mar-24
41
Responsive Font Size: viewport width
The text size can be set with a vw unit, which means the "viewport width".
That way the text size will follow the size of the browser window:
Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is
50cm wide, 1vw is 0.5cm.
Example
<h1 style="font-size:10vw">Hello World</h1>
2-Mar-24
44
text-align
We wants it, we needs it. Must have the precious. They stole it from us.
Sneaky little hobbitses. Wicked, tricksy, false!
output
2-Mar-24
45
text-align
2-Mar-24
46
CSS properties for colors
2-Mar-24
47
CSS properties for colors
p {
Color names: aqua, black, blue, color: red;
fuchsia, gray, green, lime, maroon, }
h2 {
navy, olive, purple, red, silver, teal, color: rgb(128, 0, 196);
}
white (white), yellow
h4 {
color: #FF8800;
RGB codes: red, green, and blue } CSS
values from 0 (none) to 255 (full) This paragraph uses the first style above
Introduction
CSS Syntax
CSS Selector
Summary
2-Mar-24
Questions