introduction to css
introduction to css
CSS (Cascading Style Sheets) is a style sheet language used to describe the presenta on of a document
wri en in HTML. It controls the layout, colors, fonts, spacing, and overall look of a webpage. CSS
separates the content from the visual design, allowing developers to keep HTML focused on structure
and CSS focused on design.
Selectors: CSS applies styles to HTML elements based on selectors. Selectors define which HTML
elements will be styled.
p{
color: blue;
Class Selector: Targets elements with a specific class a ribute. Class selectors are defined with a
period (.) before the class name.
.example-class {
color: green;
This applies green color to any element with the class example-class.
ID Selector: Targets a specific element with an id a ribute. ID selectors are defined with a hash (#).
#example-id {
color: red;
CSS Syntax: A CSS rule set consists of a selector and a declara on block. The declara on block contains
one or more declara ons inside curly braces {}. Each declara on includes a property and a value
separated by a colon (:), and each declara on ends with a semicolon (;).
Example:
h1 {
color: blue;
font-size: 24px;
}
This styles all <h1> elements to be blue and have a font size of 24 pixels.
Cascading: If mul ple rules apply to the same element, the browser decides which rule to apply
based on the source order, specificity, and importance.
Inheritance: Some proper es (like text color and font) are inherited by child elements, meaning if
you set a property on a parent element, its children will inherit that property unless otherwise
specified.
Types of CSS:
Inline CSS: Styles are applied directly within an HTML element using the style a ribute.
Internal CSS: Styles are placed within a <style> tag inside the <head> sec on of the HTML document.
<head>
<style>
p{
color: green;
</style>
</head>
External CSS: Styles are defined in an external CSS file and linked to the HTML document using a
<link> tag in the <head>.
<head>
</head>
body {
background-color: lightgray;
Borders:
Tables:
table, th, td {
border: 1px solid;
}
table {
width: 100%;
}
td {
text-align: center;
}
th, td {
padding: 15px;
text-align: le ;
}
th {
background-color: #04AA6D;
color: white;
}
Box Model: Every HTML element is treated as a rectangular box in CSS, and the box model consists
of:
Example:
div {
width: 200px;
padding: 20px;
margin: 10px;
Posi oning: CSS allows you to posi on elements on the page using different techniques:
Flexbox and Grid: Modern layout models for building complex designs.
.absolute-box {
top: 50px;
le : 100px;
Responsive Design: CSS enables the crea on of responsive web designs that adjust based on the
screen size. Media queries are o en used for this purpose.
body {
background-color: lightblue;
Pseudoclasses: Apply styles to elements in specific states, such as when a user hovers over a link.
a:hover {
color: red;
}
p::first-le er {
font-size: 2em;
color: blue;
body {
background-color: white;
h1 {
color: darkblue;
text-align: center;
/* Style paragraphs */
p{
color: gray;
font-size: 16px;
line-height: 1.5;
a:hover {
color: green;
h1 {
font-size: 18px;
p{
font-size: 14px;
Conclusion:
CSS is essen al for crea ng visually appealing and responsive websites. It offers a wide range of styling
op ons, from typography to layout, making websites flexible and easy to manage.