What tag specifies the insertion. We add images to the WEB page, as well as video and audio! Set the dimensions of the image

Before answering the question " how to insert a picture in HTML?“, it should be noted that it is not worth overloading web pages with a huge amount of graphic material, since this will not only improve the visual perception of the resource by the user, but will also increase the page loading time.

When creating websites, the most commonly used graphic formats are PNG, GIF and JPEG, and for design work with images - graphics editor Adobe Photoshop, which has rich capabilities for compressing and resizing images without losing quality, which is incredibly important for web development.

How to insert an image in HTML?

To insert an image into an HTML page, use a single simple tag:

,

where xxx is the image address. If the image is in the same directory as the page, the tag will look like:

However, high-speed and stable Internet has not yet reached all corners of the globe, and it happens that the image on the website simply does not load. For such cases, there is the concept of alternative text.

It is shown in place of the picture when it is unavailable, loading, or in the browser operating mode “without pictures”. It is added using alt attribute tag .

Example of adding alternative text to a graphic file:

Alternative text

Assigning image sizes in HTML

In order to change the display dimensions of a graphic file, use the height and width tags, where height is the height and width is the width, measured in pixels.

When using these attributes, the browser first allocates space for graphic content, prepares the overall page layout, displays text, and then loads the image itself.

The drawing is placed in a rectangle with given dimensions, and if the parameters are smaller or larger than the original ones, the picture is stretched or compressed.

If the height and width attributes are not used, the browser loads the image immediately, delaying the display of text and other page elements.

These parameters can be specified both in pixels (the size of the picture is constant and does not depend on the user’s screen resolution) and in percentages (the size of the picture depends on the screen resolution).

For example:

It should be remembered that at the moment when you change the original size of the image, it is necessary to maintain its proportions.

To do this, it is enough to specify the value of only one of the parameters ( width or height), and the browser will calculate the value of the second one automatically.

Image location in HTML

As with many HTML tags, Apply the align attribute, which aligns the image:

- the picture is located above the text;

- the picture is located below the text;

- the picture is located to the left of the text;

- the picture is located to the right of the text.

Picture link

This is done as follows:

As you can see, a graphic insert can be a link and, when clicked, redirect to any address written in full or abbreviated form.

How can I make an image a background in HTML?

The image can not only be inserted onto the page as a visible object, but also made into the background. To define an image as a background, you need to specify the background=”xxx” attribute in the tag, where xxx is the address of the image, specified in the same way as in the examples above.

For example, let's set the following texture image as a background image:

Save the image in a folder with a prepared page and write the following lines:

Page with <a href="https://zhumor.ru/en/internet/kak-zadat-fon-bez-povtoreniya-v-css-background-v-css-color-position-image-repeat.html">background picture</a></head>

Background with text.

The background image on the page is set.

Hello dear blog readers! In this article you will learn everything about how to insert an image into an html page. Do you have several images that you want to put on your page or do you want to put a logo on your site? All this is easy. After reading this article, you will be able to insert pictures into your html pages without any difficulties. To do this, we will talk in detail about img tag and its attributes, we'll take a quick look at graphic file formats such as gif, jpeg and png, and also look at new HTML5 features that make it easier to insert video and audio into your site.

Due to the fact that graphic data and html text cannot be combined in one file; to display them on the site, a different approach is used than with other elements of html pages. First of all, graphic images, and other multimedia data is stored in separate files. And to implement them into a web page, they use special tags that contain links to these separate files. In particular, such a tag is img tag. Having encountered such a tag with an address, the browser first requests the corresponding file with an image, audio or video from the Web server, and only then displays it on the Web page.

All graphic images and, in general, any data that is stored in files separate from the web page are called implemented page elements.

Before inserting pictures and looking at the “img” tag in detail, it’s worth learning a little about graphic formats.

Graphic image formats.

There are many different graphic formats, but browsers only support a few. Let's look at three of them.

1. JPEG format(Joint Photographic Experts Group). Quite a popular format used for storing images. Supports 24-bit color and keeps all halftones in photos unchanged. But jpeg does not support transparency and distorts small details and text in images. JPEG is used primarily for storing photographs. Files in this format have the extensions jpg, jpe, jpeg.

2. GIF format(Graphics Interchange Format). The main advantage of this format is the ability to store several images at once in one file. This allows you to create entire animated videos. Secondly, it supports transparency. The main drawback is that it supports only 256 colors, which is not suitable for storing photos. GIF is mainly used to store logos, banners, images with transparent areas and containing text. Files in this format have the extension gif.

3. PNG format(Portable Network Graphics). This format was developed as a replacement for the legacy GIF and, to some extent, JPEG. Supports transparency, but does not allow animation. This format has a png extension.

When creating websites, they usually use images in JPEG or GIF format, but sometimes they use PNG. The main thing is to understand in which cases which format is better to use. In short:

    JPEG is best used for storing photographs or grayscale images that do not contain text;

  • GIF is used primarily for animation;
  • PNG is the format for everything else (icons, buttons, etc.).

Inserting images into html pages

So, how do you insert an image onto a web page? You can insert an image using a single img tag. The browser places the image at the location on the web page where it encounters the img tag.

Code for inserting images into html the page looks like this:

This html code will place on the web page an image stored in the image.jpg file, which is located in the same folder as the web page. As you may have noticed, the address of the picture is indicated in src attribute. I already told you what it is. So, the src attribute is a required attribute that serves to indicate the address of the file with the image. Without the src attribute, the img tag is meaningless.

Here are a few more examples of specifying the address of a file with an image:

This html code will insert an image on the page called image.jpg, which is stored in the images folder located at the root of the website.

The src attribute can contain more than just relative links to images. Since images are stored online along with html pages, so each image file has its own url. Therefore, you can insert the image url into the src attribute. For example:

This code will insert an image from the site mysite.ru onto the page. A URL is typically used when you are pointing to an image on another site. For images stored on your site, it is better to use relative links.

The img tag is an inline element, so it is better to place it inside a block element, for example inside the “P” tag - paragraph:

Let's practice and paste into our page from previous articles about html image. I will create an “images” folder next to the html file of my page and place a “bmw.jpg” image file there, which looks like this:

Then the html code of the page with the inserted image will be like this:

And look at the display result in the browser:

As we see, there is nothing complicated about placing images on web pages. Next, let's look at a few other important attributes of the "img" tag.

The alt attribute is a fallback option

Because image files are stored separately from web pages, the browser has to make separate requests to retrieve them. But what if there are a lot of images on the page and the network connection speed is low, then loading additional files it will take considerable time. And it’s even worse if the image was deleted from the server without your knowledge.

In these cases, the web page itself will load successfully, only white rectangles will be displayed instead of images. Therefore, to tell the user what the image is, . Using this attribute, you specify the so-called replacement text, which will be displayed in an empty rectangle until the image is loaded:

And this is roughly what it looks like:

Set the dimensions of the image

There are still a couple of img tag attributes you should know about. This is a couple of attributes width And height. You can use these to specify the image dimensions:

Both attributes indicate the size in pixels. The width attribute tells the browser how wide the image should be, and the height attribute how tall it should be. These two attributes can be used together or separately. For example, if you specify only the width attribute, then the browser will automatically select the height in proportion to the specified width and also in the case of using only the height attribute. If you do not specify these attributes at all, the browser will automatically determine the size of the image before displaying it on the screen. It is only worth noting that specifying the image sizes will slightly speed up the browser when displaying the page.

That’s all about inserting images onto pages for now, then we’ll look at how to insert audio or video onto a website...

Inserting video and audio using HTML 5

The new html5 specification introduces several new tags that make it very easy to embed media files. This primarily applies to video and audio.

To insert audio HTML5 provides paired tag AUDIO. The address of the file in which the audio clip is stored is indicated using the src attribute that is already familiar to us:

The “audio” tag creates a block element on the page. By default, the browser will not play the audio clip. For it to do this, you need to specify a special attribute in the audio tag autoplay. Special because it doesn't matter. The mere presence of this attribute in the tag is enough for it to take effect.

By default, the audio clip is not displayed on the web page. But if you put an attribute without a value in the “audio” tag controls, the browser will display controls for playing the audio clip in the place on the web page where the audio tag is placed. They include a play/pause button, a playback bar, and a volume control.

the body of the table is located. The body consists of rows and columns. The table is filled in line by line.

Each tag creates a new line. Further in nested columns are created. You can create multiple columns. In this case, you need to monitor the number of columns in each row. For example, if the first row had 5 columns, then the following rows should also have 5 columns. Otherwise the table will float. It is possible to merge cells.

How to make a table in html

Let's give an example, html code:

Example table
Column 1 Column 2

Pay attention to the cell . We use the special colspan attribute to span cells horizontally. Its numeric value indicates the number of columns to be merged. There is also an analogue of this attribute: tag (table header), where you also need to enter colspan. The result will be the same. But often they use regular td.

Now let's take a closer look at all the tag attributes

.

Tag Attributes and Properties

To opening tag

You can specify various attributes.

1. Property align="parameter" - sets the table alignment. Can take the following values:

In the example above, we aligned the table to the center align="center" .

This attribute can be applied not only to the table, but also to individual table cells

. Thus, the alignment will be different in different cells.

For example

, , , or
  • cols - line is displayed between columns
  • none - all borders are hidden
  • rows - a border is drawn between table rows created through the tag
  • 12. Property width="number" - sets the width of the table: either in pixels or in percentages.

    13. Property class="class_name" - you can specify the name of the class to which the table belongs.

    14. Property style="styles" - styles can be set individually for each table.

    Now it's time to dive inside the table and look at the attributes of the table cells. These attributes should be written in the opening tag

    And The same options are available as for will be hierarchically applied to all
    or lines
    ... ... ...

    2. Property background="URL" - sets the background image. Instead of the URL, the address of the background image should be written.

    Example

    Example table
    Column 1 Column 2

    Converts to the following on the page:

    In the example above, our background image is located in the img folder (which is in the same directory as the html page), and the image is called fon.gif. Please note that in the tag we added style="color:white;" . Since the background is almost black, to prevent the text from blending into the background, we made the text white.

    3. Property bgcolor="color" - sets the background color of the table. You can choose any color from the entire palette (see codes and names of html colors)

    4. Property border="number" - sets the thickness of the table border. In previous examples, we specified border="1" , which means the border thickness is 1 pixel.

    5. Property bordercolor="color" - sets the color of the border. If border="0" then there will be no border and the border color will have no meaning.

    6. Property cellpadding="number" - indent from the frame to the cell contents in pixels.

    7. Property cellspacing="number" - the distance between cells in pixels.

    8. Property cols="number" - the number of columns. If you do not set it, the browser itself will determine the number of columns. The only difference is that specifying this parameter will most likely speed up the loading of the table.

    9. Property frame="parameter" - how to display borders around the table. Can take the following values:

    • void - do not draw borders
    • border - border around the table
    • above - border along the top edge of the table
    • below - border at the bottom of the table
    • hsides - add only horizontal borders (top and bottom of table)
    • vsides - draw only vertical borders (to the left and right of the table)
    • rhs - border only on the right side of the table
    • lhs - border only on the left side of the table

    10. Property height="number" - sets the height of the table: either in pixels or in percentages.

    11. Property rules="parameter" - where to display borders between cells. Can take the following values:

    • all - a line is drawn around each table cell
    • groups - a line is displayed between groups formed by tags
    .

    Attributes and Properties

    1. Property align="parameter" - sets the alignment of an individual table cell. Can take the following values:

    • left - left alignment
    • center - center alignment
    • right - right alignment

    2. Property background="URL" - sets the background image of the cell. Instead of the URL, the address of the background image should be written.

    3. Property bgcolor="color" - sets the background color of the cell.

    4. Property bordercolor="color" - sets the color of the cell border.

    5. Property char="letter" - specifies the letter from which the alignment should be made. The value of the align attribute must be set to char.

    6. Property colspan="number" - sets the number of horizontal cells to be merged.

    7. Property height="number" - sets the height of the table: either in pixels or as a percentage.

    8. Property width="number" - sets the width of the table: either in pixels or as a percentage.

    9. Property rowspan="number" - sets the number of vertical cells to be merged.

    10. Property valign="parameter" - vertical alignment of the cell contents.

    • top - align cell contents to the top edge of the row
    • middle - middle alignment
    • bottom - alignment to the bottom edge
    • baseline - alignment to the baseline
    Note 1

    For tag

    . Parameters for one tag
    inside him

    How to prevent cell borders in a table from sticking together

    If you use a border (cell border) and zero padding between cells, they are still stuck together and you get a double border . To avoid this, you need to specify border-collapse: collapse in the table styles:

    ...

    Dear reader, now you have learned much more about html tag table. Now I advise you to move on to the next lesson.

    To insert images in HTML The two main formats used are GIF and JPEG. The GIF format can store simple animation(dynamic banners), JPEG is great for images with big amount flowers, such as photographs. The third format for web graphics is PNG format, but it is not widely used in web design. Any image in GIF or JPEG formats is inserted onto a web page using the tag < img > , there is no closing tag.

    SRC attribute

    Via attribute src the address (URL) of the image file is specified, i.e. the browser finds the desired image in the site directory using the path (URL) specified in this attribute. For convenience, all site images are located in a separate folder, usually named image. For example, take any image, preferably a small format, and save it in the created folder image, with the name primer.jpg. Next we will refer to it for training.

    Well, let's try to insert a picture on the page? We write the code (path - URL, written depending on the location of the folder with images):

    < img src="image/primer.jpg">

    Alternative text. ALT attribute

    Why is alternative text specified? Let's go in order. Every browser has a function to disable images, so a user using this function can see the description specified in the tag alt, which represents the image. This may not be relevant today, since preference is given to unlimited tariffs, but still this is considered a rule of good manners. Let's look at the code example:

    You can see this only by turning off the display of images in the browser.

    Set the size. WIDTH and HEIGHT attributes

    Let me note right away that width and height are very important parameters, so you shouldn’t ignore them. Judge for yourself, with unspecified image sizes, and with images disabled in the browser, when images do not load, small empty squares may be located in a completely different place from where you planned to place them. This will not add beauty to the page. Therefore, do not be lazy and always write down the dimensions in the HTML code.

    Image sizes are set using tags width- width and height— height, values ​​are set both in pixels and as a percentage of the screen width (with percentages, be very careful). Let's look at the code:

    Conclusion

    Images attract site visitors and carry a lot of visual information, are easy to use, but you still need to follow some recommendations if you want to create a good, popular website.

    You should not use too large image file sizes, as this will affect page loading speed. Believe me, this is far from the last indicator.

    The ALT attribute has a very important, you must add it for each IMG tag. Content text message must describe the image very accurately, and briefly.

    Images on a web page must correspond to the text content.

    After studying this section, you can already try yourself as a webmaster and create a full-fledged HTML page. And then let's move on to inserting multimedia.

    Internet