- HTMLImageElement: src property
- Value
- Examples
- Specifying a single image
- HTML
- Result
- Using src with an image set
- HTML
- Result
- Specifying a fallback for viewport-based selection
- HTML
- Result
- Specifications
- Browser compatibility
- Found a content problem with this page?
- HTML Tag
- Browser Support
- Attributes
- Html img element src
- Синтаксис
- Атрибуты
- Закрывающий тег
HTMLImageElement: src property
The HTMLImageElement property src , which reflects the HTML src attribute, specifies the image to display in the element.
Value
When providing only a single image, rather than a set of images from which the browser selects the best match for the viewport size and display pixel density, the src attribute is a string specifying the URL of the desired image. This can be set either within the HTML itself using the src content attribute, or programmatically by setting the element’s src property.
If you use the srcset content attribute to provide multiple image options for different display pixel densities, the URL specified by the src attribute is used in one of two ways:
- as a fallback for browsers that don’t support srcset .
- as an equivalent for specifying an image in srcset with the size multiplier 1x ; that is, the image specified by src is used on low-density screens (such as typical 72 DPI or 96 DPI displays).
Additionally, if you use src along with both sizes (or the corresponding sizes content attribute) and srcset in order to choose an image based on the viewport size, the src attribute is only used as a fallback for browsers that don’t support sizes and srcset ; otherwise, it’s not used at all.
Examples
Specifying a single image
HTML
img src="grapefruit-slice-332-332.jpg" width="160" alt="Slices of grapefruit, looking yummy." />
Result
Using src with an image set
When using a set of images with the srcset property, the src serves as either a fallback for older browsers, or as the 1x size of the image.
HTML
Result
Specifying a fallback for viewport-based selection
When using viewport-bases selection of an image from a srcset by also specifying the sizes property, the src property serves as the fallback to be used on browsers that don’t support viewport-based selection. Browsers that do support viewport-based selection will ignore src in this situation.
HTML
Result
Specifications
Browser compatibility
BCD tables only load in the browser
Found a content problem with this page?
This page was last modified on Apr 7, 2023 by MDN contributors.
Your blueprint for a better internet.
HTML
Tag
The 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 tag creates a holding space for the referenced image.
The 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 tag inside an tag (see example below).
Browser Support
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 | 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 |
Html img element src
Тег предназначен для отображения на веб-странице изображений в графическом формате GIF, JPEG или PNG. Адрес файла с картинкой задаётся через атрибут src . Если необходимо, то рисунок можно сделать ссылкой на другой файл, поместив тег
в контейнер . При этом вокруг изображения отображается рамка, которую можно убрать, добавив атрибут border=»0″ в тег
.
Рисунки также могут применяться в качестве карт-изображений, когда картинка содержит активные области, выступающие в качестве ссылок. Такая карта по внешнему виду ничем не отличается от обычного изображения, но при этом оно может быть разбито на невидимые зоны разной формы, где каждая из областей служит ссылкой.
Синтаксис
Атрибуты
align Определяет как рисунок будет выравниваться по краю и способ обтекания текстом. alt Альтернативный текст для изображения. border Толщина рамки вокруг изображения. height Высота изображения. hspace Горизонтальный отступ от изображения до окружающего контента. ismap Говорит браузеру, что картинка является серверной картой-изображением. longdesc Указывает адрес документа, где содержится аннотация к картинке. lowsrc Адрес изображения низкого качества. src Путь к графическому файлу. vspace Вертикальный отступ от изображения до окружающего контента. width Ширина изображения. usemap Ссылка на тег , содержащий координаты для клиентской карты-изображения.
Также для этого тега доступны универсальные атрибуты и события.
Закрывающий тег
Lorem ipsum dolor sit amet.