簡體   English   中英

如何創建自己的 HTML 標簽?

[英]how can i create my own HTML tag?

how can i create my own html tags in HTML or HTML5 so i can make my own html tag and css library such as

<mymenu> ul li or some text </mymenu>

<heading> Yeah My Own Heading</heading>

他們有辦法做到這一點嗎? 如果是的話,請告訴我我真的很好奇。 並告訴我在制作個性化標簽后我會遇到什么問題(如果你知道的話)。

這樣做的“正確”方法是使用類: <div class="mymenu"> 話雖如此,我所知道的每個瀏覽器都會很好地顯示您的<mymenu>標記,並且您可以根據需要設置它的樣式:

mymenu {
    display    : block;
    background : teal;
}

演示: http://jsfiddle.net/cwolves/DPMCM/2/

請注意,IE<9 不會立即正確顯示。 為了解決這個問題,只需在頁面上的任何位置(在創建元素之前)使用以下 JS:

document.createElement('mymenu');

這將告訴 IE CSS 引擎mymenu標簽存在。

這是 html,而不是 xml。 正確的做法是使用<div>並應用您自己的.mymenu class ,您可以將其設置為看起來像一個菜單,或者一個標題 class 來定義它的外觀。

是的,有辦法!

CSS 代碼:

mymenu {
    display    : block;
    background : black;

}

heading {
    font-family: cursive;
    /* MORE CUSTOMIZE */
}

HTML 代碼:

<mymenu> ul li or some text </mymenu>
<heading> Yeah My Own Heading</heading>

或者,如果您想自定義 h1..

h1 {
/*etc..*/
}

可以在 < IE9 中執行自定義元素,但它需要(無 javascript)小心 doctypes、命名空間以及完全正確的 xhtml,一個匹配的 DTD。

像這樣的東西...

<!DOCTYPE html SYSTEM "http://your.domain/xhtml-custom.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' 
      xmlns:custom="http://your.domain/" 
      xml:lang='en-US'>

DTD 包含諸如...

<!ENTITY % attrs "%coreattrs; %i18n; %events;">
<!ENTITY % custom "custom:attribution | custom:quote ">
<!ENTITY % block "p | div | isindex |fieldset | table | %custom; ">
<!ENTITY % Flow "(#PCDATA | %block; | form )*">
<!ENTITY % custom "custom:attribution | custom:quote">
<!ELEMENT custom:attribution %Flow;>
<!ATTLIST custom:attribution %attrs;>

...等等。

您最終會遇到需要命名空間(例如 custom:customtag)的情況,並且使用 CSS 定位它需要轉義冒號......

custom\:customtag { display:block; }

...這太麻煩了——考慮到使用自定義元素的全部意義在於產生更多的語義標記。

我在 IE6 時代對此進行了詳細調查,當時 xhtml 似乎是未來並解決了所有問題,但由於解決方案的剪切繁瑣性質,我從未試圖在任何地方實施它。

無論如何,世界大多放棄了xhtml,因為它太麻煩了。

歸根結底,為了更好的語義而定制元素幾乎不值得,因為無論你做什么,你的標記都可能會受到演示需求的影響(我已經嘗試了幾十年了)你就是不能完全在線將內容與演示分開。

在此處查看 HTML5 Shiv 的故事:

http://paulirish.com/2011/the-history-of-the-html5-shiv/

您可以使用相同的方法來啟用您的自定義標簽。

但是不要。 這只是愚蠢的。 將 span 或 div 與類一起使用。

在 CSS 中創建一個標簽,沒有 class (.) 或 id (#)。

CSS:

mymenu {
    /* Styling Here */
}

heading {
    /* Styling Here */
}

HTML:

<mymenu> ul li or some text </mymenu>
<heading> Yeah My Own Heading </heading>

為此,您可以使用 css 創建自定義標簽:

c-r {
    color:red;
}

我在Custom Markup上執行此操作。 檢查一下,因為它可能已經有了你想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM