แท็ก <font> ใน HTML คืออะไร?
แท็ก <font> ใช้สำห ตั้งค่าสีฟอนต์ (color), ชื่อฟอนต์ (face), และขนาดฟอนต์ (size)** ใน HTML แบบเก่า แต่ แท็กนี้ล้าสมัย (Deprecated) ใน HTML5 แล้ว** ห้ามใช้ในโปรเจกต์ใหม่
แอตทริบิวต์ของ <font> (ล้าสมัย)
| แอตทริบิวต์ | ฟังก์ชัน | CSS ที่แทนที่ |
|---|---|---|
| color | สีตัวอักษร | color: red; / #ff0000 |
| face | ชื่อฟอนต์/Font Family | font-family: Arial, sans-serif; |
| size | ขนาดฟอนต์ (1-7 หรือ +N/-N) | font-size: 16px; / 1.2em |
ตัวอย่าง (โค้ดเก่า - ไม่ควรใช้)
<!DOCTYPE html> <html> <body> <h2>เล่นกับฟอนต์</h2> <p><font face="arial" size="20" color="yellow">Demo Text!</font></p> <p><font size="10" color="gray">Demo Text!</font></p> <p><font size="15" face="verdana" color="magenta">Demo Text!</font></p> <p><font size="8" face="calibri" color="magenta">Demo Text!</font></p> </body> </html>
วิธีที่ถูกต้องใน HTML5: ใช้ CSS
<!DOCTYPE html>
<html>
<head>
<style>
.demo-text {
font-family: 'Calibri', 'Verdana', Arial, sans-serif;
font-size: 1.2rem;
color: magenta;
}
.text-large { font-size: 2rem; color: orange; }
.text-small { font-size: 0.875rem; color: gray; }
</style>
</head>
<body>
<h2>เล่นกับฟอนต์ (CSS)</h2>
<p class="demo-text">Demo Text!</p>
<p class="text-large">ข้อความใหญ่ สีส้ม</p>
<p class="text-small">ข้อความเล็ก สีเทา</p>
</body>
</html>
ทำไมต้องเลิกใช้ <font>?
- ผสมผสาน Content (HTML) และ Presentation (Style) → บำรุงรักษายาก
- ไม่รองรับ Responsive Design, Media Queries
- ไม่สามารถ Override/Get Computed Style ได้ง่าย
- Accessibility: Screen Reader ไม่ได้รับข้อมูลโครงสร้าง
- SEO: ความหมายทางความหมาย (Semantics) หายไป
- Deprecated ใน HTML5, ลบออกจากมาตรฐาน
แท็กจัดรูปแบบเก่าอื่นๆ ที่ล้าสมัย
| แท็ก | ฟังก์ชัน | CSS แทนที่ |
|---|---|---|
<center> | จัดกึ่งกลาง | text-align: center; / margin: 0 auto; |
<big> | ข้อความใหญ่ | font-size: larger; |
<small> | ข้อความเล็ก (ยังใช้ได้แต่ semantic แตกต่าง) | font-size: smaller; |
<strike>, <s> | ขีดทับ | text-decoration: line-through; |
<u> (เก่า) | ขีดเส้นใต้ | text-decoration: underline; |
<b> | ตัวหนา (ใช้ <strong> แทน) | font-weight: bold; |
<i> | ตัวเอียง (ใช้ <em> แทน) | font-style: italic; |
สรุป
ใช้ **Semantic HTML** (<strong>, <em>, <mark>, <cite>) ร่วมกับ **CSS** สำหรับการจัดรูปแบบทั้งหมด