The map tag in html examples. Draw an image map with the mouse

We have already talked about how to make a picture a link .. in this chapter we will talk about how to make a fragment, a picture area with a link to a particular document, as well as about how to make different areas of the same figure with links to different documents.

To accomplish this task, we cannot do without so-called navigation maps.

Suppose we have a drawing like this:

And we need to do so that the user, by clicking on one of these "buttons" follows the link to a particular document .. what do we need for this?

First of all, let's designate our drawing not as an ordinary graphic image, but as a navigational map by assigning our individual name to this drawing. This is done using the attribute usemap tag (I think you shouldn't be reminded that the tag has a required attribute src which indicates the path to a particular picture )

Let's name our drawing / map panel... It will look like this:

usemap = "# panel">

Do not forget to put the sign according to the syntax rules # lattice in front of the name ..

Well, now, in fact, let's draw up a navigation map. It is specified by the tag which has an attribute name- name .. do you catch where I am leading? Well, as you probably already guessed, we will indicate on the basis of which drawing we will build our navigation map by indicating its name ..


Now let's get down to the fact that we will indicate to the browser the areas of the picture that will be links, and at the same time we will write down the paths of transitions to these links. The tag takes over this task. , it does not require a closing tag, and has the following attributes that we will work with:

href- indicates the path to the opened document (just like in the tag )
shape- the shape of the drawing area that will serve as a link. It can have one of three values:
  • rect- rectangular area
  • poly- the area is a kind of polygon
  • circle- the area defined by the circle
coords- shape coordinates

Rectangular area

Now our map looks like this:



In fact, now the "green" square button has become operational.

What you can see by clicking on it in this example:



Navigation map



usemap = "# panel">








I hurried a little with an example without really explaining the essence of what I wrote .. Let's focus on the attributes of the tag .

href = "primer1.html"- here I think it's clear, this is the path to the document, which should open when you click on the "green button".

Since the button is square, and the square, as you know, is a "regular" rectangle, we assign the shape of the drawing area to a rectangular shape = "rect" .

And now the fun part coords = "15,15,82,82"- coordinates .. For a rectangle, they are set by two points according to the principle "X1, Y1, X2, Y2" Where X1, Y1 is the first point and, accordingly, X2, Y2 is the second. Coordinates are indicated in pixels .. Our picture / map has dimensions of 300 by 100 pixels, its top-left pixel has coordinates X = 0, Y = 0, and the bottom-right one is X = 300, Y = 100. If it's not clear, let's plunge into geometry for the fifth grade ..

Take a look at the picture:

So, choosing a rectangular shape shape = "rect" for our area in the form of a square button, we specified the coordinates of its upper-left and lower-right pixels .. which are quite enough to designate the "working" area of ​​the entire button.

Polygon (polygon).

Let's deal with the "yellow button", we have it represented in the form of a triangle. In order to select its "working" area from our drawing, we assign to the attribute shape meaning poly- a polygon, which will define this area as a kind of polygon, where the coordinates separated by commas will be its vertices, there can be any number of them "X1, Y1, X2, Y2, X3, Y3, X4, Y4 ... X124, Y124" the figure formed by these points with vertices of corners can have the form of any polygon, both regular and irregular. In our case, there are only three angles, that's why it is a triangle, therefore its coordinates will be set by three pairs of values ​​"X1, Y1, X2, Y2, X3, Y3"

So we write everything together like this:

shape = "poly" coords = "148,15,185,82,110,82">

And here is a picture that clearly shows where the coordinates of the points come from.



Navigation map







shape = "poly" coords = "148,15,185,82,110,82">





Circle

Well, the last "red button" we have it is round .. so the shape of the area will be round shape = "circle"... This time with coordinates, things are a little different. We need three values ​​X, Y, R. X and Y are the coordinates of the center of our circle, and R is the length of the radius in pixels.

Here's a drawing:

Here's an example:



Navigation map








shape = "circle" coords = "250,50,33">





We bring it to mind ..

Now a little about what else it would be desirable to do with our map before "mounting" it to any page.

Define the fixed dimensions of the map drawing by attributes width and height

Let's write an alternative text, both for the entire picture of the map, and for its individual areas using the attribute alt, as well as a description of the elements with the attribute title .

Let's get rid of the border frame .. let's do border = "0".. (well, if you like the border more, you can not do it .. I do not insist ..)

In the end, it should look something like this:



Navigation map



width = "300" height = "100" border = "0" alt = "(! LANG: Control Panel" title="Control Panel"> !}


alt = "(! LANG: Green button" title="Green button"> !}
alt = "(! LANG: Yellow button" title="Yellow button"> !}
alt = "(! LANG: Red button" title="Red button"> !}





Crossing areas

Sometimes it is convenient to form the "working" area of ​​the image by "mixing" different shapes.

Suppose our next button looks like this:

Of course, you can define the shape as a polygon, but you have to set a lot of coordinates for the rounded part of such a button (well, this is if you need special accuracy in the navigation map).

And you can define in this example two rectangle shapes rect and circle circle as it shown on the picture:

And in the code, specify the path to the same document:



Navigation map













"Not an area"

Let's look at an example .. suppose you need to make such a button:

What about the hole in it?

Tag besides the attribute href has the opposite attribute nohref- an inactive area, that is, if the user clicks on such an area, then absolutely nothing will happen, which is what we actually need to achieve when making a "hole" in our map.

The map will be defined by two areas, an inactive circle circle and active rectangular area rect and, as shown in the drawing, have the following coordinates:

As mentioned earlier, when crossing areas, the area that is inside the tag in the code will have the highest priority. listed first

Therefore, the example will look like this:



Navigation map






nohref shape = "circle" coords = "76,74,35" title = "(! LANG: hole"> !}






The map is on the server.

Excerpt from lookup (attributes of the tag ):

usemap- the image is a client side navigation map.
ismap- the image is a navigation map on the server.

Unclear? Then we read ..

With attribute usemap we seem to have figured it out .. Look, the user (client), having opened your page, simultaneously with all the rest of the content, loads "on his side" both the picture itself and the navigation map to it, and the whole thing is processed by his browser.

And here is the attribute ismap tag tells the browser that there is a navigation map for this image on the server, well, where you posted or are going to publish your site (read the article :). And now, when a visitor (client) clicks on any area of ​​the picture with such an attribute, the browser will remember the coordinates of this click and send them to the server, where a special program will process this data and redirect the user to the address indicated in the map on the server, in accordance with with the coordinates of the point received from the client's browser.

It is written like this:

Where the address of the navigation map on any site .. hosted on a particular server ..

Isn't it clear anyway? If yes, then don't bother about it .. use usemap, in my opinion, such a solution will be the best in most cases when using navigation maps.

    To easily determine the coordinates of a point on your navigation map, open the drawing with a graphical editor, the same Paint, for example .. there, when you move the pointer over the drawing, you will see two changing numbers, these are the coordinates of the pixel X and Y in this picture ... In Paint, this bar is located at the bottom of the screen.

    When creating a page with a navigation map, the tag should always be above the card itself That is, write like this:



    It is possible, but not necessary .. because when loading the page, problems may arise, since the map with markup has already been loaded, but the picture itself is not yet present ..

    And as for loading ..

    In this part of the code, there can be anything long text, tables, graphics .. but still it is better not to write anything here



    And write here, because while the page is loading, the user, without waiting for its end, may try to click on the buttons indicated in the navigation map, which by this time has not yet loaded ..

Hi. Recently I happened to deal with such a specific html feature as an image map. To be honest, I didn't often use it, and then, usually, everything was done with rectangular zones. But this was not the case. The task was to post links to individual regions of the image, which was the map of the country, and, unfortunately, there was no question of any canvases or svg. Only html only hardcore! So, the task is set, Google is activated, and you can start.

Theory

Let's start with a theory, where can we go without it. An image map contains two tags: map- card container and area- selection area. The map is not limited to one zone and can contain an unlimited number of them. Tag area besides the standard attributes, it has its own:
  • coords- coordinates of the selection area
  • href- the link to which the transition will be made when clicking on the zone
  • nohref- indicates that the zone contains no links
  • shape- form of selection
    • circle- selection area in the form of a circle
    • default- selects the entire image area
    • poly- selection area in the form of a polygon
    • rect- selection area in the form of a rectangle
  • target- defines where the link will open
To connect a map to an image, specify the tag map attribute name with an arbitrary name, and we hang a tag on the images usemap, the value of which is specified in the format "#name".

Since the selection area I had to be polygonal, the value of the shape attribute, area tag, we specify as poly - a polygonal area. In this mode, comma-separated coordinates of the point relative to the upper left corner - x, y. Periods are also separated by commas, which is confusing at first when reading such code.

Writing Paint

I was not amused by the prospect of finding the coordinates of each point with Photoshop, manually rewriting the numbers from the Info window, and the tools that I came across in Google were too monstrous. It was decided on my knee to write my own small script that would allow you to place points simply by clicking on the desired area on the image, and would display the finished code.

First, let's prepare the layout:


Buttons will be inserted into #bar to control the "pint".
The generated html code will be displayed in #info.

Body (margin: 0; padding: 20px; font-family: Arial, Helvetica, sans-serif;) img (border: none; outline: none; display: block; -moz-user-select: none; -webkit-user -select: none; user-select: none;) .canvas (border: 2px solid # 333; padding: 2px; margin-bottom: 16px; display: inline-block; // display: inline; // zoom: 1; ) .canvas.draw (border-color: # 3C0;) .canvas .inner (position: relative;) .canvas .point (width: 1px; height: 1px; background-color: #fff; border: 1px solid # 000 ; overflow: hidden; position: absolute;) .bar (margin-bottom: 16px;) .info (background-color: #FCFCFC; border: 1px dotted #CCC; font-size: 12px; font-style: italic; padding : 8px; word-wrap: break-word;)
In javascript, everything is quite simple. In the process of writing, I used my combat library, so do not be surprised at non-standard functions. First, let's hang up the mousedown event on #canvas, in which the point on the image will be rendered and its coordinates will be recorded.

Var addPoint = function (e) (var e = _.getEvent (e), offset = _.getOffset (nodes ["canvas"]), x = e.clientX + _.getDocScrollLeft () - offset, y = e. clientY + _.getDocScrollTop () - offset, node = nodes ["canvas"]. appendChild (_. node ("div", ("class": "point"))); node.style.top = y-1 + "px"; node.style.left = x-1 + "px"; points.push (("x": x, "y": y, "node": node)); e.preventDefault && e. preventDefault (); return false;);
Then we will write a function that will generate the html code of our map.

Var renderInfo = function () (var text; _.clearNode (nodes ["info"]); nodes ["info"]. AppendChild (_. Node ("span", " ")); nodes [" info "]. appendChild (_. node (" br ")); for (var i = 0, l = areas.length; i< l; i++){ if(areas[i].length >0) (text = " 0) (text + = ",";) text + = areas [i] ["x"] + "," + areas [i] ["y"]; ) text + = ""> "; nodes [" info "]. appendChild (_. node (" span ", text)); nodes [" info "]. appendChild (_. node (" br "));) ) nodes ["info"]. appendChild (_. node ("span", "")); };
Let's frame everything into a class, some helper functions, that's all. I hope sowing tools will be useful to someone.

HTML tags

Meaning and application

Tag used to define a map image. A map image is an image with a defined active area. Element contains a number tags that define the interactive areas in the map image, i.e. when you click on a certain area of ​​the image, certain actions occur, for example, a separate page opens with a description of this area of ​​the image.

Browser support

Tag
Opera

IExplorer

Edge
YesYesYesYesYesYes

Attributes

Tag attributes we specify both the coordinates of the area (the coords attribute) and the type of shape we need (the shape attribute):



Usage example

Let's consider an example in which when you click on a certain shape in one figure, you go to different web pages that describe these shapes (links to Wikipedia):

</span> Example using the tag <map>

Choose a shape:

"There are 4 shapes available for selection"
> <span"Red Square"> coords = "200,75,50" href = "green.html" alt = "(! LANG: Green circle." > !} <span"Blue Triangle"> "450,25,435,60,400,75,435,90,450,125,465,90,500,75,465,60" href = "yellow.html" alt = "(! LANG: Yellow star" > !}

And so, in order, what we did in this example:

I draw your attention to the fact that if the tag

Let's see an example of what an image map looks like in html:

When you hover the mouse over the area of ​​the green rectangle, you can see that it is a link to "#green_link" (the label is selected for example, you can make a link to any page on the Internet). The red square has "#red_link", and the blue circle, respectively, "#blue_link".

The code for this example:

img / primer-kartu-izobrazheniy-1.jpg"> !}

As you can see from the example, the code is not that complicated. Let's take a look at the tags for creating an image link map.

Description of the example

1. It is necessary to create a bunch of images and maps ... In the image, you need to reference the map using the usemap = "# primer1" attribute. And below you need to describe the card code.

When creating a linkmap, the usemap attribute is required for the img tag.

2. Description of the card consists of the required attribute name, which will be used to bind to the image.

3. Each element of the map is described using a tag , which specifies the type of object using the shape attribute (rectangle, circle, or polygon) and its coordinates.

Note

Areas can overlap each other. In this case, the link will lead to the object that is described last.

Tag attributes

1. Attribute shape = "object_type" - sets the object type. It can take the following values:

  • circle - circle;
  • rect - rectangle;
  • poly - polygon;

2. Attribute coords = "coordinate_values"- defines the geometric location of the object and its dimensions.

The image reference point is the upper left corner. Those. if you specified a padding of 10 in height, it means 10 pixels down.

Depending on the type of object, you need to set coordinate values ​​in different formats. All values ​​are specified in pixels (you do not need to write the px mark).

  • For type circle: coords = (x, y, r), where x, y are the coordinates of the center of the circle, and r is the radius of the circle;
  • For the rect type: coords = (x1, y1, x2, y2), where x1, y1 are the coordinates of the upper left point of the rectangle, x2, y2 are the coordinates of the lower right point of the rectangle;
  • For type poly: coords = (x1, y1, x2, y2, ..., xn, yn), the x, y coordinates of each point of the polygon are sequentially indicated;

3. Attribute href = "hop_address"- sets the link for the transition (similarly). In addition to the jump address, you can use JavaScript functions to perform some action.

4. Attribute target = "value" - similar to the tag defines the action of following a link. Can take values:

  • _blank - opens the page in a new window
  • _self - loads the page into the current window
  • _parent - loads the page into the parent frame
  • _top - cancels all frames and loads the page in full browser window

5. Attribute title = "(! LANG: hint" - выводит всплывающую подсказку. можно также сделать более красивую всплывающую подсказку: как сделать красивую всплывающую подсказку для ссылки »!}

6. Attribute nohref - makes the area inactive. Used when overlapping objects. It is rarely used, but sometimes it can be an indispensable solution. For example, you can make a small circle within a large circle inactive.

The nohref area must go first.

Sample code:

The use of a map of links in the image takes place only in a narrow range of tasks. For example, when creating some kind of scheme or driving map. In other cases, using a map to create menus and other graphic elements looks unreasonable.

HTML images are added to web pages using a tag ... The use of graphics makes web pages more visually appealing. Images help to better convey the essence and content of a web document.

Using HTML tags and you can create image maps with active areas.

Inserting images into an HTML document

1. Tag

Element represents the image and its fallback content, which is added using the alt attribute. Since the element is inline, it is recommended to place it inside a block element, for example,

Or

.

Tag has the required src attribute, the value of which is the absolute or relative path to the image:

For tag the following attributes are available:

Table 1. Tag Attributes
Attribute Description, accepted value
alt The alt attribute adds alternative text for the image. It is displayed at the place where the image appears before it is loaded or when the graphics are disabled, and is also displayed as a tooltip when you hover the mouse cursor over the image.
Syntax: alt = "(! LANG: image description" . !}
crossorigin The crossorigin attribute allows you to load images from resources on a different domain using CORS requests. Images uploaded to the canvas using CORS requests can be reused. Allowed values:
anonymous - The cross-origin request is made using an HTTP header and no credentials are passed. If the server does not provide credentials to the server from which the content is requested, the image will be corrupted and its use will be restricted.
use-credentials - Cross-origin request is performed with credentials passed.
Syntax: crossorigin = "anonymous".
height The height attribute sets the height of the image in px.
Syntax: height = "300".
ismap The ismap attribute indicates that the picture is part of a map-image located on the server (map-image is an image with clickable areas). When you click on the map image, the coordinates are sent to the server as a URL query string. The ismap attribute is only allowed if the element is a descendant of the element with a valid href attribute.
Syntax: ismap.
longdesc The URL for an extended description of the image, in addition to the alt attribute.
Syntax: longdesc = "http://www.example.com/description.txt".
src The src attribute specifies the path to the image.
Syntax: src = "flower.jpg".
sizes Sets the size of the image depending on the display parameters. Works only when the srcset attribute is specified. The attribute value is one or more strings separated by commas.
srcset Creates a list of image sources to be selected based on screen resolution. Can be used in conjunction with or instead of the src attribute. The attribute value is one or more lines separated by commas.
usemap The usemap attribute specifies the image as an image map. The value must start with a # character. The value is associated with the value of the name or id attribute of the tag and creates a link between the elements and ... The attribute cannot be used if the element is a descendant of the element or
width The width attribute sets the width of the image in px.
Syntax: width = "500".

1.1. Image address

The image address can be specified in full (absolute URL), for example:
url (http://anysite.ru/images/anyphoto.png)

Or via a relative path from document or root directory site:
url (../ images / anyphoto.png) - relative path from the document,
url (/images/anyphoto.png) is the relative path from the root directory.

This is interpreted as follows:
../ - means to go up one level, to the root directory,
images / - go to the folder with images,
anyphoto.png - Points to an image file.

1.2. Image dimensions

Without setting the image size, the drawing is displayed on the page in real size. You can edit the dimensions of the image using the width and height attributes. If only one of the attributes is set, the second will be calculated automatically to maintain the proportions of the picture.

1.3. Graphics file formats

JPEG format (Joint Photographic Experts Group). JPEG images ideal for photographs, they can contain millions of different colors. Compresses images better than GIFs, but text and large areas of solid color can become smudged.

GIF format (Graphics Interchange Format)... Ideal for compressing images that have solid color areas, such as logos. GIFs let you set one of the colors to be transparent so that the background of a web page can appear through part of the image. GIFs can also include simple animations. GIFs contain only 256 shades, making the images appear blotchy and unrealistic like posters.

PNG format (Portable Network Graphics)... Includes the best features of GIF and JPEG formats. Contains 256 colors and makes it possible to make one of the colors transparent, while compressing images into smaller size than a GIF file.

APNG format (Animated Portable Network Graphics)... Image format based on PNG format... Allows animation to be stored and also supports transparency.

SVG format (Scalable Vector Graphics)... An SVG drawing consists of a set of geometric shapes described in XML format: line, ellipse, polygon, etc. Both static and animated graphics are supported. The set of functions includes various transformations, alpha masks, filter effects, the ability to use templates. SVG images can be resized without quality degradation.

BMP format (Bitmap Picture)... Uncompressed (original) bitmap whose template is a rectangular grid of pixels. A bitmap file consists of a header, palette, and graphic data. The header contains information about the graphic image and the file (pixel depth, height, width and number of colors). The palette is specified only in those Bitmap files that contain palette images (8 or less bits) and consist of no more than 256 elements. The graphical data represents the picture itself. The color depth in this format can be 1, 2, 4, 8, 16, 24, 32, 48 bits per pixel.

ICO format (Windows icon)... File icon storage format in Microsoft Windows... Also, Windows icon is used as an icon on sites on the Internet. It is a picture of this format that is displayed next to the site address or bookmark in the browser. One ICO file contains one or several icons, the size and color of each of which is set separately. The size of the icon can be any, but the most common icons are square icons with sides of 16, 32 and 48 pixels.

2. Tag

Tag serves to represent graphic image in the form of a map with hotspots. Hot spots are determined by changing the appearance of the mouse cursor on hover. By clicking on hotspots, the user can navigate to linked documents.

The name attribute is available for the tag, which specifies the name of the map. The value of the name attribute for the tag must match the name in the element's usemap attribute :

...

Element contains a number of elements that define the interactive areas in the map display.

3. Tag

Tag describes only one hotspot within a client-side image map. The element has no end tag. If one hotspot overlaps another, then the first link from the list of scopes will be implemented.

Table 2. Attributes of the tag
Attribute Short description
alt Specifies alternative text for the active area of ​​the map.
coords Determines the position of the area on the screen. Coordinates are specified in length units and separated by commas:
for circle- coordinates of the center and radius of the circle;
for rectangle- coordinates of the upper left and right lower corners;
for polygon- the coordinates of the vertices of the polygon in the desired order, it is also recommended to indicate the last coordinates, equal to the first, for the logical completion of the figure.
download Complements the href attribute and tells the browser that the resource should be loaded the moment the user clicks on the link, instead of, for example, pre-opening it (like a PDF file). By giving a name to the attribute, we are thus giving a name to the loaded object. It is allowed to use the attribute without specifying its value.
href Specifies the URL for the link. An absolute or relative link can be specified.
hreflang Specifies the language of the linked web document. Used only in conjunction with the href attribute. Accepted values ​​are an abbreviation consisting of a pair of letters denoting a language code.
media Determines what types of devices the file will be optimized for. The value can be any media query.
rel Supplements the href attribute with information about the relationship between the current document and the linked document. Accepted values:
alternate - a link to an alternate version of the document (for example, page printable, translation, or mirror).
author - link to the author of the document.
bookmark is a persistent url used for bookmarks.
help - link to help.
license - a link to the copyright information for this web document.
next / prev - indicates the relationship between individual URLs. Thanks to this markup, Google can determine that the content of these pages is linked in a logical sequence.
nofollow - prohibits search engine follow the links on this page or a specific link.
noreferrer - indicates that following a link, the browser should not send an HTTP request header (Referrer), which records information about which page the site visitor came from.
prefetch - indicates that the target document should be cached, i.e. the browser in the background loads the content of the page to its cache.
search - Indicates that the target document contains a search tool.
tag - indicates keyword for the current document.
shape Sets the shape of the hotspot on the map and its coordinates. It can take the following values:
rect - active area of ​​rectangular shape;
circle - active area in the form of a circle;
poly - active area in the form of a polygon;
default - the active area occupies the entire image area.
target Indicates where the document will be loaded when following the link. It takes the following values:
_self - the page is loaded into the current window;
_blank - the page opens in a new browser window;
_parent - the page is loaded into the parent frame;
_top - the page is loaded into the full browser window.
type Specifies the MIME type of the referenced files, i.e. file extension.

4. An example of creating an image map

1) We mark the original image on the active areas of the desired shape. The coordinates of the areas can be calculated using a photo processing program, for example, Adobe photoshop or Paint.

Rice. 1. An example of image markup for creating a map

2) Set the name of the map by adding it to the tag using the name attribute. We assign the same value to the usemap attribute of the tag .

Jpg "alt =" (! LANG: flowers_foto" width="680" height="383" usemap="#flowers"> !} gerbera hyacinth camomiles narcissus tulip
Rice. 2. An example of creating a map-image, when you press the mouse cursor on a flower, you go to a page with a description