Computer Science
Grade 8
20 min
Styling Web Pages
Styling Web Pages
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Select any HTML element using JavaScript selectors like `querySelector` and `getElementById`.
Modify the inline CSS of an element by changing its `.style` property.
Add, remove, and toggle CSS classes on an element using the `.classList` property.
Create an event listener that triggers a style change when a user clicks a button.
Use a loop to apply the same style change to a list of HTML elements.
Explain the difference between styling with the `.style` property and styling with `.classList`.
Ever wondered how a website switches to dark mode with a single click? 🌙 Let's learn how to give our web pages superpowers with JavaScript!
In this lesson, you'll discover how to use JavaScript to change the appearance of your web pages dynamically. We�...
2
Key Concepts & Vocabulary
TermDefinitionExample
DOM (Document Object Model)The DOM is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. Think of it as a tree made of all your HTML elements that JavaScript can climb and change.When you use `document.getElementById('header')`, you are using the DOM to find the element with the ID 'header' in your HTML document.
Element SelectionThe process of telling JavaScript which specific HTML element you want to work with. You need to select an element before you can change its style.`const title = document.querySelector('h1');` selects the first `<h1>` element on the page and stores it in a variable named `title`.
.style PropertyAn object attached to ever...
3
Core Syntax & Patterns
Changing a Single Style
element.style.property = 'value';
Use this to change one specific CSS property of an element. Remember to use camelCase for the property name (e.g., `backgroundColor`) and put the value in quotes.
Toggling a Class
element.classList.toggle('className');
Use this to add a class if it's not there, or remove it if it is. This is perfect for features like on/off switches or dark mode toggles.
The Event Listener Pattern
element.addEventListener('eventName', function() { // code to run });
This is the standard pattern for making your page interactive. First, select the element. Then, attach a listener for an event (like 'click'). Finally, write the code you want to run inside the function.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A page has a 'dark mode' button that toggles a `.dark-mode` class on the `<body>` (which sets a black background). It also has a 'highlight' button that sets `document.body.style.backgroundColor = 'yellow'`. If you click 'dark mode' ON, then click 'highlight', what will the background color be and why?
A.Black, because the `.dark-mode` class is still on the body.
B.Yellow, because inline styles (from the `.style` property) have a higher specificity and will override styles from a CSS class.
C.It will flash between black and yellow because of a conflict.
D.White, because the two styles will cancel each other out.
Challenging
A script is supposed to make all elements with the class `card` fade out by adding a `fade-out` class. The developer writes: `const cards = document.querySelector('.card'); cards.classList.add('fade-out');`. Why will this code fail to work as expected if there are multiple cards on the page?
A.The `.add()` method cannot be used on a variable named `cards`.
B.`querySelector` only selects the FIRST matching element. To select all, you must use `querySelectorAll` and a loop.
C.The class `fade-out` is not a valid CSS class name.
D.The `.classList` property does not exist on elements selected by `querySelector`.
Challenging
To change a title's color, font size, and border when a button is clicked, which approach is more maintainable and why?
A.Using multiple `.style` assignments, because it keeps all the logic inside the JavaScript file.
B.Using a single `classList.add('active-title')`, because it separates the styles (in a CSS file) from the behavior (in JavaScript), making it easier to update the styles later.
C.Both are equally maintainable and it is just a matter of personal preference.
D.Using multiple `.style` assignments, because it is faster for the browser to process.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free