Html link img and text

HTML Tag

In the browser, hyperlinks differ in their appearance and color. By default, HTML links appear as underlined blue text. When you hover your mouse over a link, it turns red (active link). Links that are already clicked (visited links) become purple.

You can change the color of links, remove underline or change the color of the links using CSS styles.

The «download», «media», «hreflang», «target», «rel», and «type» attributes will be present only if the «href» attribute is present.

Syntax

Attributes

The href attribute

a href="url">the link texta> a href="#a">the link texta>

Example of the HTML tag with the href attribute:

html> html> head> title>Title of the document title> head> body> a href="https://www.w3docs.com/">W3docs.com a> body> html>

Result

Click on the link, and you will be redirected to the home page of our website.

Example of the HTML tag for creating a linked image:

html> html> head> title>Title of the document title> style> img < height: 90vh; > style> head> body> a href="https://en.wikipedia.org/wiki/France"> img src="https://images.unsplash.com/photo-1549144511-f099e773c147?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" alt="France"> a> body> html>

The target attribute

The target attribute is used to tell the browser where to open the document (by default, links open in the current window).

The target attribute can have the following values:

  • _blank– opens the link in a new window.
  • _self-opens the link in a current window.
  • _parent — opens the document in the parent frame.
  • _top — opens the document in full width of the window.

Example of the HTML tag with the target attribute:

html> html> head> title>Title of the document title> head> body> a href="https://www.w3docs.com/" target="_blank">W3docs.com a> body> html>

Result

The rel attribute

This attribute sets the relationship of the current document to the linked one. The attribute values can be as follows:

  • alternate — an alternative version of the document.
  • author— reference to the author of the document or article.
  • bookmark — a permanent link to be used for bookmarks.
  • nofollow — links to an unendorsed document (this instructs the search engines that the crawler should not follow that link).

No follow value

If you want to create a nofollow link, use rel=»nofollow». This informs search engines that you don’t support the content at the other end of the link. The nofollow attribute value is generally used on paid links and advertising. Sometimes the unfollow is considered to be a tag or attribute, but in fact, it’s a value of the rel attribute.

Example of the rel attribute with the «nofollow» value:

html> html> head> title>Title of the document title> head> body> p>Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book. p> p>This text is from a href="https://www.lipsum.com/" rel="nofollow" target="_blank">Lorem Ipsum a>. p> body> html>

Attributes

Attribute Value Description
charset char_encoding Defines the character-set of a linked document.
Not used in HTML5.
coords coordinates Defines the coordinates of a link.
Not used in HTML5.
download filename Defines that the target will be downloaded when a hyperlink is clicked.
href URL Defines the URL of the linked document.
hreflang language_code Defines the language of the linked document.
media media_query Defines what media/device the linked document is optimized for.
name section_name Defines the name of an anchor.
Not used in HTML5.
ping list_of_URLs Defines a space-separated list of URLs to which, when the link is followed, post requests with the body ping will be sent by the browser (in the background). Typically used for tracking.
rel alternate
author
bookmark
external
help
license
next
nofollow
noreferrer
noopener
prev
search
tag
Defines the relationship between the target object and the linked document.
rev text Defines a reverse link, the inverse relationship of the «rel» attribute.
Not used in HTML5.
shape default
rect
circle
poly
Defines the shape of the hyperlink.
Not used in HTML5.
target _blank
_parent
_self
_top
Defines where to open the linked document.
type media_type Defines the media type in the form of a MIME- type for the linked URL.
Читайте также:  Sublime text автодополнение python

How to style tag?

Common properties to alter the visual weight/emphasis/size of text in tag:

  • CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
  • CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
  • CSS font-size property sets the size of the font.
  • CSS font-weight property defines whether the font should be bold or thick.
  • CSS text-transform property controls text case and capitalization.
  • CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in tag:

  • CSS color property describes the color of the text content and text decorations.
  • CSS background-color property sets the background color of an element.

Text layout styles for tag:

  • CSS text-indent property specifies the indentation of the first line in a text block.
  • CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
  • CSS white-space property specifies how white-space inside an element is handled.
  • CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for tag:

  • CSS text-shadow property adds shadow to text.
  • CSS text-align-last property sets the alignment of the last line of the text.
  • CSS line-height property specifies the height of a line.
  • CSS letter-spacing property defines the spaces between letters/characters in a text.
  • CSS word-spacing property sets the spacing between words.

Источник

We are going to take a beginner friendly look at how to create image links on your website.

We will look at which HTML tags you need to use in the code.

If you have never done this before, don’t worry! I will guide you through the whole process.

Читайте также:  Http bbs mydigit cn read php tid 6292

By the end of this article, you will be creating image links all over your web page and the Internet.

We are also going to look at some more advanced techniques towards the end of the article if you are feeling brave.

Grab a coffee, let’s dive in!

To get links on your home page you need to create a hyperlink. To do this in HTML you need to use an tag. Each tag has two parts the opening and closing tag.

In the above, you can see that the open tag:

Creates the start of the hyperlink. Then we close the tag with:

Any text that we write between the open and close tag is your link text. For Example:

To turn this into a link we need to add a URL. To do this we add the URL to the href attribute. The URL can point to anywhere on the internet. For this example we are going to use the PageDart homepage:

a href="https://pagedart.com"> My Great Link a>

Make sure to include the full URL with HTTPS at the front.

That is all there is to create a text-only link in HTML. Next, let’s look at how we can add an image to a web page using HTML.

How do you Create an image in HTML?

To add an image to your web page use an img tag.

This tag is a bit different from an a tag in that it does not have an opening and closing tag.

To create an img tag you need to give the tag a source file. Use the src attribute to do this:

This will add an image to the screen like this:

Add Image Tag Add Image Tag

Now we have an image we need to turn it into a link. To do that we need to add a tag inside the tags creating a linking image in HTML.

Let’s look at an example next.

To turn an image into a link you need to add the tag inside the tag. So taking our earlier example the source code would look like this:

a href="https://pagedart.com"> img src="/favicon-32x32.png"> a>

Which would produce this HTML:

Add Image Tag to the Hyperlink Tag Add Image Tag to the Hyperlink Tag

If you are using WordPress then you can add this HTML code to your page using the text view in the page editor.

You can also add text and images inside the link, let’s look at this.

The example above showed how to add only an image as the link.

What if we wanted to add both an image and text to a link.

Well, in this case, we need to make sure that we add the text before we close the tag:

a href="https://pagedart.com"> img src="/favicon-32x32.png"> PageDart Home a>

Example Two Example Two

There is one more attribute on the tag that we have not discussed and you should be using.

Images on the internet should have an alt attribute. This is a text description of the image.

Screen readers use the alt text to help visually impaired users. The screen readers will read out the text description when it finds an image.

To do this we add an alt attribute to the img tag like this:

a href="https://pagedart.com"> img alt="PageDart.com" src="/favicon-32x32.png"> a>

Now when the screen reader reads this image it will say “PageDart.com” out loud.

It is important to follow best practices when writing image alt text.

Читайте также:  Observer design pattern python

Make sure you think about the person who is using the screen reader. Write your alt text description for them and describe the image as best you can.

This is all you need to create linking images in HTML. Yet, there are a few more advanced techniques that you can use. If you are feeling brave we will look at these next.

A more advanced technique is to use CSS. CSS stands for Cascading Style Sheet and it allows you to change how a HTML page looks.

With CSS you can set a background image on an element such as our tag.

Here we would have HTML that looks like this:

style> a < background-image: url("/favicon-32x32.png"); display: block; height:32px; width:32px; > style> a href="https://pagedart.com">a>

Setting a Link Image With CSS Setting a Link Image With CSS

We can use a similar technique with a tag.

Advanced Technique: Using a Button

In this example, we are going to use the tag instead of the tag.

We can create the same output as the example above using this HTML:

style> .link < background-image: url("/favicon-32x32.png"); border: 0; height:32px; width:32px; > style> script> function goHome() < return window.open("https://pagedart.com", "_blank") > script> input type="button" class="link" onclick="goHome()"/>

Which creates this output:

Example Four Example Four

Here we are using JavaScript to open a new browser tab. Sending the user to the PageDart home page.

I have no idea why you would ever do this! But, I wanted to show you what is possible and how flexible HTML, CSS and JavaScript can be.

The last example I am going to show is using an image map.

Advanced Technique: Image Map

Image maps are single images with clickable areas. They have lost popularity over the years but they are still part of the HTML5 specification. This means that browsers such as Internet Explorer, Chrome and Safari will support them.

Charts are good examples of Image Maps such as organizational charts, like this:

Example Five Example Five

Then to make each pink area a clickable link we would create a map like this:

img src="org.png" alt="Staff Organisation Chart" usemap="#Map"> map name="Map" id="Map"> area shape="rect" coords="176,14,323,58" href="…" alt="Chairman" > area shape="rect" coords="81,75,226,114" href="…" alt="Secretary" > area shape="rect" coords="6,138,155,182" href="…" alt="Marketing Director" > area shape="rect" coords="175,138,323,182" href="…" alt="Sales Director" > area shape="rect" coords="345,136,496,186" href="…" alt="Finance Director" > map>

We have added the attribute usemap to the img tag.

Then within the map tag we have an area tag. Each of these areas creates a clickable area that acts as a button.

Using a Linking Image in HTML

We have looked at five different ways to add an image as a link on your web page:

  1. An image as a link
  2. Image and text as a link
  3. Using CSS to set an image as a link
  4. Using a button as a link
  5. Using image maps

All these can turn images into links. You should now have the confidence to try this yourself.

We have also looked at accessibility and how to set Alt text to help the visually impaired.

Recent Posts

In this tutorial we will add Google search to a website using the custom site search tool

In this tutorial we are going to look at how to create a WordPress plugin.

We look at how to add a search bar in HTML to your website and search connect it to Google search

Источник

Оцените статью