HTML image
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h2>Norwegian Mountain Trip</h2> <img border="0" src="https://img.php.cn/upload/article/000/000/003/5a9675a3b2106284.jpg" alt="Pulpit rock" width="304" height="228"> </body> </html>
Click "Run" Example" button to view online examples
Online examples
This example demonstrates how to display images in a web page.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p> 一个图像: <img src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="Smiley face" width="32" height="32"></p> <p> 一个动图: <img src="/upload/course/000/000/010/58046a41c6e3c115.gif" alt="Computer man" width="48" height="48"></p> <p> 注意插入动图的语法和静态图的语法是一样的。 </p> </body> </html>
Click the "Run Instance" button to view the online instance
Insert pictures from different locations
This example demonstrates how to display pictures from other folders or servers into a web page.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>一个来自文件夹中的图像:</p> <img src="/upload/course/000/000/010/58046b14a8b9f738.gif" alt="Google Chrome" width="33" height="32"><p>一个来自php中文网的图像:</p> <img src="/upload/course/000/000/010/58046c29b47ef575.png" alt="php.cn" width="336" height="69"> </body> </html>
Click the "Run Instance" button to view the online instance
(More examples can be found at the bottom of this page.)
HTML Images - Image tag (<img>) and source attribute (Src)
In HTML, images Defined by the <img> tag.
<img> is an empty tag, which means that it only contains attributes and has no closing tag.
To display an image on the page, you need to use the source attribute (src). src refers to "source". The value of the source attribute is the URL address of the image.
The syntax for defining images is:
<img src="url" alt="some_text">
URL refers to the location where the image is stored. If an image named "../style/images/boat.gif" is located in the images directory of m.sbmmt.com, its URL is //m.sbmmt.com/images/boat.gif.
The browser displays the image in the document where the image tag appears. If you place an image tag between two paragraphs, the browser will display the first paragraph first, then the image, and finally the second paragraph.
HTML Image - Alt attribute
alt attribute is used to define a string of prepared alternative text for the image.
The value of the replacement text attribute is user-defined.
<img src="../style/images/boat.gif" alt="Big Boat">
When the browser cannot load the image, the replacement text attribute tells readers the information they lost. At this point, the browser will display this alternative text instead of the image. It's a good practice to add the alt text attribute to all images on the page. This helps display information better and is very useful for those who use text-only browsers.
HTML Image - Set the height and width of the image
The height (height) and width (width) attributes are used to set the height and width of the image.
The default unit of the attribute value is pixels:
<img src="../style/images/pulpit.jpg" alt="Pulpit rock" width="304" height="228">
Tip: It is a good habit to specify the height and width of the image. If an image has a specified height and width, the specified dimensions will be retained when the page loads. If the size of the image is not specified, the overall layout of the HTML page may be destroyed when the page is loaded.
Basic Notes - Helpful Tips:
Note: If an HTML file contains ten images, then in order to display the page correctly, it needs Loading 11 files. Loading images takes time, so our advice is: use images with caution.
Note: When loading the page, pay attention to the path to insert the page image. If the position of the image cannot be set correctly, the browser cannot load the image, and the image tag will display a broken image.
More examples
1. Arrange images
This example demonstrates how to arrange images in text.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h4>默认对齐的图像 (align="bottom"):</h4> <p>这是一些文本。 <img src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="Smiley face" width="32" height="32"> 这是一些文本。</p> <h4>图片使用 align="middle":</h4> <p>这是一些文本。 <img src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="Smiley face" align="middle" width="32" height="32">这是一些文本。</p> <h4>图片使用 align="top":</h4> <p>这是一些文本。 <img src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="Smiley face" align="top" width="32" height="32">这是一些文本。</p> <p><b>注意:</b>在HTML 4中 align 属性已废弃,HTML5 已不支持该属性,可以使用 CSS 代替。</p> </body> </html>
Click the "Run Instance" button to view the online instance
2. Floating image
This example demonstrates how to make an image float to the left or right of a paragraph.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p> <img src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="Smiley face" style="float:left" width="32" height="32"> 一个带图片的段落,图片浮动在这个文本的左边。 </p> <p> <img src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="Smiley face" style="float:right" width="32" height="32"> 一个带图片的段落,图片浮动在这个文本的右边。 </p> <p><b>注意:</b> 在这里我们使用了 CSS "float" 属性,在HTML 4中 align 属性已废弃,HTML5 已不支持该属性,可以使用 CSS 代替。</p> </body> </html>
Click the "Run Instance" button to view the online instance
3. Set image link
This example demonstrates how to use an image as a link.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>创建图片链接: <a href="//m.sbmmt.com/html/html-tutorial.html"> <img src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="HTML 教程" width="32" height="32"></a></p> <p>无边框的图片链接: <a href="//m.sbmmt.com/html/html-tutorial.html"> <img border="0" src="/upload/course/000/000/010/58046a2b7e46d104.gif" alt="HTML 教程" width="32" height="32"></a></p> </body> </html>
Click the "Run Instance" button to view the online instance
4. Create an image map
This example shows how to create an image map with clickable areas. Each of these fields is a hyperlink.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>点击太阳或其他行星,注意变化:</p> <img src="/upload/course/000/000/010/58046db4ca85a572.gif" width="145" height="126" alt="Planets" usemap="#planetmap"> <map name="planetmap"> <area shape="rect" coords="0,0,82,126" alt="Sun" href="sun.htm"> <area shape="circle" coords="90,58,3" alt="Mercury" href="mercur.htm"> <area shape="circle" coords="124,58,8" alt="Venus" href="venus.htm"> </map> </body> </html>
Click the "Run Instance" button to view the online instance
HTML Image Tag
Tag | Description |
---|---|
##<img> | Define image|
Define image map | |
Define the clickable area in the image map |