Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom tags for html [closed]

Tags:

html

tags

I'd like to learn how to create custom tags for html, with special attributes and behavior, if someone can give advice I'd be the most grateful.

like image 564
Aktheus Avatar asked Apr 15 '11 22:04

Aktheus


People also ask

How do you create a closing tag in HTML?

An opening tag begins a section of page content, and a closing tag ends it. For example, to markup a section of text as a paragraph, you would open the paragraph with an opening paragraph tag <p> and close it with a closing paragraph tag </p> (closing tags always proceed the element with a /).

Can I create custom HTML tags?

All standard rules of HTML elements apply to custom elements. Just like standard elements, you can create a custom element in the DOM using JavaScript, or declare it in HTML.

Can custom elements be self closing?

Custom elements cannot be self-closing because HTML only allows a few elements to be self-closing.

Do all HTML tags need to be closed?

No, not all the tags are ending tags. Tags may be either paired or unpaired (or single). Paired tags need to be closed, i.e. they contains both ending and non-ending tags, whereas unpaired tags need not be closed, i.e. they only contain opening tags.


2 Answers

There's an interesting and in depth article from HTML5Rocks.com on how to work with custom elements : Custom Elements: Defining New Elements in HTML

Here's an excerpt from the article on how to do it.

Instantiating elements

The common techniques of creating elements still apply to custom elements. As with any standard element, they can be declared in HTML or created in DOM using JavaScript. Instantiating custom tags

Declare them:

<x-foo></x-foo> 

Create DOM in JS:

var xFoo = document.createElement('x-foo'); xFoo.addEventListener('click', function(e) {   alert('Thanks!'); }); 

Use the new operator:

var xFoo = new XFoo(); document.body.appendChild(xFoo); 
like image 68
Jed Burke Avatar answered Sep 19 '22 07:09

Jed Burke


Depending on what you mean by "special attributes and behavior," you can "create" custom HTML tags right now. The following shows up in all browsers, and even works with the various JavaScript methods:

<my-book data-pages="400" data-author="Nietzsche">The Wanderer and His Shadow</my-book> 

There are just a couple things you have to keep in mind:

  1. Hyphenation! Custom elements should consist of at least one - like my-book or app-menu or header-title etc. Just, don't use data-* since it's reserved for data- attributes.

  2. All custom elements have a display of inline by default. You can change that with CSS or JavaScript, however.

  3. Internet Explorer does not recognize any of these elements unless you first "create" them with JavaScript:

    document.createElement('my-book'); 

So you have to do that before you can use them in your CSS, HTML, or JS.

like image 23
sdleihssirhc Avatar answered Sep 18 '22 07:09

sdleihssirhc