Introduction to CSS

CSS (Cascading Style Sheets) is the language used to style HTML. While HTML defines structure, CSS defines appearance:

CSS works in three ways:

  1. Inline CSS: Directly on an element:
<p style="color:red;">Hello World</p>
  1. Internal CSS: Inside <style> tag in the <head>:
<style>
p { color: blue; }
</style>
  1. External CSS: Using a separate .css file linked with <link>:
<link rel="stylesheet" href="styles.css">

Theory Notes:

1. Selectors and Syntax

CSS uses selectors to target HTML elements.

1.1 Types of Selectors

p { color: red; }
.highlight { background-color: yellow; }
#main { font-size: 20px; }
* { margin: 0; padding: 0; }
h1, h2, h3 { font-family: Arial; }

1.2 Combinators

div p { color: blue; } /* all p inside div */
div > p { font-weight: bold; }
h1 + p { margin-top: 0; }
h1 ~ p { color: gray; }

2. Colors, Fonts, and Text

2.1 Colors

2.2 Fonts

2.3 Text Properties

3. Box Model

Every element is a rectangular box:

  1. Content → text or images
  2. Padding → space inside element
  3. Border → around padding
  4. Margin → space outside element
div {
  width: 200px;
  padding: 10px;
  border: 2px solid black;
  margin: 20px;
}

Notes:

4. Backgrounds and Borders

4.1 Background

4.2 Borders

5. Positioning and Layout

5.1 Display

5.2 Position

5.3 Float and Clear

5.4 Flexbox

5.5 Grid

6. Lists and Navigation Styling

7. Forms Styling

8. Pseudo-classes and Pseudo-elements

8.1 Pseudo-classes

8.2 Pseudo-elements

9. Transitions and Animations

9.1 Transitions

button {
  background-color: blue;
  transition: background-color 0.5s ease;
}
button:hover {
  background-color: red;
}

9.2 Animations

@keyframes example {
  0% { transform: translateX(0); }
  50% { transform: translateX(100px); }
  100% { transform: translateX(0); }
}

div {
  animation: example 3s infinite;
}

10. Media Queries and Responsive Design

@media (max-width: 768px) {
  body {
    background-color: lightgray;
  }
  nav {
    display: block;
  }
}

11. Advanced CSS Features