CSS - Part 1
CSS - Part 1
1
● Introduce Trainer
2
Agenda
Week 1st Week
Day
Duration 4hrs
4
Introduction
Web browsers display HTML pages using basic rules that may
not look very attractive. To make web pages visually appealing,
we use Cascading Style Sheets (CSS).
4. HTML and CSS separate semantic (structure and function) content from
visual presentation, allowing greater flexibility in design.
5. Unlike fixed layouts in PDF files, HTML web pages are designed to be
adaptable to different screen sizes and devices.
Using the Style tag inside the <head> or <body> section of the document
<style type="text/css">
p { color: purple; font-size: larger } .
</style>
CSS in External files. These .css files get cached by the browser
1. Content
a. content is not just a property; it represents the actual content within an HTML element,
such as text or images.
b. In CSS, the `::before` and `::after` pseudo-elements are often used to insert content
before or after an element's actual content.
p::before {
content: "Before text: ";
}
2. Padding
a. padding is the space between the content of an element and its inner edge (border). It
creates internal spacing within the element.
b. It can be set individually for each side (top, right, bottom, left) or using shorthand
properties like `padding-top`, `padding-right`, `padding-bottom`, and `padding-left`.
div {
padding: 20px;
}
Content, Padding, Margin and Border
3. Margin
a. margin is the space between an element's outer edge and adjacent elements. It controls
the spacing between elements on a webpage.
b. Like padding, margins can also be set individually for each side or with shorthand
properties (`margin-top`, `margin-right`, `margin-bottom`, `margin-left`).
h1 {
margin: 10px 0;
}
4. Border
a. border` is the line or border around an element's content and padding. It can be used to
create visual boundaries for elements.
b. You can define the border's width, style, and color using properties like `border-width`,
`border-style`, and `border-color`.
img {
border: 2px solid #333;
}
By adjusting the `content`, `padding`, `margin`, and `border` of elements, a precise control over the
spacing, borders, and overall presentation of the web content can be achieved.
CSS Rules
Structure
● The basic syntax of a CSS rule is ,
selector { property: value }
Grouping Properties
● Multiple properties can be grouped together within a rule, separated by
semicolons.
p { color: purple; font-size: larger }
Inheritance
● A CSS rule for a parent element automatically applies to its children
elements.
● Example: Styles applied to <body> would affect all text on the page, inside or
outside <p> tags.
The <Style> Tag
The <style> tag lets us write CSS rules inside the HTML page we want to style. It
allows the browser to differentiate the CSS code from the HTML code. The <style>
tag goes in the head section of the document
<html>
<head>
<style>
h1 {color:red;}
p {color:blue;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS In External Files
Benefits of External CSS Files
● Enables sharing styling rules across multiple HTML documents.
● Browser caching improves loading times for subsequent visits.
File Structure
● Unlike HTML files, CSS files lack hierarchical tags like <head> or <body>.
● CSS files are simply lists of rules processed sequentially by the browser.
16
Placing CSS Rules in External File
● The CSS rules can be placed in the style.css file, yielding the same results for
webpage visitors.
● If the CSS file is not in the same directory as the HTML document, its relative or full
path should be specified in the href attribute of the <link> tag.
17
Q&A Session
Questions
18
Break – 15 Mins
19
Teacher Demonstration – 30 Mins
20
Group Discussion
1. Why is it not recommended to use a tag to identify a word in an
ordinary sentence?
2. What happens if you start a comment with /* in the middle of a CSS
file, but forget to close it with */?
3. Why is it not recommended to use the style attribute of the
tag if there are sibling paragraphs that should look the same?
4. How can you indicate that a stylesheet from a <style> or <link> tag
should be used only by speech synthesizers?
21
Learning Objectives
23