Right-to-left scripts are a great way to stretch your frontend skills. In this tutorial we'll build a small, dependency-free Arabic alphabet practice tool: a grid of letters you can click to see the letter's name, its joined forms, and a quick "find the letter" quiz.
Along the way we'll cover:
- Mixing RTL and LTR content correctly with
dir - Building a responsive grid with CSS Grid
- Keeping the UI keyboard- and screen-reader-friendly
- Storing data in a plain JavaScript array and rendering from it
No frameworks, no build step — one HTML file.
1. The data
Each letter needs a character, a transliterated name, and its positional forms. Arabic letters change shape depending on whether they appear alone, at the start, in the middle or at the end of a word. We can let the browser's text shaping produce those forms for us by joining the letter with a tatweel (ـ, U+0640) on either side.
const letters = [
{ ch: 'ا', name: 'Alif', joinsLeft: false },
{ ch: 'ب', name: 'Ba', joinsLeft: true },
{ ch: 'ت', name: 'Ta', joinsLeft: true },
{ ch: 'ث', name: 'Tha', joinsLeft: true },
{ ch: 'ج', name: 'Jim', joinsLeft: true },
{ ch: 'ح', name: 'Ha', joinsLeft: true },
{ ch: 'خ', name: 'Kha', joinsLeft: true },
{ ch: 'د', name: 'Dal', joinsLeft: false },
{ ch: 'ذ', name: 'Dhal', joinsLeft: false },
{ ch: 'ر', name: 'Ra', joinsLeft: false },
{ ch: 'ز', name: 'Zay', joinsLeft: false },
{ ch: 'س', name: 'Sin', joinsLeft: true },
{ ch: 'ش', name: 'Shin', joinsLeft: true },
{ ch: 'ص', name: 'Sad', joinsLeft: true },
{ ch: 'ض', name: 'Dad', joinsLeft: true },
{ ch: 'ط', name: 'Ta (heavy)', joinsLeft: true },
{ ch: 'ظ', name: 'Dha (heavy)', joinsLeft: true },
{ ch: 'ع', name: 'Ayn', joinsLeft: true },
{ ch: 'غ', name: 'Ghayn',joinsLeft: true },
{ ch: 'ف', name: 'Fa', joinsLeft: true },
{ ch: 'ق', name: 'Qaf', joinsLeft: true },
{ ch: 'ك', name: 'Kaf', joinsLeft: true },
{ ch: 'ل', name: 'Lam', joinsLeft: true },
{ ch: 'م', name: 'Mim', joinsLeft: true },
{ ch: 'ن', name: 'Nun', joinsLeft: true },
{ ch: 'ه', name: 'Ha (light)', joinsLeft: true },
{ ch: 'و', name: 'Waw', joinsLeft: false },
{ ch: 'ي', name: 'Ya', joinsLeft: true },
];
The joinsLeft flag matters: six letters (ا د ذ ر ز و) never connect to the letter that follows them, so they have no distinct "initial" or "medial" form.
const TATWEEL = '\u0640';
function forms({ ch, joinsLeft }) {
return {
isolated: ch,
initial: joinsLeft ? ch + TATWEEL : ch,
medial: joinsLeft ? TATWEEL + ch + TATWEEL : TATWEEL + ch,
final: TATWEEL + ch,
};
}
2. The markup
The page is English (LTR), but the letter grid is Arabic. Set dir="rtl" and lang="ar" only on the Arabic parts. That gives correct ordering and lets screen readers switch pronunciation.
<main>
<h1>Arabic Alphabet Practice</h1>
<div id="grid" class="grid" dir="rtl" lang="ar" role="list"></div>
<section id="detail" aria-live="polite" hidden>
<p class="big" lang="ar" dir="rtl" id="detail-letter"></p>
<h2 id="detail-name"></h2>
<dl class="forms" dir="rtl" lang="ar" id="detail-forms"></dl>
</section>
<section class="quiz">
<h2>Find the letter</h2>
<p>Click the letter called <strong id="target"></strong>.</p>
<p id="feedback" aria-live="polite"></p>
</section>
</main>
aria-live="polite" means screen-reader users hear the update when the detail panel or quiz feedback changes, without it interrupting them.
3. The styles
CSS Grid with auto-fill gives us a responsive grid without a single media query. Because the grid has dir="rtl", it fills from the right automatically.
:root { --accent: #0f6b4c; --line: #e4e1d6; }
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
gap: 8px;
}
.grid button {
font: 2rem/1.2 "Amiri", "Scheherazade New", serif;
padding: .4rem 0;
border: 1px solid var(--line);
border-radius: 10px;
background: #fff;
cursor: pointer;
}
.grid button:hover,
.grid button:focus-visible {
border-color: var(--accent);
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.grid button.correct { background: #e3f2ea; }
.grid button.wrong { background: #fde8e8; }
.big { font: 5rem/1 "Amiri", serif; margin: 0; }
.forms { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.forms div { text-align: center; }
.forms dd { font: 2.2rem "Amiri", serif; margin: 0; }
.forms dt { font: .8rem system-ui; color: #666; direction: ltr; }
Using real <button> elements (instead of divs with click handlers) gives us keyboard focus, Enter/Space activation and the correct role for free.
4. Rendering and interaction
const grid = document.getElementById('grid');
letters.forEach((letter, i) => {
const btn = document.createElement('button');
btn.type = 'button';
btn.textContent = letter.ch;
btn.setAttribute('aria-label', letter.name);
btn.setAttribute('role', 'listitem');
btn.dataset.index = i;
grid.appendChild(btn);
});
grid.addEventListener('click', (e) => {
const btn = e.target.closest('button');
if (!btn) return;
const letter = letters[btn.dataset.index];
showDetail(letter);
checkQuiz(letter, btn);
});
function showDetail(letter) {
document.getElementById('detail').hidden = false;
document.getElementById('detail-letter').textContent = letter.ch;
document.getElementById('detail-name').textContent = letter.name;
const f = forms(letter);
const labels = { isolated: 'Isolated', initial: 'Initial', medial: 'Medial', final: 'Final' };
document.getElementById('detail-forms').innerHTML = Object.entries(f)
.map(([k, v]) => `<div><dd>${v}</dd><dt>${labels[k]}</dt></div>`)
.join('');
}
A single delegated listener on the grid is cheaper than 28 separate listeners and keeps working if you add letters later.
5. A tiny quiz
let target;
function newQuestion() {
target = letters[Math.floor(Math.random() * letters.length)];
document.getElementById('target').textContent = target.name;
}
function checkQuiz(letter, btn) {
const fb = document.getElementById('feedback');
const ok = letter === target;
btn.classList.add(ok ? 'correct' : 'wrong');
setTimeout(() => btn.classList.remove('correct', 'wrong'), 600);
fb.textContent = ok ? `Correct — that's ${letter.name}!` : `Not quite, that's ${letter.name}. Try again.`;
if (ok) setTimeout(newQuestion, 700);
}
newQuestion();
6. Accessibility checklist
- ✅
lang="ar"on Arabic content, so screen readers use Arabic pronunciation - ✅
dir="rtl"scoped to the Arabic regions only - ✅ Native buttons with visible
:focus-visiblestyles - ✅
aria-labelwith the letter's name on each button - ✅
aria-liveregions for detail and quiz feedback - ✅ Colour is never the only signal — the feedback text says correct/incorrect too
Where to take it next
- Add audio: record or source a pronunciation clip per letter and play it with
new Audio(src).play(). - Add vowel marks (fatha, kasra, damma) as a second practice mode.
- Save quiz streaks with
localStorage. - Group similar-looking letters (ب ت ث) to practise telling them apart.
This tool came out of building learning resources for Quran Classes 4 U, an online Quran academy. Building for RTL learners is a good reminder of how much the browser's text engine does for us when we let it.
Related: if you want to see a full responsive education homepage built in plain HTML/CSS, here's a CodePen demo of an online Quran learning homepage.
If you build your own version, share it in the comments!
Top comments (0)