Introduction to CSS notes
Introduction to CSS notes
Why CSS?
Efficiency:
Changes in style (like color or font) can be made in one place (a single
CSS file), and those changes automatically propagate across all linked
HTML documents.
This reduces time and effort, particularly for large websites.
Examples:
color: blue;
font-size: 24px;
margin: 10px;
Example CSS Rule:
h1 {
color: blue;
font-size: 24px;
}
This rule styles all <h1> elements with blue text and a font size of 24px.
Evolution of CSS
CSS1 (1996):
CSS2 (1998):
Introduced modularization:
The specification was divided into modules (e.g., Selectors, Box Model,
Backgrounds and Borders).
Added advanced features:
Animations and Transitions: Create interactive, smooth animations.
Responsive Design: Use media queries to adapt styles for various screen
sizes.
Why “Cascading”?
Overview
1. Inline Styles
2. Internal (Document-level) Styles
3. External Style Sheets
1. Inline Styles
Inline styles are CSS rules that are directly embedded within an
individual HTML element using the style attribute. They apply only to
the specific element where they are declared.
Characteristics
Syntax
html
Copy
<element style="property1: value1; property2: value2;">
Advantages
Disadvantages
Poor maintainability
Code repetition
Mixes content with presentation
Difficult to update multiple elements
Internal styles, also known as document-level styles, are CSS rules that
are embedded directly within an HTML document using the <style> tag
in the document's <head> section. They apply to all matching elements
within that specific HTML document.
Characteristics
Syntax
html
Copy
<style type="text/css"> selector { property1: value1; property2:
value2; }</style>
Advantages
External style sheets are CSS rules that are stored in a separate .css file,
which can then be linked to multiple HTML documents. This provides a
way to maintain consistent styles across an entire website.
Characteristics
Syntax
HTML:
html
Copy
<link rel="stylesheet" type="text/css" href="styles.css">
css
Copy
selector { property1: value1; property2: value2;}
Advantages
Maximum reusability
Easier maintenance
Reduced file size for HTML documents
Browser caching benefits
Clear separation of concerns
Why “Cascading”?
CSS uses a **cascading mechanism** to determine which style rule to
apply when multiple rules target the same element:
1. Inline styles have the highest priority.
2. Internal styles come next.
3. External styles are applied last, unless overridden by inline or internal
styles.
Additionally, **specificity** (how detailed the selector is) and the use of
`!important` can influence which rules are applied.
Summary
- Inline Styles: Quick fixes for individual elements.
- Internal Styles: Uniform styles for a single document.
- External Styles: Reusable, centralized styling for multiple documents.
Selector
Simple Selector
css
Copy
/* Targets all h1 headings */
h1
{ color: blue;
font-size: 24px;
}/* Targets multiple elements at once */
h2, h3, h4
{ font-family: Arial;
margin-bottom: 15px;
}
Class Selector
css
Copy
/* Basic class selector */
.highlight
{ background-color: yellow;
padding: 5px;}/* Element-specific class selector */p.important
{ color: red;
font-weight: bold;}/
* Multiple classes on one rule */
.large.bold
{
font-size: 20px;
font-weight: bold;}
HTML usage:
html
Copy
<p class="highlight">Highlighted text</p><p
class="important">Important text</p><p class="large bold">Large bold
text</p>
Generic Selector
A class selector that's not tied to any specific HTML element
Can be applied to any type of element
Promotes code reusability
Useful for creating utility classes
Helps maintain consistent styling across different elements
css
Copy
/* Can be applied to any element */
.center
{ text-align: center;
margin: 0 auto;}
/* Utility classes */
.mt-20
{ margin-top: 20px;
}
.text-large {
font-size: 1.5em;
}
HTML usage:
html
Copy
<div class="center">Centered div</div><p class="center">Centered
paragraph</p><h1 class="center mt-20">Centered heading with
margin</h1>
ID Selector
css
Copy
/* Main header styling *
#header
{ background: black;
color: white;
position: fixed;
top: 0;
}/* Specific element styling */
#main-logo
{
width: 150px;
height: auto;}
HTML usage:
html
Copy
<header id="header">
<img id="main-logo" src="logo.png"></header>
Contextual Selector
css
Copy
/* Targets paragraphs inside articles */
article p
{ line-height: 1.6;
margin-bottom: 15px;
}/* Multiple levels of nesting */
nav ul li a
{ text-decoration: none;
color: blue;}/* Child selector (>) for direct children only */
<div >
p { color: red;}
Pseudo-Class Selector
css
Copy
/* Link states */a:link
{ color: blue;
}
a:visited {
color: purple;
}a:hover
{
text-decoration: underline;
color: red;}
a:active
{ color: orange;
}/* Form element states */
input:focus
{ border-color: blue;
outline: none;}/* Structural pseudo-classes */
li:first-child
{
font-weight: bold;
}
Universal Selector
Copy
/* Reset all elements */*
{ margin: 0;
padding: 0;
box-sizing: border-box;
}/* Target all elements within a container */
.container *
{ color: blue;}/* Combine with other selectors */div *.highlight
{
background-color: yellow;
}
1. Fonts
Font properties control the style, size, and appearance of text on a
webpage.
Font-Families
- Specifies which fonts should be used for an element's text.
- A list of fonts is provided as a fallback mechanism. If the browser
cannot load the first font, it moves to the next one.
Example:
css
h2 {
font-family: "Arial", "Verdana", sans-serif;
}
Font Sizes
- Defines the height and size of the text.
- Can use **absolute** values (e.g., `medium`, `small`) or **relative**
values (e.g., `120%`, `1.5em`).
- **Relative Sizes** adapt to the surrounding elements, making them
more flexible for responsive design.
- Example:
css
p{
font-size: 14px; /* Absolute size */
}
p{
font-size: 1.2em; /* Relative size */
}
Font Variant
- Customizes how text appears. For example, it can display text in
**small caps**.
Example:
css
p{
font-variant: small-caps;
}
Font Style
- Controls whether the text is italicized or remains normal.
- Options include:
- `italic` – Slanted text.
- `oblique` – Slightly angled text.
- `normal` – Regular text.
- Example:
css
h3 {
font-style: italic;
}
Font Weight
- Adjusts the thickness or boldness of the text.
- Values range from `100` (thin) to `900` (boldest), or you can use
keywords like `bold`, `normal`, `lighter`, and `bolder`.
Example:
css
p{
font-weight: 700; /* Bold */
}
Font Shorthands
- Instead of specifying multiple font-related properties individually,
shorthand notation combines them.
- **Order**: Style, Weight, Size, Line Height, Family.
Example:
```css
p{
font: italic bold 16px/1.5 "Times New Roman", serif;
}
- This sets the font to italic and bold, 16px in size, with a line height of
1.5, and uses Times New Roman as the font family.
Text Decoration
- Adds decorative effects to text, such as:
- `underline` – Underlines text.
- `overline` – Adds a line above the text.
- `line-through` – Strikes through text.
- `none` – Removes decorations.
Example:
```css
a{
text-decoration: underline;
}
Text Spacing
- **Letter-Spacing**: Adjusts space between letters in a word.
- **Word-Spacing**: Adjusts space between words in a sentence.
Example:
css
p{
letter-spacing: 2px; /* Increases letter spacing */
word-spacing: 5px; /* Adds space between words */
}
2. Lists
CSS can style both ordered and unordered lists.
List-Style-Type
- Defines the type of marker used for list items:
- `disc` – A filled circle (default for unordered lists).
- `decimal` – Numbers for ordered lists.
- `square`, `circle`, etc.
Example:
css
ul
{
list-style-type: square;
}
List-Style-Position
- Defines where the marker (bullet/number) is positioned:
- `inside` – Aligned with the list text.
- `outside` – Positioned to the left of the text.
- Example:
css
ol {
list-style-position: inside;
}
3. Alignment of Text
CSS provides properties to align text horizontally or vertically.
Text-Align
- Aligns text within an element.
- `left` – Aligns text to the left.
- `right` – Aligns text to the right.
- `center` – Centers text.
- `justify` – Spreads text to fill the container.
Example:
css
p{
text-align: justify;
}
4. Margins
Margins create space around elements.
Margin
- Specifies space around an element's border.
- Can use **absolute units** (e.g., `px`, `cm`) or **relative units** (e.g.,
`%`).
- **Example**:
```css
div {
margin: 20px; /* 20px on all sides */
}
```
Margin Shorthand
- Combines top, right, bottom, and left margins in one declaration.
- **Order**: Top, Right, Bottom, Left.
- **Example**:
```css
div {
margin: 10px 15px 20px 5px;
}
5. Colors
CSS provides several ways to specify colors.
Color Formats
1. Color Names: `red`, `blue`, `green`, etc.
2. RGB: `rgb(255, 0, 0)` (Red).
3. Hexadecimal: `#FF0000` (Red).
4. HSL: `hsl(0, 100%, 50%)` (Red).( hue, saturation, and lightness)
5. RGBA: `rgba(255, 0, 0, 0.5)` (Transparent red).(red, green, blue, alpha)
Example:
css
h1 {
color: rgb(0, 128, 0); /* Green */
background-color: #FF00FF; /* Fuchsia */
}
Box Model
Example:
div {
width: 200px;
height: 100px;
}
This defines the size of the content area of the element, meaning the text
or image inside will be constrained to this width and height.
B. Padding
What it is: The space between the content and the element’s border.
Purpose: Padding provides space inside the element, pushing the content
away from the borders. This enhances readability and ensures that the
content does not touch the edges of the element.
padding-top
padding-right
padding-bottom
padding-left
Example:
div {
padding: 10px;
}
This adds 10px of inner space around the content, increasing the total size
of the box by pushing the border outward.
Practical Use Case: Imagine a button with text inside it:
Without padding: The text sticks to the edges of the button, making it
look cramped.
With padding: The text has breathing room, making the button look clean
and professional.
C. Border
What it is: The boundary that surrounds the padding. It can be visible or
invisible depending on the design.
Customization:
Example:
div {
border: 2px solid black;
}
This adds a 2px black solid line around the element, clearly defining its
boundaries.
Importance:
Borders are used to visually separate content or highlight specific areas of
a webpage. For instance, a border can be used to frame an image or
define the boundary of a section.
When combined with padding, borders contribute to a well-structured and
visually appealing layout.
D. Margin
What it is: The space outside the element’s border, creating distance
between the element and its neighboring elements.
Purpose: Margins help in spacing out elements, preventing them from
being too close to each other and improving the overall layout and
readability.
margin-top
margin-right
margin-bottom
margin-left
Example:
div {
margin: 20px;
}
This adds 20px of space around the element, separating it from other
elements and creating a more spacious layout.
Practical Use Case: Consider a set of paragraphs:
Without margins: The paragraphs appear cluttered and are hard to read.
With margins: Each paragraph is spaced out neatly, making the text easier
to read and visually appealing.
Background Properties
Background-Color
Background-Image
Background-Repeat
Background-Size
Defines the size of the background image. The value can be set as
specific dimensions (e.g., 100px 100px), percentage values, or keywords
like cover and contain.
Example: background-size: cover; ensures that the image covers the
entire background area without distortion, while maintaining its aspect
ratio.
Border Properties
Borders are used to outline an element, making it visually distinct and
allowing for separation between different parts of a page or sections.
Border-Width
Specifies the thickness of the border. The value can be in absolute units
such as pixels (e.g., px), or relative units like ems (em), rems (rem), etc.
Example: border-width: 2px; sets a border width of 2 pixels.
Border-Style
Defines the style of the border. It can take various values like solid (a
solid line), dotted (dotted line), dashed (dashed line), double (two lines),
and more.
Example: border-style: dashed; creates a dashed border around the
element.
Border-Color
Border Shorthand
Absolute Units
These units have a fixed size, regardless of any other factors such as font
size or viewport size. Common absolute units include:
px (pixels)
cm (centimeters)
mm (millimeters)
in (inches)
Example: width: 200px; sets the width of an element to exactly 200 pixels.
Relative Units
These units are based on other factors such as the font size or parent
element dimensions, making them flexible and more responsive to
changes in layout or screen size.
em: Relative to the font-size of the element. For example, 1em is equal to
the current font size of the element.
rem: Relative to the font-size of the root element (html).
%: Relative to the parent element's dimensions (for example, width,
height, margin, padding).
Example: font-size: 1.5em; means the font size is 1.5 times the parent
element’s font size.
Percentages