Basics of html and css programming for beginners. Basics of the WEB programming language HTML

HTML Basics contains the basic rules of the HTML language, a description of the structure of an HTML page, and the relationships in the structure of an HTML document between HTML elements.

An HTML document is a regular text document that can be created either in a regular text editor (Notepad) or in a specialized one with code highlighting (Notepad++, Visual Studio Code, etc.). An HTML document has a .html extension.

An HTML document consists of a tree of HTML elements and text. Each element is identified in the source document by a start (opening) and an ending (closing) tag (with rare exceptions).

The start tag shows where the element begins, the end tag shows where it ends. The closing tag is formed by adding a slash / before the tag name: .... Between the start and closing tags is the content of the tag - the content.

Single tags cannot store content directly; it is written as an attribute value, for example, a tag will create a button with the text Button inside.

Tags can be nested inside each other, for example,

Text

. When investing, you should follow the order in which they are closed (the “matryoshka” principle), for example, the following entry will be incorrect:

Text

.

HTML elements can have attributes (global, applied to all HTML elements, and their own). Attributes are written in the opening tag of the element and contain a name and value, specified in the format attribute name="value" . Attributes allow you to change the properties and behavior of the element for which they are set.

Each element can be assigned multiple class values ​​and only one id value. Multiple class values ​​are written separated by a space, . The class and id values ​​must consist only of letters, numbers, hyphens, and underscores, and must begin with only letters or numbers.

The browser views (interprets) the HTML document, building its structure (DOM) and displaying it in accordance with the instructions included in this file (style sheets, scripts). If the markup is correct, the browser window will display an HTML page containing HTML elements - headers, tables, images, etc.

The interpretation process (parsing) begins before the web page is fully loaded into the browser. Browsers process HTML documents sequentially, from the beginning, while processing CSS and relating style sheets to page elements.

An HTML document consists of two sections - the header - between the tags ... and the content part - between the tags ....

Web page structure 1. HTML document structure

HTML follows the rules contained in the document type declaration file (Document Type Definition, or DTD). A DTD is an XML document that defines which tags, attributes, and their values ​​are valid for a particular HTML type. Each version of HTML has its own DTD.

DOCTYPE is responsible for the correct display of a web page by the browser. DOCTYPE specifies not only the HTML version (eg html) but also the corresponding DTD file on the Internet.

...

The elements inside the tag form a document tree, the so-called document object model, DOM (document object model). In this case, the element is the root element.


Rice. 1. The simplest structure of a web page

To understand the interaction of web page elements, it is necessary to consider the so-called “family relationships” between elements. Relationships between multiple nested elements are classified as parent, child, and sister.

An ancestor is an element that contains other elements. In Figure 1, the ancestor for all elements is . At the same time, the element is the ancestor of all the tags it contains: ,

, , etc.

A descendant is an element located within one or more element types. For example, is a descendant of , and the element

Is a descendant of both and .

A parent element is an element that is related to other elements at a lower level and is located above them in the tree. In Figure 1 and . Tag

Is parent only to .

A child element is an element that is directly subordinate to another element at a higher level. In Figure 1 there are only elements , ,

And are children of .

A sister element is an element that has a common parent element with the one in question, the so-called elements of the same level. In Figure 1 and are elements of the same level, as well as elements , and

They are sisters to each other.

1.1. Element 1.2. Element

Section ... contains technical information about the page: title, description, keywords for search engines, encoding, etc. The information you enter there is not displayed in the browser window, but it contains information that tells the browser how to process the page.

1.2.1. Element

The required section tag is . The text placed inside this tag appears in the title bar of the web browser. The title should be no more than 60 characters long to fit completely in the title. The title text should contain the most complete description of the content of the web page.

1.2.2. Element

An optional section tag is a single tag. With its help, you can set a description of the page content and keywords for search engines, the author of the HTML document, and other metadata properties. An element can contain multiple elements because they carry different information depending on the attributes used.

The description of the page content and keywords can be simultaneously specified in several languages, for example, in Russian and English:

Using a tag, you can block or allow indexing of a web page by search engines:

To automatically reload the page after a specified period of time, you need to use the refresh value:

The page will be reloaded after 30 seconds. To redirect the visitor to another page, you need to specify the URL in the url parameter:

Table 2. Tag attributes Attribute
charset Specifies the character encoding for the current HTML document:
content Contains arbitrary text that specifies the value associated with the http-equiv or name attribute, depending on their value.
http-equiv Controls browser actions on a given web page (equivalent to HTTP headers). When rendering the page, the browser will follow the instructions specified in the attribute:
default-style specifies the preferred style to use on the page. The content attribute must contain the ID of the element that references the CSS style sheet, or the ID of the element that contains the style sheet.
refresh indicates the time in seconds before the page is reloaded or the time before redirecting to another page if the content attribute contains the line "url=page_address" after the time.
Automatically reload the page after a specified period of time, in this example, after 30 seconds:

If you need to immediately redirect the visitor to another page, you can specify the URL in the url parameter:
name Associated with the value contained in the content attribute. Should not be used if the element already has the http-equiv , charset , or itemprop attributes set.
application-name specifies the name of the web application used on the page.
author specifies the name of the document's author in free format.
description defines a short description of the page content, for example:

generator specifies one of the software packages used to create the document, for example:
.
keywords contains a comma-separated list of keywords that match the content of the page, for example:
.
Also, the name attribute can take the following values ​​from the extended specification, such as creator, googlebot, publisher, robots, slurp, viewport, although none of these have been officially adopted yet.
1.2.3. Element

Inside this element, styles that are used on the page are set. CSS is used to set styles in an HTML document. There may be several such elements on a page.

Inside this element you can write formatting code for both the web page elements themselves and the entire web page.

.paper ( width: 200px; height: 300px; background-color: #ef4444; color: #666666; )

To connect a given style to an element, you need to assign the appropriate name to the element using the class (or id) attribute:

...

CSS code can be embedded directly into the markup element as the value of the style attribute, for example:

1.2.4. Element

You can also set styles for a document using another method - write them in a separate file with the .css extension, for example, style.css.

There are two ways to connect a file with styles to a web page:
via the @import url directive

@import url(style.css);

using the element. The element does not require a closing tag. This element defines the relationship between the current page and other documents. There may be several such elements on a page. The entry will look like this:

Table 4. Tag attributes Attribute Description, accepted value
crossorigin Indicates whether CORS (a browser technology that allows a web page to access resources from another domain) should be used when retrieving an image from a site.
anonymous — the browser automatically adds an Origin header to a cross-domain request, containing the name of the domain from which the request was made. If the server does not respond with the CORS header Access-Control-Allow-Origin: * (or the domain name instead of an asterisk), then loading the image will be blocked.
use-credentials - If the server does not provide credentials using Access-Control-Allow-Credentials: true , then image loading will be blocked.
href The main attribute of the tag, the value is the path to the file with styles.
hreflang Determines the language of the text in the referenced document.
media Specifies the type of device to which the link resource should be applied.
nonce A randomly generated string variable on the server that sets rules for using inline styles to protect content. The attribute value is a text string.
rel The attribute defines the relationship between the current document and the document being referenced.
alternate - a link to the same document, but in a different format (for example, pages for printing, translation, mirror, feed in RSS or Atom format),
.


archives - indicates that the linked document is of historical interest. The link may point to a collection of records, documents, or other materials.
author link to a page about the author of the document or to a page with the author’s contact information.
bookmark is a reference to the article's closest ancestor that is the link, or to the section of the article most closely related to the element if there is no ancestor.
external is used to indicate that the page being linked to is not part of this site.
first specifies a link pointing to the first document in a sequence of documents.
help link to a help document.
icon specifies the path to the icon that will be used for the current document.
last specifies a link pointing to the last document in a sequence of documents.
license A link to the copyright information for a document.
next indicates that this document is part of a series and that the link goes to the next document in that series.

nofollow indicates that the link is not endorsed by the page's author or that the link is of a commercial nature.
noreferrer indicates that the client request header containing the url of the request source should not be passed when following the link.
pingback specifies the address of the pingback server, which allows the blog to automatically notify sites that link to it.
prefetch specifies that the referenced file should be cached in advance.
prev indicates that this document is part of a series and that the link is to a previous document in that series.

search indicates that the referenced document contains an interface for search and related resources.
sidebar indicates that the linked document will be shown in additional browser context, if possible, and some browsers, when you click a hyperlink, open a window to add the link to your bookmarks bar.
stylesheet is a link to an external file that will be used as a style sheet for this document.
tag indicates that the hyperlinked tag applies to this document.
up indicates that the page is part of a hierarchical structure, and that the hyperlink points to a higher level of resource in the structure.
sizes Specifies the size of icons for visual display. The sizes attribute is used only in conjunction with rel="icon" and can take the following values:
widths-height - defines a list of sizes separated by spaces, each size must be in the format - widths-height (icon dimensions are specified in pixels), for example:
;
any - the icon can be scaled to any size.
title Defines the title of a link or the name of a set of alternative style sheets. The attribute value is text.
type Specifies the MIME type of the document being referenced. In this case it takes the value "text/css" .
1.2.5. Element Table 5. Tag attributes Attribute Description, accepted value
async The attribute indicates that the script will run asynchronously with the rest of the page (the script will start executing at the same time the page loads).
charset Defines character encoding
crossorigin Determines whether CORS will be used when loading external scripts (using the src attribute).
anonymous — before loading the script into a cross-domain request, the browser automatically adds the Origin header, without passing access parameters (cookie, X.509 certificate, login/password for basic authentication via HTTP). If the server response does not contain the Access-Control-Allow-Origin: domain name header, the script will not be loaded.
use-credentials — before loading the script into a cross-domain request, the browser automatically adds an Origin header indicating access parameters (cookie, SSL certificate or login/password pairs). If the server response does not contain the Access-Control-Allow-Credentials: true header, the script will not be loaded.
defer Interpretation of scripts is delayed until the document is rendered on the user's device.
nonce Provides security by protecting against cross-site scripting (XSS) attacks. Sets rules for using built-in scripts using nonce values ​​and hashes. During page rendering, the browser calculates hashes for each inline script and compares them with those listed in the CSP. Downloads from resources not included in the “white list” are blocked.
src Indicates the location of the file with the script; the attribute value is the url of the file containing the JavaScript program.
type Used to declare the script language used to compose the content of the tag.
1.3. Element

This section contains all the contents of the document. Available for the element.

Table 5. Tag attributes Attribute Description, accepted value
onafterprint An event that fires after a page is sent for printing or after the print window is closed.
onbeforeprint An event that fires before a page is sent for printing.
onbeforeunload The event is triggered when the visitor initiates a transition to another page or clicks “close window”. Allows you to display a message in the confirmation dialog to let the user know if they want to stay or leave the current page.
onhashchange The event is fired when the hash part of the URL changes, for example when the user goes from example.domain/test.aspx#page1 to example.domain/test.aspx#page2 .
onmessage An event occurs when a message is received through an event source.
onoffline The event is triggered by the browser when the browser determines that the Internet connection has been lost.
online The event is triggered by the browser when the Internet connection has been restored.
onpagehide The event occurs when the user leaves the page through navigation, such as clicking a link, refreshing the page, filling out a form, etc.
onpageshow The event occurs when the user navigates to the web page, after the onload event.
onunload The event is triggered if the page does not load for some reason, or when the browser window is closed.

This article is not intended to be a comprehensive guide to HTML document markup language. It describes the basics of HTML - the basic principles, concepts and definitions of this technology, having mastered which, you can easily move on in the study of HTML coding.

To study the lesson, download the archive with the necessary files.

HTML is a document markup language. The correct pronunciation is HTT.

Have you probably ever worked in the Word document editor or similar office applications? You probably know that this type of editor has rich capabilities for editing text, arranging elements, inserting pictures, etc.

Why, you might ask, write about word processors in an article on HTML? But why. If you figure it out, what is an office editor? This is an application for editing and displaying documents.

The key word here is document. That is, we create, edit and view a document in some program, in this case - in an office editor. If we open such a document in a simple text editor, for example, Notepad, we will see many strange symbols and signs. This mess of symbols is incomprehensible to humanity, but understandable to computers. Thanks to this internal language, the Word document takes on a certain structure and appearance in the editor itself, and the document appears before us in all its glory with formatted text and pictures in their place.

HTML is a document markup language for the browser. Word here is the browser, and the document is the HTML page. This is the very basis of HTML technology, the understanding of which is necessary in order not to confuse the markup language of web documents with programming languages. The name speaks for itself - using HTML we marking, where on the page the element, picture or text will be shown, and in what order they will appear next to each other.

Yes, simple typing and formatting of text in office applications has nothing to do with programming. But the observant reader will notice an important detail - in a word processor we type, edit and format text and pictures using visual buttons and menus, but why is HTML code written by hand? Why learn so many technical details of writing markup for a document?

In fact, there are a lot of editors with which you can create and edit HTML pages, similar to Word. That is, the source HTML code is hidden for us and we don’t get into it.

A sort of Word for HTML. These visual editors are called:

WYSIWYG editors - W hat Y ou S ee I s W hat Y ou G et. That is, if we translate it into Russian: what we see is what we get.

I call them "wuzivoogies". Although this is phonetically incorrect, it clearly demonstrates the meaninglessness of this invention. Beginners very often use such editors to create their first websites. Of course, this is convenient - you don’t need to delve into the study of tags, design styles and other, at first glance, unpleasant and complex things. The editor itself automatically converts our actions into HTML code.

But, as they say, nothing just happens. More specifically, this approach has very serious drawbacks. What prevents everyone from using visual editors to design HTML pages? The fact is that pages formed in this way usually have a lot of unnecessary code and a lot of errors from a semantic point of view. Now, of course, there are no problems with a high-speed Internet connection and the difference in page size of 400 kb and 100 kb is not significant for speed, however, optimized and correctly written HTML code eliminates many problems and provides many advantages, namely: