Lesson 32 / الدرس 32

Linking CSS and JS to your page / ربط CSS و JS بصفحتك

A page becomes real once you connect it to a stylesheet with <link> and a script with <script> — two tags that pull in the files that make the page look good and do things.

تصبح الصفحة حقيقية حين تربطها بملف تنسيق عبر <link> وبملف سكربت عبر <script> — وسمان يجلبان الملفات التي تُجمّل الصفحة وتجعلها تفعل شيئًا.

Every tag you have written so far describes structure, and the browser supplies its own plain default look. Two files change that: a stylesheet decides how the page looks, and a script decides what it does. You connect both with a single tag each — <link> for the stylesheet, <script> for the script.

Linking a stylesheet with <link>

<link rel="stylesheet" href="styles.css"> goes inside <head>. rel says what kind of file this is related to — "stylesheet" here — and href says where to find it. Putting it in the head means the browser downloads and applies your styles before it shows the page, so the visitor never sees a plain, unstyled flash of it first. What actually goes inside that file — the real rules of CSS — is a separate course; here you only need to know how to point at it.

<head>
    <meta charset="UTF-8">
    <title>Amman Dates</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Amman Dates</h1>
    <p>We sell dates and honey from local farms.</p>
</body>
The stylesheet link sits with the rest of the head. This preview has no styles.css to load, so the page still renders in the browser's plain default look — the point here is only that the tag itself is written correctly.

Adding a script with <script>

<script src="app.js"></script> connects a JavaScript file. Unlike <link>, <script> always has a closing tag, even when — as here — nothing goes between the two. The browser reads your page from top to bottom, and a script placed in <head> runs before the body exists, before there is anything below it for the script to touch. That is why script tags traditionally sit right before the closing </body> tag: by the time the browser reaches them, everything above has already loaded.

<body>
    <h1>Amman Dates</h1>
    <p>We sell dates and honey from local farms.</p>

    <script src="app.js"></script>
</body>
The script tag is the very last thing before </body>. This preview has no real app.js to run, but notice the page above it renders exactly as if the script were not there at all — that is the whole point of the placement.
TagWhere it goesKey attributeWhat it connects
<link>Inside <head>hrefA stylesheet — loaded before the page is shown
<script>Just before </body>srcA script — runs after the page already exists

Link and script, side by side.

That is the last thing HTML itself teaches you about styling a page: how to point at a stylesheet. What actually goes inside styles.css — colors, spacing, layout, every rule that makes a page look designed instead of default — is exactly where the CSS course picks up.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Where should <link rel="stylesheet"> go?

2. Why do <script> tags commonly go just before </body> instead of in <head>?

3. Which attribute holds the file location on <script>?

Your task / مهمتك

Take the page below and connect it to two files: a stylesheet called styles.css, linked correctly in the head, and a script called app.js, connected correctly just before the page ends.

خذ الصفحة أدناه واربطها بملفين: ملف تنسيق باسم styles.css، مربوط بشكل صحيح داخل الرأس، وملف سكربت باسم app.js، مربوط بشكل صحيح قبل نهاية الصفحة مباشرة.

  • A <link rel="stylesheet" href="styles.css"> inside <head> وسم <link rel="stylesheet" href="styles.css"> داخل <head>
  • A <script src="app.js"></script> right before </body> وسم <script src="app.js"></script> قبل </body> مباشرة
  • Neither tag is placed in the other's spot لا يوجد وسم في مكان الآخر
How do you want to submit? / كيف تريد التسليم؟