Taghtml5

HTML 5: When to use <a> or <button>

tl:dr

  • <a> is used for page navigation
  • <button> is used for actions on the page
  • <input type="button" /> is used in a form and the value is used in the form

The Problem

Lets take a look at this small piece of code:

Now half of the people I showed this piece of code, said: well there is nothing wrong with the code.
That is a perfectly fine way of defining a button.

If you take a closer look, you will discover a couple of smaller issues with this piece of code.

First you would notice that hovering over the button you get a text cursor instead of an ‘hand’-cursor.
This is caused by leaving the attribute “href” undefined, you can fix it via defining something like ‘#’ or via CSS.

Then you would notice that by defining the “href” tag you would cause the browser to navigate to another page or the top of the page.
To prevent this you would then need to add some Javascript code and call ‘event.preventDefault()’, or add to your previous hack and define the href as ‘#0’. This solves the problem by relying on the browser not knowing what to do with illegal element ids (ids can never start with a number).

Voila, you are now in the situation that you are using some hack pattern that degrades the readability of your code.
Someone may come along and assume that someone made a mistake defining the href attribute /or used a placeholder and forgot to replace it.

The Solution

Now let’s improve the readability of the code:

We are now using the correct tag <button>, no need for any hacks to fix the cursor or some wierd href, and no additional line of Javascript.
You will need CSS to ensure that the button looks the same in all Browsers, however you would probably anyway used CSS to style your <a>-button.

But what about IE8? – for that we set the type=’button’, and IE8 is almost of no concern anymore (even for big corporate customers)

<button> vs <input type="button" />

Both types of buttons work in practice the same way.
However the Button element allows you to add content elements, like an image.
And usually you would use the <input> tag in a <form>.

As we are not working with a form and just want to have a button to execute some JS-Code I would use the Button tag.

Summary

  • <a> is used for page navigation
  • <button type='button'> is used for actions on the page, can contain other html elements
  • <input type="button" /> is used in a form

Some More Reading

Forcing IE 10 to render pages as IE 10

Web Design designed by Simple Icons from the thenounproject.com

Now isn’t that a silly title, sadly it’s one of those things I never thought I would have to deal with. Especially as Internet Explorers market-share is down to 8.8% in June. And seriously people switch to something that supports most of the web standard.

As a matter of fact Internet Explorer 10 defaults to render Webpages with its built in Compatibility Mode (IE 7) when displaying Intranet Websites.

Basically the thinking behind it is, we have an Advanced Browser (mostly) capable of rendering HTML5 Websites, but our Corporate Partners are so backwards that they are still running age-old Websites that possibly could be displayed incorrectly. In matter of fact they call this stupid Feature “Smart Defaults“.

Now as a HTML5 developer you would think, well yeah whatever I’ll just add a meta-tag to the <head></head>

<meta http-equiv="X-UA-Compatible" content="IE=edge">

Well IE 10 looks at it and says “That can’t be right, I am in the intranet, and we don’t have cool HTML 5 sites here! I better render this in compatibility mode!!!”

So the only way to force Internet Explorer to do the right thing is to actually modify the settings of your webserver to add the exact same meta information to the HTTP Response object; Only then IE 10 will say, ok fine ill render it with my weird IE 10 render engine.

You can verify this using the Chrome Developer Tools

  1. Open Chrome, Press F12 to view the Development Tools
  2. Switch to the tab ‘Network’
  3. Navigate to the specific Page
  4. Verify that in the Get Request, the response Header “X-UA-Compatible: IE=10” or “IE=edge”Chrome-IE10-Header

© 2017 Neal Bürger

Theme by Anders NorénUp ↑