Table of Contents
2. Styling an Tag to Look Like a Button
3. Using a Button Inside a Form with formaction
4. Enhancing Accessibility with ARIA (if using JavaScript method)
Recommendation
Home Web Front-end H5 Tutorial How to create a button that acts like a link in HTML5?

How to create a button that acts like a link in HTML5?

Aug 12, 2025 am 12:04 AM

The window.location method of JavaScript can be used to realize button jump through the onclick event, but lacks the accessibility and right-click function of native links; 2. Beautifying the <a> tag into the button appearance in CSS style is the best practice, retaining the complete link behavior, supporting quick operations and clear semantics; 3. Using form buttons with formation attributes can be used to realize jump, but relying on the form submission mechanism, it is not easy to support the opening of new tabs; 4. If JavaScript buttons must be used, role="link" and keyboard events should be added to improve accessibility, but the code is more complicated; it is recommended to give priority to the use of styled <a> tags, because their semantics are correct, easy to implement and compatible with all link functions, and other solutions will be considered when you really need button semantics.

How to create a button that acts like a link in HTML5?

You can create a button that acts like a link in HTML5 in a few different ways, depending on your needs—whether it's for navigation, accessibility, or styling. Here are the most common and effective approaches:

How to create a button that acts like a link in HTML5?

1. Using JavaScript with window.location

This is the simplest way: attach an onclick event to a <button></button> that changes the page location.

 <button onclick="window.location.href=&#39;https://example.com&#39;">
  Go to Example
</button>

Pros:

How to create a button that acts like a link in HTML5?
  • Looks and behaves like a button
  • Fully customized with CSS
  • No form needed

Cons:

  • Not inherently accessible (screen readers may not treat it as a link)
  • Doesn't open in a new tab with Ctrl click or right-click → "Open in new tab"

2. Styling an <a> Tag to Look Like a Button

Instead of making a button act like a link, make a link look like a button. This is often the better semantic choice.

How to create a button that acts like a link in HTML5?
 <a href="https://example.com" class="button-link">Go to Example</a>

With CSS:

 .button-link {
  display: inline-block;
  padding: 10px 15px;
  background-color: #007bff;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  border: 1px solid #0056b3;
  font-family: sans-serif;
}

.button-link:hover {
  background-color: #0056b3;
}

Pros:

  • Full link behavior (Ctrl click, right-click, etc.)
  • Accessible and semantic
  • Easy to style

Cons:

  • Requires CSS to look like a button

This method is often recommended by accessibility experts because it preserves native link functionality.


3. Using a Button Inside a Form with formaction

If you're OK with a slight redirect behavior, you can use a button inside a dummy form:

 <form action="https://example.com">
  <button type="submit">Go to Example</button>
</form>

Or with formaction for more control:

 <form>
  <button type="submit" format="https://example.com">Go to Example</button>
</form>

Pros:

  • Uses native HTML form submission
  • Works without JavaScript

Cons:

  • Triggers a full page load (like a link) but through form submission
  • May not support all link interactions (like opening in new tabs easy)

4. Enhancing Accessibility with ARIA (if using JavaScript method)

If you go with the JavaScript button approach and want to improve accessibility:

 <button onclick="window.location.href=&#39;https://example.com&#39;"
        role="link"
        tabindex="0">
  Go to Example
</button>

You can also add key event support for Enter/Space:

 <button 
  onclick="window.location.href=&#39;https://example.com&#39;"
  onkeydown="if(event.key===&#39;Enter&#39;) window.location.href=&#39;https://example.com&#39;"
  role="link"
  tabindex="0">
  Go to Example
</button>

But this gets messy—again, just using <a> with button styles is cleaner.


Recommendation

Best practice: Use a styled <a> tag (Method 2).
It's semantic, accessible, supports all native link behaviors, and is easy to style.

Only use a JavaScript-driven button if you're in a context where you absolutely need button semantics (eg, inside a complex form where you don't want submission) and are OK sacrificing some accessibility or link features.

Basically, if you want a link, use a link. Just make it look like a button.

The above is the detailed content of How to create a button that acts like a link in HTML5?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1585
276
Why is my image not showing up in HTML? Why is my image not showing up in HTML? Jul 28, 2025 am 02:08 AM

Image not displayed is usually caused by a wrong file path, incorrect file name or extension, HTML syntax issues, or browser cache. 1. Make sure that the src path is consistent with the actual location of the file and use the correct relative path; 2. Check whether the file name case and extension match exactly, and verify whether the image can be loaded by directly entering the URL; 3. Check whether the img tag syntax is correct, ensure that there are no redundant characters and the alt attribute value is appropriate; 4. Try to force refresh the page, clear the cache, or use incognito mode to eliminate cache interference. Troubleshooting in this order can solve most HTML image display problems.

Headless CMS and Static Site Generation (SSG) with Astro Headless CMS and Static Site Generation (SSG) with Astro Jul 26, 2025 am 07:31 AM

Use headless CMS in conjunction with Astro's static site generation (SSG) to build high-performance, content-driven websites. 2.Astro gets content from headless CMS (such as Sanity, Contentful, Strapi, WordPress, or DatoCMS) through APIs and pre-renders as static pages. 3. Use getStaticPaths() to generate the page path, obtain data through CMSAPI calls, and separate the content from the front-end. 4. Advantages include excellent performance (fast loading, SEO-friendly), friendly editing experience, architectural flexibility, high security and scalability. 5. Content updates require rebuilding of the site, and you can use CMSwebhook to touch

The Importance of Semantic HTML for SEO and Accessibility The Importance of Semantic HTML for SEO and Accessibility Jul 30, 2025 am 05:05 AM

SemanticHTMLimprovesbothSEOandaccessibilitybyusingmeaningfultagsthatconveycontentstructure.1)ItenhancesSEOthroughbettercontenthierarchywithproperheadinglevels,improvedindexingviaelementslikeand,andsupportforrichsnippetsusingstructureddata.2)Itboostsa

How does the HTML5 parser handle errors? How does the HTML5 parser handle errors? Aug 02, 2025 am 07:51 AM

HTML5parsershandlemalformedHTMLbyfollowingadeterministicalgorithmtoensureconsistentandrobustrendering.1.Formismatchedorunclosedtags,theparserautomaticallyclosestagsandadjustsnestingbasedoncontext,suchasclosingabeforeaandreopeningitafterward.2.Withimp

Defining custom vocabularies using HTML5 Schema.org markup. Defining custom vocabularies using HTML5 Schema.org markup. Jul 31, 2025 am 10:50 AM

The Schema.org tag helps search engines understand the structured data format of web page content through semantic tags (such as item scope, item type, itemprop); it can be used to define custom vocabulary, methods include extending existing types or using additionalType to introduce new types; in actual applications, keeping the structure clear, using official attributes first, testing code validity, and ensuring that custom types are accessible; precautions include accepting partial support, avoiding spelling errors, and choosing a suitable format such as JSON-LD.

What are HTML5 data attributes? What are HTML5 data attributes? Aug 06, 2025 pm 05:39 PM

HTML5dataattributesarecustom,validHTMLattributesusedtostoreextrainformationinelementsforJavaScriptorCSS.1.Theyaredefinedasdata-*attributes,likedata-user-id="123".2.Theyallowembeddingprivate,customdatadirectlyinmarkupwithoutaffectinglayoutor

How to handle mouse events on an HTML5 canvas? How to handle mouse events on an HTML5 canvas? Aug 02, 2025 am 06:29 AM

To correctly handle mouse events on HTML5 canvas, first add an event listener to canvas, then calculate the coordinates of the mouse relative to canvas, then judge whether it interacts with the drawn object through geometric detection, and finally realize interactive modes such as drag and drop. 1. Use addEventListener to bind mousedown, mousemove, mouseup and mouseleave events for canvas; 2. Use getBoundingClientRect method to convert clientX/clientY to coordinates relative to canvas; 3. Detect mouse through manual geometric calculations (such as the distance formula of rectangle boundary or circle)

Optimizing Font Loading and Performance on the Web Optimizing Font Loading and Performance on the Web Jul 26, 2025 am 04:17 AM

Preloadonly1–2criticalfontsusingrel="preload"withas="font",type="font/woff2",andcrossorigintospeedupdeliverywithoutblockingotherresources.2.Usefont-display:swapin@font-facetoensuretextisvisibleimmediately,preventingFOITa

See all articles