D DevBrainBox

CSS

CSS Introduction

What is CSS ?

CSS (Cascading Style Sheets) is a stylesheet language used to control the presentation and layout of HTML elements on a webpage. It allows developers to separate content (HTML) from design (CSS), making it easier to maintain and update webpages.

Types of CSS

Inline CSS: Written directly inside an HTML element.

<p style="color:blue;">Inline CSS example</p>

Internal CSS: Written within <style> tags inside the HTML head section.

<style>
  p { color: green; }
</style>

External CSS: Written in a separate file, linked in HTML.

<link rel="stylesheet" href="styles.css">

CSS Cascade & Specificity

When multiple CSS rules apply to the same element, CSS decides which styles take priority using the cascade.

Order of precedence (highest to lowest):

  1. Inline styles (style attribute).
  2. Internal & external styles (in <style> or external files), determined by specificity.
  3. Browser default styles.

Specificity (strongest to weakest):

  • ID Selector (#id)
  • Class Selector (.class)

Advantages of CSS:

  • Simplifies website design and maintenance.
  • Increases site loading speed.
  • Provides consistent look across multiple webpages.
  • Makes responsive web design achievable.