Học cách tạo liên kết, chèn hình ảnh, video, audio và nội dung multimedia
Thẻ <a> được sử dụng để tạo liên kết đến trang khác, file, email hoặc vị trí khác trên cùng trang.
<a href="https://www.google.com">Đi đến Google</a>
<a href="mailto:email@example.com">Gửi email</a>
<a href="tel:+84123456789">Gọi điện thoại</a>
| Thuộc tính | Ý nghĩa | Giá trị | Ví dụ |
|---|---|---|---|
href |
Đường dẫn đích - xác định nơi liên kết sẽ dẫn đến | URL, email, số điện thoại, anchor | href="https://example.com" |
target |
Cách mở liên kết - quyết định liên kết mở ở đâu | _blank, _self, _parent, _top | target="_blank" |
rel |
Mối quan hệ giữa trang hiện tại và trang đích | noopener, noreferrer, nofollow | rel="noopener" |
download |
Yêu cầu tải xuống file thay vì mở | tên file hoặc để trống | download="document.pdf" |
title |
Tooltip hiển thị khi hover - thông tin bổ sung | văn bản mô tả | title="Mở trong tab mới" |
hreflang |
Ngôn ngữ của trang đích | mã ngôn ngữ (vi, en, ja...) | hreflang="en" |
<!-- Liên kết bên ngoài - mở tab mới + bảo mật -->
<a href="https://www.example.com" target="_blank" rel="noopener noreferrer">
Trang web bên ngoài
</a>
<!-- Liên kết download -->
<a href="document.pdf" download="TaiLieu.pdf" title="Tải xuống tài liệu">
Tải tài liệu PDF
</a>
<!-- Liên kết nội bộ -->
<a href="about.html" target="_self" title="Trang giới thiệu">
Giới thiệu
</a>
Liên kết nội bộ cho phép nhảy đến vị trí cụ thể trong cùng trang. Hữu ích cho navigation và mục lục.
<!-- Tạo liên kết anchor -->
<a href="#section1">Đi đến phần 1</a>
<a href="#top">Về đầu trang</a>
<!-- Phần tử đích -->
<section id="section1">
<h2>Nội dung phần 1</h2>
</section>
Lưu ý: "Table of Contents" là mục lục, dùng <nav> + <ul>, không phải thẻ <table>.
<nav class="table-of-contents">
<h3>📚 Mục lục</h3>
<ul>
<li><a href="#gioi-thieu">Giới thiệu</a></li>
<li><a href="#noi-dung">Nội dung chính</a></li>
<li><a href="#ket-luan">Kết luận</a></li>
</ul>
</nav>
rel="noopener" để bảo mậtThẻ <img> được sử dụng để chèn hình ảnh vào trang web.
<img src="duong-dan-hinh-anh.jpg" alt="Mô tả hình ảnh">
<!-- Với kích thước cụ thể -->
<img src="logo.png" alt="Logo công ty" width="200" height="100">
<!-- Từ URL bên ngoài -->
<img src="https://images.unsplash.com/photo-1461988320302-91bde64fc8e4?w=300&h=200&fit=crop&auto=format" alt="Hình ảnh demo">
| Thuộc tính | Ý nghĩa & Vai trò | Các giá trị có thể | Ví dụ & Khi nào dùng |
|---|---|---|---|
src |
Bắt buộc - Đường dẫn đến file hình ảnh |
• Relative path: images/photo.jpg• Absolute URL: https://domain.com/img.jpg• Data URL: data:image/jpeg;base64,...• Root relative: /assets/logo.png
|
src="./images/avatar.jpg" - File localsrc="https://picsum.photos/300" - URL external
|
alt |
Bắt buộc - Văn bản thay thế cho accessibility |
• Descriptive text: Mô tả nội dung ảnh • Empty string: alt="" cho decorative images• Functional text: Mô tả chức năng nếu ảnh là button/link |
alt="CEO John Smith đang thuyết trình"alt="" - Ảnh trang tríalt="Tìm kiếm" - Icon button
|
loading |
Chiến lược tải ảnh - tối ưu performance |
• lazy: Tải khi ảnh sắp vào viewport (mặc định cho ảnh không critical) • eager: Tải ngay lập tức cùng với trang • auto: Browser tự quyết định |
loading="lazy" - Ảnh ở cuối trangloading="eager" - Hero image, logoKhông set = auto |
decoding |
Cách decode/xử lý ảnh |
• async: Decode không đồng bộ, không block render • sync: Decode đồng bộ, có thể block render • auto: Browser tự chọn (mặc định) |
decoding="async" - Ảnh không quan trọngdecoding="sync" - Critical imagesdecoding="auto" - Thông thường
|
fetchpriority |
Độ ưu tiên tải ảnh so với resources khác |
• high: Tải với priority cao (hero images, above fold) • low: Priority thấp (ảnh không quan trọng) • auto: Browser tự quyết định (mặc định) |
fetchpriority="high" - Banner chínhfetchpriority="low" - Thumbnailfetchpriority="auto" - Default
|
width |
Chiều rộng hiển thị (ưu tiên dùng CSS) |
• Pixel value: Số nguyên dương (VD: 300, 150) • Không có đơn vị: Tự hiểu là pixel • Responsive: Nên dùng CSS thay vì HTML attribute |
width="300" - Fixed widthCSS: width: 100%; max-width: 400px; - Responsive
|
height |
Chiều cao hiển thị (ưu tiên dùng CSS) |
• Pixel value: Số nguyên dương • Auto aspect ratio: Không set height để giữ tỷ lệ • CSS preferred: Flexible hơn HTML attribute |
height="200" - Fixed heightCSS: height: auto; - Giữ tỷ lệ
|
title |
Tooltip khi hover - thông tin bổ sung |
• Descriptive text: Thông tin chi tiết về ảnh • Context info: Ngày chụp, tác giả, địa điểm • Caption alternative: Khi không có figcaption |
title="Chụp tại Hà Nội, tháng 3/2024"title="Logo được thiết kế năm 2020"
|
crossorigin |
CORS policy khi tải ảnh từ domain khác |
• anonymous: Không gửi credentials (cookies, auth) • use-credentials: Gửi credentials • Không set: Không có CORS request |
crossorigin="anonymous" - CDN imagescrossorigin="use-credentials" - Private images
|
sizes |
Kích thước ảnh sẽ hiển thị (dùng với srcset) |
• Media condition: (max-width: 768px) 100vw• Default size: 50vw• Multiple conditions: Danh sách cách nhau bởi dấu phẩy |
sizes="(max-width: 768px) 100vw, 50vw"Kết hợp với srcset
|
srcset |
Danh sách các nguồn ảnh cho responsive/high-DPI |
• Width descriptor: image.jpg 300w• Density descriptor: image@2x.jpg 2x• Multiple sources: Cách nhau bởi dấu phẩy |
srcset="small.jpg 300w, large.jpg 800w"srcset="normal.jpg 1x, retina.jpg 2x"
|
Hình ảnh demo với các thuộc tính: alt, title, loading, decoding
<!-- Ảnh hero với priority cao -->
<img src="hero-banner.jpg"
alt="Khóa học lập trình web 2024 - Từ cơ bản đến nâng cao"
width="1200"
height="600"
title="Banner khóa học - Click để tìm hiểu thêm"
fetchpriority="high"
decoding="sync">
<!-- Ảnh trong content với lazy loading -->
<img src="tutorial-step1.png"
alt="Bước 1: Tạo file HTML cơ bản với thẻ DOCTYPE"
loading="lazy"
decoding="async"
title="Hướng dẫn chi tiết từng bước">
<!-- Ảnh trang trí (không cần alt) -->
<img src="decoration.svg"
alt=""
role="presentation"
loading="lazy">
<a href="https://www.example.com">
<img src="https://images.unsplash.com/photo-1557804506-669a67965ba0?w=400&h=200&fit=crop&auto=format" alt="Banner quảng cáo">
</a>
HTML5 cung cấp thẻ <video> để chèn video trực tiếp vào trang web.
<video width="400" height="300" controls>
<source src="https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_1mb.mp4" type="video/mp4">
<source src="https://sample-videos.com/zip/10/webm/SampleVideo_1280x720_1mb.webm" type="video/webm">
Trình duyệt của bạn không hỗ trợ video.
</video>
| Thuộc tính | Ý nghĩa & Vai trò | Giá trị | Khi nào sử dụng |
|---|---|---|---|
controls |
Hiển thị nút điều khiển (play, pause, volume, fullscreen) | Không cần giá trị (boolean) | Hầu hết trường hợp để người dùng điều khiển |
autoplay |
Tự động phát video khi trang load | Không cần giá trị (boolean) | ⚠️ Cẩn thận: làm phiền người dùng, cần kèm muted |
loop |
Lặp lại video khi kết thúc | Không cần giá trị (boolean) | Video ngắn, demo, background video |
muted |
Tắt âm thanh mặc định | Không cần giá trị (boolean) | Bắt buộc khi dùng autoplay |
poster |
Ảnh hiển thị trước khi video phát | Đường dẫn đến file ảnh | Tạo ấn tượng đầu, thumbnail video |
preload |
Cách tải video khi trang load | none, metadata, auto | Tối ưu hiệu suất trang |
width/height |
Kích thước hiển thị video | Số pixel | Định dạng layout, ưu tiên dùng CSS |
playsinline |
Phát inline trên mobile thay vì fullscreen | Không cần giá trị (boolean) | Video nhỏ, không muốn fullscreen |
<!-- Video giới thiệu sản phẩm -->
<video width="800" height="450"
controls
poster="https://images.unsplash.com/photo-1536240478700-b869070f9279?w=800&h=450&fit=crop&auto=format"
preload="metadata"
playsinline>
<source src="https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_2mb.mp4" type="video/mp4">
<source src="https://sample-videos.com/zip/10/webm/SampleVideo_1280x720_1mb.webm" type="video/webm">
<p>Trình duyệt của bạn không hỗ trợ video HTML5.
<a href="https://sample-videos.com/zip/10/mp4/SampleVideo_1280x720_2mb.mp4">Tải video</a>
</p>
</video>
<!-- Background video tự động phát -->
<video width="100%" height="400"
autoplay
muted
loop
playsinline
preload="auto">
<source src="background-video.mp4" type="video/mp4">
<source src="background-video.webm" type="video/webm">
<!-- Fallback image cho trường hợp không hỗ trợ video -->
<img src="fallback-image.jpg" alt="Background image">
</video>
<!-- Video bài giảng với track subtitle -->
<video controls preload="metadata">
<source src="lesson-video.mp4" type="video/mp4">
<track kind="subtitles" src="subtitles-vi.vtt" srclang="vi" label="Tiếng Việt">
<track kind="subtitles" src="subtitles-en.vtt" srclang="en" label="English">
</video>
Thẻ <audio> được sử dụng để chèn file âm thanh. Có nhiều thuộc tính tương tự video nhưng tối ưu cho âm thanh.
| Thuộc tính | Ý nghĩa & Vai trò | Giá trị | Khi nào sử dụng |
|---|---|---|---|
controls |
Hiển thị player controls (play, pause, volume, timeline) | Boolean attribute | Khi muốn user có thể điều khiển audio |
autoplay |
Tự động phát khi trang load | Boolean attribute | ⚠️ Tránh dùng - gây khó chịu cho user |
loop |
Lặp lại audio liên tục | Boolean attribute | Nhạc nền, sound effect |
muted |
Tắt tiếng mặc định | Boolean attribute | Khi dùng autoplay hoặc background audio |
preload |
Chiến lược tải audio | none, metadata, auto | Tối ưu performance và UX |
volume |
Mức âm lượng mặc định (0.0 đến 1.0) | Số thập phân | Đặt âm lượng phù hợp với context |
<!-- Audio player cơ bản -->
<audio controls preload="metadata">
<source src="https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_MP3_700KB.mp3" type="audio/mpeg">
<source src="https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_OGG_700KB.ogg" type="audio/ogg">
<p>Trình duyệt của bạn không hỗ trợ audio HTML5.
<a href="https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_MP3_700KB.mp3">Tải file audio</a>
</p>
</audio>
<!-- Playlist audio với controls đầy đủ -->
<audio controls loop preload="metadata" volume="0.8">
<source src="https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_MP3_1MG.mp3" type="audio/mpeg">
<source src="https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_WAV_1MG.wav" type="audio/wav">
<p>Trình duyệt không hỗ trợ phát audio.</p>
</audio>
<!-- Background music (ít dùng) -->
<audio autoplay muted loop preload="auto" volume="0.3">
<source src="background-music.mp3" type="audio/mpeg">
<source src="background-music.ogg" type="audio/ogg">
</audio>
<!-- Audio với JavaScript control -->
<audio id="myAudio" preload="none">
<source src="notification.mp3" type="audio/mpeg">
</audio>
<button onclick="document.getElementById('myAudio').play()">
Phát âm thanh thông báo
</button>
https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_MP3_700KB.mp3https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_MP3_1MG.mp3https://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_OGG_700KB.ogghttps://file-examples.com/storage/fe68c8606b6515ad79b2a56/2017/11/file_example_WAV_1MG.wav| Format | Ưu điểm | Nhược điểm | Khi nào dùng |
|---|---|---|---|
| MP3 | Tương thích cao, file nhỏ | Chất lượng hơi giảm khi nén | Nhạc, podcast, âm thanh thông dụng |
| WAV | Chất lượng cao, không nén | File rất lớn | Audio chuyên nghiệp, sound effect ngắn |
| OGG | Mã nguồn mở, chất lượng tốt | Hỗ trợ hạn chế trên Safari/IE | Web app, game |
| AAC | Chất lượng tốt hơn MP3 | Ít tương thích hơn MP3 | iOS, Apple devices |
metadata: Khuyến nghị - tải thông tin cơ bảnnone: Tiết kiệm bandwidth - tải khi user click playauto: Tải toàn bộ - chỉ dùng cho audio ngắn, quan trọngThẻ <iframe> cho phép nhúng một trang web khác vào trang hiện tại. Rất mạnh mẽ nhưng cần thận trọng về bảo mật.
| Thuộc tính | Ý nghĩa & Vai trò | Giá trị | Lưu ý bảo mật |
|---|---|---|---|
src |
Bắt buộc - URL của trang cần nhúng | URL tuyệt đối | Chỉ nhúng từ nguồn tin cậy |
width/height |
Kích thước của iframe | Pixel hoặc phần trăm | Không ảnh hưởng bảo mật |
sandbox |
Quan trọng - Hạn chế quyền của iframe | allow-forms, allow-scripts, v.v. | Luôn sử dụng để tăng bảo mật |
loading |
Chiến lược tải iframe | lazy, eager | lazy loading giúp tối ưu performance |
allow |
Chính sách tính năng (Feature Policy) | camera, microphone, autoplay, v.v. | Hạn chế quyền truy cập thiết bị |
referrerpolicy |
Chính sách gửi referrer | no-referrer, same-origin, v.v. | Bảo vệ thông tin trang gốc |
title |
Mô tả nội dung iframe cho accessibility | Văn bản mô tả | Quan trọng cho screen reader |
"" (rỗng): Hạn chế tối đa - an toàn nhấtallow-scripts: Cho phép JavaScriptallow-forms: Cho phép submit formallow-same-origin: Coi như cùng domain<!-- Iframe cơ bản với bảo mật tối thiểu -->
<iframe src="https://www.google.com/maps/embed?..."
width="600"
height="400"
style="border:0;"
sandbox="allow-scripts allow-same-origin"
loading="lazy"
title="Bản đồ Google Maps">
</iframe>
<iframe width="560"
height="315"
src="https://www.youtube.com/embed/VIDEO_ID"
title="Tiêu đề video YouTube"
frameborder="0"
sandbox="allow-scripts allow-same-origin allow-presentation"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
loading="lazy"
referrerpolicy="no-referrer-when-downgrade">
</iframe>
<iframe
src="https://www.google.com/maps/embed?pb=!1m18!1m12..."
width="100%"
height="300"
style="border:0;"
sandbox="allow-scripts allow-same-origin"
allowfullscreen=""
loading="lazy"
referrerpolicy="no-referrer-when-downgrade"
title="Vị trí địa điểm trên Google Maps">
</iframe>
<!-- Iframe với bảo mật cao nhất -->
<iframe src="https://trusted-site.com/content"
width="800"
height="600"
sandbox=""
loading="lazy"
referrerpolicy="no-referrer"
title="Nội dung được nhúng an toàn">
<p>Trình duyệt của bạn không hỗ trợ iframe.</p>
</iframe>
<!-- Iframe cho phép form nhưng không có script -->
<iframe src="https://survey.com/form"
sandbox="allow-forms"
title="Form khảo sát">
</iframe>
Các thẻ semantic để nhóm media với caption của chúng. Rất quan trọng cho SEO và accessibility.
| Thẻ | Ý nghĩa | Khi nào sử dụng | SEO Benefits |
|---|---|---|---|
<figure> |
Container semantic cho nội dung độc lập (ảnh, video, code, chart) | Khi có nội dung kèm caption hoặc có thể di chuyển mà không ảnh hưởng flow | Giúp search engine hiểu cấu trúc nội dung |
<figcaption> |
Caption/mô tả cho figure - có thể ở đầu hoặc cuối figure | Mô tả, giải thích, credit cho media content | Cung cấp context cho search engine và screen reader |
<figure>
<img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=600&h=400&fit=crop&auto=format"
alt="Phong cảnh núi non hùng vĩ với những đỉnh cao phủ đầy tuyết trắng"
loading="lazy">
<figcaption>
<strong>Dãy núi Alps, Thụy Sĩ</strong> -
Chụp vào lúc hoàng hôn từ đỉnh Matterhorn.
<em>Ảnh: John Smith, 2023</em>
</figcaption>
</figure>
<!-- Figure với video -->
<figure>
<video controls width="500" loading="lazy">
<source src="https://sample-videos.com/zip/10/mp4/SampleVideo_640x360_1mb.mp4" type="video/mp4">
<p>Trình duyệt không hỗ trợ video.</p>
</video>
<figcaption>
Video hướng dẫn: <cite>"Cách tạo website responsive"</cite> -
Phần 1: Thiết lập HTML cơ bản
</figcaption>
</figure>
<!-- Figure với code snippet -->
<figure>
<pre><code>
function calculateSum(a, b) {
return a + b;
}
</code></pre>
<figcaption>
<strong>Listing 1.1:</strong> Hàm tính tổng cơ bản trong JavaScript
</figcaption>
</figure>
<!-- Figure với quote -->
<figure>
<blockquote>
"Học lập trình không chỉ là học syntax, mà là học cách tư duy logic."
</blockquote>
<figcaption>
— <cite>Linus Torvalds</cite>, Tạo ra Linux kernel
</figcaption>
</figure>
<!-- Figure với table/chart -->
<figure>
<table>
<thead>
<tr><th>Năm</th><th>Doanh số</th></tr>
</thead>
<tbody>
<tr><td>2022</td><td>100M</td></tr>
<tr><td>2023</td><td>150M</td></tr>
</tbody>
</table>
<figcaption>
<strong>Bảng 2.1:</strong> Tăng trường doanh số công ty qua các năm
</figcaption>
</figure>
function calculateSum(a, b) {
return a + b;
}
// Sử dụng hàm
console.log(calculateSum(5, 3)); // 8
Yêu cầu: Tạo một trang giới thiệu cá nhân đơn giản với navigation, avatar, video, audio và liên kết.
Tôi là một lập trình viên web đam mê tạo ra những sản phẩm hiện đại.
|
ReactJS + Node.js |
React Native + Firebase |
📍 Vị trí văn phòng:
Yêu cầu: Tạo gallery ảnh với HTML cơ bản.
|
Tây Bắc hùng vĩ |
Di sản văn hóa thế giới |
Thành phố không ngủ |
|
Đặc sản thủ đô |
Thiên đường biển đảo |
Trường đại học đầu tiên |
📸 Gallery gồm 6 địa điểm | 🕒 7 ngày | 💫 Những kỷ niệm không thể quên