Reputation: 343
I've seen this done before on multiple websites.
What I'm looking to do is use tags like <header>
and <description>
and <contact>
without any attributes, in place of comments, as it looks much cleaner.
However, I'm wondering whether or not they'll be any repercussions for doing so, and whether or not it's safe to do?
Upvotes: 5
Views: 1519
Reputation: 599
just consider:
document.createElement('custom-tag')
This means your website will only render correctly with JavaScript turned on<span>
, this means you have to write CSS to declare them as custom-tag { display: block }
<pane>
and <tab>
) in its examples on the front page.<div>
and there is no existing HTML 4/5
equivalent. This is especially true for web applications which
parse the DOM for special tags/attributes/classes to create
components (like Angular.js).Upvotes: 0
Reputation: 4753
If you want to make additional tags you can use:
document.createElement("custom")
in javascript, then you can use
<custom>bla bla bla</custom>
and apply CSS rules like this:
custom { display:none;}
but this will not be valid HTML and it is not advised to do this
Upvotes: 0
Reputation: 168655
Firstly, <header>
is in fact a valid HTML5 tag. So if you're planning to use it because it's your own custom invention and not a valid tag, then you need to come up with an alternative.
Generally speaking, it's not a good idea to add your own tags.
As you can see already from the first paragraph of this answer, there's nothing to say that a tag you've chosen as a 'custom' tag won't become a standard one in the future. The next version of HTML may well contain a <description>
tag. And it may have rules in future browsers which conflict with how you're using it. The <header>
tag probably already does this. Be extremely cautious.
In general, it's far better to use the existing HTML tags, and simply give them a class name which indicates their function. This is the standard behaviour, and is what the browsers expect.
If you must use custom tags, be very careful with styling them, as even if it works in different browsers, they may get different default styles in different browsers. This means you need to thoroughly test your site in every version of every browser. This is good practice anyway, obviously, but using custom tags makes it even more important, and more likely to throw up unexpected quirks.
Custom tags will work in most browsers. HTML5 added a bunch of new tags, and they worked just fine in almost all existing browsers, albeit with odd styling for some. The exception (as always) is IE, but even here it can be made to work fairly easily.
IE requires a special hack to get custom styles to work. There is a small Javascript tool known as the HTML5Shiv which does this hack for all the new HTML5 tags. IE has a quirk which means it won't accept tags it doesn't recognise until they've been created via Javascript, and this quirk is exploited by the script. Weird, but it works.
Therefore, if you really want to have a custom tag, it will work in all browsers except IE. In IE, you need to do a Javascript createElement()
using your custom tag, and then it will work. And then use a CSS reset stylesheet to ensure they look the same in all browsers.
So it can be done. But I still don't recommend it.
Upvotes: 0
Reputation: 35822
If you read W3C's specifications, you see that it is necessary for browsers not to bubble upo any exception. Browsers have many parsers, one for HTML, one for CSS and one for JavaScript. When you use custom tags in your HTML document, and browser tries to understand it, it simply doesn't understand. But the good point is that, it doesn't throw exception. In other words, your custom element won't cause any problem in browser.
However, you loos valid badge, that is, your HTML document won't be regarded as valid anymore.
From the point of CSS, applying CSS to tags is not limited to official HTML tags, as you can apply CSS to something like XML too (ex. Applying CSS on XML). So, you can technically format your custom element across browsers.
If you don't mind, I have a better recommendation for you. Why not sticking to community-agreed-upon HTML5 custom attributes? The start with data
prefix and can pass validation tests. Ex. <a href='...' data-custom-attribute='custom-value'></a>
.
Upvotes: 0
Reputation: 943185
Upvotes: 2
Reputation: 7061
Short answer; don't. It will not be valid (except in XHTML where you can add such tags)
It will not only break in some browsers it will be confusing for other people to read as well.
See this qustion: https://stackoverflow.com/questions/211394/when-to-use-custom-html-tags
Upvotes: 2