Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🎨 Introduction: Make It
Look Good with JavaScript
Changing text and content is great—but what really brings
your webpage to life is styling. Whether you want to highlight important
content, hide/show elements, or apply visual effects on the fly, JavaScript
allows you to manipulate CSS dynamically using:
Let’s dive in and turn your static designs into interactive,
styled experiences.
🛠️ 1. Styling Elements
Directly with .style
The simplest way to apply styles is through the style
property on any DOM element.
🔹 Syntax:
javascript
element.style.propertyName
= "value";
Note: Property names use camelCase (e.g.,
backgroundColor, not background-color).
🧾 Example:
html
<p
id="text">Hello World!</p>
javascript
const
text = document.getElementById('text');
text.style.color
= "red";
text.style.fontSize
= "20px";
text.style.fontWeight
= "bold";
🔍 Commonly Used Style
Properties:
JavaScript |
CSS Equivalent |
Use Case |
backgroundColor |
background-color |
Change background
color |
color |
color |
Text color |
fontSize |
font-size |
Text size |
margin, padding |
margin,
padding |
Spacing |
display |
display |
Show/hide elements |
visibility |
visibility |
Hide without
layout shift |
width, height |
width, height |
Sizing elements |
⚠️ Limitation:
🎯 2. Working with CSS
Classes Using .classList
Instead of setting each style individually, you can define
styles in your CSS and toggle them with JavaScript using classList.
🔹 HTML:
html
<div
id="card" class="box">Content</div>
🔹 CSS:
css
.highlight
{
border: 2px solid orange;
background-color: #fffbe6;
}
🔹 JavaScript:
javascript
const
card = document.getElementById('card');
card.classList.add('highlight');
📚 .classList Methods
Explained
Method |
Description |
Example |
.add() |
Adds a class if it’s
not already there |
element.classList.add("active") |
.remove() |
Removes a
class |
element.classList.remove("hidden") |
.toggle() |
Adds class if not
present, removes if it is |
element.classList.toggle("dark") |
.contains() |
Checks if
class exists |
element.classList.contains("open") |
🧪 Example: Toggle Dark
Mode
html
<button
id="themeBtn">Toggle Theme</button>
<body
id="body"></body>
css
.dark-mode
{
background-color: #121212;
color: white;
}
javascript
const
btn = document.getElementById('themeBtn');
const
body = document.getElementById('body');
btn.addEventListener('click',
() => {
body.classList.toggle('dark-mode');
});
📦 3. Combining Classes
and Inline Styles
You can use both .style and .classList together for flexible
styling:
javascript
element.classList.add('error');
element.style.border
= "1px solid red";
Use:
🧠 4. Conditional Styling
Based on Events
You can dynamically apply or remove classes/styles based on
user interaction:
🔹 Example: Highlight
Active Tab
html
<ul>
<li class="tab">Tab
1</li>
<li class="tab">Tab
2</li>
</ul>
javascript
const
tabs = document.querySelectorAll('.tab');
tabs.forEach(tab
=> {
tab.addEventListener('click', () => {
tabs.forEach(t =>
t.classList.remove('active'));
tab.classList.add('active');
});
});
👁️ 5. Hide/Show Elements
Dynamically
🔹 Using display:
javascript
element.style.display
= "none"; // hide
element.style.display
= "block"; // show
⚠️ Removes the element from
layout flow
🔹 Using visibility:
javascript
element.style.visibility
= "hidden"; // hides but retains space
element.style.visibility
= "visible";
🔹 Using Classes:
css
.hidden
{
display: none;
}
javascript
element.classList.add('hidden');
element.classList.remove('hidden');
🎨 6. Adding Styles to
Multiple Elements
You can loop through elements and apply styles or classes:
javascript
const
cards = document.querySelectorAll('.card');
cards.forEach(card
=> {
card.classList.add('highlight');
});
📐 Advanced Example:
Interactive Card Hover
🔹 HTML:
html
<div
class="card">Hover over me</div>
🔹 CSS:
css
.card
{
padding: 20px;
border: 1px solid #ccc;
transition: all 0.3s ease;
}
.card.hovered
{
transform: scale(1.05);
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}
🔹 JavaScript:
javascript
const
card = document.querySelector('.card');
card.addEventListener('mouseenter',
() => card.classList.add('hovered'));
card.addEventListener('mouseleave',
() => card.classList.remove('hovered'));
📊 Summary Table
Task |
Method |
Example |
Change color |
.style.color |
el.style.color =
"red"; |
Add class |
.classList.add() |
el.classList.add("active") |
Remove class |
.classList.remove() |
el.classList.remove("hidden") |
Toggle theme |
.classList.toggle() |
el.classList.toggle("dark-mode") |
Inline CSS |
.style.property |
el.style.marginTop =
"10px"; |
🧱 Best Practices
💡 Real-World Styling Use
Cases
Feature |
DOM Use |
Dark mode toggle |
.classList.toggle("dark") |
Toast/alert display |
el.style.display
= "block" |
Input validation |
Add/remove error class |
Dropdowns & menus |
Use hidden
class or toggle visibility |
Active tabs |
Loop and highlight
selected tab |
📍 Summary: What You’ve
Learned
Next up: Creating, Cloning, and Removing DOM Elements
— where we’ll dynamically build content from scratch.
A: The DOM (Document Object Model) is a
tree-like structure that represents your web page. JavaScript can access and
modify this structure to change elements, styles, content, and more—dynamically
and in real-time.
A: Not exactly. The DOM is a Web API provided by
the browser, but JavaScript interacts with it using built-in methods like
getElementById, querySelector, and addEventListener.
Use DOM methods like:
document.getElementById('id')
document.querySelector('.class')
document.querySelector('tag')
element.style.color
= "red";
element.style.backgroundColor
= "yellow";
Or better, toggle CSS classes:
element.classList.add('active');
element.classList.remove('hidden');
const
div = document.createElement('div');
div.textContent = "Hello!";
document.body.appendChild(div);
element.remove();
// Modern way
// OR
element.parentNode.removeChild(element);
// Older method
const
button = document.querySelector('button');
button.addEventListener('click',
function() {
alert('Button clicked!');
});
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)