Html image source file

: The Media or Image Source element

The HTML element specifies multiple media resources for the , the element, or the element. It is a void element, meaning that it has no content and does not have a closing tag. It is commonly used to offer the same media content in multiple file formats in order to provide compatibility with a broad range of browsers given their differing support for image file formats and media file formats.

Try it

Attributes

This element includes the global attributes.

Address of the media resource.

A list of one or more strings, separated by commas, indicating a set of possible images represented by the source for the browser to use. Each string is composed of:

  1. One URL specifying an image.
  2. A width descriptor, which consists of a string containing a positive integer directly followed by «w» , such as 300w . The default value, if missing, is the infinity.
  3. A pixel density descriptor, that is a positive floating number directly followed by «x» . The default value, if missing, is 1x .

Each string in the list must have at least a width descriptor or a pixel density descriptor to be valid. The two types of descriptors should not be mixed together and only one should be used consistently throughout the list. Among the list, the value of each descriptor must be unique. The browser chooses the most adequate image to display at a given point of time. If the sizes attribute is present, then a width descriptor must be specified for each string. If the browser does not support srcset , then src will be used for the default source.

Читайте также:  Css border color 2 colors

A list of source sizes that describes the final rendered width of the image represented by the source. Each source size consists of a comma-separated list of media condition-length pairs. Before laying the page out, the browser uses this information to determine which image is defined in srcset to use. Please note that sizes will have its effect only if width dimension descriptors are provided with srcset instead of pixel ratio values (200w instead of 2x for example).

Media query of the resource’s intended media.

The intrinsic height of the image, in pixels. Must be an integer without a unit.

The intrinsic width of the image in pixels. Must be an integer without a unit.

If the type attribute isn’t specified, the media’s type is retrieved from the server and checked to see if the user agent can handle it; if it can’t be rendered, the next is checked. If the type attribute is specified, it’s compared against the types the user agent can present, and if it’s not recognized, the server doesn’t even get queried; instead, the next element is checked at once.

When used in the context of a element, the browser will fall back to using the image specified by the element’s child if it is unable to find a suitable image to use after examining every provided .

Usage notes

Theelement is a void element, which means that it not only has no content but also has no closing tag. That is, you never use « » in your HTML.

For information about image formats supported by web browsers and guidance on selecting appropriate formats to use, see our Image file type and format guide on the web. For details on the video and audio media types you can use, see the Guide to media types formats used on the web.

Examples

Video example

This example demonstrates how to offer a video in Ogg format for users whose browsers support Ogg format, and a QuickTime format video for users whose browsers support that. If the audio or video element is not supported by the browser, a notice is displayed instead. If the browser supports the element but does not support any of the specified formats, an error event is raised and the default media controls (if enabled) will indicate an error. Be sure to reference our guide to media types and formats on the web for details on what media file formats you can use and how well they’re supported by browsers.

video controls> source src="foo.webm" type="video/webm" /> source src="foo.ogg" type="video/ogg" /> source src="foo.mov" type="video/quicktime" /> I'm sorry; your browser doesn't support HTML video. video> 

For more examples, the learning area article Video and audio content is a great resource.

Picture example

picture> source srcset="mdn-logo-wide.png" media="(min-width: 800px)" /> source srcset="mdn-logo-medium.png" media="(min-width: 600px)" /> img src="mdn-logo-narrow.png" alt="MDN Web Docs" /> picture> 

With the element, you must always include an with a fallback image, with an alt attribute to ensure accessibility (unless the image is an irrelevant background decorative image).

Picture with height & width attributes example

In this example, three elements with height and width attributes are included in a element. A media query allows the browser to select an image to display with the height and width attributes based on the viewport size.

picture> source srcset="landscape.png" media="(min-width: 1000px)" width="1000" height="400" /> source srcset="square.png" media="(min-width: 800px)" width="800" height="800" /> source srcset="portrait.png" media="(min-width: 600px)" width="600" height="800" /> img src="fallback.png" alt="Image used when the browser does not support the sources" width="500" height="400" /> picture> 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jul 5, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

HTML img src (image source) with path options (5 examples)

The img src stands for image source, which is used to specify the source of an image in the HTML tag.

Absolute path example Relative example

For example, this is how the image path is set along with title and alt attributes in img tag:

alt text here

Two options to specify a source

You may use absolute or relative paths to specify the source of the image in HTML img src attribute.

The absolute path

In this option, the complete URL of the image is specified in the src attribute of HTML img tag. For example:

alt text here

HTML img src

See online demo and code

You may also specify the URL of the image without “http”.

alt text here

See online demo and code

This will be useful if the server is changed from HTTP to HTTPs in the future, for example. This will work in both cases.

Where should I use Absolute path in img src?

  • You should use this option when using images from some other web server.
  • You do not want or permitted to host those images at your own server.
  • In that case, the image download bandwidth consumption will cost at the server where it is hosted.

Although, absolute options will also work for the same server, however, recommended way is to use the relative path in case images are existing at the same server where your website is hosted.

The relative path option

In this option, you will specify image source based at the current directory. An example of relative path is:

”alt

See online demo and code

If you are working in a source file e.g. index.html and use above line of code, it means:

  • The images directory/folder is at the same location where source file (index.html) is placed.
  • The images directory contains the banana.jpg file.

See another possibility to specify path:

Banana is good in taste!

HTML img src-relative

See online demo and code

In that case, I used “../”, that specifies to go back one step.

To get back two steps, use “../../”. e.g.

”Banana

See online demo and code

What if the image does not load? The alt attribute

There may be a few reasons that images does not load, for example:

  • The specified path in img src tag is not correct.
  • There are speed issues at the user’s side and the image is too heavy to be loaded.
  • You web server got slower or taking much time to complete the request.
  • And more.

In any case, where an image is unable to download at user’s computer the alt attribute text will be shown. The alt means alternate which is specified in the HTML tag. For example:

Banana is good in taste!

HTML img src not exist

See online demo and code

So if the path is incorrect or image is not loaded for any reason, the text “Banana is good in taste” will be shown in place of the image, as shown in the demo.

The HTML alt attribute is recommended to use for images generally. This will benefit as far as SEO is concerned. As such, search engines are unable to read the text inside the images so the only way you may tell the purpose of images is by “alt” or “title” tags.

Display title as Bootstrap tooltip

The title tag of the tag (and others) can be used to display the tooltips. By default, tooltips use default style which is small in size and small text.

By using tooltip with CSS, you may style it quite beautifully.

In this demo, I will show you tooltip by using the Bootstrap component. For that, I simply included the Bootstrap libraries and jQuery in the head section.

HTML img src tooltip

See online demo and code

The tooltip is called in the section by using jQuery (see script section). The style contains the look and feel of the tooltip.

For more on Bootstrap Tooltips, go to its detailed guide.

Conclusion

Out of the two options to specify the path in img src tag of HTML, you should use relative if images are located at your own hosting.

If you are referring images from other servers then use absolute URLs in the src attribute.

You should also consider using absolute URLs if you are using high-quality images and your hosting package offers limited bandwidth consumption.

There are third party image hosting solutions where you may host images while the bandwidth cost is incurred by that server.

Источник

Оцените статью