HTML Img Tag - W3Schools
Có thể bạn quan tâm
Example
How to insert an image:
<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600"> Try it Yourself »More "Try it Yourself" examples below.
Definition and Usage
The <img> tag is used to embed an image in an HTML page.
Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding space for the referenced image.
The <img> tag has two required attributes:
- src - Specifies the path to the image
- alt - Specifies an alternate text for the image, if the image for some reason cannot be displayed
Note: Also, always specify the width and height of an image. If width and height are not specified, the page might flicker while the image loads.
Tip: To link an image to another document, simply nest the <img> tag inside an <a> tag (see example below).
Browser Support
Element | |||||
---|---|---|---|---|---|
<img> | Yes | Yes | Yes | Yes | Yes |
Attributes
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 | eagerlazy | 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-referrerno-referrer-when-downgradeorigin origin-when-cross-originunsafe-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 |
Global Attributes
The <img> tag also supports the Global Attributes in HTML.
Event Attributes
The <img> tag also supports the Event Attributes in HTML.
More Examples
Example
Align image (with 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"> Try it Yourself »Example
Add image border (with CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="border:5px solid black"> Try it Yourself »Example
Add left and right margins to image (with CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:0px 50px"> Try it Yourself »Example
Add top and bottom margins to image (with CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:50px 0px"> Try it Yourself »Example
How to insert images from another folder or from another web site:
<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"> Try it Yourself »Example
How to add a hyperlink to an image:
<a href="https://www.w3schools.com"><img src="w3html.gif" alt="W3Schools.com" width="100" height="132"></a> Try it Yourself »Example
How to create an image map, with clickable regions. Each region is a hyperlink:
<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> Try it Yourself »Related Pages
HTML tutorial: HTML Images
HTML DOM reference: Image Object
CSS Tutorial: Styling Images
Default CSS Settings
Most browsers will display the <img> element with the following default values:
Example
img { display: inline-block;} Try it Yourself » ❮ Previous Complete HTML Reference Next ❯ ★ +1 Track your progress - it's free! Log in Sign UpTừ khóa » Html Tag For Website Logo
-
Html - What Tag Should Be Used For A Logo Element If The Logo Is Text?
-
Logo Tag Html Code Example
-
How To Insert An Image In HTML In 6 Easy Steps - Hostinger
-
How To Add Icon Logo In Title Bar Using HTML ? - GeeksforGeeks
-
HTML Images - W3Schools
-
HTML5 Logo Tag - Dave Woods Freelance Web Designer UK
-
How To Use HTML To Insert A Logo - Techwalla
-
W3C HTML5 Logo
-
What Is Preferred? H1 As The Logo And H2 As The Page Title Or A Div ...
-
How Will I Add A Logo Or An Image In Top Left Corner Of My Web Page?
-
How To Add Image In The Title Bar - W3docs
-
Images In HTML - Learn Web Development | MDN
-
HTML And CSS Tutorial - Nanyang Technological University
-
Html Logo Maker | BrandCrowd