HTML 가이드
처음 HTML을 배우는 분들을 위한 핵심 가이드입니다. 아래 단계를 따라가며 무엇을, 왜, 어떻게 배우는지 감을 잡아보세요.
- HTML 문서의 기본 구조와 필수 태그 익히기
- 텍스트, 링크, 이미지, 표, 폼 등 자주 쓰는 요소 실습하기
- 블록/인라인, 시맨틱 태그, 레이아웃 개념 이해하기
- 멀티미디어, 메타 태그, 폼 유효성 검사 활용하기
- 보안(XSS 등)과 접근성(A11y) 기본 감각 쌓기
HTML 기본 구조
모든 페이지는 <!DOCTYPE html> 선언으로 시작하고,
<html> 안에 <head>와 <body>가 들어갑니다.
<head>는 문서 설정(제목/메타/스타일/스크립트), <body>는 화면에 보이는 내용입니다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>내 첫 HTML 페이지</title>
</head>
<body>
<h1>Hello World</h1>
본문 내용은 여기 들어갑니다.
</body>
</html>
<meta name="viewport">는 항상 넣어 주세요.
텍스트: 제목·단락·강조
제목은 <h1>~<h6>(중요도 순), 단락은 <p>로 묶습니다.
의미를 강조할 때는 <strong>(강조), <em>(강세)를 사용하세요.
<h1>가장 중요한 제목</h1>
<h2>그 다음 제목</h2>
<p>여기는 하나의 단락입니다. 문장은 단락 안에 들어갑니다.</p>
문장 중 <strong>중요한 부분</strong>과 <em>강세를 주고 싶은 부분</em>을 구분할 수 있어요.
<b>, <i>만 쓰지 마세요.
의미가 있는 강조는 <strong>, <em>를 권장합니다(접근성에 유리).
링크 & 이미지
링크는 <a href="...">, 이미지는 <img src="..." alt="...">로 넣습니다.
alt는 이미지 대체 텍스트로, 시각장애인 사용자와 검색엔진에 매우 중요합니다.
<a href="https://www.example.com" target="_blank" rel="noopener">사이트로 이동</a>
<img src="/images/puppy.jpg" alt="강아지가 잔디밭에서 뛰노는 사진" width="300">
target="_blank"와 함께
보안/성능을 위해 rel="noopener"를 같이 쓰세요.
리스트 & 표
순서 없는 목록은 <ul>, 순서 있는 목록은 <ol>.
표는 <table> 안에 <tr>(행), <th>(머리셀), <td>(데이터셀)를 구성합니다.
<ul>
<li>사과</li>
<li>바나나</li>
<li>체리</li>
</ul>
<ol>
<li>첫 번째</li>
<li>두 번째</li>
</ol>
<table border="1">
<tr><th>이름</th><th>나이</th></tr>
<tr><td>홍길동</td><td>20</td></tr>
</table>
폼(입력) 기초
사용자 입력은 <form> 안에서 이루어집니다. 라벨과 컨트롤을 짝지어 접근성을 높이세요.
<form action="/signup" method="post">
<label for="username">아이디</label>
<input id="username" name="username" type="text" required>
<label for="password">비밀번호</label>
<input id="password" name="password" type="password" required>
<button type="submit">가입하기</button>
</form>
<label for="... ">와 입력의 id를 매칭하면
스크린리더가 필드를 정확히 읽어줍니다.
시맨틱 HTML
의미 있는 태그(<header>, <nav>, <main>, <article>, <section>, <footer>)를 사용하면
구조가 명확해지고 SEO, 접근성에 유리합니다.
<header>사이트 헤더</header>
<nav>메뉴</nav>
<main>
<article>
<h1>글 제목</h1>
<p>글 내용...</p>
</article>
</main>
<footer>푸터</footer>
멀티미디어 삽입
HTML5에서는 <audio>와 <video> 태그로
음성과 영상을 직접 삽입할 수 있습니다.
<audio controls>
<source src="music.mp3" type="audio/mpeg">
브라우저가 오디오를 지원하지 않습니다.
</audio>
<video controls width="400">
<source src="movie.mp4" type="video/mp4">
브라우저가 비디오를 지원하지 않습니다.
</video>
controls 속성을 주면 재생/일시정지 버튼이 자동 생성됩니다.
메타데이터 & SEO 태그
검색엔진과 소셜미디어에서 페이지를 더 잘 이해하도록 <meta> 태그를 활용합니다.
<head>
<title>맛있는 파스타 레시피</title>
<meta name="description" content="간단하고 맛있는 파스타 레시피 모음">
<meta property="og:title" content="맛있는 파스타 레시피">
<meta property="og:description" content="누구나 쉽게 따라하는 파스타 요리법">
<meta property="og:image" content="pasta.jpg">
</head>
og:* 속성은 SNS 공유 시 미리보기 카드에 표시됩니다.
접근성 심화 (ARIA)
role과 aria-* 속성을 통해 화면 읽기 보조기기에 더 친절한 웹을 만들 수 있습니다.
<nav role="navigation" aria-label="주 메뉴">
<ul>
<li><a href="/home">홈</a></li>
<li><a href="/about">소개</a></li>
</ul>
</nav>
<button aria-expanded="false" aria-controls="menu">메뉴 열기</button>
HTML5 입력 타입
HTML5에서는 다양한 입력 타입을 지원하여 브라우저가 적절한 입력 UI를 자동 제공합니다.
<form>
날짜: <input type="date" name="birthday">
색상: <input type="color" name="favcolor">
범위: <input type="range" min="0" max="100" step="10">
이메일: <input type="email" required>
</form>
외부 콘텐츠 삽입 (iframe)
<iframe>은 다른 웹페이지나 외부 서비스를 현재 페이지 안에 삽입할 수 있습니다.
<iframe src="https://www.example.com" width="600" height="400"></iframe>
<!-- 유튜브 영상 삽입 -->
<iframe width="560" height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
frameborder="0" allowfullscreen></iframe>
sandbox 속성을 사용하면 더 안전합니다.
Canvas 그래픽
<canvas> 태그는 자바스크립트와 함께 그래픽이나 애니메이션을 그릴 수 있습니다.
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000;"></canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(20, 20, 150, 60);
</script>
SVG 벡터 그래픽
<svg>는 확대/축소해도 깨지지 않는 벡터 이미지를 직접 HTML 안에서 정의할 수 있습니다.
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green"
stroke-width="4" fill="yellow" />
</svg>
canvas는 픽셀 기반,
svg는 수학적 좌표 기반입니다.
구조화된 데이터 (SEO 강화)
검색엔진이 더 잘 이해하도록 JSON-LD 같은 구조화 데이터를 추가할 수 있습니다.
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Recipe",
"name": "파스타 알리오 올리오",
"author": "홍길동",
"recipeIngredient": [
"스파게티 면",
"올리브 오일",
"마늘",
"페퍼론치노"
],
"cookTime": "PT20M"
}
</script>
브라우저 로컬 저장소 (LocalStorage)
localStorage는 사용자의 브라우저에 데이터를 영구 저장할 수 있게 해줍니다.
<button onclick="saveName()">이름 저장</button>
<button onclick="loadName()">이름 불러오기</button>
<script>
function saveName() {
localStorage.setItem("username", "홍길동");
alert("저장 완료!");
}
function loadName() {
alert("저장된 이름: " + localStorage.getItem("username"));
}
</script>
드래그 앤 드롭 (Drag & Drop)
HTML5에서는 간단하게 요소를 드래그하고 다른 영역에 드롭할 수 있습니다.
<div id="dropZone" style="width:200px;height:100px;border:2px dashed gray;">여기에 드롭하세요</div>
<img id="dragItem" src="/images/html.png" width="80" draggable="true">
<script>
var item = document.getElementById("dragItem");
var zone = document.getElementById("dropZone");
zone.ondragover = function(event) { event.preventDefault(); }
item.ondragstart = function(event) { event.dataTransfer.setData("id", "dragItem"); }
zone.ondrop = function(event) {
event.preventDefault();
var id = event.dataTransfer.getData("id");
zone.appendChild(document.getElementById(id));
}
</script>
위치 정보 (Geolocation API)
navigator.geolocation을 사용하면 사용자의 현재 위치를 가져올 수 있습니다.
<button onclick="getLocation()">내 위치 확인</button>
<p id="loc"></p>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPos);
} else {
document.getElementById("loc").innerText = "브라우저가 지원하지 않습니다.";
}
}
function showPos(position) {
document.getElementById("loc").innerText =
"위도: " + position.coords.latitude +
", 경도: " + position.coords.longitude;
}
</script>
웹 컴포넌트 (Web Components)
Web Components는 자신만의 HTML 태그를 정의할 수 있는 기술입니다. Shadow DOM, Custom Elements, HTML Templates를 활용합니다.
<template id="hello-template">
<style> p { color: blue; font-weight: bold; } </style>
<p>안녕하세요! 저는 웹 컴포넌트입니다.</p>
</template>
<script>
class HelloComponent extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
const tmpl = document.getElementById("hello-template");
shadow.appendChild(tmpl.content.cloneNode(true));
}
}
customElements.define("hello-world", HelloComponent);
</script>
<hello-world></hello-world>
웹 접근성 (ARIA)
ARIA (Accessible Rich Internet Applications)는 장애가 있는 사용자도 웹을 쉽게 이용할 수 있도록 돕는 속성입니다.
<button aria-label="검색 실행 버튼">
🔍
</button>
<div role="alert">
⚠ 오류가 발생했습니다.
</div>
<nav aria-label="주 메뉴">
<ul>
<li><a href="/home">홈</a></li>
<li><a href="/edu">스터디</a></li>
</ul>
</nav>
PWA 기초 (Progressive Web App)
PWA는 웹사이트를 마치 앱처럼 설치하고, 오프라인에서도 동작하게 만드는 최신 기술입니다.
<!-- manifest.json 연결 -->
<link rel="manifest" href="/manifest.json">
<!-- Service Worker 등록 -->
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/sw.js").then(() => {
console.log("서비스 워커 등록 성공");
});
}
</script>
SEO (검색 엔진 최적화)
검색 엔진에서 사이트를 잘 노출시키려면 메타 태그와 의미 있는 HTML 구조가 중요합니다.
<head>
<title>HTML 스터디 페이지</title>
<meta name="description" content="초보자를 위한 HTML 가이드">
<meta name="keywords" content="HTML, 웹 개발, 스터디">
<meta property="og:title" content="HTML 스터디">
<meta property="og:description" content="처음 배우는 사람을 위한 HTML 가이드">
</head>
보안과 HTML
HTML 코드 작성 시 보안을 고려하지 않으면 XSS (Cross-Site Scripting) 같은 공격에 취약해집니다.
<!-- 사용자 입력을 출력할 때는 반드시 이스케이프 처리 -->
<p>안전한 출력: @userInput</p>
<!-- iframe 보안 속성 -->
<iframe src="https://example.com"
sandbox="allow-scripts allow-same-origin">
</iframe>
<!-- 외부 링크는 rel="noopener noreferrer" 추가 -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">외부 링크</a>
반응형 이미지 (Responsive Images)
같은 이미지를 기기별로 최적화해서 로드하면 속도 향상과 데이터 절약에 도움이 됩니다.
<img src="small.jpg"
srcset="small.jpg 480w, medium.jpg 800w, large.jpg 1200w"
sizes="(max-width: 600px) 480px,
(max-width: 1000px) 800px,
1200px"
alt="반응형 이미지 예제">
마이크로데이터 & 구조화된 데이터
검색 엔진에 더 많은 의미를 전달하려면 Schema.org 같은 구조화된 데이터를 활용하세요.
<div itemscope itemtype="https://schema.org/Person">
<span itemprop="name">홍길동</span>
<img itemprop="image" src="hong.jpg" alt="홍길동">
<span itemprop="jobTitle">웹 개발자</span>
<a href="mailto:[email protected]" itemprop="email">[email protected]</a>
</div>
최신 HTML API
HTML5는 단순한 마크업을 넘어 다양한 API를 제공합니다. 예를 들어, 위치, 오디오/비디오, 드래그앤드롭 기능입니다.
<!-- 위치 정보 API -->
<script>
navigator.geolocation.getCurrentPosition(function(pos) {
console.log("위도:", pos.coords.latitude, "경도:", pos.coords.longitude);
});
</script>
<!-- 오디오 재생 -->
<audio controls>
<source src="music.mp3" type="audio/mpeg">
지원되지 않는 브라우저입니다.
</audio>
<!-- 드래그 앤 드롭 -->
<div draggable="true">드래그 가능</div>