The pseudo-class:target is one of the great features of CSS3. It matches the element pointed to by the identifier in the document URI.

The identifier in the URI contains the character “#” followed by the name of the identifier, corresponding to the value attribute id element in the document.

Support

Since we are talking about CSS3, the pseudo class is supported by everyone modern browsers, with the exception of IE versions 6 through 8. The usual disappointment should not stop you from deciding to use:target. Already IE9 supports pseudo class:target .

How to use:target?

This pseudo class can have its own style, just like pseudo classes :hover, :active or :focus for links. Same as the mentioned pseudo classes :target used for certain actions with the website. Especially when using a fragment identifier like this: http://example.com/index.html#lorem-ipsum.

Demonstration

The demo page provides a very clear example of how and when to use :target. Pseudo class :target can improve the usability of your resource.

HTML markup

Below is an example from the demo. We have 4 links and the same number of blocks. Each group has its own unique identifier.

  • Block 1
  • Block 2
  • Block 3
  • Block 4

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus, felis at varius eleifend, neque orci porta magna, consequat aliquam ligula velit quis erat. Aenean porttitor pellentesque risus, eu tincidunt ipsum blandit in.

Sed lobortis placerat elit tincidunt tempor. Nam dignissim euismod quam nec tempor. Sed tortor lorem, ultricies a auctor nec, auctor ut neque. Aenean varius, urna eget adipiscing feugiat, nunc ligula molestie massa, id accumsan turpis metus ac ante. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Pellentesque quis tortor vitae elit cursus vulputate et vel dui. Nunc commodo pretium arcu in ultricies. Nunc vel velit enim, et tincidunt leo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Quisque eget tempor sapien. Cras convallis tempor orci pulvinar scelerisque. Nullam et erat eu nibh sollicitudin congue sit amet id diam. Sed in lectus ut ague euismod porta. Quisque non lacus odio. Nunc ornare adipiscing egestas.

CSS

The following CSS code allows you to achieve the desired effect, with the help of which the block with the corresponding ID is highlighted (a shadow appears around the rectangle).

/* Add some space */ ul, div ( margin-bottom: 10px; ) /* Default block style */ div ( padding: 10px; border: 1px solid #eaeaea; -moz-border-radius: 5px; border- radius: 5px; ) /* Change the output type when matching */ div:target ( border-color: #9c9c9c; -moz-box-shadow: 0 0 4px #9c9c9c; -webkit-box-shadow: 0 0 4px #9c9c9c ; box-shadow: 0 0 4px #9c9c9c; )

Below is the result of the code. If you click on a link, the corresponding block is highlighted and becomes active.

Hello, dear readers of the blog site. I decided to devote today’s publication to the most important aspect of webmastering, where I will try to explain in detail what a hyperlink is, which is inextricably linked with, which, in turn, without exaggeration, is the basis of the Internet.

But in order to correctly create hyperlinks and insert them into the code of web pages (for example, your website), you need to study the corresponding area of ​​​​hypertext markup language (), since these elements are formed using the HTML a tag, which has the ability to upgrade the link to the desired one kind.

So, today we will look at what parts a hyperlink consists of, how using the target blank attribute makes it possible to open a page in a new window (tab), how to make any picture a link, and many other important details. Among other things, this information will advance you in learning the HTML language.

What is a hyperlink and can it be called a link?

Answering the question asked in the title, I will say that the term “link” has a wider semantic spectrum (link to Siberia, banking to identify the payer, text in a book, etc.), including the meaning inherent in the concept of “hyperlink” , which is associated only with hypertext, which makes it possible to nonlinearly perceive information.

Thus, hyperlink this special case links, so it is quite possible to use them in today’s topics on an equal basis. This is what I will use to my advantage throughout today's post, using both terms to avoid unnecessary keyword spam.

But there are also invisible service ones, created using and located within, whose task is to send numerous signals and commands to browsers to perform a particular function.

For example, using service hyperlinks it is possible to display an icon. They can only be seen as part of the HTML code (to do this, click on any page open in the browser):


For now, let’s leave service links alone and consider the general pattern of creating hyperlinks. What they have in common is that they all have required href attribute, the value of which is the document address (). HTML link can lead to both an internal page of the site and an external document.

One more important note. The use of the href attribute makes hyperlinks clickable, that is, it allows users to automatically go to the corresponding page of the website as a result of clicking on them.

How to make a hyperlink in HTML using href

So, we already know that to create a link, the a tag and the href attribute are required, the parameters of which can be URLs various types. Because the a this, then between the opening and closing contains the content that will be displayed on the web page.

This the content is called anchor and can be presented in the form of text or image (we’ll talk more about how to make a picture a link below). As I already noted, the anchor will be clickable. Let's look at an example of a hyperlink with text content. This is what its design will look like in HTML code:

a little about anchors

In addition to HTTP, the secure HTTPS protocol can be used. The link does not have to lead to a web page. It all depends on the href value, which can be the path to some file:

//site/wp-content/uploads/2012/05/giperssylqi-v-html.jpg

download

On the web page this link will take the following form:

The browser “understands” that an object with the .zip extension can only be intended for downloading, which is what the user is prompted to do.

By the way, the path to any file is sometimes indicated via FTP(). Then in the URL that is used as the href attribute parameter, you should simply replace the HTTP protocol (HTTPS) with FTP. The file link will look like this:

download from server

But that's not all. In a similar way a link is created to email using the mailto pseudo-protocol For quick access to the letter writing tool:

write letters

After clicking on such a link, a window should appear with the default installed mail client, in which you can compose the text of the message and send it to the already specified address, taken, as you probably guessed, from the href.

In practice, hyperlinks to mail were very often previously used by webmasters and owners of large resources to ensure the convenience of users and clients who could access the mail form in one click.

But this type of communication on web resources is no longer so popular (especially since it can be installed, for example), because email addresses opened in this way are actively intercepted by software and used by various kinds of spammers.

I gave as an example only the most common protocols that are part of the URL, which, in turn, is a parameter of the href attribute when forming a hyperlink. In addition to HTTP (HTTPS), FTP and mailto, there are others for solving more specialized problems. Perhaps we will look at them in more detail in other publications.

Types of hyperlinks

Now let’s try to classify links according to certain criteria.

According to their scope:

1. External— lead to pages located outside the site on which they are posted;

2. Domestic— connect web pages located within the same web resource.

By format:

2. Graphically e - in this case, the anchor of the hyperlink is an image (including a thumbnail), banner, button, etc.

By type of URL serving as the value of the href attribute:

1. Absolute, which contain an explicit indication of the data transfer protocol (for example, HTTP) and Domain name website (all about domains).

Such links are most often used when referring to pages of external resources. In this case, the href value will include the full path to the desired file or web pages. Here is an example of an absolute hyperlink:

Context - what is it?

2. Relative, one of the options for creating which will be the path specified relative to the root folder of the website (hence the name of this type of hyperlink). In this case, the protocol (HTTP) and the site domain will be removed from the URL:

Context - what is it?

Relative hyperlinks on a web page can provide links to internal pages. In this form they are shorter, which makes the HTML code easier. Of course, everything is not so simple, and requires wider coverage, which you can see if you follow the link provided.

Attributes of the a tag

Now let's see what other attributes exist, besides the required href, and how they can affect the creation of a hyperlink. The most complete data in this regard, so to speak, “first-hand,” you can find on the official website of the W3C International Consortium, where actual information appears the fastest.

They are completely identical and initiate the opening of a web page in the current tab. If you want pages to open in a new tab when following links, you should add when creating a link target attribute with blank parameter:

Context - what is it?

Although some webmasters and SEO specialists believe that it is better if the visitor is given the opportunity to choose (that is, not specify a target blank), because if necessary, the page can be opened in a new tab using context menu(move the cursor to the link, click right click mouse and click on the appropriate item):


Moreover, as you can see, in this case the user has the advantage that it is possible to open the web page not only in a new tab, but also in a new window(this option cannot be implemented via HTML, only using scripts).

But I believe that not all novice users are fully familiar with the capabilities and settings of popular browsers (). It is quite likely that a reader, having followed an external hyperlink and having lost sight of the source page (the recipient document will appear instead), will simply close the tab in frustration.

Thus, the owner of a web resource may well not only lose a promising visitor, but also get worse behavioral factors(), which will entail a loss of page position when ranking.

In addition, opening a page in an adjacent tab is very convenient from a usability point of view (). The user follows a hyperlink to a web page and receives Additional information and, as if nothing had happened, continues to study the main material.

To create an objective picture, it should be added that applying “target="_blank"” to any external links, it turns out, poses some security risks. If anyone is interested, you can read, where recommendations are given for troubleshooting problems when using target blank, and also presented alternative options implementing the task of opening a web page in a new tab.

It seems that I noted all the pros and cons of using the blank value to open a hyperlink in a new tab. If you read articles on my blog, you probably already guessed my attitude towards this target attribute parameter.

On this moment For me, its pros outweigh its cons, and I am an unconditional supporter. Based on the above facts, you can form your own opinion on this issue. Although...taking into account the emergence of new circumstances, it may change.

How to create hash links, anchors and why are they needed?

Next, I would like to draw your attention to another type of hyperlinks, which can be useful if the material located on a web page is quite voluminous and should be structured to improve navigation.

You don’t have to look far for an example, just look at the table of contents of this publication at the very beginning. Do you see there is a list of sections of the article? Hyperlinks to these sections are just hash links. After clicking on any of them, the browser will scroll the page to the place where the corresponding part begins.

Such links are created quite simply. First you need to create anchor by placing a corresponding label in the required place on the page in the form of an ID, which is one of the global attributes applied to any HTML tag.

Thanks to the universality of ID, such anchor tags can be installed almost anywhere on a web page. Since text is usually divided into paragraphs, they can also be applied to . I mainly put down anchors with which articles are divided into logical parts:

Having thus marked all the necessary places in the HTML code, we then create hyperlinks to them. To do this, at the end, after the last slash “/” in the URL (which, as you already know, is the value of the href attribute), we write sequentially the pound sign “#” and the label name (ID):

How to form hash links and anchors?

Moreover, if such an anchor link is placed on the same page as the anchors, then part of the URL up to and including the last slash before the hash can be omitted and only “#” plus the ID name are used as the href parameter (in essence, this is one of the options for relative links):

How to form hash links and anchors?

That is, when compiling a table of contents for a manual to optimize HTML code, you can easily use this lightweight option. If you put only one hash sign as the URL, without the name of the identifier, then from the place where such a link is located, the page will scroll all the way up:

Top

Actually, this simplest option, which again serves as an additional advantage for the usability of the resource, since it makes life easier for its visitors. You can use an attractive picture as an anchor to create a button.

How to remove underline and change link color

HTML developers have thought through almost all the nuances that can help in optimizing this or that aspect, the same applies to hyperlinks. For example, following a hyperlink to another web page and then returning back, the user will see that the hyperlink has changed color.

If he needs to repeat such actions repeatedly, he will be able to quickly determine which links he has already clicked and which ones he has not. This option is included in almost every browser. There is no need to explain how convenient this is and how much time it saves.

By default and without application CSS tools hyperlinks highlighted with underlining and three color options, each of which has its own attribute for:

  • link - sets the color of the hyperlink on the web page (blue by default, which is designated #0000ff);
  • alink — the color of the active hyperlink for the period of time while it is being processed by the web browser (red #ff0000);
  • vlink — the color of the link visited by the user (purple, #800080).

How can you change the colors of links that browsers display for your site? Well, for a simple HTML site where pages are created manually (and I think there are practically no fully functional resources of this kind left in modern realities, except perhaps simple diaries and landing pages), you just need to find the opening tag and specify the necessary parameters for it (by the way, you can use its name to indicate a color), for example:

If you have installed a content management system () to administer your web resource, then, regardless of the type of engine you use, you need to open for editing the file responsible for displaying the header (header), where there is an opening tag .

If your resource is running WordPress, then to practically implement this idea, you can use it as a tool for automatically inserting an image inside a hyperlink, where you first select from the library or download the desired image and insert it into the text:

Then select the resulting image code and press the editor’s “link” button, after which you paste the desired copied hyperlink in the window that appears:


You can easily find many tutorials on the Internet using the pseudo-class:target. However, let's not follow someone else's code, but try to understand it and understand how it works? Of course, we can’t do without examples.

What is:target?

In CSS:target, this is a pseudo-class that allows you to select a whole “piece” of your HTML document on which some action will be performed. This could be a paragraph of text or a heading, for example.

Pseudo-classes should not be confused with pseudo-elements, which can select only a certain part of an element, such as the first letter or the first line of a paragraph.

Pseudo-classes:

  • a:link(color:#111)
  • a:hover(color:#222)
  • div:first-child(color:#333)

Pseudo-elements:
  • p::first-letter(color:#444)
  • p::first-line(color:#555)

I think it's clear from the syntax what this or that pseudo-class or pseudo-element does. The most popular pseudo-class is :hover, all webmasters come across it; it describes the styles of an element when hovering. target works similarly and describes the styles of an element when a certain situation occurs.

Fragment IDs

In short, this is a thing to which our pseudo-class is tied. This is a hashtag with a word or phrase that is placed at the end of the address. It looks like this:

This technique can be used for transitions within an article. For example, at the beginning of the article you create a small table of contents, when clicking on an element of which a person is taken to the section where the link leads; the section needs to add an identifier.

It works in pure HTML, no tricks required. Small identifiers.

Handling a click using:target

Now let’s try to make sure that when you click on the desired section from our improvised menu, the title style changes, showing which paragraph is currently active.

H1 ( font: 30px Arial sans-serif; ) h1:target ( font-size: 50px; text-decoration: underline; color: #37aee4; )

The code is very simple - when clicked, the title changes its size, color and is underlined. You can add life (Ruslan, hi) and make an animation for changing the title color:

H1 ( font: 30px Arial sans-serif; -webkit-transition: color 0.5s ease; -moz-transition: color 0.5s ease; -o-transition: color 0.5s ease; -ms-transition: color 0.5s ease; transition: color 0.5s ease; )

Highlighting the active title is a good thing, but what if you need to change the design of the text that follows it? CSS gives us this opportunity. It looks like this:

H1:target + p ( background: #eaeaea; padding: 10px; )

In this case, the plus sign means that the style should be applied to the paragraph following the heading. Thus, when the active header changes, we also change the section design with the text that “belongs” to him.

Browser support

The target pseudo-class belongs to the third edition of CSS and is perfectly supported by all browsers except IE, older than version 9. Therefore, you should not implement it if your audience uses this browser. Although, there is a way out - this is Selectvizr, a JS library with which you can make IE work with CSS3. We just add the script and that's it, it works.

The only thing is, if you don’t use JQuery or MooTools, you need to connect it for this script to work. The official website has a table showing which libraries support what. If you are interested, read it. This .

Conclusion

Using pseudo classes may seem complicated, but once you understand how they work, you can do amazing things using just CSS and nothing else. Pseudo-class:target- excellent confirmation of this. With its help, you can radically change the interaction of a page with a visitor. The example above is the simplest, but it still adds a bit of interactivity to the page. But this is just a couple of lines of code.

Don't overdo it with beauty and browser support and everything will be fine.

Have a nice day

target="_blank is used to make the tag opened the link in a new window. But what is target HTML? Why should it be blank? And the most interesting thing is, why is there an underscore at the beginning? Let's take a closer look at this code and understand what it does.

target attribute

Target values

The four most common values ​​for the target attribute are:

_self

The only situation in which this can happen is if the tag is used in the HTML , which sets a specific opening method for all links. For example, if between tags added the following code , you need to use target="_self" so that the link opens in the same window.

_blank

Opens a link in a new tab or window. This is determined by the user's local settings, in most browsers this is a new tab page. You might think that using this value you can implement pop-up ad units. But that's not true. Most often, JavaScript is used for this rather than HTML.

This target HTML attribute value is best used to open links to external sites or PDF files in a new tab. Thanks to this, after closing these tabs, the user is returned to your site. But this should not be abused, since it will be quite difficult for the user to navigate if each link opens in a new tab.

_parent

The value _parent opens a link in the parent frame for the frame you are in. This is not a popular value, since the tags for creating frames And are not supported in HTML5. However, this value can still be used inside tags