Introduction to CSS
(cascading style
sheet)
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
External stylesheets are stored in CSS files
Why we Use CSS?
define styles for your web pages, including the design, layout and
variations in display for different devices and screen sizes
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
A CSS rule consists of a selector and a declaration block.
The selector points to the HTML element you want to style.
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.
CSS Selector
A CSS selector selects the HTML element(s) you want to style.
CSS selectors are used to "find" (or select) the HTML elements you
want to style.
We can divide CSS selectors into five categories:
Simple selectors (select elements based on name, id, class)
Combinator selectors (select elements based on a specific
relationship between them)
Pseudo-class selectors (select elements based on a certain state)
Pseudo-elements selectors (select and style a part of an element)
Attribute selectors (select elements based on an attribute or attribute
value)
The CSS element Selector
The element selector selects HTML elements based on the element
name. p {
text-align: center;
color: red;
}
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.
.center {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be
affected by a class.
p.center {
text-align: center;
color: red;
}
The Universal Selector
The universal selector (*) selects all HTML elements on the page.
affect every HTML element on the page
* {
text-align: center;
color: blue;
}
The CSS Grouping Selector
selects all the HTML elements with the same style definitions
Better to group the selectors, to minimize the code.
Separate each selector with a comma.
Ungrouped Grouped
h1 { h1, h2, p {
text-align: center; text-
color: red; align: center;
} color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
Inline CSS
Add CSS directly to the tag within your HTML file. This method is generally
not recommended for large projects since it makes the code harder to
maintain.
Add CSS directly to the <nav style="background-color: #333; color: white;
padding: 10px;">
<!-- Navbar content here -->
</nav>
Internal CSS
Place the CSS code in the <style> tag within the <head> section of your HTML
file. This method is useful if you only need the CSS on a single page.
<style>
nav { background-color: #333; color: white; padding: 10px; }
nav a { color: white; margin: 0 10px; text-decoration: none; }
nav a:hover { color: #ddd; }
</style>
External CSS
Create an external CSS file (e.g., styles.css).
Link the CSS file in your HTML file within the <head> section.
nav {
background-color: #333;
color: white;
padding: 10px;
}
nav a {
color: white;
margin: 0 10px;
text-decoration: none;
}
nav a:hover {
color: #ddd;
}
CSS example for <p> tag
p{
color: red;
text-align: center;
}
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