"; } .cdvPseudo301 { color: red; } メインコンテンツへスキップ

style要素


1.仕様

STYLE要素 は、
文書あるいは文書の一部分の
スタイル情報を含みます。

style

スタイル情報 (Style info)

種類
生テキスト要素

テキストを持つことができる要素です。

使用できるコンテキスト
  • メタデータコンテンツを受け入れる要素
  • head要素の子であるnoscript要素に 0個以上

メタデータコンテンツを受け入れる要素のコンテンツとして、使用できます。
head要素の子であるnoscript要素に 0個以上を そのコンテンツとして、使用できます。

メタデータコンテンツを受け入れる要素は、head要素 です。

コンテンツモデル

適合したスタイルシートを与えるテキスト。

タグの省略

どちらのタグも省略できません。

コンテンツ属性
グローバル属性 他
media属性
該当するメディア
blocking属性
要素が潜在的にレンダリングをブロックしているかどうか
title属性
この要素に特別なセマンティクスで、CSSスタイルシートセット名。

type属性がありましたが、廃止されました。作成者はstyle要素にtype属性を指定すべきではありません。属性が存在する場合、その値は 'text/css'の大文字小文字を区別しないASCIIマッチでなければなりません。データ・ブロックでは、 styleの代わりに scriptをコンテナとして使います。

DOM インターフェイス
HTMLStyleElement
  1. [Exposed=Window]
  2. interface HTMLStyleElement : HTMLElement {
  3. [HTMLConstructor] constructor();
  4.  
  5. attribute boolean disabled;
  6. [CEReactions] attribute DOMString media;
  7. [SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;
  8.  
  9. // also has obsolete members
  10. };
  11.  
  12. HTMLStyleElement includes LinkStyle;
HTMLElement
  1. [Exposed=Window]
  2. interface HTMLElement : Element {
  3. [HTMLConstructor] constructor();
  4.  
  5. // metadata attributes
  6. [CEReactions] attribute DOMString title;
  7. [CEReactions] attribute DOMString lang;
  8. [CEReactions] attribute boolean translate;
  9. [CEReactions] attribute DOMString dir;
  10.  
  11. // user interaction
  12. [CEReactions] attribute (boolean or unrestricted double or DOMString)? hidden;
  13. [CEReactions] attribute DOMString inert;
  14. undefined click();
  15. [CEReactions] attribute DOMString accessKey;
  16. readonly attribute DOMString accessKeyLabel;
  17. [CEReactions] attribute boolean draggable;
  18. [CEReactions] attribute boolean spellcheck;
  19. [CEReactions] attribute DOMString writingSuggestions;
  20. [CEReactions] attribute DOMString autocapitalize;
  21.  
  22. [CEReactions] attribute [LegacyNullToEmptyString] DOMString innerText;
  23. [CEReactions] attribute [LegacyNullToEmptyString] DOMString outerText;
  24.  
  25. ElementInternals attachInternals();
  26.  
  27. // The popover API
  28. undefined showPopover();
  29. undefined hidePopover();
  30. boolean togglePopover(optional boolean force);
  31. [CEReactions] attribute DOMString? popover;
  32. };
  33.  
  34. HTMLElement includes GlobalEventHandlers;
  35. HTMLElement includes ElementContentEditable;
  36. HTMLElement includes HTMLOrSVGElement;
Element
  1. [Exposed=Window]
  2. interface Element : Node {
  3. readonly attribute DOMString? namespaceURI;
  4. readonly attribute DOMString? prefix;
  5. readonly attribute DOMString localName;
  6. readonly attribute DOMString tagName;
  7.  
  8. [CEReactions] attribute DOMString id;
  9. [CEReactions] attribute DOMString className;
  10. [SameObject, PutForwards=value] readonly attribute DOMTokenList classList;
  11. [CEReactions, Unscopable] attribute DOMString slot;
  12.  
  13. boolean hasAttributes();
  14. [SameObject] readonly attribute NamedNodeMap attributes;
  15. sequence<DOMString> getAttributeNames();
  16. DOMString? getAttribute(DOMString qualifiedName);
  17. DOMString? getAttributeNS(DOMString? namespace, DOMString localName);
  18. [CEReactions] undefined setAttribute(DOMString qualifiedName, DOMString value);
  19. [CEReactions] undefined setAttributeNS(DOMString? namespace, DOMString qualifiedName, DOMString value);
  20. [CEReactions] undefined removeAttribute(DOMString qualifiedName);
  21. [CEReactions] undefined removeAttributeNS(DOMString? namespace, DOMString localName);
  22. [CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
  23. boolean hasAttribute(DOMString qualifiedName);
  24. boolean hasAttributeNS(DOMString? namespace, DOMString localName);
  25.  
  26. Attr? getAttributeNode(DOMString qualifiedName);
  27. Attr? getAttributeNodeNS(DOMString? namespace, DOMString localName);
  28. [CEReactions] Attr? setAttributeNode(Attr attr);
  29. [CEReactions] Attr? setAttributeNodeNS(Attr attr);
  30. [CEReactions] Attr removeAttributeNode(Attr attr);
  31.  
  32. ShadowRoot attachShadow(ShadowRootInit init);
  33. readonly attribute ShadowRoot? shadowRoot;
  34.  
  35. Element? closest(DOMString selectors);
  36. boolean matches(DOMString selectors);
  37. boolean webkitMatchesSelector(DOMString selectors); // legacy alias of .matches
  38.  
  39. HTMLCollection getElementsByTagName(DOMString qualifiedName);
  40. HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
  41. HTMLCollection getElementsByClassName(DOMString classNames);
  42.  
  43. [CEReactions] Element? insertAdjacentElement(DOMString where, Element element); // legacy
  44. undefined insertAdjacentText(DOMString where, DOMString data); // legacy
  45. };
GlobalEventHandlers
  1. interface mixin GlobalEventHandlers {
  2. attribute EventHandler onabort;
  3. attribute EventHandler onauxclick;
  4. attribute EventHandler onbeforeinput;
  5. attribute EventHandler onbeforematch;
  6. attribute EventHandler onbeforetoggle;
  7. attribute EventHandler onblur;
  8. attribute EventHandler oncancel;
  9. attribute EventHandler oncanplay;
  10. attribute EventHandler oncanplaythrough;
  11. attribute EventHandler onchange;
  12. attribute EventHandler onclick;
  13. attribute EventHandler onclose;
  14. attribute EventHandler oncontextlost;
  15. attribute EventHandler oncontextmenu;
  16. attribute EventHandler oncontextrestored;
  17. attribute EventHandler oncopy;
  18. attribute EventHandler oncuechange;
  19. attribute EventHandler oncut;
  20. attribute EventHandler ondblclick;
  21. attribute EventHandler ondrag;
  22. attribute EventHandler ondragend;
  23. attribute EventHandler ondragenter;
  24. attribute EventHandler ondragleave;
  25. attribute EventHandler ondragover;
  26. attribute EventHandler ondragstart;
  27. attribute EventHandler ondrop;
  28. attribute EventHandler ondurationchange;
  29. attribute EventHandler onemptied;
  30. attribute EventHandler onended;
  31. attribute OnErrorEventHandler onerror;
  32. attribute EventHandler onfocus;
  33. attribute EventHandler onformdata;
  34. attribute EventHandler oninput;
  35. attribute EventHandler oninvalid;
  36. attribute EventHandler onkeydown;
  37. attribute EventHandler onkeypress;
  38. attribute EventHandler onkeyup;
  39. attribute EventHandler onload;
  40. attribute EventHandler onloadeddata;
  41. attribute EventHandler onloadedmetadata;
  42. attribute EventHandler onloadstart;
  43. attribute EventHandler onmousedown;
  44. [LegacyLenientThis] attribute EventHandler onmouseenter;
  45. [LegacyLenientThis] attribute EventHandler onmouseleave;
  46. attribute EventHandler onmousemove;
  47. attribute EventHandler onmouseout;
  48. attribute EventHandler onmouseover;
  49. attribute EventHandler onmouseup;
  50. attribute EventHandler onpaste;
  51. attribute EventHandler onpause;
  52. attribute EventHandler onplay;
  53. attribute EventHandler onplaying;
  54. attribute EventHandler onprogress;
  55. attribute EventHandler onratechange;
  56. attribute EventHandler onreset;
  57. attribute EventHandler onresize;
  58. attribute EventHandler onscroll;
  59. attribute EventHandler onscrollend;
  60. attribute EventHandler onsecuritypolicyviolation;
  61. attribute EventHandler onseeked;
  62. attribute EventHandler onseeking;
  63. attribute EventHandler onselect;
  64. attribute EventHandler onslotchange;
  65. attribute EventHandler onstalled;
  66. attribute EventHandler onsubmit;
  67. attribute EventHandler onsuspend;
  68. attribute EventHandler ontimeupdate;
  69. attribute EventHandler ontoggle;
  70. attribute EventHandler onvolumechange;
  71. attribute EventHandler onwaiting;
  72. attribute EventHandler onwebkitanimationend;
  73. attribute EventHandler onwebkitanimationiteration;
  74. attribute EventHandler onwebkitanimationstart;
  75. attribute EventHandler onwebkittransitionend;
  76. attribute EventHandler onwheel;
  77. };
ElementContentEditable
  1. interface mixin ElementContentEditable {
  2. [CEReactions] attribute DOMString contentEditable;
  3. [CEReactions] attribute DOMString enterKeyHint;
  4. readonly attribute boolean isContentEditable;
  5. [CEReactions] attribute DOMString inputMode;
  6. };
HTMLOrSVGElement
  1. interface mixin HTMLOrSVGElement {
  2. [SameObject] readonly attribute DOMStringMap dataset;
  3. attribute DOMString nonce; // intentionally no [CEReactions]
  4.  
  5. [CEReactions] attribute boolean autofocus;
  6. [CEReactions] attribute long tabIndex;
  7. undefined focus(optional FocusOptions options = {});
  8. undefined blur();
  9. };
LinkStyle
  1. interface mixin LinkStyle {
  2. readonly attribute CSSStyleSheet? sheet;
  3. };
ARIA in HTML
暗黙的="No" ARIAロール="No" aria-* 属性="No"

暗黙的な ARIAセマンティクスがありませんが、意味はあり、この意味は、ARIA によって提供されず、ARIA のユーザーに公開されていないロール、状態、プロパティで表現される可能性があります。

作成者は、暗黙的な ARIAセマンティクス, または HTML要素のネイティブセマンティクスを上書きしてはいけません。

記述できる aria-* 属性はありません。

レンダリング
ボックスを生成しない
非表示の要素
  1. style {
  2. display: none;
  3. }

CSS を大雑把に読むと、 要素とその子孫はボックスやテキストシーケンスを生成しません。 などになります。

説明
style要素は、文書あるいは文書の一部分のスタイル情報を表します。

style要素は、使用すると、作成者はCSSスタイルシートをドキュメントに埋め込む事ができます。style要素は、スタイリング処理モデルへのいくつかの入力の1つです。この要素は、ユーザーのコンテンツを表すものではありません。
スタイルシート(Style Sheets.)とは、文書の見栄えに関する情報を記録等したデータやファイル等の事です。

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>style要素の例 - (Example of style element) | 離珠のHTMLでしぃ!</title>
  6. <style>body { color: white; background-color: teal; }</style>
  7. </head>
  8. <body>
  9. <h1>style要素の例</h1>
  10. <p>この例の style要素は、body要素の文字の色を白, 背景の色を緑と指定しています。</p>
  11. </body>
  12. </html>

このサンプルへのリンクです。
(./exstyle.htm)

JSONデータ
JSON形式での、データのイメージです。
  1. {
  2. "style": {
  3. "check": "2024-09-01",
  4. "group": "Metadata",
  5. "element": "- - %StyleSheet; -- style info. (Raw text elements.) [Metadata] --",
  6. "attlist": {
  7. "%Global;": "",
  8. "media": "%MediaDesc; #IMPLIED -- designed for use with these media --",
  9. "title": "%Text; #IMPLIED -- advisory title --",
  10. "blocking": "%Blocking; #IMPLIED -- Whether the element is potentially render-blocking --",
  11. },
  12. "comment": "スタイル情報 (Style info)",
  13. "point": "文書あるいは文書の一部分のスタイル情報を表します。",
  14. "description": [
  15. "使用すると、作成者はCSSスタイルシートをドキュメントに埋め込む事ができます。style要素は、スタイリング処理モデルへのいくつかの入力の1つです。この要素は、ユーザーのコンテンツを表すものではありません。",
  16. "スタイルシート(Style Sheets.)とは、文書の見栄えに関する情報を記録等したデータやファイル等の事です。",
  17. ],
  18. "kind": "Raw",
  19. "categories": "Metadata",
  20. "used": [
  21. "parent (%Metadata)*",
  22. "HEAD NOSCRIPT (STYLE)*",
  23. ],
  24. "contentmodel": "適合したスタイルシートを与えるテキスト。",
  25. "tag": [
  26. "-",
  27. "-",
  28. ],
  29. "attributes": {
  30. "media": "該当するメディア",
  31. "blocking": "要素が潜在的にレンダリングをブロックしているかどうか",
  32. "title": "この要素に特別なセマンティクスで、CSSスタイルシートセット名。",
  33. },
  34. "attributessupplement": [
  35. "type属性がありましたが、廃止されました。作成者はstyle要素にtype属性を指定すべきではありません。属性が存在する場合、その値は 'text/css'の大文字小文字を区別しないASCIIマッチでなければなりません。データ・ブロックでは、 styleの代わりに scriptをコンテナとして使います。",
  36. ],
  37. "dominterface": "HTMLStyleElement",
  38. "rendering": "Hidden",
  39. "reference": [
  40. {
  41. "title": "HTML Living Standard. 4.2.6 The style element.",
  42. "href": "https://html.spec.whatwg.org/multipage/semantics.html#the-style-element",
  43. "heading": "HTML Living Standard. 4.2.6 The style element",
  44. "sub": "Last Updated 27 August 2024",
  45. },
  46. {
  47. "title": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements.",
  48. "href": "https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements",
  49. "heading": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements",
  50. "sub": "Last Updated 27 August 2024",
  51. },
  52. {
  53. "title": "HTML 5.2 W3C Recommendation. 4.2.6 The style element. W3C Recommendation, 14 December 2017.",
  54. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/document-metadata.html#the-style-element",
  55. "heading": "HTML 5.2 W3C 勧告. 4.2.6 The style element",
  56. "sub": "W3C勧告、2017年12月14日",
  57. },
  58. {
  59. "title": "HTML 5.2 W3C Recommendation. 10.3.1. Hidden elements.",
  60. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/rendering.html#hidden-elements",
  61. "heading": "HTML 5.2 W3C 勧告. 10.3.1. Hidden elements",
  62. "sub": "W3C勧告、2017年12月14日",
  63. },
  64. {
  65. "title": "HTML 5.1 W3C Recommendation. 4.2.6 The style element. W3C Recommendation, 1 November 2016.",
  66. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/document-metadata.html#the-style-element",
  67. "heading": "HTML 5.1 W3C 勧告. 4.2.6 The style element",
  68. "sub": "W3C勧告、2016年11月1日",
  69. },
  70. {
  71. "title": "HTML 5.1 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 1 November 2016.",
  72. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/rendering.html#hidden-elements",
  73. "heading": "HTML 5.1 W3C 勧告. 10.3.1. Hidden elements",
  74. "sub": "W3C勧告、2016年11月1日",
  75. },
  76. {
  77. "title": "HTML 5 W3C Recommendation. 4.2.6 The style element. W3C Recommendation, 28 October 2014.",
  78. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/document-metadata.html#the-style-element",
  79. "heading": "HTML 5 W3C 勧告. 4.2.6 The style element",
  80. "sub": "W3C勧告、2014年10月28日",
  81. },
  82. {
  83. "title": "HTML 5 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 28 October 2014.",
  84. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/rendering.html#hidden-elements",
  85. "heading": "HTML 5 W3C 勧告. 10.3.1. Hidden elements",
  86. "sub": "W3C勧告、2014年10月28日",
  87. },
  88. {
  89. "title": "HTML 4.01 Specification W3C Recommendation. 14.2.3 Header style information: the STYLE element. W3C Recommendation 24 December 1999.",
  90. "href": "https://www.w3.org/TR/html401/present/styles.html#h-14.2.3",
  91. "heading": "HTML 4.01 Specification W3C 勧告. 14.2.3 Header style information: the STYLE element",
  92. "sub": "W3C勧告1999年12月24日",
  93. },
  94. {
  95. "title": "HTML 3.2 Reference Specification. W3C Recommendation 14-Jan-1997.",
  96. "href": "https://www.w3.org/TR/2018/SPSD-html32-20180315/#style",
  97. "heading": "HTML 3.2 Reference Specification STYLE and SCRIPT",
  98. "sub": "W3C勧告1997年1月14日",
  99. },
  100. {
  101. "title": "MDN Web Docs. HTML <style>: The Style Information element.",
  102. "href": "https://developer.mozilla.org/ja/docs/Web/HTML/Element/style",
  103. "heading": "<style>: スタイル情報要素 - HTML: ハイパーテキストマークアップ言語 | MDN",
  104. },
  105. {
  106. "title": "CSS Text Module Level 3.",
  107. "href": "https://drafts.csswg.org/css-text-3/",
  108. "heading": "CSS Text Module Level 3",
  109. "sub": "編集者草案2023年10月20日",
  110. },
  111. ],
  112. "version": {
  113. "first": "HTML3",
  114. "latest": "HTML Living Standard",
  115. },
  116. }
  117. }

注意: 簡易に描画する為に、各項目の後ろに , を付与しています。JSON の形式では、末尾の項目の後には , は付けません。

最終確認日

2024-09-01


2.使用できるコンテキスト

style要素 は、
head要素に
配置できます。

style要素が使用できるコンテキスト(Context文脈)は、head要素head要素内にある noscript要素コンテンツとしてです。

document
html
head
body
style
noscript
フロー
style

style要素は、メタデータコンテンツを受け入れる要素に配置できます。
head要素にも, head要素内にあるnoscript要素の内部にも配置できます。


3.コンテンツモデル

style要素 は、
適合したスタイルシートを
与えるテキストを
配置できます。

style要素子や子孫としてどのようなコンテンツ(Content内容) が含まれなければならないかというと、適合するスタイルシート」を与えるテキストです。

style要素は、テキストを持つことができる生テキスト要素です。

type属性を用いてスタイル情報の言語を指定できます。それに適合したスタイルシートを与えるテキストが配置できます。type属性の既定値は "text/css" (CSS) です。

スタイルシート(Style Sheets.)とは、文書の見栄えに関する情報を記録等したデータやファイル等の事です。

test
  1. <style>
  2. .cdvPseudo301::after { content: "< /style>"; }
  3. .cdvPseudo301 { color: blue; }
  4. .cdvPseudo301::before { content: "</style>"; }
  5. .cdvPseudo301 { color: red; }
  6. </style>
  1. <div class="cdvPseudo301">test</div>

適合したスタイルシートを与えるテキストと言ってもHTML文書の中なので、HTMLのタグ, コメントには注意が必要です。 要素の終了とされたりコメントの終了とされたりする可能性があります。 普段から>の扱いに注意して文字参照を使うとか、文字列を分割して結合するなど工夫が必要だと思います。
尚、外部ファイルにして読み込む場合はこの配慮は不要ですが、習慣づけとして意識した方が無難です。


4.コンテンツ属性

style要素 は、
グローバル属性と、
media,
blocking を
設定できます。

style要素に指定することができる属性は、グローバル属性media属性, blocking属性 を設定できます。
また、グローバル属性である title属性ですが、この要素では特別な意味を持ちます。

media属性

  1. <style media="screen and (max-width: 640px)">

media属性は、メディアクエリを指定します。参照先のリソースが適用されるメディアを指定します

  • media : 属性名
  • "screen and (max-width: 640px)" : 属性値 (メディアクエリ)
    • screen : メディアタイプ
    • and : 論理演算子
    • (max-width: 640px) : メディア特性

メディアタイプは、端末の全般的なカテゴリを説明します。all(すべてのデバイス), print(プリンタ), screen(プリンタ以外のすべてのデバイス), tty(テレタイプなど), tv(テレビ), projection(プロジェクター), handheld(携帯電話), braille(点字ディスプレイ), embossed(点字プリンタ), aural(音声デバイス), speech(音声出力機器) などがあります。

メディア特性は、特定のユーザーエージェントや、出力端末や、環境などの特性を記述します。特性式は、括弧で囲む必要があります。

論理演算子は、and 演算子(論理積), カンマ区切り(論理和), not 演算子(論理否定), only 演算子(古いブラウザー対応) があります。

blocking属性

  1. <style blocking="render">

blocking属性は、render キーワードを指定した場合、要素は潜在的にレンダリング ブロックになります。これは個々の要素で定義されます。
デフォルトでは、要素は暗黙的にレンダリングをブロックする可能性はありません。

title属性

  1. <style title="Recommendation">

title属性は、この要素に関する特別なセマンティクスがあります。title属性によって CSSスタイルシートセットが定義されます。

link要素title属性と同様に、style要素title属性は、タイトルのないリンクが親要素のタイトルを継承せず、単にタイトルがないという点で、他のほとんどの要素のグローバル属性title属性とは異なります。

type属性

  1. <style type="text/css">

type属性は、組み込みリソースのタイプを指定します。HTML Living Standard で廃止された属性です。常に text/css として処理されているという事の様です。


5.DOM インターフェイス

style要素
直接表す
プロパティは
ありません。

style要素を JavaScript などで操作するには、documentgetElementById()メソッドを用いてid属性で指定して、style要素を取得する方法があります。この時に取得できる値の形式はHTMLStyleElement インターフェイスです。

他には、documentquerySelectorAll()メソッドを用いて要素セレクタstyleと指定して用いるとか、name属性があればdocumentgetElementsByName()メソッドを用いるとか、状況, 目的によりいろいろあります。

この例示の文字の色は、です。
  1. <form class="cfmSample501" onchange="document.getElementById('isyJs501').textContent = document.getElementById('iseJs502').value;">
  2. この例示の文字の色は、<select id="iseJs502">
  3. <option value=".cfmSample501 { color: inherit; }">継承する</option>
  4. <option value=".cfmSample501 { color: red; }"></option>
  5. <option value=".cfmSample501 { color: blue; }"></option>
  6. </select>です。
  7. </form>
  8. <style id="isyJs501">
  9. .cfmSample501 { color: inherit; }
  10. </style>

style要素id属性を指定して、そのid属性で要素を取得する例です。例示では、cssのテキストそのままを選択して設定する処理としています。例示で動的に表示する為に、フォームの内容が変更された時に処理するコードとしています。

この例は、style要素を扱う例なので、スタイルシートをスクリプトで扱うという視点ではありません。事前にスタイルシートをクラスセレクタで用意して、class属性を更新するとか、style属性を更新するとか、状況に応じて検討する事が望ましいです。


6.レンダリング

  1. style {
  2. display: none;
  3. }

style要素ユーザーエージェントレベルスタイルシートデフォルトでは、displayプロパティの値は none であり、この要素とその子孫はボックステキストシーケンスを生成しません。

文書あるいは文書の一部分のスタイル情報を表すメタデータを記述する為の要素なので、ビジュアルメディアでボックスを生成する様な指定は行わないのが良いでしょう。

  1. <style class="csySample601">
  2. .csySample601 {
  3. display: block;
  4. }
  5. /* <h1>見出し</h1> */
  6. /*
  7. .csySample601 {
  8. color: red;
  9. }
  10. */
  11. </style>

スタイルシートの記述を見えるようにした例です。ビジュアルメディアでボックスを生成するだけで、スタイルシートの機能は変わりません。CSSのコメントは、CSSとしてはコメントとして扱われています。ただ、CSSのコメントはHTMLでは関係ないので、そのまま見えます。また、これはテキストとして扱われているので、そこに書かれているHTMLタグはテキスト文字列として扱われ、タグとは扱われません。

とはいえ、style要素終了タグを記述すれば、style要素はそこまでと認識されるので、注意は必要です。デバッグでコメントアウトしようとした時にスタイルシートの記述内に-->があった場合も同じです。

尚、この例示は検証用に記述してみたものです。HTML の規則としては、そもそもstyle要素は、この例示の箇所に記述できません。


7.気を付ける点

文字参照

CSSのcontentプロパティで文字列として --> を扱いたい場合は、--\03E文字参照を用いておくべきです。通常は文字列として扱われていても <!-- からコメントの終わりを探されている途中に文字列の --> があると文字列として扱われずにコメントの終わりの記述として扱われてしまいます。 あくまで、HTML上で <!-- でコメントが始まり、style要素開始タグもコメント扱いされてスタイル情報もコメント内として扱われる可能性を考慮しての注意点です。状況としては、コメントアウトしたい途中に style要素があるという可能性です。

文字列として </style> を扱いたい場合は、\03C/style\03E文字参照を用いておくべきです。style要素内の記述は、HTML文書内の記述という事で、タグの解釈が先という事です。尚、文字列なので分けて結合する記述でも充分です。

  1. <style>
  2. .cdvPseudo701::before { content: "TEXT --\03E"; }
  3. .cdvPseudo701::after { content: "TEXT \03C/style\03E"; }
  4. </style>
  1. <div class="cdvPseudo701">
  2. </div>

HTML文書でのコメントの終わりを示す --> と、style要素の終わりを示すstyle要素終了タグを示す </style> の文字列をスタイルシート内で用いる例です。

スタイル情報

スタイル情報を HTML文書に適用させる方法として、link要素を用いて外部スタイルシートを適用する, style要素を用いてスタイル情報を含んで適用する, style属性を用いて個々の要素にスタイル情報を適用する 以上の方法があります。HTMLファイル内に書き込むか、CSSファイルで外部ファイルとして書いて読み込むか、用途によって使い分けるべきです。

スタイルシートで装飾するという意図からも、出来るだけ別ファイルに分けるべきです。当サイトでは、一部でCSSの装飾自体が記事である箇所に於いて、意図的にHTMLファイルに書き込む方法を用いています。

尚、理想は別としてlink要素を理解しないで用いるのはセキュリティから良くないので、とりあえずはstyle要素を用いる事をお勧めします。出来ればlink要素を早めに理解するなり外部スタイルシートを適用する記述だけを定型で学んで用いるのが良いかと思います。

CSS には優先度という物があり、style属性での記述の方がstyle要素, link要素での記述より優先して扱われます。あくまで記述箇所の重みづけでの事なので、他の点も含めて優先順位は決まります。 記述箇所によっては、装飾の結果が変わる事があるので、この事は意識しておくべきかと思います。


8.付記