ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
UI ကို လှအောင် လုပ်ဖို့ CSS ရေးတတ်ရုံနဲ့ မလုံလောက်ပါဘူး။ Production မှာ ဖုန်းအတွက် 4000 pixel ပုံကြီးကို download လုပ်ခိုင်းလို့ page နှေးတာ၊ ပုံ ရောက်လာတဲ့အခါ စာတွေ အောက်ကို ခုန်ဆင်းသွားတာ၊ font load ပြီးမှ စာလုံးပုံစံ ရုတ်တရက် ပြောင်းသွားတာမျိုးတွေကိုလည်း ကာကွယ်ရပါတယ်။
Next.js က ဒီပြဿနာတွေကို framework အဆင့်မှာ ကူညီပေးပါတယ်။ CSS Modules က class နာမည် တိုက်မိတာကို ကာကွယ်၊ next/image က ပုံရဲ့ အရွယ်အစားကို ကြိုသိပြီး device နဲ့ ကိုက်တဲ့ size ကို ပို့၊ next/font က font ကို build time မှာ ကိုယ်ပိုင် server ကနေ serve လုပ်ပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ: size မပါတဲ့ ပုံနဲ့ တိုက်မိတဲ့ class နာမည်
Note card မှာ ရိုးရိုး <img> ကို width/height မပါဘဲ ထည့်ထားပါတယ်။ ပုံ download မပြီးခင် browser က နေရာ မသိလို့ height 0 နဲ့ ဆွဲပြီး ပုံ ရောက်လာတဲ့အခါ အောက်က စာတွေ ရုတ်တရက် ဆင်းသွားပါတယ် — ဒါကို layout shift (CLS) လို့ ခေါ်ပါတယ်။ ဖိုင်ကလည်း camera ကနေ တိုက်ရိုက်ယူထားတဲ့ 3 MB ပုံမို့ ဖုန်းမှာ ကြာပါတယ်။
တစ်ချိန်တည်းမှာ component နှစ်ခုက globals.css ထဲမှာ .cover class ကို ကိုယ်စီ ရေးထားလို့ နောက်က rule က ရှေ့က rule ကို override လုပ်ပြီး card တစ်ခုရဲ့ ပုံက ကြီးသွားပါတယ်။
// No size: the browser reserves no space, then the text jumps
export function NoteCard() {
return (
<article className="card">
<img src="/notebook-original.jpg" className="cover" />
<h2>What I learned today</h2>
</article>
);
}
/* globals.css: .cover is also defined by ProfileCard further down
the file, and the later rule wins for both components */ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Browser က ပုံရဲ့ aspect ratio ကို width နဲ့ height attribute ကနေပဲ ကြိုသိနိုင်ပါတယ်၊ မပါရင် နေရာ ချန်မထားနိုင်ပါဘူး။ Global CSS ရဲ့ class နာမည်တွေကလည်း page တစ်ခုလုံးမှာ တစ်ခုတည်းသော namespace ထဲ ရှိနေလို့ component နှစ်ခုက နာမည်တူ သုံးမိရင် တိုက်ပါတယ်။ next/image က size ကို မဖြစ်မနေ တောင်းပြီး CSS Modules က class တစ်ခုချင်းကို file အလိုက် unique နာမည် ပြောင်းပေးလို့ ပြဿနာ နှစ်ခုလုံး ပျောက်ပါတယ်။
Core concept: style နေရာ နှစ်မျိုး၊ ပုံနဲ့ font ကို framework က ကိုင်တွယ်ပုံ
globals.css ကို root layout ကနေ import လုပ်ပြီး reset၊ color variable၊ body font၊ heading size လို site တစ်ခုလုံးနဲ့ ဆိုင်တဲ့ style တွေကိုပဲ ထားပါ။ Component တစ်ခုနဲ့ပဲ ဆိုင်တဲ့ style ကိုတော့ note-card.module.css လို .module.css file မှာ ရေးပြီး import styles from "./note-card.module.css" နဲ့ ယူကာ className={styles.card} လို့ သုံးပါတယ်။ Build လုပ်တဲ့အခါ Next.js က .card ကို file အလိုက် unique ဖြစ်တဲ့ နာမည်အဖြစ် ပြောင်းလို့ တခြား component ရဲ့ .card နဲ့ ဘယ်တော့မှ မတိုက်ပါဘူး။ create-next-app ရဲ့ default မှာ Tailwind CSS လည်း setup လုပ်ပြီးသား ပါလာနိုင်ပြီး CSS Modules နဲ့ ရောသုံးလို့ ရပါတယ်။
next/image ရဲ့ <Image> က <img> ကို အစားထိုးပြီး အလုပ်သုံးခု လုပ်ပေးပါတယ်။ ပထမ — width နဲ့ height (ဒါမှမဟုတ် fill) ကနေ aspect ratio ကို သိလို့ browser က နေရာ ကြိုချန်ပြီး layout shift မဖြစ်ပါဘူး။ ဒုတိယ — screen size အလိုက် ပုံ အရွယ်အစား အမျိုးမျိုးနဲ့ srcset ထုတ်ပြီး browser က support လုပ်ရင် WebP လို ပိုသေးတဲ့ format နဲ့ ပို့ပါတယ်။ တတိယ — screen ပေါ် မရောက်သေးတဲ့ ပုံတွေကို lazy load လုပ်ပါတယ်။
Public folder ထဲက ပုံကို "/notebook.jpg" လို string နဲ့ ပေးရင် width နဲ့ height ကို ကိုယ်တိုင် ရေးရပါတယ်။ import cover from "./notebook.jpg" လို static import နဲ့ ပေးရင်တော့ Next.js က size ကို build time မှာ ဖတ်ပေးပြီး placeholder="blur" ကိုလည်း သုံးနိုင်ပါတယ်။ Parent box ကို အပြည့် ဖြည့်စေချင်ရင် fill ကို သုံးပြီး parent မှာ position: relative နဲ့ sizes prop ကို ထည့်ရပါတယ်။ အခြား server ကနေ လာတဲ့ ပုံတွေအတွက်တော့ next.config.ts ရဲ့ images.remotePatterns မှာ ခွင့်ပြုထားရပါမယ်။
next/font က Google Fonts ဒါမှမဟုတ် local font file ကို build time မှာ download လုပ်ပြီး ကိုယ့် site ကနေပဲ serve လုပ်ပါတယ်၊ browser က Google ဆီ request မပို့ရပါဘူး။ Fallback font ရဲ့ size ကိုလည်း ညှိပေးလို့ font ပြောင်းသွားတဲ့အခါ စာကြောင်းတွေ မခုန်ပါဘူး။ Variable font မဟုတ်တဲ့ font တွေမှာ weight ကို ရေးပေးရပြီး ဘယ် subset (latin၊ myanmar) လိုလဲ ရွေးရပါတယ်။
- Site တစ်ခုလုံးသုံး style -> globals.css
- Component style -> .module.css
- ပုံ -> <Image> နဲ့ size
- Font -> layout ထဲ next/font
Style တစ်ခုကို component တစ်ခုတည်းက သုံးရင် module ထဲ ထားပါ — global ထဲ ရောက်သွားတိုင်း တိုက်နိုင်ခြေ တိုးပါတယ်။
အထွေထွေ ပုံစံ
import Image from "next/image";
import styles from "./component-name.module.css";
<div className={styles.className}>
<Image src="/file.jpg" alt="what the image shows" width={W} height={H} />
<Image src={importedImage} alt="..." /> {/* size read at build */}
<div style={{ position: "relative" }}>
<Image src="/file.jpg" alt="..." fill sizes="100vw" />
</div>
</div>CSS Module ကို object အဖြစ် import လုပ်ပြီး styles.className နဲ့ သုံးပါတယ်။ <Image> မှာ alt အမြဲ လိုပြီး size ကို ပေးနည်း သုံးမျိုး ရှိပါတယ် — width/height ကိုယ်တိုင်ရေး၊ static import လုပ်၊ ဒါမှမဟုတ် position ပါတဲ့ parent ထဲမှာ fill သုံးပါ။
Example: ပုံပါတဲ့ NoteCard
ဒီ component က public/notebook.jpg ပုံကို <Image> နဲ့ ပြပြီး style တွေကို note-card.module.css ကနေ ယူပါတယ်။ CSS file ရဲ့ ပုံစံကို example အောက်က section မှာ ပြထားပါတယ်။
import Image from "next/image";
import styles from "./note-card.module.css";
export function NoteCard() {
return (
<article className={styles.card}>
<Image
src="/notebook.jpg"
width={640}
height={360}
alt="စားပွဲပေါ်ရှိ မှတ်စုစာအုပ်"
className={styles.cover}
/>
<h2>ဒီနေ့လေ့လာခဲ့တာ</h2>
</article>
);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Imports
import styles from "./note-card.module.css";CSS Module ကို import လုပ်ရင် class နာမည်တွေ ပါတဲ့ object ရပါတယ်။ styles.card ရဲ့ တကယ့် တန်ဖိုးက build ကျရင် unique ဖြစ်တဲ့ class string တစ်ခုပါ။
- src
src="/notebook.jpg"public/notebook.jpg ကို /notebook.jpg နဲ့ ညွှန်းပါတယ်။ String src ဖြစ်လို့ Next.js က size ကို မသိနိုင်ပါဘူး။
- Size
width={640}width={640} နဲ့ height={360} က 16:9 aspect ratio ကို ပြောပြီး browser က ပုံ မရောက်ခင် နေရာ ချန်ထားပါတယ်။ ဒါက ပြသမယ့် pixel အတိအကျ မဟုတ်ပါဘူး — CSS နဲ့ responsive လုပ်လို့ ရပါတယ်။
- alt
alt="A notebook on a desk"ပုံကို မမြင်ရတဲ့ user အတွက် ပုံက ဘာကို ပြလဲ ဖော်ပြပါတယ်။ "image" ဒါမှမဟုတ် file နာမည် မထည့်ပါနဲ့၊ အလှဆင်ပုံသက်သက်ဆိုရင် alt="" ပေးပါ။
- Scoped class
className={styles.cover}<Image> ကလည်း className ကို လက်ခံပါတယ်။ styles.cover က ဒီ module ရဲ့ cover ပဲ ဖြစ်လို့ တခြား component ရဲ့ .cover နဲ့ မတိုက်ပါဘူး။
အရွယ်အစားတည်ငြိမ်ပြီး optimized image ပါသော note card ပေါ်လာမည်။CSS Module file နဲ့ responsive ပုံ
width/height attribute ရှိနေရင်တောင် CSS မှာ width: 100% နဲ့ height: auto ပေးရင် ပုံက card အကျယ်အတိုင်း ကျုံ့ချဲ့ပြီး aspect ratio ကို ထိန်းထားပါတယ်။
.card {
border: 1px solid #e5e7eb;
border-radius: 12px;
overflow: hidden;
}
.cover {
width: 100%;
height: auto;
}next/font နဲ့ Latin + Myanmar font
Myanmar Notes မှာ English စာနဲ့ Myanmar စာ နှစ်မျိုးလုံး ပါလို့ font နှစ်ခု လိုပါတယ်။ Font တစ်ခုစီကို CSS variable အဖြစ် ထုတ်ပြီး globals.css ရဲ့ font-family stack ထဲမှာ အစဉ်လိုက် ထည့်ပါတယ်။ Noto Sans Myanmar က variable font မဟုတ်လို့ weight ကို ရေးရပါတယ်။
import { Inter, Noto_Sans_Myanmar } from "next/font/google";
import "./globals.css";
const latin = Inter({ subsets: ["latin"], variable: "--font-latin" });
const myanmar = Noto_Sans_Myanmar({
subsets: ["myanmar"],
weight: ["400", "700"],
variable: "--font-myanmar",
});
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="my" className={`${latin.variable} ${myanmar.variable}`}>
<body>{children}</body>
</html>
);
}
// globals.css: body { font-family: var(--font-latin), var(--font-myanmar), sans-serif; }Remote image ကို ခွင့်ပြုခြင်း
CMS ဒါမှမဟုတ် storage bucket ကနေ လာတဲ့ ပုံကို <Image> မှာ သုံးဖို့ hostname ကို next.config.ts မှာ ခွင့်ပြုရပါတယ်။ ဒီ list က မည်သူမဆို ကိုယ့် server ကို သုံးပြီး ဘယ် site ကပုံကိုမဆို optimize ခိုင်းတာကို ကာကွယ်ပါတယ်။ Config ပြင်ပြီးရင် dev server ကို restart လုပ်ပါ။
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
images: {
remotePatterns: [{ protocol: "https", hostname: "images.example.com" }],
},
};
export default nextConfig;လက်တွေ့ project တွေမှာ
Real project တွေမှာ
E-commerce site တွေမှာ product list တစ်ခုထဲမှာ ပုံ ရာချီ ပါပါတယ်။ <Image> က screen ပေါ် ရောက်လာမှ load လုပ်ပြီး ဖုန်းအတွက် သေးတဲ့ size ကိုပဲ ပို့လို့ data သုံးစွဲမှုနဲ့ load time သိသိသာသာ လျော့ပါတယ်။ Google ရဲ့ Core Web Vitals မှာ layout shift (CLS) နဲ့ အကြီးဆုံး content ပေါ်ချိန် (LCP) ကို တိုင်းလို့ ပုံ size မှန်ကန်တာက search ranking နဲ့ user experience နှစ်ခုလုံးကို ထိခိုက်ပါတယ်။ Page ထိပ်ဆုံးက hero ပုံလို ချက်ချင်း မြင်ရမယ့် ပုံကိုတော့ lazy load မလုပ်ဘဲ ကြို load လုပ်ခိုင်းရပါတယ် — Next.js 15 မှာ priority prop ကို သုံးပြီး ဒီ API က version အလိုက် ပြောင်းနေလို့ ကိုယ့် version ရဲ့ Image docs ကို စစ်ပါ။
Team တွေမှာ design token (color၊ spacing) ကို globals.css ထဲ CSS variable အဖြစ် ထားပြီး component module တွေက var(--color-primary) လို ပြန်သုံးလေ့ ရှိပါတယ်။ ဒါကြောင့် brand color ပြောင်းရင် နေရာတစ်ခုတည်းမှာ ပြင်ရပြီး component style တွေကတော့ တစ်ခုနဲ့ တစ်ခု သီးခြား ကျန်နေပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. <Image> မှာ width/height ဒါမှမဟုတ် fill မပေးတာ
<Image src="/notebook.jpg" alt="..." /> လို string src ကို size မပါဘဲ ပေးရင် Next.js က width property လိုအပ်ကြောင်း error ပြပါတယ်။ width နဲ့ height ကို ထည့်ပါ၊ static import ကို သုံးပါ၊ ဒါမှမဟုတ် fill ကို position: relative ပါတဲ့ parent ထဲမှာ သုံးပါ။ fill သုံးရင် sizes မထည့်ရင် browser က ပုံကို screen အကျယ်အပြည့်လို့ ယူဆပြီး လိုတာထက် ကြီးတဲ့ ဖိုင်ကို download လုပ်နိုင်ပါတယ်။
2. Component တိုင်းရဲ့ style ကို globals.css ထဲ ပုံထည့်တာ
globals.css က page တိုင်းမှာ load ဖြစ်ပြီး class နာမည်တွေ တစ်ခုတည်းသော namespace ထဲ ရှိလို့ file ကြီးလာလေ .title၊ .card လို နာမည်တွေ တိုက်လေပါ။ Component တစ်ခုနဲ့ပဲ ဆိုင်တဲ့ style ကို သူ့ဘေးက .module.css ထဲ ရွှေ့ပါ။
3. Remote ပုံကို config မလုပ်ဘဲ သုံးတာ
src="https://images.example.com/a.jpg" ကို remotePatterns မထည့်ဘဲ သုံးရင် hostname ကို next.config မှာ configure မလုပ်ရသေးကြောင်း error ရပါတယ်။ Hostname ကို တိတိကျကျ ထည့်ပါ၊ wildcard ကျယ်ကျယ်ကြီး မသုံးပါနဲ့။
Mini challenge
Mini challenge: profile card
Team page အတွက် ProfileCard component တစ်ခု ရေးပါ။ ဓာတ်ပုံက square box ကို အပြည့် ဖြည့်ရမယ်၊ style တွေက CSS Module ထဲမှာ ရှိရမယ်။
- Profile type မှာ name၊ role၊ photo (public folder ထဲက path) ပါရမယ်
- ပုံက fill ကို သုံးပြီး position: relative နဲ့ aspect-ratio: 1 / 1 ပါတဲ့ box ထဲမှာ ရှိရမယ်
- sizes prop ထည့်ပြီး alt မှာ လူရဲ့ နာမည် ပါရမယ်
- app/team/page.tsx က card တစ်ခုကို render လုပ်ရမယ်
// components/profile-card.tsx
import Image from "next/image";
import styles from "./profile-card.module.css";
type Profile = {
// TODO: name, role, photo
};
export function ProfileCard(/* TODO: profile prop */) {
// TODO: <article> with a photo box (fill image), the name and the role
}
// app/team/page.tsx
// TODO: import ProfileCard and render it with one profile
/* profile-card.module.css
TODO: .card, .photoBox (relative, square), .photo (object-fit) */ရလဒ်: /team မှာ square ပုံ ("Portrait of Su Su" alt နဲ့)၊ "Su Su" heading နဲ့ "Editor" စာကြောင်းပါတဲ့ card ပေါ်ပါမယ်။ Browser ကို ကျဉ်းကျဉ်း ချဲ့ချဲ့ လုပ်ကြည့်ရင် ပုံက square အတိုင်း ကျန်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
fill ပုံက position ပါတဲ့ အနီးဆုံး parent ကို ဖြည့်ပါတယ်။ ပုံ မပုံပျက်အောင် object-fit: cover ကို .photo class မှာ ထည့်ပါ။ Card က 300px အကျယ်ဆိုရင် sizes="(max-width: 600px) 100vw, 300px" လို ရေးနိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
// components/profile-card.tsx
import Image from "next/image";
import styles from "./profile-card.module.css";
export type Profile = {
name: string;
role: string;
photo: string;
};
export function ProfileCard({ profile }: { profile: Profile }) {
return (
<article className={styles.card}>
<div className={styles.photoBox}>
<Image
src={profile.photo}
alt={`Portrait of ${profile.name}`}
fill
sizes="(max-width: 600px) 100vw, 300px"
className={styles.photo}
/>
</div>
<h2>{profile.name}</h2>
<p>{profile.role}</p>
</article>
);
}
// app/team/page.tsx
import { ProfileCard } from "../../components/profile-card";
export default function TeamPage() {
return (
<ProfileCard profile={{ name: "Su Su", role: "Editor", photo: "/team/su-su.jpg" }} />
);
}
/* components/profile-card.module.css
.card { max-width: 300px; border-radius: 12px; overflow: hidden; }
.photoBox { position: relative; aspect-ratio: 1 / 1; }
.photo { object-fit: cover; }
*/Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Site တစ်ခုလုံးသုံး style ကို root layout ကနေ import လုပ်တဲ့ globals.css မှာ၊ component style ကို .module.css မှာ ထားပါ။
- <Image> က size ကနေ နေရာ ကြိုချန်ပြီး device နဲ့ ကိုက်တဲ့ size၊ format နဲ့ lazy load ကို ပေးပါတယ် — alt နဲ့ size (width/height၊ static import ဒါမှမဟုတ် fill) အမြဲ လိုပါတယ်။
- fill သုံးရင် parent မှာ position: relative နဲ့ ပုံမှာ sizes ထည့်ပြီး remote ပုံအတွက် remotePatterns ကို config လုပ်ပါ။
- next/font က font ကို build time မှာ ကိုယ့် site ကနေ serve လုပ်ပြီး layout shift ကို လျှော့ပါတယ်၊ Myanmar စာအတွက် myanmar subset ပါတဲ့ font ကို ရွေးပါ။
Next.js — Image Optimization — Next.js
Next.js — Image Optimization — Next.js