Lesson 4 / الدرس 4

Element selectors / محدِّدات العناصر

The simplest selector is a bare tag name: write p and the rule reaches every <p> on the page at once — one rule, as many elements as the page happens to have.

أبسط مُحدِّد هو اسم وسم مجرّد: اكتب p فتصل القاعدة إلى كل <p> في الصفحة دفعة واحدة — قاعدة واحدة، بعدد ما تملكه الصفحة من عناصر.

You met the selector already: the part of a rule that says which elements it applies to. This lesson is about the simplest kind there is — a bare tag name, called an element selector — and what it means for one rule to reach many elements at once.

p {
    color: navy;
}
Write the tag name with no punctuation around it. This rule reaches every <p> on the page, however many there are.

One rule, every matching element

It makes no difference whether the page has one <p> or thirty — the rule above reaches every one of them, because the selector names the tag, not a specific element. Compare that to inline style, which touches only the one element it is written on: an element selector is what lets you style a whole page's worth of paragraphs from a single rule.

<p style="color: navy;">First paragraph.</p>
<p style="color: navy;">Second paragraph.</p>
<p style="color: navy;">Third paragraph.</p>
The inline way of reaching the same result: the same style repeated on every single paragraph. The element selector on the previous example said the same thing once.

Any tag name works as a selector this way: h1, a, li, div — whatever element you want to reach. Each tag name starts its own separate rule; styling <h2> and styling <li> together needs two rules, not one.

h2 {
    color: navy;
}

li {
    color: #555;
}
Two separate rules, one per tag. Each reaches only the elements matching its own selector.

Getting the syntax right

SelectorSelects
pEvery <p> on the page
h1Every <h1> on the page
aEvery link
liEvery list item
imgEvery image

A handful of element selectors and what they reach.

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

Preview / المعاينة

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

1. Which selector selects every <li> element on the page?

2. A page has five <p> elements and you write p { color: navy; }. How many turn navy?

3. What is wrong with the selector <p> { color: navy; }?

Your task / مهمتك

Style the recipe page below using only element selectors: give the heading and the paragraph different colors, and make every list item share one consistent color.

نسّق صفحة الوصفة أدناه بمحدِّدات العناصر فقط: أعطِ العنوان والفقرة ألوانًا مختلفة، واجعل عناصر القائمة تشترك في لون واحد ثابت.

  • Uses a tag selector for at least two different tags يستعمل محدِّد عنصر لوسمين مختلفين على الأقل
  • Every element of a matched tag gets the style, not just one كل عنصر من الوسم المطابق يأخذ التنسيق، لا عنصر واحد فقط
  • The heading, the paragraph, and the list items are visibly different from each other العنوان والفقرة وعناصر القائمة تبدو مختلفة عن بعضها بوضوح
How do you want to submit? / كيف تريد التسليم؟