ข้อมูลอ้างอิงHTML

HTML โดยตัวอักษร HTML ตามหมวดหมู่ รองรับ HTML Browser แอตทริบิวต์ HTML HTML Global Attributes เหตุการณ์ HTML HTML สี HTML Canvas HTML เสียง/วิดีโอ ชุดอักขระ HTML HTML Doctypes การเข้ารหัส URL HTML รหัสภาษา HTML รหัสประเทศ HTML ข้อความ HTTP วิธี HTTP ตัวแปลง PX เป็น EM แป้นพิมพ์ลัด


HTML <img>แท็ก


ตัวอย่าง

วิธีแทรกรูปภาพ:

<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">

ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง


ความหมายและการใช้งาน

แท็ ก<img>นี้ใช้เพื่อฝังรูปภาพในหน้า HTML

ในทางเทคนิคแล้วรูปภาพจะไม่ถูกแทรกลงในหน้าเว็บ รูปภาพเชื่อมโยงกับหน้าเว็บ แท็ ก<img>สร้างช่องว่างสำหรับภาพอ้างอิง

แท็ ก<img>มีแอตทริบิวต์ที่จำเป็นสองประการ:

  • src - ระบุเส้นทางไปยังรูปภาพ
  • alt - ระบุข้อความสำรองสำหรับรูปภาพ หากไม่สามารถแสดงรูปภาพด้วยเหตุผลบางประการได้

หมายเหตุ:นอกจากนี้ ให้ระบุความกว้างและความสูงของรูปภาพเสมอ หากไม่ระบุความกว้างและความสูง หน้าอาจกะพริบขณะโหลดรูปภาพ

เคล็ดลับ:หากต้องการลิงก์รูปภาพไปยังเอกสารอื่น เพียงวาง<img>แท็กไว้ในแท็ ก <a> (ดูตัวอย่างด้านล่าง)


รองรับเบราว์เซอร์

Element
<img> Yes Yes Yes Yes Yes

คุณลักษณะ

Attribute Value Description
alt text Specifies an alternate text for an image
crossorigin anonymous
use-credentials
Allow images from third-party sites that allow cross-origin access to be used with canvas
height pixels Specifies the height of an image
ismap ismap Specifies an image as a server-side image map
loading eager
lazy
Specifies whether a browser should load an image immediately or to defer loading of images until some conditions are met
longdesc URL Specifies a URL to a detailed description of an image
referrerpolicy no-referrer
no-referrer-when-downgrade
origin
origin-when-cross-origin
unsafe-url
Specifies which referrer information to use when fetching an image
sizes sizes Specifies image sizes for different page layouts
src URL Specifies the path to the image
srcset URL-list Specifies a list of image files to use in different situations
usemap #mapname Specifies an image as a client-side image map
width pixels Specifies the width of an image


คุณสมบัติสากล

แท็ ก<img>ยังสนับสนุน Global Attributes ในHTML


คุณสมบัติของเหตุการณ์

แท็ ก<img>ยังสนับสนุนแอตทริบิวต์เหตุการณ์ใน HTML


ตัวอย่างเพิ่มเติม

ตัวอย่าง

จัดแนวรูปภาพ (ด้วย CSS):

<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:bottom">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:top">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="float:right">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="float:left">

ตัวอย่าง

เพิ่มขอบภาพ (ด้วย CSS):

<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="border:5px solid black">

ตัวอย่าง

เพิ่มระยะขอบซ้ายและขวาให้กับรูปภาพ (ด้วย CSS):

<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:0px 50px">

ตัวอย่าง

เพิ่มระยะขอบบนและล่างให้กับรูปภาพ (ด้วย CSS):

<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:50px 0px">

ตัวอย่าง

วิธีแทรกรูปภาพจากโฟลเดอร์อื่นหรือจากเว็บไซต์อื่น:

<img src="/images/stickman.gif" alt="Stickman" width="24" height="39">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="Lamp" width="32" height="32">

ตัวอย่าง

วิธีเพิ่มไฮเปอร์ลิงก์ให้กับรูปภาพ:

<a href="https://www.w3schools.com">
<img src="w3html.gif" alt="W3Schools.com" width="100" height="132">
</a>

ตัวอย่าง

วิธีสร้างแผนที่รูปภาพพร้อมพื้นที่ที่คลิกได้ แต่ละภูมิภาคเป็นไฮเปอร์ลิงก์:

<img src="workplace.jpg" alt="Workplace" usemap="#workmap" width="400" height="379">

<map name="workmap">
  <area shape="rect" coords="34,44,270,350" alt="Computer" href="computer.htm">
  <area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
  <area shape="circle" coords="337,300,44" alt="Cup of coffee" href="coffee.htm">
</map>

หน้าที่เกี่ยวข้อง

กวดวิชา HTML: HTML รูปภาพ

การอ้างอิง HTML DOM: Image Object

กวดวิชา CSS: การจัดรูปแบบรูปภาพ


การตั้งค่า CSS เริ่มต้น

เบราว์เซอร์ส่วนใหญ่จะแสดง<img>องค์ประกอบด้วยค่าเริ่มต้นดังต่อไปนี้:

ตัวอย่าง

img {
  display: inline-block;
}