Creating links in html using buttons

How to Add an HTML Button that Acts Like a Link

There are several ways of creating an HTML button, that acts like a link (i.e., clicking on it the user is redirected to the specified URL). You can choose one of the following methods to add a link to the HTML button.

Add an inline onclick event

You can add an inline onclick event to the tag.

Example of adding an onclick event to the tag:

html> html> head> title>Title of the document title> head> body> button onclick="window.location.href='https://w3docs.com';"> Click Here button> body> html>

It is also possible to add an inline onclick event to the tag within the element.

Example of adding an onclick event to the tag:

html> html> head> title>Title of the document title> head> body> form> input type="button" onclick="window.location.href='https://www.w3docs.com';" value="w3docs" /> form> body> html>

Use the action or formaction attribute.

Another way of creating a button that acts like a link is using the action or formaction attribute within the element.

Example of creating a button acting like a link with the action attribute:

html> html> head> title>Title of the document title> head> body> form action="https://www.w3docs.com/"> button type="submit">Click me button> form> body> html>

To open the link in a new tab, add target=»_blank» .

html> html> head> title>Title of the document title> head> body> form action="https://www.w3docs.com/" method="get" target="_blank"> button type="submit">Click me button> form> body> html>

Since there is no form and no data is submitted, this may be semantically incorrect. However, this markup is valid.

Читайте также:  Viewing html in chrome

Example of creating a button acting like a link with the formaction attribute:

html> html> head> title>Title of the document title> head> body> form> button type="submit" formaction="https://www.w3docs.com">Click me button> form> body> html>

The formaction attribute is only used with buttons having type=»submit» . Since this attribute is HTML5-specific, its support in old browsers may be poor.

Add a link styled as a button with CSS properties. A href attribute is the required attribute of the tag. It specifies a link on the web page or a place on the same page where the user navigates after clicking on the link.

html> html> head> title>Title of the document title> style> .button < background-color: #1c87c9; border: none; color: white; padding: 20px 34px; text-align: center; text-decoration: none; display: inline-block; font-size: 20px; margin: 4px 2px; cursor: pointer; > style> head> body> a href="https://www.w3docs.com/" class="button">Click Here a> body> html>

Let’s see one more example.

html> html> head> title>Title of the document title> style> .button < display: inline-block; padding: 10px 20px; text-align: center; text-decoration: none; color: #ffffff; background-color: #7aa8b7; border-radius: 6px; outline: none; transition: 0.3s; > .button:hover < background-color: #c2c7c7; > style> head> body> a class="button" href="https://www.w3docs.com/learn-html/html-button-tag.html">HTML button tag a> body> html>

How about the accessibility?

Let’s take accessibility into our account for the last example. Here are some improvements to make the code more accessible:

If the button contained an image, it would be important to provide an alt attribute to make the image accessible to screen readers. Since this button doesn’t have an image, we don’t need to worry about this.

Adding a label to the button will help users who rely on assistive technology understand the purpose of the button. We can do this by wrapping the button text in a element and adding an aria-label attribute to the button.

To improve visibility for users with low vision, we can increase the contrast between the text color and background color of the button. We can achieve this by making the background color darker or the text color lighter.

Adding a focus style to the button will help users who navigate using the keyboard to see which element is currently focused. We can add a simple border to the button to achieve this.

Here’s the updated code with these improvements:

html> html> head> title>Title of the document title> style> .button < display: inline-block; padding: 10px 20px; text-align: center; text-decoration: none; color: #ffffff; background-color: #3c5d6e; border-radius: 6px; outline: none; transition: 0.3s; border: 2px solid transparent; > .button:hover, .button:focus < background-color: #c2c7c7; border-color: #7aa8b7; > style> head> body> a class="button" href="https://www.w3docs.com/learn-html/html-button-tag.html" aria-label="Learn about the HTML button tag">span>HTML button tag span> a> body> html>

Источник

Читайте также:  Функция list в python примеры

HTML is known as Hypertext Markup Language which gathers data from different servers in one place by linking them. When we create a website in HTML that website is actually a combination of text and links. We can make links to images, buttons, texts, etc.

The link binding is used to navigate to pages inside the website or the web-pages of other websites. This article aims to guide you on various methods on creating a button link to another page in HTML.

Additionally, this guide serves the following outcomes:

In HTML, the tag and the tag are used to create a button link. Mostly, the tag is utilized to make a link on a button. Moreover, the href =”” attribute specifies the path to another page.

The button link can be created by using the , , and tag. Each tag refers to a specific attribute to create a link to another page. For instance, the href attribute of tag serves the purpose.

This segment provides a detailed synopsis of all the methods to make button links to another page in HTML.

To create a button link to another page in HTML,just add tag and wrap it around the simple Html button. Inside a tag simply use href=”” attribute to give the path of the desired page.

Example Below:

In the above example, we have created a button and the tag creates a link to another page (www.google.com)

Output of code:

The output shows that, after clicking the Click button, you will be navigated to Google instantly.

We can create a button with a link to another page with the help of the tag. To do so, we need tag and onclick=”” attribute to specify the link.

The following code makes use of the tag to create a button with a link to another page.

Example Below:

In the code, the tag is used with type, onclick, class, and value attributes. A link is pasted in the onclick attribute of HTML.

Output of code:

The output shows that, after clicking the Click button, you will be navigated to the Instagram login page instantly.

We can create a button with a link to another page with the help of the tag. To do that we need the tag with action attribute to specify the page path. The following code represents the functionality of tag to make a button link to another page:

Читайте также:  Определение размера экрана python

Example Below:

In this example we use tag with action attribute to specify the path. A button is created using tag.

Output of code:

The output shows that, after clicking the Click button, you will be navigated to the twitter login page instantly.

Conclusion

In HTML, a button link to another page can be by using the tag, tag, and the tag. A link on a button is get by href=”” attribute of tag. The type=button and onclick=link attributes are used to create a link on the button. The action=link attribute of the tag can also be used to make a button link to another page. You have learned a detailed usage of all these tags and attributes to make a button link.

Источник

How to Create an HTML Button That Acts Like a Link

How to Create an HTML Button That Acts Like a Link

Sometimes you may want to use a button to link to another page or website rather than to submit a form or something like that. This is fairly simple to do and can be achieved in several ways.

How to Create an HTML Button Using the Button Tag in an A Tag

One way is to simply wrap your tag in an tag:

This transforms your entire button into a link.

A second option is to create your link as you normally would with your tag and then style it via CSS:

Once you’ve created your link, you can the use CSS to make it look like a button. For instance, you could add a border, a background color, some styles for when the user is hovering the link.

How to Put a Button inside a Form Using HTML

Another way to add a button is to wrap an input inside form tags. Specify the desired target URL in the form action attribute.

I hope you’ve found tutorial helpful. Happy coding.

If this article was helpful, tweet it .

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

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