Skip to content

Study Guide: HTML + CSS Basics

ก่อนจะเริ่มอ่าน — ลองกดเล่น interactive demo ด้านล่างเพื่อทำความเข้าใจ Box Model, Flexbox, Grid และ Responsive Design แบบ visual

layout.css LIVE DEMO
margin 16px 16px 16px 16px
border 2px 2px 2px 2px
padding 20px 20px 20px 20px
200 x 100
box-sizing:
content-box: width: 200px total = 200 + 20+20 + 2+2 + 16+16 = 276px

box-sizing เปลี่ยนวิธีคำนวณขนาดกล่อง -- border-box ทำให้ padding + border ไม่เพิ่มขนาด

A
B
C
D
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
direction:
justify:
align:

Flexbox จัด element เป็นแถวหรือคอลัมน์ -- ลองสลับค่าต่างๆ เพื่อดูว่า layout เปลี่ยนอย่างไร

1
2
3
4
5
6
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
columns:
gap:

CSS Grid จัด layout 2 มิติ -- auto-fit + minmax ทำให้ responsive โดยไม่ต้องเขียน media query

viewport:
320px
Header
Sidebar
Content
/* Mobile: stack layout */
.body { display: flex; flex-direction: column; }

Responsive Design ทำให้ layout ปรับตัวตามขนาดหน้าจอ -- sidebar จะย้ายไปด้านบนบน mobile

HTML (HyperText Markup Language) คือภาษาที่ใช้สร้าง โครงสร้าง ของหน้าเว็บ — เหมือนโครงกระดูกของร่างกาย

โครงสร้างพื้นฐาน

Section titled “โครงสร้างพื้นฐาน”
index.html
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>สวัสดีชาวโลก!</h1>
<p>นี่คือเว็บแรกของฉัน</p>
</body>
</html>

Tag ที่ใช้บ่อยที่สุด

Section titled “Tag ที่ใช้บ่อยที่สุด”
Tagใช้ทำอะไรตัวอย่าง
<h1><h6>หัวข้อ (ใหญ่ → เล็ก)<h1>Title</h1>
<p>ย่อหน้า<p>Hello</p>
<a>ลิงก์<a href="/about">About</a>
<img>รูปภาพ<img src="pic.jpg" alt="photo">
<div>กล่องจัดกลุ่ม<div class="card">...</div>
<ul> / <li>รายการ<ul><li>Item</li></ul>
<button>ปุ่ม<button>Click</button>
<input>ช่องกรอกข้อมูล<input type="text" placeholder="Name">
<form>ฟอร์ม<form>...</form>
Output
<h1>
Heading 1
<p>
Paragraph text
<a>
Link text
<ul>
• Item 1 • Item 2
<img>
<button>
Click me

ใช้ tag ที่มีความหมายแทน <div> ทุกที่ — ช่วยเรื่อง SEO และ Accessibility

semantic-example.html
<header>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
<main>
<section id="about">
<h2>About Me</h2>
<p>I'm a UX/UI Designer learning to code.</p>
</section>
<section id="skills">
<h2>Skills</h2>
<ul>
<li>Figma</li>
<li>HTML & CSS</li>
</ul>
</section>
</main>
<footer>
<p>&copy; 2025 My Portfolio</p>
</footer>

CSS (Cascading Style Sheets) คือภาษาที่ใช้ ตกแต่ง หน้าเว็บ — สี, ขนาด, ระยะห่าง, layout ทุกอย่าง

style.css
/* selector { property: value; } */
h1 {
color: #333;
font-size: 2rem;
}
.card {
background: white;
padding: 20px;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
#hero {
text-align: center;
}

ทุก element บนเว็บคือ “กล่อง” — ต้องเข้าใจ Box Model ก่อน

┌────────────── margin ──────────────┐
│ ┌────────── border ──────────┐ │
│ │ ┌────── padding ──────┐ │ │
│ │ │ │ │ │
│ │ │ content │ │ │
│ │ │ │ │ │
│ │ └─────────────────────┘ │ │
│ └────────────────────────────┘ │
└────────────────────────────────────┘
style.css
.card {
/* เนื้อหาข้างใน */
padding: 20px;
/* เส้นขอบ */
border: 1px solid #ddd;
/* ระยะห่างจาก element อื่น */
margin: 16px;
/* ทำให้ padding ไม่เพิ่มขนาดกล่อง (สำคัญมาก!) */
box-sizing: border-box;
}
Output
margin 16px 16px 16px 16px
border 16px 16px 16px 16px
padding 16px 16px 16px 16px
content
200 × 100

Flexbox — จัด Layout แนวเดียว

Section titled “Flexbox — จัด Layout แนวเดียว”

Flexbox ใช้จัด element เป็น แถวหรือคอลัมน์ — ใช้บ่อยที่สุดในงานจริง

style.css
.navbar {
display: flex;
justify-content: space-between; /* จัดแนวนอน */
align-items: center; /* จัดแนวตั้ง */
gap: 16px; /* ระยะห่างระหว่าง items */
}
index.html
<nav class="navbar">
<div class="logo">MyBrand</div>
<div class="links">
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</div>
</nav>

Flex Properties ที่ใช้บ่อย

Section titled “Flex Properties ที่ใช้บ่อย”
Propertyค่าที่ใช้บ่อยผลลัพธ์
justify-contentcenter, space-between, space-aroundจัดแนวนอน
align-itemscenter, flex-start, flex-end, stretchจัดแนวตั้ง
flex-directionrow, columnแถวหรือคอลัมน์
flex-wrapwrap, nowrapตัดบรรทัดหรือไม่
gap8px, 1remช่องว่างระหว่าง items
style.css
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px; /* ขยายได้, หดได้, เริ่มที่ 300px */
padding: 24px;
background: white;
border-radius: 12px;
}
Output
A
B
C
D
direction:
justify:
align:
display: flex; flex-direction: row; justify-content: flex-start; align-items: flex-start;

CSS Grid — จัด Layout 2 มิติ

Section titled “CSS Grid — จัด Layout 2 มิติ”

Grid ใช้จัด layout ที่ซับซ้อนกว่า — มีทั้ง แถวและคอลัมน์

style.css
.grid-layout {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 คอลัมน์เท่ากัน */
gap: 20px;
}
style.css
.projects {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
}
index.html
<section class="projects">
<div class="card">Project 1</div>
<div class="card">Project 2</div>
<div class="card">Project 3</div>
<div class="card">Project 4</div>
</section>
Output
1
2
3
4
5
6
columns:
gap:
grid-template-columns: repeat(3, 1fr); gap: 8px;

ทำให้เว็บแสดงผลดีทุกขนาดหน้าจอ — mobile, tablet, desktop

style.css
/* Mobile first — เขียน style สำหรับ mobile ก่อน */
.container {
padding: 16px;
}
/* Tablet ขึ้นไป (768px) */
@media (min-width: 768px) {
.container {
padding: 32px;
max-width: 720px;
margin: 0 auto;
}
}
/* Desktop ขึ้นไป (1024px) */
@media (min-width: 1024px) {
.container {
max-width: 960px;
}
}
Unitใช้เมื่อตัวอย่าง
remขนาดตัวอักษร, spacingfont-size: 1.5rem
%ความกว้างที่ยืดหยุ่นwidth: 100%
vw / vhเต็มจอheight: 100vh
pxค่าตายตัว เช่น borderborder: 1px solid
Output
Active: @media (max-width: 768px)

CSS Variables ช่วยให้จัดการสี, ขนาด, spacing จากจุดเดียว — เปลี่ยนทีเดียว เปลี่ยนทั้งเว็บ

style.css
/* ประกาศ variable ที่ :root (ใช้ได้ทุกที่) */
:root {
--color-primary: #3b82f6;
--color-bg: #ffffff;
--color-text: #1f2937;
--spacing-md: 16px;
--radius: 12px;
}
/* ใช้งานด้วย var() */
.btn {
background: var(--color-primary);
color: white;
padding: var(--spacing-md);
border-radius: var(--radius);
}
/* Dark theme — แค่เปลี่ยน variables */
body.dark {
--color-bg: #0a0a0f;
--color-text: #e4e4e7;
}

:hover — เปลี่ยน style เมื่อเอาเมาส์ชี้

Section titled “:hover — เปลี่ยน style เมื่อเอาเมาส์ชี้”
style.css
.btn {
background: #3b82f6;
color: white;
padding: 12px 24px;
border-radius: 8px;
cursor: pointer;
}
.btn:hover {
background: #2563eb;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

transition — ทำให้การเปลี่ยนแปลงเป็น animation

Section titled “transition — ทำให้การเปลี่ยนแปลงเป็น animation”
style.css
.card {
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
/* transition: property duration timing-function */
transition: all 0.3s ease;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}

  1. ไปที่ fonts.google.com
  2. เลือก font + weights ที่ต้องการ
  3. Copy <link> tag ไปใส่ใน <head>
index.html
<head>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
</head>
style.css
body {
font-family: 'Inter', sans-serif;
}

เปิด DevTools ด้วย Cmd + Option + I หรือ F12

  • Inspect element — คลิกขวา > Inspect เพื่อดู HTML + CSS ของ element นั้น
  • แก้ไข CSS live — คลิกที่ค่า CSS ใน Styles panel แล้วพิมพ์ค่าใหม่ได้เลย
  • Box Model — ดู margin/border/padding/content ใน Computed tab
  • Toggle property — คลิก checkbox หน้า CSS property เพื่อเปิด/ปิด
  • Responsive mode — กด Cmd + Shift + M จำลองหน้าจอ mobile

style.css
/* padding เพิ่มขนาดกล่อง — กล่อง 300px + padding 20px = 340px */
.box { width: 300px; padding: 20px; }
/* padding ไม่เพิ่มขนาด — กล่องยังคง 300px */
*, *::before, *::after { box-sizing: border-box; }

2. ใช้ fixed width กับ responsive layout

Section titled “2. ใช้ fixed width กับ responsive layout”
style.css
/* ไม่ responsive */
.container { width: 960px; }
/* responsive */
.container { max-width: 960px; width: 100%; }
<!-- div soup — ไม่สื่อความหมาย -->
<div class="header"><div class="nav">...</div></div>
<!-- semantic — สื่อความหมาย, ดีต่อ SEO + accessibility -->
<header><nav>...</nav></header>

style.css
/* BEM-like: block__element--modifier */
.card { }
.card__title { }
.card__title--large { }
/* ง่ายกว่า: descriptive names */
.nav-link { }
.hero-title { }
.btn-primary { }
style.css
/* 1. Reset / Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; }
/* 2. Variables */
:root { --color-primary: #3b82f6; }
/* 3. Layout */
.container { max-width: 960px; margin: 0 auto; }
/* 4. Components */
.card { }
.btn { }
.nav { }
/* 5. Utilities */
.text-center { text-align: center; }
.hidden { display: none; }
style.css
/* เขียน mobile ก่อน แล้วเพิ่ม style สำหรับจอใหญ่ */
.grid { display: grid; grid-template-columns: 1fr; }
@media (min-width: 768px) {
.grid { grid-template-columns: repeat(2, 1fr); }
}

  1. สร้าง Navbar — ใช้ Flexbox จัด logo ซ้าย, links ขวา
  2. สร้าง Card Grid — ใช้ CSS Grid แสดง 3 cards ที่ responsive
  3. สร้าง Hero Section — จัดข้อความตรงกลางจอด้วย Flexbox + min-height: 100vh
  4. เล่นเกม Flexbox Froggy ให้ครบทุกด่าน
  5. เล่นเกม Grid Garden ให้ครบทุกด่าน