Options for styling links in CSS. Including CSS in HTML (page 3)
Hypertext is text that contains links to other text. An example would be the author's notes on complex definitions or translators' footnotes at the bottom of the book page if it contains text in a foreign language. Internet sites are a complex system of hypertext transitions from one page to another, within the page itself, as well as between resources unrelated to a single topic. This structure is practical, saves a lot of time, allows the visitor to quickly find necessary information and don't get confused large quantities transitions.
Inserting a hyperlink
In HTML, a descriptor (tag) is used to insert a hyperlink. , which is inserted into the desired location. It is usually placed among the text, since the hyperlink itself is a text structure. But links can also be graphic (icons, buttons, pictures); they will be discussed further. Their location on a web page is not limited to text, but depends on the design decision of the site creator.
This is an example of how to insert a hyperlink into an HTML document, which is done using the tag . A site visitor will see underlined text, different from the color of the surrounding text (the color of the HTML link can be any), “Google home page”, clicking on which will take him to the main page of the Google search engine. It should be noted that the text of the hyperlink must contain information about where the transition will be made. This principle should be adhered to and accepted as a rule!
Tag structure ...
You can notice that the tag - paired: closing tag required.
href—attribute tag, indicates the purpose of the link.
https://google.com/ - attribute value, which contains the URL of the resource to which the transition will take place. It is enclosed in double or single quotes. It depends on the tag nesting structure according to HTML rules.
this whole structure is called element web document.
According to HTML rules, some elements can contain other elements. Tag not an exception. If a programmer needs to highlight the word Google in bold, then this is done using a tag according to the general rules of text formatting, observing the sequence of nesting of tags. A webmaster must know how to create a hyperlink in HTML without errors, otherwise they will not work. Broken links are called “broken” in computer slang.
Here: element
contains a nested element
Absolute hyperlinks
protocol://domain name/path to file
An example of an address for a search engine common in America: https://aol.com - absolute, since it contains the domain name.com.
Absolute hyperlinks are used to navigate to pages on other sites or to access resources located on another server. The transition is carried out using Internet protocols. Protocols are not the topic of this article, but since they are involved in creating hyperlinks, it is necessary to at least briefly mention them:
- http And https - most common; they are used to navigate between Internet pages of different sites;
- ftp - protocol for uploading files to the server or downloading by the user from the site;
- mailto - postal protocol used to send Email directly from the site, without going into your personal email.
There are several other special-purpose protocols (gopher, telnet), which are quite rare, the use of which requires special knowledge in programming or system administration.
Relative hyperlinks
With relative addressing, the use of hyperlinks in HTML serves to navigate within the resource and does not lead beyond its boundaries. If the page is so big it works vertical stripe scrolling, sometimes very long, as, for example, in dictionaries, it is very convenient and advisable to use relative links for quick transition to the desired letter.
When creating an online dictionary, the programmer places the alphabet at the beginning of the page, and if it were not for the use of links, the user would have to turn the mouse wheel for a very long time to get to the letter “I”.
Where ya called anchor, A Go to letter I- destination anchor. To display anchors correctly, it is recommended to use Latin letters and numbers, so you shouldn’t write “I”, although it would be clearer.
Now, in order to transition from the alphabet at the beginning of the page to the letter “I”, you need to anchor the anchor in the place in the HTML document where words starting with the letter “I” begin:
there is a hash sign in front of the anchor, without which the transition to the letter will not work.
Relative addressing when creating a site
A convenient and most generally accepted algorithm for creating a website by a programmer:
- creating a folder on your computer and placing it in a location quick access for comfort;
- creating the main folder in this folder;
- creation of secondary web pages (index.html/page2);
- and placement of graphic files in it;
- creating a folder and placing other objects in it (files for downloading, for example);
- filling the site with content;
- hosting site files.
You will definitely have to use links to connect between site elements, and it will be very useful to know how to insert a hyperlink in HTML to another page of the same site. If the site files are located in the same directory, on the same server, then there is no need to use absolute addressing. When transferring site files to the hosting, the connection between the objects will be preserved, because they will also be placed in the same directory on the hosting.
Let's say a programmer has created the main page of the site index.html, which has a link to another page page2.html, decorated with an img.png image. Then relative path this picture will look like this:
Tip: When studying this topic, it is best to use a simple text editor due to the fact that you need to gain dexterity in correct spelling transition addresses and to learn to understand someone else's code. At this stage there will be good result a hyperlink written in notepad without errors,HTML does not forgive them and produces errors.
Methods for following hyperlinks
Default new page opens in the current browser window when the user clicks on a hyperlink. But a web programmer can change the default value and force the page to open, for example, in a new window. There is an attribute for this target with a certain meaning. This can be most clearly expressed in a table.
Attribute syntax target:
The Google home page will open in a new window.
Note: to open pages in a new tab, there are no values for this attribute, but is set by the user himself in the browser settings.
Hyperlink color
An experienced Internet user should have noticed over time that hyperlinks are a different color from the surrounding text, and they are usually blue. Links that he followed and then returned to the previous page turn purple. Using hyperlinks in HTML in non-standard color scheme not much, but it makes the site stand out from the rest.
Set the colors of links in the tag
using attributes and their values, in which HTML color in the rgb system (#00FF00) or with a direct indication of the color name (“green”). There are three types of attributes for links:- link — sets the color of an unvisited link;
- vlink - sets the color of the link that the user has already followed;
- alink - sets the color of the link at the time of transition to another page. This happens quickly, so it is not always possible to catch this effect.
Markup example:
If you apply these attributes in a tag
, links to this web document will be dark blue, visited links will be purple, and active links will be orange-red.Graphic hyperlinks
The progress and development of web design requires you to know how to insert a hyperlink in HTML as an image. It is clear that the image must correspond to the content of the destination page. For example, home page website about medicinal plants can be presented in the form of photographs of plants, by clicking on which the user will be redirected to a page that describes the medicinal properties of the plant.
A widely used method for replacing static buttons ( ) to beautiful graphics created by a web designer in graphics editors (GIMP, Photoshop).
To insert graphics as hyperlinks to site pages, the same syntax is used, but instead of text, an image insert tag is used according to HTML rules:
The same applies to the attributes for setting alternative text, width, height, and others.
Calls from the site
The html5 standard has been extended functionality use of the Internet, and now you can make calls not only from your phone, but directly from the website. You can also use hyperlinks for this purpose. HTML document, and they have this syntax:
...subscriber...
Instead of the word “subscriber”, a contact that is understandable to the caller is written, as in phone book. You can also place graphic file(photo of the subscriber).
In order for calls to be made from the site, it is necessary to have not only a link to the subscriber’s phone number, but also a headset (microphone, headphones), a VoIP program installed on the computer, and the Internet speed must exceed 0.5 Mb/sec. Payment for calls is carried out by traffic consumption. Therefore, if the Internet is unlimited, then calls are free.
The Ethics of Creating Hyperlinks
When placing a website on the Internet, a webmaster must know what types of hyperlinks exist in HTML, and not only apply them correctly and professionally, but also adhere to the following provisions:
- The hyperlink should be clearly visible and distinct from the surrounding text. The user must know that this is a hyperlink.
- It should be clear to the user where he will go when he clicks on the link. For this it is advisable to use another attribute title, which succinctly describes the transition page. The syntax for using the attribute is:
- The user must receive true information about the file that will be downloaded when clicking on the link.
If the user lands on a page that is not the one he expected or downloads the wrong file, in 99% of cases the user will immediately leave the site and will never visit it in the future.
Anti-Seo when creating hyperlinks
Except technical side The question of how to insert a hyperlink in HTML should also cover the moral aspect. There are many sites that users are blocked from accessing by security programs (antivirus) or even by the government. These are the sites that were created by dishonest web programmers.
One of the tricks they resort to is inserting "invisible" hyperlinks into a web page. Fraudsters know how to create a hyperlink in HTML, and use attributes to remove the underline of the link and assign it the color of the surrounding text so that the average user does not see it. And with the help of other web technology tools (CSS, JavaScript, PHP) you can program their behavior. For example, the OnMouseOver event JavaScript language activates the action of a web page element. When a user hovers over an invisible link, they are taken to advertising page another site. Or even worse, when there is an invisible link to a file and unnecessary things begin to be downloaded and installed on his computer software. Usually these are viruses that change home page browsers are cluttered HDD a lot of programs and so on.
Soon such sites will be blacklisted virus databases, security systems and among Internet users themselves. Such sites do not last long, and they have to change their names, endlessly migrate across the Internet, changing host providers. This does not contribute to the promotion of the site, which is what its creator always strives for, and will never make it a mega-portal, such as, for example, social networks. Among other things, such tricks cause a lot of negative emotions in people affected by these actions.
How to create links in an HTML document(You will find more examples bottom of this page)
HTML Hyperlinks (Links)
- To create a link to another document - using the href attribute
- To make a bookmark inside a document - using the name attribute
HTML Syntax Links
Example
Visit the website |
it will be displayed by the browser like this:
HTML Links - Target Attribute
The target attribute specifies where to open the linked (the one to which the link refers) document.
The example below will open the linked document in a new browser window or tab:
HTML Links - Name Attribute
The name attribute is used to create a bookmark (“anchor”) within an HTML document.
Comment:
The upcoming HTML5 standard suggests using the id attribute instead of the name attribute to specify the name of the link.
Using the id attribute actually works in HTML4 in all modern browsers.
Bookmarks are not showing up in any way in a special way. They are not visible to the reader.
Comment: Always add a trailing slash to subdirectory links. If you create a link like this: href="http://site/html", then two requests are generated to the server, first the server adds a slash to the address, and then creates new request: href="http://site/html/".
Advice: Named links are often used to create a "table of contents" at the beginning of a large document. Each chapter within a document is assigned a named anchor, and links to each of these named anchors are inserted at the beginning of the document.
Advice: If the browser does not find the specified named link, it goes to the beginning of the document. No errors occur.
Often this link is not very noticeable. Only those who realize that the article did not end at the second or third paragraph, and that there is something else there, will specifically look for it. To somehow highlight this link, you need to format it accordingly. But how to do this correctly? After all, if you look into the code of the majority WordPress templates, then you can find that the text of this link is, as it were, “hardwired” into the PHP code for displaying the article. For example, like this:
Designing the more tag in WordPress. Method 1
Firstly, at WordPress engine There is already a provision for this in the form of the more-link class. That is, no matter how you distort the template, the engine itself will in any case substitute class="more-link" for this link. And this is a very useful thing. It says that we can easily use this class by simply adding a certain set of rules for it to the sheet css styles our topic. In fact, all we have to do is add something like this to the style.css file of the template:
More-link (
background: #ffc;
}
But! There is a small ambush here. The fact is that we cannot be too free with this link for one simple reason: this link inherits all the rules that are already predefined for links in the post. The background can be changed, the font size can also be changed. But you can’t change the color of the links. In addition, replacing a text link with a picture is also not an easy task. Therefore, we read another way.
Designing the more tag in WordPress. Method 2
Secondly, you can put this link in a separate block, which can be given any display rules you like. How to do it? Lehhhko and at ease! First, in the index.php file, let’s slightly correct the place where it says about displaying the contents of the article. It's still the same part:
Here we will simply remove everything between the single quotes and leave something like this:
If any class of the entry type was responsible for displaying the text, then it’s time to close here this block(put
It is clear that the image should already be in the images folder of your template and have the specified dimensions and name bg-more.gif.
It's simple! If you have other solutions to this problem, I'd be glad to hear.
Between the opening and closing tags there can be inline and inline-block elements, such as: span, code, strong, img .. ,
and also line break
Warning!
Link attributes
Tag a can contain multiple attributes. Along with the class, style, and id tags that are common to most tags, they are used with some specific attributes.
href
URL (Uniform Resource Locator)- universal resource index.
The href value can be any valid absolute or relative url, including a fragment ID or JavaScript code snippet.
target
Not required. Typically this attribute has 2 values:
target="_self" is the default value for the a tag. The hyperlinked document must be displayed in the same window.
target="_blank" - The document pointed to by the hyperlink will be opened in a new untitled window.
title
Optional attribute. Allows you to define the text that you would like to display as a tooltip when you hover over a hyperlink.
The attribute value is an arbitrary string enclosed in quotes. Can be used to display the title of a document, or when used in conjunction with the target="_blank" attribute, politely warn that the document will open in a new window
"opens in a new window"> "image title"/ > </a>Other attributes, if necessary, can be found in the directory. They are not used very often, so they are not discussed here.
path/document.html
Typically, a relative URL will omit the server name and provide a shortened document address that is automatically combined with the base address.
× Base URL
Must be defined in the document using a tag
Let's look at how a relative URL will be formed, depending on the location of the files. It's easier to understand with an example.
Main document- the referring document in which the link is located
Current folder- where is the main document located?
Subfolder- inside the current one
Nearby folder- together with the current one, located in the common parent
Parent folder- external to the current one
Goal-1</a> "nested/target-2.html"> Goal-2</a> "../neighbor/target-3.html"> Goal-3</a> Goal-4</a>There are more complex options with a large number of nesting levels.
A few simple rules can be formulated:
If the target is in another folder of the current directory (subfolder),
path = dirname/target
The name of the external (relative to the current) directory is not specified, and at the beginning of the path there are 2 dots and a slash - ../ and then the path
Absolute links
If the requested document is located on another server, then you must specify an absolute URL
http://server/path/document.html
URL type http
The most common URL, most often used as the target of a hyperlink. It might look like this:
http://www.site-name.com
In general this type has the following format: http://server:port/path
Path is a hierarchical sequence indicating the location of the document on the server.
One or more names separated by a slash. All names except the last one are directory names. The last name is the document name (by default, web servers accept the file name index.html)
Server - computer system, which stores and provides a web resource upon external request and has a unique IP address. The name consists of several parts, including the server's own name and a sequence of domain names separated by dots.
Port- (usually can be omitted, rarely indicated) - the number of the server communication port through which the client browser connects.
URL type file
A file URL points to a file stored on a computer, without restricting the protocol used to download it. This allows you to load and display a local document.
File://server/path
The file server, like the http server, must be domain name or the IP address of the computer containing the downloaded file. The difference is that the connection protocol is not specified. File server may be an unqualified but unique computer name in personal network, or a storage device.
The path to the requested file on the specified server may vary depending on operating system on server
URL for mailto
letter to me</a> "mailto:fоg@fоgnsnow.ru?subject=Test mailto"> email me + subject</a>URL type ftp
The ftp type resource pointer is used to retrieve documents from FTP servers. In general it looks like this:
ftp://user:password@server:port/path;type=type_code
FTP(Fail Transfer Protocol) - a service that requires authentication. This means that to receive a document from the server, you must be a registered user and know the password.
Many FTP servers provide limited access everyone can access their contents under the login - anonymous or quest, and sometimes without any login (implied by default)
Never!
Do not put the ftp URL with username and password in any document!
The browser itself will prompt you to enter them after connecting to the server
The server and port are specified according to the same rules as in the http URL (if the port is not specified, then port 21 is assigned by default)
Path is a sequence of directories separated by a slash character leading to the requested file.
Transfer_type_code - by default, files are transferred as binary. If you specify type=
d - directory name
a - file containing ASCII text
see the rest in the manual...
Links within one page
- Create a fragment that will serve as a target for a hyperlink
- Create a link to this fragment
To create a fragment, we use the id attribute, which we use to place the link target directly into a defining tag, such as a title.
Link attributes</h3> ... Fragment "Link attributes"</a>
If the snippet created above were on another page, such as somepage.html, then the address of that page would have to be included in the URL before the # sign
Fragment on another page</a>When using an absolute URL, the address is formed similarly:
"http://site-name.com/somepage.html#Atribut_link"> Absolute link to fragment</a>The default image is an inline block element and can be used as hyperlink content.
"Image link" / > </a>In the above example, when you click on the image in the same window, document.html will open
If you want a full-size photo to open in a new window when clicked, you need to specify the address of this photo as the target and add the target attribute.
</a>To design hyperlinks, built-in styles are used, classes and identifiers are assigned, i.e. the same design methods are applied as for other tags.
How to make a block link
It's very easy to get around this rule. Any inline element can be made block-level using CSS.
/* CSS. Create classes and indexes for the link */ /* make the link block */ a.block_link (display: block;) /* floating (automatically becomes block */ a#float_link ( float : left ; ) /* absolute (automatically becomes block */ a.absolut_link ( position : absolute ; ) Block link</a>can be treated like a block element.
Note
href is a required attribute. If we want the link to not lead anywhere, we use url #/ . When specified as url # without a slash, it will go to the top of the page.
Pseudo-classes
Let's look at the features of displaying hyperlinks by a browser (variants are possible for different browsers):
- Color. Browsers typically display unvisited links in blue.
- Underline
- On hover to the link, the cursor appearance changes
- When clicked, the link color changes to red
- After visiting, the link color turns purple
Most browsers use certain styling rules by default when displaying hyperlinks.
We can set our own default rules for displaying links. For example, display them in a larger and/or denser font, set the color to your liking, apply italics, remove underlining, and much more.
But what to do with the color change when you click on a link, or after visiting?
Browsers are capable of changing the appearance of the tag's content depending on the state. You can manage these changes using pseudo-classes
a:pseudo-class (set of style rules;)
There are 4 special states of hyperlinks.
- a:link - unselected, unvisited hyperlink
- a:visited - link visited previously
- a:hover - the selected link that the cursor is hovering over 12px ; border : 1px solid #CDECD3 ; background : #E9FFEE ; border-radius: 6px ; ) .link_button :hover ( background : black ; color : #fff ; ) .link_button :active ( color : red ; )
Attention specificity!
Why do pseudo-classes sometimes not work?
Since all the considered pseudo-classes have the same priority, the next one overrides the previous one. Here The order in which styles are recorded is important.
"L o V e& H.A. te" - love and hate (Link, Visited, Hover, Active). Stick to the specified order, and everything will be fine.
What can you do with CSS only without using JavaScript scripts, etc.
The secret is quite simple. The pseudo-class:hover allows you to control the parameters of the elements contained in the a tag. It can contain different elements, such as an image or text.
This means that when you hover over a link containing a specific element, we can change the text color (color), the visibility of the element (visibility: hidden|visible), control the display of elements (display: none|block|inline|...), background color, address of the background image (background: color url(file name)), etc.
We can position this element absolutely (position:absolute)! It will immediately fall out of the general flow and stick to the upper left corner of the nearest positioned parent block.
All that remains is to place the elements in the desired area of the containing positioned block using the left, top, right and bottom properties (moreover, we can move the element outside the parent block by assigning a negative value to the corresponding of the listed properties), if necessary, setting the dimensions, paddings and other parameters.
That's all. A visual illusion of the element’s independence is created.
Don't forget that our "independent element" remains part of the link and when you hover over it (unless you cancel the output), the same thing happens as when you hover over the link. This can be considered a disadvantage of the considered method. You can slightly soften such an obvious connection of a link with a remote object by assigning the cursor:default property. At least when you hover over an object it will look like a normal pointer.
To control the appearance of various links in CSS, pseudo-classes are used, which are specified after the A selector separated by a colon. In table 1 shows valid pseudo-classes and their descriptions.
Typically, using the link pseudo-class has the same effect as applying a style to the A selector. Therefore, this pseudo-class can be omitted.
Links without underlining
One of the most popular uses of CSS is to hide underlines from links. From a usability standpoint, this is not a completely correct solution, since the user may not immediately realize that the text he sees is a link. Everyone is already used to it - if an underscore is used, it means it is a link. But when used correctly, the lack of underlining on links can add a certain effect to the site. It is often done that when the cursor is hovered, the link becomes underlined, changes its color, or both effects are used simultaneously (example 1).
Below is the usage of this example. When you hover over a link, it becomes underlined and red.
Underlined and overlined links
Another example demonstrates the use of both underscore and overline in links. With this effect, thin lines will appear above and below the link when you hover over it. This is achieved by using the property text-decoration: underline overline in the A:hover selector.
Resizing a link
Example 3 shows how to resize a link when you hover over it.
Change the underline color
With links, using CSS, you can make an interesting feature. The color of the link, when you hover the mouse cursor over it, remains unchanged, but it has an underline of a different color than the link itself (example 4).
Example 4: Create an underline of a different color
Links in different colors
Often there is a need to use links of different colors and sizes on one page. And apply the appropriate type of links for each area of the web page. Some are for menus, others are for text. In this case, we create two or more classes with our own parameters and use them at our discretion. In example 6, it is enough to change the values of the corresponding class, and the colors of the links where this class is used will change automatically.
The result of this example is shown in Fig. 1.