CSS • Chapter 2 • Foundations to Production
Adding CSS to HTML
Every topic includes a CSS/HTML coding example, ten focused teaching examples, code explanation, expected result, practice, and responsive/translated-layout checks.
2.1 External Stylesheets
External Stylesheets is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size. The goal is to connect the syntax to the browser's layout and cascade decisions rather than memorize a property name.
For External Stylesheets in Chapter 2, inspect the matched selector, computed value, containing block or formatting context, intrinsic size, and any min/max constraints. When translation is enabled, also inspect whether longer text or RTL direction changes the available inline space.
Start with one small example for External Stylesheets, resize the viewport, increase text size, translate the label, and add a competing rule. This shows whether the technique remains predictable under real content rather than only in a fixed desktop mockup. This explanation is specific to Chapter 2, topic 1.
Concept in plain language
External Stylesheets is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size.
10 teaching examples
Example 1: Smallest useful case
Build a minimal article for External Stylesheets. Keep only the declarations needed to see the behavior clearly, then inspect the computed styles. Chapter 2, topic 1, example 1.
Example 2: Change one value
Change one important CSS value in the profile panel example for External Stylesheets. Predict the visual difference before reloading the page. Chapter 2, topic 1, example 2.
Example 3: Narrow-screen test
Test External Stylesheets in the search form at 320px, 375px, and tablet width. Watch for min-content sizing, unwrapped text, and elements that refuse to shrink. Chapter 2, topic 1, example 3.
Example 4: Translated-text test
Translate the dashboard into a language with longer labels while using External Stylesheets. Confirm buttons, headings, and navigation wrap without widening the page. Chapter 2, topic 1, example 4.
Example 5: Arabic/Persian test
Switch the photo gallery to Arabic or Persian. Verify logical spacing, start/end alignment, sidebar direction, and LTR code islands. Chapter 2, topic 1, example 5.
Example 6: Accessibility test
Check External Stylesheets in the pricing table with keyboard focus, zoom, reduced motion, forced colors, and high text scaling where relevant. Chapter 2, topic 1, example 6.
Example 7: Cascade test
Add a competing rule to the lesson sidebar and determine why the declaration for External Stylesheets wins or loses using the browser's Styles and Computed panels. Chapter 2, topic 1, example 7.
Example 8: Content-stress test
Fill the contact form with very long words, URLs, translated text, images, and nested controls. Adjust External Stylesheets without hiding meaningful content. Chapter 2, topic 1, example 8.
Example 9: Refactoring test
Rewrite the button group so External Stylesheets uses reusable tokens, logical properties, or a component class while preserving the same behavior. Chapter 2, topic 1, example 9.
Example 10: Production review
Review External Stylesheets in the data table for browser support, fallback behavior, RTL, small screens, print, motion preferences, and maintainability. Chapter 2, topic 1, example 10.
CSS/HTML coding example
<!-- HTML -->
<div class="demo external-stylesheets">CSS topic: External Stylesheets</div>
<style>
.external-stylesheets {
padding: 1rem;
border: 1px solid #94a3b8;
}
</style>Step-by-step code explanation
- Identify the selector and declaration that demonstrate External Stylesheets.
- Read the HTML structure first so you know which box or relationship the CSS is styling.
- The selector targets the demonstration for External Stylesheets, and the declaration changes a visible part of its presentation.
- Resize the viewport and test long translated content. Confirm that flex/grid items use min-width:0 where needed and that only code or genuinely wide content scrolls locally.
- Switch to Arabic or Persian and verify logical start/end spacing, text alignment, sidebar direction, and LTR isolation for code.
Expected result: The browser visibly demonstrates External Stylesheets without requiring the page to exceed the viewport width. If the example contains wide code, only the code block scrolls horizontally. This expected result belongs to Chapter 2, topic 1.
Practice exercise
Build a fresh example for External Stylesheets from Chapter 2. Change one value, inspect the computed styles, test at 320px and tablet width, translate the visible text, switch the page to RTL, and explain why the final declaration or layout behavior occurs.
2.2 style Element
style Element is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size. The goal is to connect the syntax to the browser's layout and cascade decisions rather than memorize a property name.
For style Element in Chapter 2, inspect the matched selector, computed value, containing block or formatting context, intrinsic size, and any min/max constraints. When translation is enabled, also inspect whether longer text or RTL direction changes the available inline space.
Start with one small example for style Element, resize the viewport, increase text size, translate the label, and add a competing rule. This shows whether the technique remains predictable under real content rather than only in a fixed desktop mockup. This explanation is specific to Chapter 2, topic 2.
Concept in plain language
style Element is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size.
10 teaching examples
Example 1: Smallest useful case
Build a minimal dashboard for style Element. Keep only the declarations needed to see the behavior clearly, then inspect the computed styles. Chapter 2, topic 2, example 1.
Example 2: Change one value
Change one important CSS value in the photo gallery example for style Element. Predict the visual difference before reloading the page. Chapter 2, topic 2, example 2.
Example 3: Narrow-screen test
Test style Element in the pricing table at 320px, 375px, and tablet width. Watch for min-content sizing, unwrapped text, and elements that refuse to shrink. Chapter 2, topic 2, example 3.
Example 4: Translated-text test
Translate the lesson sidebar into a language with longer labels while using style Element. Confirm buttons, headings, and navigation wrap without widening the page. Chapter 2, topic 2, example 4.
Example 5: Arabic/Persian test
Switch the contact form to Arabic or Persian. Verify logical spacing, start/end alignment, sidebar direction, and LTR code islands. Chapter 2, topic 2, example 5.
Example 6: Accessibility test
Check style Element in the button group with keyboard focus, zoom, reduced motion, forced colors, and high text scaling where relevant. Chapter 2, topic 2, example 6.
Example 7: Cascade test
Add a competing rule to the data table and determine why the declaration for style Element wins or loses using the browser's Styles and Computed panels. Chapter 2, topic 2, example 7.
Example 8: Content-stress test
Fill the media player with very long words, URLs, translated text, images, and nested controls. Adjust style Element without hiding meaningful content. Chapter 2, topic 2, example 8.
Example 9: Refactoring test
Rewrite the notification so style Element uses reusable tokens, logical properties, or a component class while preserving the same behavior. Chapter 2, topic 2, example 9.
Example 10: Production review
Review style Element in the language selector for browser support, fallback behavior, RTL, small screens, print, motion preferences, and maintainability. Chapter 2, topic 2, example 10.
CSS/HTML coding example
<!-- HTML -->
<div class="demo style-element">CSS topic: style Element</div>
<style>
.style-element {
padding: 1rem;
border: 1px solid #94a3b8;
}
</style>Step-by-step code explanation
- Identify the selector and declaration that demonstrate style Element.
- Read the HTML structure first so you know which box or relationship the CSS is styling.
- The selector targets the demonstration for style Element, and the declaration changes a visible part of its presentation.
- Resize the viewport and test long translated content. Confirm that flex/grid items use min-width:0 where needed and that only code or genuinely wide content scrolls locally.
- Switch to Arabic or Persian and verify logical start/end spacing, text alignment, sidebar direction, and LTR isolation for code.
Expected result: The browser visibly demonstrates style Element without requiring the page to exceed the viewport width. If the example contains wide code, only the code block scrolls horizontally. This expected result belongs to Chapter 2, topic 2.
Practice exercise
Build a fresh example for style Element from Chapter 2. Change one value, inspect the computed styles, test at 320px and tablet width, translate the visible text, switch the page to RTL, and explain why the final declaration or layout behavior occurs.
2.3 Inline Style Attribute
Inline Style Attribute is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size. The goal is to connect the syntax to the browser's layout and cascade decisions rather than memorize a property name.
For Inline Style Attribute in Chapter 2, inspect the matched selector, computed value, containing block or formatting context, intrinsic size, and any min/max constraints. When translation is enabled, also inspect whether longer text or RTL direction changes the available inline space.
Start with one small example for Inline Style Attribute, resize the viewport, increase text size, translate the label, and add a competing rule. This shows whether the technique remains predictable under real content rather than only in a fixed desktop mockup. This explanation is specific to Chapter 2, topic 3.
Concept in plain language
Inline Style Attribute is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size.
10 teaching examples
Example 1: Smallest useful case
Build a minimal lesson sidebar for Inline Style Attribute. Keep only the declarations needed to see the behavior clearly, then inspect the computed styles. Chapter 2, topic 3, example 1.
Example 2: Change one value
Change one important CSS value in the contact form example for Inline Style Attribute. Predict the visual difference before reloading the page. Chapter 2, topic 3, example 2.
Example 3: Narrow-screen test
Test Inline Style Attribute in the button group at 320px, 375px, and tablet width. Watch for min-content sizing, unwrapped text, and elements that refuse to shrink. Chapter 2, topic 3, example 3.
Example 4: Translated-text test
Translate the data table into a language with longer labels while using Inline Style Attribute. Confirm buttons, headings, and navigation wrap without widening the page. Chapter 2, topic 3, example 4.
Example 5: Arabic/Persian test
Switch the media player to Arabic or Persian. Verify logical spacing, start/end alignment, sidebar direction, and LTR code islands. Chapter 2, topic 3, example 5.
Example 6: Accessibility test
Check Inline Style Attribute in the notification with keyboard focus, zoom, reduced motion, forced colors, and high text scaling where relevant. Chapter 2, topic 3, example 6.
Example 7: Cascade test
Add a competing rule to the language selector and determine why the declaration for Inline Style Attribute wins or loses using the browser's Styles and Computed panels. Chapter 2, topic 3, example 7.
Example 8: Content-stress test
Fill the course card with very long words, URLs, translated text, images, and nested controls. Adjust Inline Style Attribute without hiding meaningful content. Chapter 2, topic 3, example 8.
Example 9: Refactoring test
Rewrite the navigation bar so Inline Style Attribute uses reusable tokens, logical properties, or a component class while preserving the same behavior. Chapter 2, topic 3, example 9.
Example 10: Production review
Review Inline Style Attribute in the article for browser support, fallback behavior, RTL, small screens, print, motion preferences, and maintainability. Chapter 2, topic 3, example 10.
CSS/HTML coding example
<!-- HTML -->
<div class="demo inline-style-attribute">CSS topic: Inline Style Attribute</div>
<style>
.inline-style-attribute {
padding: 1rem;
border: 1px solid #94a3b8;
}
</style>Step-by-step code explanation
- Identify the selector and declaration that demonstrate Inline Style Attribute.
- Read the HTML structure first so you know which box or relationship the CSS is styling.
- The selector targets the demonstration for Inline Style Attribute, and the declaration changes a visible part of its presentation.
- Resize the viewport and test long translated content. Confirm that flex/grid items use min-width:0 where needed and that only code or genuinely wide content scrolls locally.
- Switch to Arabic or Persian and verify logical start/end spacing, text alignment, sidebar direction, and LTR isolation for code.
Expected result: The browser visibly demonstrates Inline Style Attribute without requiring the page to exceed the viewport width. If the example contains wide code, only the code block scrolls horizontally. This expected result belongs to Chapter 2, topic 3.
Practice exercise
Build a fresh example for Inline Style Attribute from Chapter 2. Change one value, inspect the computed styles, test at 320px and tablet width, translate the visible text, switch the page to RTL, and explain why the final declaration or layout behavior occurs.
2.4 Multiple Stylesheets
Multiple Stylesheets is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size. The goal is to connect the syntax to the browser's layout and cascade decisions rather than memorize a property name.
For Multiple Stylesheets in Chapter 2, inspect the matched selector, computed value, containing block or formatting context, intrinsic size, and any min/max constraints. When translation is enabled, also inspect whether longer text or RTL direction changes the available inline space.
Start with one small example for Multiple Stylesheets, resize the viewport, increase text size, translate the label, and add a competing rule. This shows whether the technique remains predictable under real content rather than only in a fixed desktop mockup. This explanation is specific to Chapter 2, topic 4.
Concept in plain language
Multiple Stylesheets is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size.
10 teaching examples
Example 1: Smallest useful case
Build a minimal data table for Multiple Stylesheets. Keep only the declarations needed to see the behavior clearly, then inspect the computed styles. Chapter 2, topic 4, example 1.
Example 2: Change one value
Change one important CSS value in the media player example for Multiple Stylesheets. Predict the visual difference before reloading the page. Chapter 2, topic 4, example 2.
Example 3: Narrow-screen test
Test Multiple Stylesheets in the notification at 320px, 375px, and tablet width. Watch for min-content sizing, unwrapped text, and elements that refuse to shrink. Chapter 2, topic 4, example 3.
Example 4: Translated-text test
Translate the language selector into a language with longer labels while using Multiple Stylesheets. Confirm buttons, headings, and navigation wrap without widening the page. Chapter 2, topic 4, example 4.
Example 5: Arabic/Persian test
Switch the course card to Arabic or Persian. Verify logical spacing, start/end alignment, sidebar direction, and LTR code islands. Chapter 2, topic 4, example 5.
Example 6: Accessibility test
Check Multiple Stylesheets in the navigation bar with keyboard focus, zoom, reduced motion, forced colors, and high text scaling where relevant. Chapter 2, topic 4, example 6.
Example 7: Cascade test
Add a competing rule to the article and determine why the declaration for Multiple Stylesheets wins or loses using the browser's Styles and Computed panels. Chapter 2, topic 4, example 7.
Example 8: Content-stress test
Fill the profile panel with very long words, URLs, translated text, images, and nested controls. Adjust Multiple Stylesheets without hiding meaningful content. Chapter 2, topic 4, example 8.
Example 9: Refactoring test
Rewrite the search form so Multiple Stylesheets uses reusable tokens, logical properties, or a component class while preserving the same behavior. Chapter 2, topic 4, example 9.
Example 10: Production review
Review Multiple Stylesheets in the dashboard for browser support, fallback behavior, RTL, small screens, print, motion preferences, and maintainability. Chapter 2, topic 4, example 10.
CSS/HTML coding example
<!-- HTML -->
<div class="demo multiple-stylesheets">CSS topic: Multiple Stylesheets</div>
<style>
.multiple-stylesheets {
padding: 1rem;
border: 1px solid #94a3b8;
}
</style>Step-by-step code explanation
- Identify the selector and declaration that demonstrate Multiple Stylesheets.
- Read the HTML structure first so you know which box or relationship the CSS is styling.
- The selector targets the demonstration for Multiple Stylesheets, and the declaration changes a visible part of its presentation.
- Resize the viewport and test long translated content. Confirm that flex/grid items use min-width:0 where needed and that only code or genuinely wide content scrolls locally.
- Switch to Arabic or Persian and verify logical start/end spacing, text alignment, sidebar direction, and LTR isolation for code.
Expected result: The browser visibly demonstrates Multiple Stylesheets without requiring the page to exceed the viewport width. If the example contains wide code, only the code block scrolls horizontally. This expected result belongs to Chapter 2, topic 4.
Practice exercise
Build a fresh example for Multiple Stylesheets from Chapter 2. Change one value, inspect the computed styles, test at 320px and tablet width, translate the visible text, switch the page to RTL, and explain why the final declaration or layout behavior occurs.
2.5 Choosing a CSS Loading Method
Choosing a CSS Loading Method is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size. The goal is to connect the syntax to the browser's layout and cascade decisions rather than memorize a property name.
For Choosing a CSS Loading Method in Chapter 2, inspect the matched selector, computed value, containing block or formatting context, intrinsic size, and any min/max constraints. When translation is enabled, also inspect whether longer text or RTL direction changes the available inline space.
Start with one small example for Choosing a CSS Loading Method, resize the viewport, increase text size, translate the label, and add a competing rule. This shows whether the technique remains predictable under real content rather than only in a fixed desktop mockup. This explanation is specific to Chapter 2, topic 5.
Concept in plain language
Choosing a CSS Loading Method is part of the CSS system covered in Chapter 2. The key is to understand what box, selector, cascade rule, layout algorithm, or user preference the declaration affects, then test the result at more than one viewport size.
10 teaching examples
Example 1: Smallest useful case
Build a minimal language selector for Choosing a CSS Loading Method. Keep only the declarations needed to see the behavior clearly, then inspect the computed styles. Chapter 2, topic 5, example 1.
Example 2: Change one value
Change one important CSS value in the course card example for Choosing a CSS Loading Method. Predict the visual difference before reloading the page. Chapter 2, topic 5, example 2.
Example 3: Narrow-screen test
Test Choosing a CSS Loading Method in the navigation bar at 320px, 375px, and tablet width. Watch for min-content sizing, unwrapped text, and elements that refuse to shrink. Chapter 2, topic 5, example 3.
Example 4: Translated-text test
Translate the article into a language with longer labels while using Choosing a CSS Loading Method. Confirm buttons, headings, and navigation wrap without widening the page. Chapter 2, topic 5, example 4.
Example 5: Arabic/Persian test
Switch the profile panel to Arabic or Persian. Verify logical spacing, start/end alignment, sidebar direction, and LTR code islands. Chapter 2, topic 5, example 5.
Example 6: Accessibility test
Check Choosing a CSS Loading Method in the search form with keyboard focus, zoom, reduced motion, forced colors, and high text scaling where relevant. Chapter 2, topic 5, example 6.
Example 7: Cascade test
Add a competing rule to the dashboard and determine why the declaration for Choosing a CSS Loading Method wins or loses using the browser's Styles and Computed panels. Chapter 2, topic 5, example 7.
Example 8: Content-stress test
Fill the photo gallery with very long words, URLs, translated text, images, and nested controls. Adjust Choosing a CSS Loading Method without hiding meaningful content. Chapter 2, topic 5, example 8.
Example 9: Refactoring test
Rewrite the pricing table so Choosing a CSS Loading Method uses reusable tokens, logical properties, or a component class while preserving the same behavior. Chapter 2, topic 5, example 9.
Example 10: Production review
Review Choosing a CSS Loading Method in the lesson sidebar for browser support, fallback behavior, RTL, small screens, print, motion preferences, and maintainability. Chapter 2, topic 5, example 10.
CSS/HTML coding example
<!-- HTML -->
<div class="demo choosing-a-css-loading-method">CSS topic: Choosing a CSS Loading Method</div>
<style>
.choosing-a-css-loading-method {
padding: 1rem;
border: 1px solid #94a3b8;
}
</style>Step-by-step code explanation
- Identify the selector and declaration that demonstrate Choosing a CSS Loading Method.
- Read the HTML structure first so you know which box or relationship the CSS is styling.
- The selector targets the demonstration for Choosing a CSS Loading Method, and the declaration changes a visible part of its presentation.
- Resize the viewport and test long translated content. Confirm that flex/grid items use min-width:0 where needed and that only code or genuinely wide content scrolls locally.
- Switch to Arabic or Persian and verify logical start/end spacing, text alignment, sidebar direction, and LTR isolation for code.
Expected result: The browser visibly demonstrates Choosing a CSS Loading Method without requiring the page to exceed the viewport width. If the example contains wide code, only the code block scrolls horizontally. This expected result belongs to Chapter 2, topic 5.
Practice exercise
Build a fresh example for Choosing a CSS Loading Method from Chapter 2. Change one value, inspect the computed styles, test at 320px and tablet width, translate the visible text, switch the page to RTL, and explain why the final declaration or layout behavior occurs.
Chapter 2 review — 10 questions and answers
1. What should you inspect first when External Stylesheets does not work as expected?
Answer: Inspect the matched rules, computed value, box size, layout context, and inherited values for External Stylesheets. Then reduce the example until the winning declaration is clear.
2. How should External Stylesheets be checked before publishing?
Answer: Test External Stylesheets on narrow and wide screens, with long translated text, at increased zoom, and in RTL where direction can affect layout. Add a fallback if browser support or user preferences require one.
3. What should you inspect first when style Element does not work as expected?
Answer: Inspect the matched rules, computed value, box size, layout context, and inherited values for style Element. Then reduce the example until the winning declaration is clear.
4. How should style Element be checked before publishing?
Answer: Test style Element on narrow and wide screens, with long translated text, at increased zoom, and in RTL where direction can affect layout. Add a fallback if browser support or user preferences require one.
5. What should you inspect first when Inline Style Attribute does not work as expected?
Answer: Inspect the matched rules, computed value, box size, layout context, and inherited values for Inline Style Attribute. Then reduce the example until the winning declaration is clear.
6. How should Inline Style Attribute be checked before publishing?
Answer: Test Inline Style Attribute on narrow and wide screens, with long translated text, at increased zoom, and in RTL where direction can affect layout. Add a fallback if browser support or user preferences require one.
7. What should you inspect first when Multiple Stylesheets does not work as expected?
Answer: Inspect the matched rules, computed value, box size, layout context, and inherited values for Multiple Stylesheets. Then reduce the example until the winning declaration is clear.
8. How should Multiple Stylesheets be checked before publishing?
Answer: Test Multiple Stylesheets on narrow and wide screens, with long translated text, at increased zoom, and in RTL where direction can affect layout. Add a fallback if browser support or user preferences require one.
9. What should you inspect first when Choosing a CSS Loading Method does not work as expected?
Answer: Inspect the matched rules, computed value, box size, layout context, and inherited values for Choosing a CSS Loading Method. Then reduce the example until the winning declaration is clear.
10. How should Choosing a CSS Loading Method be checked before publishing?
Answer: Test Choosing a CSS Loading Method on narrow and wide screens, with long translated text, at increased zoom, and in RTL where direction can affect layout. Add a fallback if browser support or user preferences require one.