Lesson 11 / الدرس 11

Responsive: prefixes instead of media queries / التجاوب: بادئات بدل استعلامات الوسائط

This is the thing frameworks do best. A media query becomes a prefix on a class, and a layout that changes at three screen sizes fits on one line.

وهذا أفضل ما تفعله الأطر. فاستعلام الوسائط يصير بادئة على صنف، والتخطيط الذي يتغير عند ثلاثة أحجام شاشة يتّسع في سطر واحد.

In the CSS course a responsive layout meant writing the rule, then writing a media query, then repeating the selector inside it. Both frameworks compress that into a prefix: md:flex means "flex, from the medium breakpoint up". Everything else about how media queries work is unchanged.

Width fromTailwindBootstrapTypically
(none)no prefixno infixPhones — the default
576px—smLarge phone
640pxsm:—Large phone
768pxmd:mdTablet
1024pxlg:lg (992px)Laptop
1280pxxl:xl (1200px)Desktop

The names line up; the exact pixel values do not. More importantly, both are min-width — the prefixed class applies at that width and above. There is no max-width prefix in normal use, and that is deliberate.

Mobile first, and why it is not just a slogan

TAILWIND
  <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">

BOOTSTRAP
  <div class="row">
    <div class="col-12 col-md-6 col-lg-3">

Read both left to right:  one column on a phone,
                          two from tablet up,
                          four from laptop up.
The unprefixed class is the phone. Every prefix is an override for a larger screen. That order is not a style preference — it falls out of both frameworks using min-width, and writing it the other way round means fighting the cascade. Run the demonstration and narrow the preview.

Showing and hiding

The prefix works on every utility, including display. hidden md:block shows something only on larger screens; block md:hidden does the opposite. Bootstrap spells the same thing d-none d-md-block. This is how a desktop navigation bar and a mobile menu button live in the same markup.

Two habits make responsive work go quickly. Design the phone layout first and only then add prefixes — starting wide and squeezing produces a page held together with overrides. And test at the awkward widths, not the convenient ones: 360px, and the moment just before each breakpoint, is where layouts actually break.

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

Preview / المعاينة

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

1. What does md:flex mean?

2. Why is mobile-first not just a preference?

3. Your breakpoints do nothing on a real phone. What is missing?

Your task / مهمتك

Build one page that changes at three sizes — a stacked column on a phone, two across on a tablet, and a sidebar layout on a laptop — in both frameworks. Include a navigation that is a row of links on wide screens and a single button on narrow ones. Then test at 360px and just below each breakpoint, and report anything that broke at a width you had not looked at.

ابنِ صفحة واحدة تتغير عند ثلاثة أحجام — عمود مكدّس على هاتف، واثنان عرضًا على لوح، وتخطيط بشريط جانبي على حاسوب محمول — في الإطارين. وأدرج تنقلًا يكون صف روابط على الشاشات العريضة وزرًا واحدًا على الضيقة. ثم اختبر عند 360 بكسل وقبل كل نقطة توقف مباشرة، وأبلغ عن كل ما انكسر عند عرض لم تنظر فيه.

  • Three distinct layouts, in both frameworks ثلاثة تخطيطات متمايزة في الإطارين
  • Written mobile-first, unprefixed class as the base مكتوبة بالهاتف أولًا، الصنف بلا بادئة أساسًا
  • A navigation that changes shape تنقّل يغيّر شكله
  • Tested at 360px and just below each breakpoint مختبَرة عند 360 بكسل وقبل كل نقطة توقف
How do you want to submit? / كيف تريد التسليم؟