Real interfaces such as sliders, accordions, dropdowns and modals use all three languages together. They follow a simple pattern that this topic names.
The Three-Layer Pattern
HTML supplies the parts, with clear class names. CSS describes how each state looks: closed and open, hidden and shown. JavaScript changes the state, usually by adding or removing a class. When the pattern is followed, each language does the job it is best at.
Definition: Accordion. A set of headings where selecting one opens or closes its panel of content.
Common Components
An accordion shows one heading per panel, and a click on the heading opens or closes the panel. A dropdown shows a menu when its button is clicked. A modal shows a dialog over a dimmed page, and a slider changes a value or an image as the user moves a control or presses next and previous buttons. In each case, a listener toggles a class, and CSS reacts to the class.
Table 4.10: Components and the class they toggle
| Component | Class toggled | CSS effect |
|---|---|---|
| Accordion panel | open | display changes from none to block |
| Dropdown menu | show | Menu becomes visible |
| Modal | active | Dialog and backdrop appear |
| Slider image | current | Only one image is visible |
Keeping Components Reliable
A component must work with the keyboard as well as the mouse. Use real buttons instead of divs, so that Tab and Enter work. Keep scripts short, test in more than one browser and on a phone, and make sure that the page is still readable if the script fails.
Course Review
The course followed the page from the inside out. HTML gave the structure, CSS the design and layout, JavaScript the behaviour and validation, and AJAX with JSON the connection to a server. A finished web application uses all four: a semantic page, a responsive design, a script that validates and responds, and requests that fetch data without a reload.
Example 4.10: An accordion panel
<style>
.head { background: #1F3864; color: white;
padding: 8px; cursor: pointer; }
.panel { display: none; padding: 8px; border: 1px solid #1F3864; }
.panel.open { display: block; }
</style>
<div class="head" id="h">What is CSS?</div>
<div class="panel" id="p">CSS controls how a page looks.</div>
<script>
const h = document.getElementById("h");
const p = document.getElementById("p");
h.addEventListener("click", () => p.classList.toggle("open"));
p.classList.add("open");
</script>
Output in the browser:
The last line opens the panel at load so that it is visible here. A click on the heading toggles the open class and so shows or hides the panel.
Common mistakes
- Building a clickable component from a div, which the keyboard cannot reach.
- Writing the state in JavaScript styles instead of toggling a class.
- Forgetting a closed state in CSS, so that nothing is hidden at the start.
- Testing only with a mouse on a desktop screen.
Key takeaways
- Build components in three layers: HTML for parts, CSS for states and JavaScript to switch the state.
- Toggle a class, and let CSS decide how each state looks.
- Use real buttons and links so that the keyboard works.
- A complete web application combines HTML, CSS, JavaScript and AJAX with JSON.
Practice questions
- What does JavaScript usually change in a component?
- Why use a button instead of a div for a click?
- Name the four technologies combined in a modern web application.
Answers to the practice questions
- A class name, which CSS then uses to show the right state.
- A button works with the keyboard and screen readers as well as the mouse.
- HTML, CSS, JavaScript and AJAX with JSON.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit IV
