JavaScript's role in web developmant centers on managing page behavior. Maintaining clear boundaries between HTML structure, CSS styling, and JavaScript behavior creates maintainable applications. This principle ensures each technology handles its designated responsibility without overlap.
Dark Mode Implementation Examples
Consider implementing light and dark mode switching functionality to demonstrate proper separation principles.
Beginner Approach
Starting with basic HTML structure:
<header>
<button id="toggleButton">☀️</button>
<h1>My Blog Content</h1>
</header>
<main>
<div class="profile">
<img src="avatar.jpg">
</div>
<div class="content">
<p>This is blog content...</p>
</div>
</main>
Basic CSS styling:
body,
html {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
body {
padding: 10px;
box-sizing: border-box;
}
#toggleButton {
font-size: 2rem;
float: right;
border: none;
background: transparent;
}
Problematic JavaScript implementation:
const buttonElement = document.getElementById('toggleButton');
buttonElement.addEventListener('click', (event) => {
const pageBody = document.body;
if (event.target.innerHTML === '☀️') {
pageBody.style.backgroundColor = 'black';
pageBody.style.color = 'white';
event.target.innerHTML = '🌙';
} else {
pageBody.style.backgroundColor = 'white';
pageBody.style.color = 'black';
event.target.innerHTML = '☀️';
}
});
This approach violates separation of concerns by having JavaScript directly manipulate both styles and DOM structure. The code becomes difficult to understend without context and requires JavaScript modifications for simple style changes.
Improved Version
Better approach using CSS classes:
Updated HTML (removing inline content from button):
<header>
<button id="toggleButton"></button>
<h1>My Blog Content</h1>
</header>
<main>
<div class="profile">
<img src="avatar.jpg">
</div>
<div class="content">
<p>This is blog content...</p>
</div>
</main>
Enhanced CSS with theme classes:
body,
html {
width: 100%;
height: 100%;
max-width: 600px;
padding: 0;
margin: 0;
overflow: hidden;
}
body {
padding: 10px;
box-sizing: border-box;
transition: all 1s;
}
#toggleButton {
font-size: 2rem;
float: right;
border: none;
outline: none;
cursor: pointer;
background: inherit;
}
body.dark-theme {
background-color: black;
color: white;
transition: all 1s;
}
#toggleButton::after {
content: '☀️';
}
body.dark-theme #toggleButton::after {
content: '🌙';
}
Clean JavaScript handling only class toggling:
const toggleBtn = document.getElementById('toggleButton');
toggleBtn.addEventListener('click', () => {
const bodyElement = document.body;
if (!bodyElement.classList.contains('dark-theme')) {
bodyElement.classList.add('dark-theme');
} else {
bodyElement.classList.remove('dark-theme');
}
});
This solution separates concerns properly. JavaScript manages state through class names, CSS handles visual presentation including transitions, and HTML maintains structural integrity.
Master-Level Solution
The ultimate approach eliminates JavaScript entirely:
<input id="themeToggle" type="checkbox">
<div class="page-container">
<header>
<label id="themeButton" for="themeToggle"></label>
<h1>My Blog Content</h1>
</header>
<main>
<div class="profile">
<img src="avatar.jpg">
</div>
<div class="content">
<p>This is blog content...</p>
</div>
</main>
</div>
Pure CSS implemenattion:
body,
html {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
body {
box-sizing: border-box;
}
.page-container {
padding: 10px;
transition: background-color 1s, color 1s;
}
#themeToggle {
display: none;
}
#themeToggle:checked + .page-container {
background-color: black;
color: white;
transition: all 1s;
}
#themeButton {
font-size: 2rem;
float: right;
}
#themeButton::after {
content: '☀️';
}
#themeToggle:checked + .page-container #themeButton::after {
content: '🌙';
}
This solution leverages HTML's built-in form controls and CSS selectors. Clicking the label toggles the checkbox, which triggers CSS pseudo-class selectors to apply different styles. The checkbox remains visually hidden while maintaining functionality.
Best Practices
- Maintain distinct responsibilities: HTML for structure, CSS for styling, JavaScript for behavior
- Avoid direct style manipulation through JavaScript when possible
- Use CSS classes to represent application states with meaningful semantic names
- Seek zero-JavaScript solutions for purely presentational interactions
- Leverage native HTML features and CSS capabilities before implementing custom JavaScript