Znacznik HTML <img>


Przykład

Jak wstawić obraz:

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

Więcej przykładów „Wypróbuj sam” poniżej.


Definicja i użycie

Tag <img>służy do osadzenia obrazu na stronie HTML.

Obrazy nie są technicznie wstawiane na stronę internetową; obrazy są połączone ze stronami internetowymi. Znacznik <img>tworzy miejsce do przechowywania obrazu odniesienia.

Tag <img>ma dwa wymagane atrybuty:

  • src - Określa ścieżkę do obrazu
  • alt - Określa alternatywny tekst dla obrazu, jeśli z jakiegoś powodu obraz nie może być wyświetlony

Uwaga: Zawsze określaj również szerokość i wysokość obrazu. Jeśli szerokość i wysokość nie zostaną określone, strona może migotać podczas ładowania obrazu.

Wskazówka: Aby połączyć obraz z innym dokumentem, po prostu zagnieździj <img>znacznik w znaczniku <a> (patrz przykład poniżej).


Obsługa przeglądarki

Element
<img> Yes Yes Yes Yes Yes

Atrybuty

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


Atrybuty globalne

Znacznik <img>obsługuje również atrybuty globalne w HTML .


Atrybuty wydarzenia

Tag <img>obsługuje również Atrybuty Zdarzeń w HTML .


Więcej przykładów

Przykład

Wyrównaj obraz (z 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">

Przykład

Dodaj obramowanie obrazu (z CSS):

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

Przykład

Dodaj lewy i prawy margines do obrazu (z CSS):

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

Przykład

Dodaj górne i dolne marginesy do obrazu (z CSS):

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

Przykład

Jak wstawić obrazy z innego folderu lub z innej witryny internetowej:

<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">

Przykład

Jak dodać hiperłącze do obrazu:

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

Przykład

Jak stworzyć mapę obrazu z regionami, które można kliknąć. Każdy region to hiperłącze:

<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>

Powiązane strony

Samouczek HTML: obrazy HTML

Odniesienie HTML DOM: Obiekt obrazu

Samouczek CSS: stylizowanie obrazów


Domyślne ustawienia CSS

Większość przeglądarek wyświetla <img>element z następującymi wartościami domyślnymi:

Przykład

img {
  display: inline-block;
}