Cascading Style Sheets
What is CSSβ
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML. CSS defines how elements should be rendered on screen, on paper, in speech, or on other media.
Core Conceptsβ
-
Selectors: Patterns used to select the elements to style. Examples include element selectors (p), class selectors (.className), ID selectors (#idName), and attribute selectors ([attribute]).
-
Properties and Values: Define the styles to apply to selected elements. Each property has a set of values, e.g., color: red;, font-size: 16px;.
-
Cascade and Inheritance: Determines which styles are applied when multiple rules match the same element. Cascade: Refers to the order of precedence based on specificity, source order, and importance. Inheritance: Certain properties can be inherited from parent elements to children, simplifying styling.
-
Box Model: Describes the rectangular boxes generated for elements in the document tree. Components: content, padding, border, and margin.
-
Layouts: Techniques to arrange elements on the page, such as Flexbox and Grid Layout. Provides powerful tools for creating complex and responsive designs.
Usage Examplesβ
- Inline CSS:
Applied directly to an HTML element using the style attribute.
<p style="color: blue; font-size: 14px;">Hello World</p>
- Internal CSS:
Defined within a <style>
tag in the HTML document's <head>
.
<head>
<style>
p {
color: blue;
font-size: 14px;
}
</style>
</head>
- External CSS:
Linked via a separate .css file, using the <link>
tag.
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>