How to create a button that acts like a link in HTML5?
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.
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:

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='https://example.com'"> Go to Example </button>
Pros:

- 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.

<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='https://example.com'" role="link" tabindex="0"> Go to Example </button>
You can also add key event support for Enter/Space:
<button onclick="window.location.href='https://example.com'" onkeydown="if(event.key==='Enter') window.location.href='https://example.com'" 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.

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

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

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

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.

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

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)

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