Chapter 2 Intro to CSS Text

📚 Chapter 2(2)_Intro_to_CSS_Text.pptx

Brief overview

This document introduces Cascading Style Sheets (CSS) and its fundamental principles for web styling. It covers how to apply styles using different methods, CSS syntax, and essential properties for text, color, and backgrounds. Understanding these concepts is crucial for creating visually appealing and well-structured web pages.

Key points

  • Overview of CSS application methods: inline, internal, and external stylesheets.
  • Understanding CSS syntax: selectors, properties, and values.
  • Styling text with font properties and defining colors.
  • Applying background colors and images to elements.
  • Exploring the CSS cascade and element spacing with margins and padding.

1. Introduction to CSS and its application methods

1.1 What is CSS?

  • CSS stands for Cascading Style Sheets.
  • It controls how web page content is presented.
  • CSS manages visual aspects like colors, margins, positioning, and sizes.

📝 Example: A CSS rule might set all headings to be blue with a 14-pixel font size and a 10-pixel margin.

1.2 Methods for applying CSS

There are three primary ways to apply CSS to an HTML document.

  • Inline styles: Applied directly to individual HTML elements.
  • Internal stylesheets: Defined within a <style> tag in the HTML <head>.
  • External stylesheets: Stored in separate .css files, linked to the HTML.

🔑 Important: External stylesheets offer the best separation of content and presentation. They also enable site-wide styling changes with one edit.

1.2.1 Inline styles

  • CSS code is added via the style attribute on an HTML element.
  • Styles only affect the specific element where they are applied.
  • This method is generally discouraged for maintainability.

📝 Example: <p style="color: red;">This text will be red.</p>

1.2.2 Internal stylesheets

  • CSS rules are placed inside <style> tags within the HTML document's <head>.
  • Styles affect all matching elements on that specific page.
  • This method improves content-presentation separation over inline styles.

📝 Example:

<head>
  <style>
    h2 {
      color: red;
    }
  </style>
</head>

1.2.3 External stylesheets

  • CSS rules are written in a separate .css file.
  • The HTML document links to this file using a <link> tag in the <head>.
  • Multiple HTML pages can link to the same external stylesheet.

📝 Example:

<!-- In HTML file -->
<head>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>

<!-- In style.css file -->
h2 {
  color: red;
}

1.3 The "cascade" and style priority

  • Browsers apply CSS from inline, internal, and external sources.
  • Conflicts arise when different sources style the same element.
  • The cascade determines which style rule wins in a conflict.

🔑 Important: The order of priority is: Inline > Internal > External > Browser Default.

1.4 CSS syntax and selectors

  • A CSS rule consists of a selector and a declaration block.
  • The declaration block contains one or more `property: value pairs.
  • Each declaration must end with a semicolon.

📝 Example: p { color: blue; font-size: 16px; }

1.4.1 Types of selectors

Selectors target specific HTML elements to apply styles.

Selector TypeSyntaxDescription
Type SelectortagnameSelects all elements of a given HTML tag type.
Class Selector.classnameSelects elements with a specific class attribute.
ID Selector#idnameSelects a single element with a specific id attribute.
Pseudo-class:hover, :focusSelects elements based on their state.
Attribute Selector[attr='value']Selects elements with a specific attribute and value.

🔑 Important: ID selectors are for unique elements; class selectors can style multiple elements.

1.4.2 Selector combinations

  • Selectors can be combined to target elements more precisely.
  • A space between selectors targets a descendant element.
  • The > symbol targets a direct child element.

📝 Example: div p targets all <p> elements inside a <div>. div > p targets only <p> elements that are direct children of a <div>.

1.4.3 Universal selector

  • The asterisk * is the universal selector.
  • It targets every single element on the web page.
  • Use it sparingly, as it can impact performance.

📝 Example: * { margin: 0; } resets margins for all elements.

1.5 Common CSS properties

1.5.1 Text and font styling

PropertyDescriptionExample Values
colorSets the foreground color of text.red, #FF0000, rgb(255, 0, 0)
font-familySpecifies the typeface (font).Arial, "Times New Roman", Verdana
font-sizeSets the size of the font.16px, 1.2em, large
font-weightDefines the thickness of the font.normal, bold, 500
font-styleSets whether text is italicized.normal, italic
text-alignHorizontally aligns text within its container.left, center, right, justify
text-decorationAdds decorations like underlines or strikethroughs.underline, line-through, none

1.5.2 Color definitions

CSS offers several ways to define colors.

MethodDescriptionExample
Named ColorsUses predefined color names.blue, green, yellow
RGB ColorMixes red, green, and blue values (0-255).rgb(0, 0, 255) for blue
HexadecimalBase-16 codes using # followed by six characters.#0000FF for blue
HSL ColorHue, Saturation, Lightness values.hsl(240, 100%, 50%) for blue
RGBA/HSLA ColorAdds an opacity (transparency) value (0-1).rgba(0, 0, 255, 0.5) for semi-transparent blue

1.5.3 Background styling

PropertyDescriptionExample Values
background-colorSets the background color of an element.lightblue, #f0f0f0
background-imageApplies an image as the background.url("images/background.jpg")
background-repeatControls how a background image repeats.repeat (default), repeat-x, repeat-y, no-repeat
background-positionSets the starting position of a background image.top left, center, 30% 50%

1.5.4 Spacing: Margins and Padding

  • Margins create space outside an element, separating it from others.
  • Padding adds space inside an element, between its content and its border.

📝 Example: margin: 10px; adds space around the element. padding: 20px; adds space within the element's borders.

1.6 Useful container tags: <span> and <div>

  • <span> tags are inline containers. They hold text without starting a new line.
  • <div> tags are block containers. They start content on a new line.
  • These tags are essential for grouping content and applying styles with classes and IDs.

1.7 Comments in CSS

  • CSS comments help explain code or temporarily disable styles.
  • They begin with /* and end with */.

📝 Example:

/* This is a CSS comment. */
p {
  color: blue;
}
/* h1 { color: red; }  This line is commented out. */

🔑 Important: Mastering selectors and understanding the cascade are crucial for effective CSS styling.

2. CSS properties for text, color, and backgrounds (part 1)

2.1 Ways to include CSS

CSS code can be added to web pages in three main ways:

  • Inline styles: Applied directly to an HTML element using the style attribute. This formats only that specific element.
  • Internal stylesheets: CSS code is placed within a <style> tag in the <head> section of the HTML document. These styles apply to matching elements on the page.
  • External stylesheets: CSS code is written in a separate .css file and linked to the HTML document using a <link> tag in the <head>. This is the most recommended method for multi-page websites.

🔑 Important: External stylesheets offer the best separation of content and presentation, leading to easier site-wide updates and faster loading.

2.1.1 CSS cascade and specificity

When conflicting styles are applied to an element, the browser uses a priority system called the "cascade" to decide which style to display.

  • Inline styles have the highest priority.
  • Internal stylesheets have the second highest priority.
  • External stylesheets have the third highest priority.
  • Browser defaults apply if no other style is defined.

If multiple conflicting styles exist within the same stylesheet, the last one declared is applied.

🔑 Important: Understanding the cascade is crucial for debugging style conflicts.

2.2 CSS selectors

Selectors target specific HTML elements to apply styles.

Selector typeSyntaxDescription
Type selectorelementSelects all elements of a specific type (e.g., p, h1).
Class selector.classnameSelects elements with a specific class attribute.
ID selector#idnameSelects a unique element with a specific ID attribute.
Pseudo-class:hoverSelects an element based on its state (e.g., when hovered).
Attribute selector[attr='value']Selects elements with a specific attribute and value.
  • Contextual selectors: You can nest selectors to target elements within specific parents. For example, div p selects all <p> elements inside <div> elements.
  • Direct child selector: The > symbol selects only direct children. ul > li selects <li> elements that are direct children of <ul>.
  • Grouping selectors: Commas , can group multiple selectors to apply the same styles. div, p, h1 { color: green; } styles all <div>, <p>, and <h1> elements.

2.3 Text styling properties

Several CSS properties control the appearance of text.

PropertyDescriptionCommon Values
font-familySets the typeface for text.Arial, "Times New Roman", Verdana
font-sizeAdjusts the size of the text.large, 120%, 20px (pixels)
font-weightDefines the thickness of the text.normal, bold
font-styleSets the style of the text (e.g., italic).normal, italic
text-alignSpecifies the horizontal alignment of text within its container.left, right, center, justify
text-decorationAdds decorations like underlines or strikethroughs.underline, line-through, blink
colorSets the foreground color of the text.Named colors, Hex, RGB, HSL values
  • text-align: Controls how text is aligned horizontally. justify spreads text evenly across the line.
  • font-size units: Can use keywords (large), percentages (120%), or fixed units (px, pt, em).

2.4 Color properties

CSS allows for extensive control over colors for text, backgrounds, and borders.

2.4.1 Defining color values

Colors can be defined in several ways:

  • Named colors: Use predefined color names (e.g., red, blue, green).
  • RGB color: Mixes red, green, and blue values from 0 to 255. Syntax: rgb(R, G, B).
  • Hexadecimal color: Uses a six-digit (or three-digit shorthand) hexadecimal code. Syntax: #RRGGBB or #RGB.
  • HSL color: Defines color using Hue (0-360), Saturation (0%-100%), and Lightness (0%-100%). Syntax: hsl(H, S%, L%).

📝 Example: color: rgb(123, 20, 233); sets text to a specific shade of purple.

2.4.2 Opacity

Opacity controls how transparent a color is.

  • rgba(): Extends RGB to include an alpha channel for transparency (0.0 is fully transparent, 1.0 is fully opaque). Syntax: rgba(R, G, B, A).
  • hsla(): Extends HSL to include an alpha channel. Syntax: hsla(H, S%, L%, A).

2.5 Background properties

These properties control the background of an element.

PropertyDescription
background-colorSets a solid color for the background of an element.
background-imageDisplays an image as the background.
background-repeatControls if and how a background image repeats.
background-positionSets the initial position of a background image.
  • background-repeat values: repeat (default), repeat-x, repeat-y, no-repeat.
  • background-position: Can use keywords (e.g., top, center, bottom, left, right), percentages, or length values.

🔑 Important: The <span> tag is an inline element, while the <div> tag is a block element, useful for containing and styling content.

🔑 Important: For any given element, the browser applies styles based on specificity and the cascade, resolving conflicts to ensure a consistent display.

3. CSS properties for text, color, and backgrounds (part 2)

3.1 Text properties

font-family property:

  • Specifies the typeface for text elements.
  • Allows customization across pages.

font-size property:

  • Adjusts text size.
  • Uses units like px, pt, or em for responsive design.

font-weight property:

  • Defines text thickness.
  • Uses values like normal, bold, or numeric weights for emphasis.

text-align property:

  • Specifies horizontal alignment of text within its container.
  • Influences layout structure.
  • Common values: left, center, right, justify.

3.2 Color properties

CSS color properties define foreground and background colors.

PropertyDescription
colorStyles an element's foreground color.
background-colorStyles an element's background color.

3.2.1 Defining color values

  • Named Colors: Use predefined color names (e.g., red, blue).

  • RGB Color: Mixes red, green, and blue values.

    • Syntax: rgb(red, green, blue)
    • Each value ranges from 0 to 255.
    • Example: rgb(123, 20, 233)
  • Hexadecimal Color: Uses a six-digit format prefixed by #.

    • Combines red, green, and blue components.
    • Values are in base 16 (hexadecimal).
    • Example: #09AA34
    • Shorthand is possible for identical pairs: #A3B is same as #AA33BB.
  • HSL Color: Defines color using Hue, Saturation, and Lightness.

    • Hue: 0-360 degrees on a color wheel.
    • Saturation: 0% (gray) to 100% (full color).
    • Lightness: 0% (black) to 100% (white).
    • Syntax: hsl(hue, saturation, lightness)
    • Example: hsl(182, 20%, 50%)

3.2.2 Opacity

  • Opacity measures how transparent a color is.
  • Use rgba() for RGB colors with transparency.
  • Use hsla() for HSL colors with transparency.
  • The last value (alpha channel) is a number between 0 (fully transparent) and 1 (fully opaque).
  • Example: rgba(123, 88, 9, 0.5) makes the color 50% transparent.

📝 Example: h1 { color: rgba(255, 0, 0, 0.7); } makes <h1> text red and 70% opaque.

3.3 Background properties

  • background-color: Sets the background color of an element.

  • background-image: Allows an image to fill an element's area.

    • Syntax: background-image: url("path/to/image.jpg");
  • background-repeat: Controls how a background image repeats.

    • Values: repeat (default), repeat-x, repeat-y, no-repeat.
  • background-position: Sets the starting position of a background image.

    • Values can be keywords (top, left, center), percentages, or length units.
    • Two values define horizontal and vertical position.
    • Example: background-position: 370px 20px;

📝 Example: bodybackground−image:url("images/bg.jpg");background−repeat:repeat−x;background−position:centertop;body { background-image: url("images/bg.jpg"); background-repeat: repeat-x; background-position: center top; } This sets a background image that repeats only horizontally, centered at the top.

3.4 Useful container elements

  • <span>: An inline container for text. It holds text without affecting layout significantly.
  • <div>: A block-level container. It starts text on a new line and creates a distinct section.

These tags are powerful when used with classes and IDs for flexible page creation.

3.5 CSS best practices

  • Comments: Use /* comment */ to explain CSS code.

    • Example: /* Styling for headings */ h1 { color: blue; }
  • Code formatting: Place curly brackets and declarations on separate, indented lines for readability.

  • Selectors: Grouping selectors with a comma applies styles to multiple elements.

    • Example: h1, h2, p { color: green; } applies green to all <h1>, <h2>, and <p> elements.
  • Universal selector (*): Applies styles to every element on the page.

    • Example: * { margin: 0; padding: 0; } resets default margins and padding for all elements.

🔑 Important: External style sheets offer the best separation of content and presentation, promoting maintainability and faster loading times for multi-page websites.

4. Advanced CSS concepts: the cascade, margins, padding, and structural elements

4.1 Understanding the CSS cascade

The cascade resolves conflicting CSS styles. When multiple rules apply to an element, the browser follows a priority order.

This ensures consistent rendering even with overlapping styles.

  • Inline styles: Have the highest priority.
  • Internal style sheets: Have the second highest priority.
  • External style sheets: Have the third highest priority.
  • Browser defaults: Apply if no other styles are defined.

🔑 Important: The cascade determines which style wins when rules conflict, making consistent design predictable.

4.1.1 How browsers process CSS

Browsers process all CSS code, combining styles from inline, internal, and external sources. If conflicts arise, the cascade's priority system decides which style to apply.

A common mistake is defining conflicting styles in the same stylesheet; only the last defined rule applies.

4.2 Controlling spacing with margins and padding

Margins create space outside an element, separating it from other elements. Padding adds space inside an element, between the content and the border.

PropertyDescriptionLocation
MarginSpace outside the element's border.Exterior
PaddingSpace inside the element, between content and border.Interior
  • margin: 20px; adds 20 pixels of space around the element.
  • padding: 10px; adds 10 pixels of space inside the element.

4.3 Structural elements: <span> and <div>

These HTML tags are crucial for structuring and styling web content. They act as containers for other elements.

  • <span style="color: #ff9f29; font-weight: 700;"><span>: An inline element. It holds text without starting a new line.
  • <div>: A block element. It causes enclosed content to start on a new line.

📝 Example: Using <span> and <div> with classes and IDs provides great flexibility for page design.

4.4 Applying CSS rules

CSS rules consist of a selector and a declaration block. The selector targets HTML elements, and the declaration block defines styles.

  • Selector: Specifies which element(s) to style (e.g., p, .highlight, #header).
  • Declaration block: Contains property-value pairs enclosed in curly braces {}.
    • Property: The style attribute to change (e.g., color, font-size).
    • Value: The setting for the property (e.g., red, 16px).
    • Each declaration ends with a semicolon ;.

🔑 Important: Omitting the semicolon after a CSS declaration is a common beginner mistake.

4.4.1 Common CSS selectors

Selectors target specific HTML elements for styling.

Selector TypeSyntaxDescription
Type (Element)elementnameStyles all elements of that type (e.g., p).
Class.classnameStyles elements with a specific class (e.g., .intro).
ID#idnameStyles a single, unique element with an ID (e.g., #header).
Pseudo-class:pseudo-classStyles elements based on their state (e.g., :hover).
Attribute[attr='value']Styles elements with a specific attribute and value (e.g., input[type="text"]).

🔑 Important: Class selectors (.) style multiple elements, while ID selectors (#) target only one unique element.

4.4.2 Combining and contextual selectors

Selectors can be combined to target elements more precisely.

  • Descendant selector (space): Styles elements inside another element (e.g., div p targets <p> inside <div>).
  • Child selector (>): Styles direct children (e.g., ul > li targets <li> directly under <ul>).
  • Grouping selector (,): Applies the same styles to multiple selectors (e.g., h1, h2, p { color: green; }).

4.4.3 Universal selector

The universal selector * applies styles to every element on the page. Use it cautiously as it affects everything.

  • * { font-family: Arial; } sets all text to Arial.

4.4.4 CSS declaration example

p { color: blue; font-style: italic; text-align: center; }

This rule styles all <p> elements to have blue, italic text centered on the page.

🔑 Important: Proper indentation and line breaks in CSS code significantly improve readability.

🔑 Important: The span tag is for inline elements, while div is for block elements, affecting layout by starting new lines.

🔑 Important: The cascade's priority order (inline > internal > external > default) is critical for resolving style conflicts.

🔑 Important: Mastering margins and padding is essential for controlling element spacing and overall page layout.

© 2026 Maeve. All rights reserved. This content is protected by copyright.

See also