02 Basic Syntax
02 Basic Syntax
UNIVERSITY
www.Khurasan.edu.af- [email protected]
Include CSS in a webpage
CSS is used for styling the look and formatting of a document written in HTML. There are
three ways to add CSS in HTML
• Inline CSS: Styles added directly to the HTML element.
• Internal CSS: Styles defined at the head section of the document.
• External CSS: Styles defined in a separate file.
Include CSS in a webpage
1. Inline Style
Inline style is the approach of adding CSS rules directly to the HTML element using
the style attribute.
e.g.
<head>
<style> //to define CSS for this page
p{ //targets all <p> elements
color: red; //apply red colors
}
</style>
</head>
Include CSS in a webpage
3. External-CSS (Recommended Approach)
External CSS is an approach to applying CSS styles to HTML pages by defining the CSS in a
separate file.
The external CSS file should have a .css extension
Syntax :
selector {
property1: value;
property2: value;
...
}
How to write CSS ?
Syntax:
Selector
specifies the HTML element that we want to apply the styles
Property1 / Property2:
specifies the attribute of HTML elements that we want to change (color, background,
and so on)
Value
specifies the new value you want to assign to the property (color of the text to red,
background to gray, and so on)
Comments in CSS
Comments are notes written along with the code that is ignored by the browser.
The CSS comment starts with /* and ends with */
div {
/* Define text color for the div*/
color: blue;
/* This line is not part of the CSS code*/
}
Why comments:
• This makes it easier for other developers to understand the code and make changes if necessary.
• Comments are also useful for anyone who needs to maintain the code in the future.
• Comments help you debug the code.
Essential Concepts of CSS
CSS is the language for styling web pages, and to use it affectively, it has a following
essential concepts.
1) Inheritance
2) Rule Order
3) Style Rule Hierarchy
4) Specificity
5) Box Model
Essential Concepts of CSS
1) Inheritance
• In CSS, inheritance passes the styles directly from the parent element to its child
elements.
• The child elements normally take the same styles that have been assigned to the parent,
unless they are provided their own styles.
<section>
<h2>This is a heading.</h2>
<p>This is a paragraph.</p> section {
color: blue;
<p>This is a paragraph.</p>
}
<p>This is a paragraph.</p>
</section>
Essential Concepts of CSS
2) Rule Order
• Rule order refers to the sequence in which CSS rules are applied to the HTML elements.
• The order of CSS rules determine the priority for the styles. The later rules overrides the
earlier ones and ensures that the most recent styles are applied.
p { color: red; }
In above example, the id selector overrides the styles of class and element selector.
This is because the id selector has more priority than the class and element selectors
Essential Concepts of CSS
4) Specificity
• Specificity in CSS determines which style rules take precedence when multiple rules
target the same element.
• This helps the browser prioritize and apply the most relevant styles.
<div>
div p { color: blue; }
<p id="para">This is a paragraph.</p>
</div> p { color: orange; }
• In the above example, the element selector p sets the color of all p elements to
orange.
• However, the selector div p is more specific for selecting paragraph so the color of
paragraph is colored blue.
Essential Concepts of CSS
5) Box Model
The box model specifies that every element in HTML is represented as a rectangular
box. It helps to understand how elements are structured and interact with each other
on a webpage.
We will cover it later.
END
THANK
YOU !
KHURASAN
UNIVERSITY
www.Khurasan.edu.af- [email protected]