1.仕様
STYLE要素
は、
文書あるいは文書の一部分の
スタイル情報を含みます。
- style
-
スタイル情報 (Style info)
- 種類
-
生テキスト要素
テキストを持つことができる要素です。
- 使用できるコンテキスト
-
- メタデータ
コンテンツ を受け入れる要素 - head
要素 の子であるnoscript要素 に 0個以上
メタデータ
コンテンツ を受け入れる要素のコンテンツとして、使用できます。
head要素 の子であるnoscript要素 に 0個以上を そのコンテンツとして、使用できます。メタデータ
コンテンツ を受け入れる要素は、head要素 です。 - メタデータ
- コンテンツモデル
-
適合したスタイルシートを与えるテキスト。
- タグの省略
-
どちらのタグも省略できません。
- コンテンツ属性
-
グローバル属性 他
- media
属性 - 該当するメディア
- blocking
属性 - 要素が潜在的にレンダリングをブロックしているかどうか
- title
属性 - この要素に特別なセマンティクスで、CSSスタイルシートセット名。
type属性がありましたが、廃止されました。作成者はstyle要素にtype属性を指定すべきではありません。属性が存在する場合、その値は 'text/css'の大文字小文字を区別しないASCIIマッチでなければなりません。データ・ブロックでは、 styleの代わりに scriptをコンテナとして使います。
- media
- DOM インターフェイス
-
HTMLStyleElement
[Exposed=Window]
interface HTMLStyleElement : HTMLElement {
[HTMLConstructor] constructor();
attribute boolean disabled;
[CEReactions] attribute DOMString media;
[SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;
// also has obsolete members
};
HTMLStyleElement includes LinkStyle;
HTMLElement
[Exposed=Window]
interface HTMLElement : Element {
[HTMLConstructor] constructor();
// metadata attributes
[CEReactions] attribute DOMString title;
[CEReactions] attribute DOMString lang;
[CEReactions] attribute boolean translate;
[CEReactions] attribute DOMString dir;
// user interaction
[CEReactions] attribute (boolean or unrestricted double or DOMString)? hidden;
[CEReactions] attribute DOMString inert;
undefined click();
[CEReactions] attribute DOMString accessKey;
readonly attribute DOMString accessKeyLabel;
[CEReactions] attribute boolean draggable;
[CEReactions] attribute boolean spellcheck;
[CEReactions] attribute DOMString writingSuggestions;
[CEReactions] attribute DOMString autocapitalize;
[CEReactions] attribute [LegacyNullToEmptyString] DOMString innerText;
[CEReactions] attribute [LegacyNullToEmptyString] DOMString outerText;
ElementInternals attachInternals();
// The popover API
undefined showPopover();
undefined hidePopover();
boolean togglePopover(optional boolean force);
[CEReactions] attribute DOMString? popover;
};
HTMLElement includes GlobalEventHandlers;
HTMLElement includes ElementContentEditable;
HTMLElement includes HTMLOrSVGElement;
Element
[Exposed=Window]
interface Element : Node {
readonly attribute DOMString? namespaceURI;
readonly attribute DOMString? prefix;
readonly attribute DOMString localName;
readonly attribute DOMString tagName;
[CEReactions] attribute DOMString id;
[CEReactions] attribute DOMString className;
[SameObject, PutForwards=value] readonly attribute DOMTokenList classList;
[CEReactions, Unscopable] attribute DOMString slot;
boolean hasAttributes();
[SameObject] readonly attribute NamedNodeMap attributes;
sequence<DOMString> getAttributeNames();
DOMString? getAttribute(DOMString qualifiedName);
DOMString? getAttributeNS(DOMString? namespace, DOMString localName);
[CEReactions] undefined setAttribute(DOMString qualifiedName, DOMString value);
[CEReactions] undefined setAttributeNS(DOMString? namespace, DOMString qualifiedName, DOMString value);
[CEReactions] undefined removeAttribute(DOMString qualifiedName);
[CEReactions] undefined removeAttributeNS(DOMString? namespace, DOMString localName);
[CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
boolean hasAttribute(DOMString qualifiedName);
boolean hasAttributeNS(DOMString? namespace, DOMString localName);
Attr? getAttributeNode(DOMString qualifiedName);
Attr? getAttributeNodeNS(DOMString? namespace, DOMString localName);
[CEReactions] Attr? setAttributeNode(Attr attr);
[CEReactions] Attr? setAttributeNodeNS(Attr attr);
[CEReactions] Attr removeAttributeNode(Attr attr);
ShadowRoot attachShadow(ShadowRootInit init);
readonly attribute ShadowRoot? shadowRoot;
Element? closest(DOMString selectors);
boolean matches(DOMString selectors);
boolean webkitMatchesSelector(DOMString selectors); // legacy alias of .matches
HTMLCollection getElementsByTagName(DOMString qualifiedName);
HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
HTMLCollection getElementsByClassName(DOMString classNames);
[CEReactions] Element? insertAdjacentElement(DOMString where, Element element); // legacy
undefined insertAdjacentText(DOMString where, DOMString data); // legacy
};
GlobalEventHandlers
interface mixin GlobalEventHandlers {
attribute EventHandler onabort;
attribute EventHandler onauxclick;
attribute EventHandler onbeforeinput;
attribute EventHandler onbeforematch;
attribute EventHandler onbeforetoggle;
attribute EventHandler onblur;
attribute EventHandler oncancel;
attribute EventHandler oncanplay;
attribute EventHandler oncanplaythrough;
attribute EventHandler onchange;
attribute EventHandler onclick;
attribute EventHandler onclose;
attribute EventHandler oncontextlost;
attribute EventHandler oncontextmenu;
attribute EventHandler oncontextrestored;
attribute EventHandler oncopy;
attribute EventHandler oncuechange;
attribute EventHandler oncut;
attribute EventHandler ondblclick;
attribute EventHandler ondrag;
attribute EventHandler ondragend;
attribute EventHandler ondragenter;
attribute EventHandler ondragleave;
attribute EventHandler ondragover;
attribute EventHandler ondragstart;
attribute EventHandler ondrop;
attribute EventHandler ondurationchange;
attribute EventHandler onemptied;
attribute EventHandler onended;
attribute OnErrorEventHandler onerror;
attribute EventHandler onfocus;
attribute EventHandler onformdata;
attribute EventHandler oninput;
attribute EventHandler oninvalid;
attribute EventHandler onkeydown;
attribute EventHandler onkeypress;
attribute EventHandler onkeyup;
attribute EventHandler onload;
attribute EventHandler onloadeddata;
attribute EventHandler onloadedmetadata;
attribute EventHandler onloadstart;
attribute EventHandler onmousedown;
[LegacyLenientThis] attribute EventHandler onmouseenter;
[LegacyLenientThis] attribute EventHandler onmouseleave;
attribute EventHandler onmousemove;
attribute EventHandler onmouseout;
attribute EventHandler onmouseover;
attribute EventHandler onmouseup;
attribute EventHandler onpaste;
attribute EventHandler onpause;
attribute EventHandler onplay;
attribute EventHandler onplaying;
attribute EventHandler onprogress;
attribute EventHandler onratechange;
attribute EventHandler onreset;
attribute EventHandler onresize;
attribute EventHandler onscroll;
attribute EventHandler onscrollend;
attribute EventHandler onsecuritypolicyviolation;
attribute EventHandler onseeked;
attribute EventHandler onseeking;
attribute EventHandler onselect;
attribute EventHandler onslotchange;
attribute EventHandler onstalled;
attribute EventHandler onsubmit;
attribute EventHandler onsuspend;
attribute EventHandler ontimeupdate;
attribute EventHandler ontoggle;
attribute EventHandler onvolumechange;
attribute EventHandler onwaiting;
attribute EventHandler onwebkitanimationend;
attribute EventHandler onwebkitanimationiteration;
attribute EventHandler onwebkitanimationstart;
attribute EventHandler onwebkittransitionend;
attribute EventHandler onwheel;
};
ElementContentEditable
interface mixin ElementContentEditable {
[CEReactions] attribute DOMString contentEditable;
[CEReactions] attribute DOMString enterKeyHint;
readonly attribute boolean isContentEditable;
[CEReactions] attribute DOMString inputMode;
};
HTMLOrSVGElement
interface mixin HTMLOrSVGElement {
[SameObject] readonly attribute DOMStringMap dataset;
attribute DOMString nonce; // intentionally no [CEReactions]
[CEReactions] attribute boolean autofocus;
[CEReactions] attribute long tabIndex;
undefined focus(optional FocusOptions options = {});
undefined blur();
};
LinkStyle
interface mixin LinkStyle {
readonly attribute CSSStyleSheet? sheet;
};
- ARIA in HTML
-
暗黙的="
No
" ARIAロール="No
" aria-* 属性="No
"暗黙的な ARIAセマンティクスがありませんが、意味はあり、この意味は、ARIA によって提供されず、ARIA のユーザーに公開されていないロール、状態、プロパティで表現される可能性があります。
作成者は、暗黙的な ARIAセマンティクス, または HTML要素のネイティブセマンティクスを上書きしてはいけません。
記述できる aria-* 属性はありません。
- レンダリング
- ボックスを生成しない
非表示の要素
style {
display: none;
}
CSS を大雑把に読むと、 要素とその子孫はボックスやテキストシーケンスを生成しません。 などになります。
- 説明
-
style
要素 は、文書あるいは文書の一部分のスタイル情報を表します。style
要素 は、使用すると、作成者はCSSスタイルシートをドキュメントに埋め込む事ができます。style要素は、スタイリング処理モデルへのいくつかの入力の1つです。この要素は、ユーザーのコンテンツを表すものではありません。
スタイルシート(Style Sheets.)とは、文書の見栄えに関する情報を記録等したデータやファイル等の事です。<!DOCTYPE html>
<html lang=
"ja"> <head>
<meta charset=
"utf-8"> <title>style要素の例 - (Example of style element) | 離珠のHTMLでしぃ!</title>
<style>body { color: white; background-color: teal; }</style>
</head>
<body>
<h1>style要素の例</h1>
<p>この例の style要素は、body要素の文字の色を白, 背景の色を緑と指定しています。</p>
</body>
</html>
このサンプルへのリンクです。
(./exstyle.htm) - 参照元
-
参照元
- HTML Living Standard. 4.2.6 The style element (Last Updated 27 August 2024)
- HTML Living Standard. 15 Rendering 15.3.1 Hidden elements (Last Updated 27 August 2024)
- HTML 5.2 W3C 勧告. 4.2.6 The style element (W3C勧告、2017年12月14日)
- HTML 5.2 W3C 勧告. 10.3.1. Hidden elements (W3C勧告、2017年12月14日)
- HTML 5.1 W3C 勧告. 4.2.6 The style element (W3C勧告、2016年11月1日)
- HTML 5.1 W3C 勧告. 10.3.1. Hidden elements (W3C勧告、2016年11月1日)
- HTML 5 W3C 勧告. 4.2.6 The style element (W3C勧告、2014年10月28日)
- HTML 5 W3C 勧告. 10.3.1. Hidden elements (W3C勧告、2014年10月28日)
- HTML 4.01 Specification W3C 勧告. 14.2.3 Header style information: the STYLE element (W3C勧告1999年12月24日)
- HTML 3.2 Reference Specification STYLE and SCRIPT (W3C勧告1997年1月14日)
- <style>: スタイル情報要素 - HTML: ハイパーテキストマークアップ言語 | MDN
- CSS Text Module Level 3 (編集者草案2023年10月20日)
- JSONデータ
-
JSON形式での、データのイメージです。
{
"style": {
"check": "2024-09-01",
"group": "Metadata",
"element": "- - %StyleSheet; -- style info. (Raw text elements.) [Metadata] --",
"attlist": {
"%Global;": "",
"media": "%MediaDesc; #IMPLIED -- designed for use with these media --",
"title": "%Text; #IMPLIED -- advisory title --",
"blocking": "%Blocking; #IMPLIED -- Whether the element is potentially render-blocking --",
},
"comment": "スタイル情報 (Style info)",
"point": "文書あるいは文書の一部分のスタイル情報を表します。",
"description": [
"使用すると、作成者はCSSスタイルシートをドキュメントに埋め込む事ができます。style要素は、スタイリング処理モデルへのいくつかの入力の1つです。この要素は、ユーザーのコンテンツを表すものではありません。",
"スタイルシート(Style Sheets.)とは、文書の見栄えに関する情報を記録等したデータやファイル等の事です。",
],
"kind": "Raw",
"categories": "Metadata",
"used": [
"parent (%Metadata)*",
"HEAD NOSCRIPT (STYLE)*",
],
"contentmodel": "適合したスタイルシートを与えるテキスト。",
"tag": [
"-",
"-",
],
"attributes": {
"media": "該当するメディア",
"blocking": "要素が潜在的にレンダリングをブロックしているかどうか",
"title": "この要素に特別なセマンティクスで、CSSスタイルシートセット名。",
},
"attributessupplement": [
"type属性がありましたが、廃止されました。作成者はstyle要素にtype属性を指定すべきではありません。属性が存在する場合、その値は 'text/css'の大文字小文字を区別しないASCIIマッチでなければなりません。データ・ブロックでは、 styleの代わりに scriptをコンテナとして使います。",
],
"dominterface": "HTMLStyleElement",
"rendering": "Hidden",
"reference": [
{
"title": "HTML Living Standard. 4.2.6 The style element.",
"href": "https://html.spec.whatwg.org/multipage/semantics.html#the-style-element",
"heading": "HTML Living Standard. 4.2.6 The style element",
"sub": "Last Updated 27 August 2024",
},
{
"title": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements.",
"href": "https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements",
"heading": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements",
"sub": "Last Updated 27 August 2024",
},
{
"title": "HTML 5.2 W3C Recommendation. 4.2.6 The style element. W3C Recommendation, 14 December 2017.",
"href": "https://www.w3.org/TR/2017/REC-html52-20171214/document-metadata.html#the-style-element",
"heading": "HTML 5.2 W3C 勧告. 4.2.6 The style element",
"sub": "W3C勧告、2017年12月14日",
},
{
"title": "HTML 5.2 W3C Recommendation. 10.3.1. Hidden elements.",
"href": "https://www.w3.org/TR/2017/REC-html52-20171214/rendering.html#hidden-elements",
"heading": "HTML 5.2 W3C 勧告. 10.3.1. Hidden elements",
"sub": "W3C勧告、2017年12月14日",
},
{
"title": "HTML 5.1 W3C Recommendation. 4.2.6 The style element. W3C Recommendation, 1 November 2016.",
"href": "https://www.w3.org/TR/2016/REC-html51-20161101/document-metadata.html#the-style-element",
"heading": "HTML 5.1 W3C 勧告. 4.2.6 The style element",
"sub": "W3C勧告、2016年11月1日",
},
{
"title": "HTML 5.1 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 1 November 2016.",
"href": "https://www.w3.org/TR/2016/REC-html51-20161101/rendering.html#hidden-elements",
"heading": "HTML 5.1 W3C 勧告. 10.3.1. Hidden elements",
"sub": "W3C勧告、2016年11月1日",
},
{
"title": "HTML 5 W3C Recommendation. 4.2.6 The style element. W3C Recommendation, 28 October 2014.",
"href": "https://www.w3.org/TR/2014/REC-html5-20141028/document-metadata.html#the-style-element",
"heading": "HTML 5 W3C 勧告. 4.2.6 The style element",
"sub": "W3C勧告、2014年10月28日",
},
{
"title": "HTML 5 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 28 October 2014.",
"href": "https://www.w3.org/TR/2014/REC-html5-20141028/rendering.html#hidden-elements",
"heading": "HTML 5 W3C 勧告. 10.3.1. Hidden elements",
"sub": "W3C勧告、2014年10月28日",
},
{
"title": "HTML 4.01 Specification W3C Recommendation. 14.2.3 Header style information: the STYLE element. W3C Recommendation 24 December 1999.",
"href": "https://www.w3.org/TR/html401/present/styles.html#h-14.2.3",
"heading": "HTML 4.01 Specification W3C 勧告. 14.2.3 Header style information: the STYLE element",
"sub": "W3C勧告1999年12月24日",
},
{
"title": "HTML 3.2 Reference Specification. W3C Recommendation 14-Jan-1997.",
"href": "https://www.w3.org/TR/2018/SPSD-html32-20180315/#style",
"heading": "HTML 3.2 Reference Specification STYLE and SCRIPT",
"sub": "W3C勧告1997年1月14日",
},
{
"title": "MDN Web Docs. HTML <style>: The Style Information element.",
"href": "https://developer.mozilla.org/ja/docs/Web/HTML/Element/style",
"heading": "<style>: スタイル情報要素 - HTML: ハイパーテキストマークアップ言語 | MDN",
},
{
"title": "CSS Text Module Level 3.",
"href": "https://drafts.csswg.org/css-text-3/",
"heading": "CSS Text Module Level 3",
"sub": "編集者草案2023年10月20日",
},
],
"version": {
"first": "HTML3",
"latest": "HTML Living Standard",
},
}
}
注意: 簡易に描画する為に、各項目の後ろに
,
を付与しています。JSON の形式では、末尾の項目の後には,
は付けません。 - 最終確認日
-
2024-09-01
2.使用できるコンテキスト
style要素
は、
head要素に
配置できます。
style
style
head
3.コンテンツモデル
style要素
は、
適合したスタイルシートを
与えるテキストを
配置できます。
style
style
type"text/css"
(CSS) です。
スタイルシート(Style Sheets.)とは、文書の見栄えに関する情報を記録等したデータやファイル等の事です。
<style>
.cdvPseudo301::after { content: "< /style>"; }
.cdvPseudo301 { color: blue; }
.cdvPseudo301::before { content: "</style>"; }
.cdvPseudo301 { color: red; }
</style>
<div class=
"cdvPseudo301">test</div>
適合したスタイルシートを与えるテキストと言ってもHTML文書の中なので、HTMLのタグ, コメントには注意が必要です。
要素の終了とされたりコメントの終了とされたりする可能性があります。
普段から>
の扱いに注意して文字参照を使うとか、文字列を分割して結合するなど工夫が必要だと思います。
尚、外部ファイルにして読み込む場合はこの配慮は不要ですが、習慣づけとして意識した方が無難です。
4.コンテンツ属性
style要素
は、
グローバル属性と、
media,
blocking を
設定できます。
style
また、グローバル属性である title属性ですが、この要素では特別な意味を持ちます。
media属性
<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属性
<style blocking=
"render">
blockingrender
キーワードを指定した場合、要素は潜在的にレンダリング ブロックになります。これは個々の要素で定義されます。
デフォルトでは、要素は暗黙的にレンダリングをブロックする可能性はありません。
title属性
<style title=
"Recommendation">
title
link
type属性
<style type=
"text/css">
typetext/css
として処理されているという事の様です。
5.DOM インターフェイス
style要素
を
直接表す
プロパティは
ありません。
styledocument
のgetElementById()メソッドを用いてid
他には、document
のquerySelectorAll()メソッドを用いて要素style
と指定して用いるとか、namedocument
のgetElementsByName()メソッドを用いるとか、状況, 目的によりいろいろあります。
<form class=
"cfmSample501" onchange= "document .getElementById( = document'isyJs501') .textContent .getElementById( 'iseJs502') .value;"> この例示の文字の色は、<select id=
"iseJs502"> <option value=
".cfmSample501 { color: inherit; }">継承する</option> <option value=
".cfmSample501 { color: red; }">赤</option> <option value=
".cfmSample501 { color: blue; }">青</option> </select>です。
</form>
<style id=
"isyJs501"> .cfmSample501 { color: inherit; }
</style>
style
この例は、style
6.レンダリング
style {
display: none;
}
stylenone
であり、この要素とその子孫はボックスやテキスト
文書あるいは文書の一部分のスタイル情報を表すメタデータを記述する為の要素なので、ビジュアルメディアでボックスを生成する様な指定は行わないのが良いでしょう。
<style class=
"csySample601"> .csySample601 {
display: block;
}
/* <h1>見出し</h1> */
/*
.csySample601 {
color: red;
}
*/
</style>
スタイルシートの記述を見えるようにした例です。ビジュアルメディアでボックスを生成するだけで、スタイルシートの機能は変わりません。CSSのコメントは、CSSとしてはコメントとして扱われています。ただ、CSSのコメントはHTMLでは関係ないので、そのまま見えます。また、これはテキストとして扱われているので、そこに書かれているHTMLタグはテキスト文字列として扱われ、タグとは扱われません。
とはいえ、style-->
があった場合も同じです。
尚、この例示は検証用に記述してみたものです。HTML の規則としては、そもそもstyle
7.気を付ける点
文字参照
CSSのcontent-->
を扱いたい場合は、--\03E
と文字参照を用いておくべきです。通常は文字列として扱われていても <!--
からコメントの終わりを探されている途中に文字列の -->
があると文字列として扱われずにコメントの終わりの記述として扱われてしまいます。
あくまで、HTML上で <!--
でコメントが始まり、style
文字列として </style>
を扱いたい場合は、\03C/style\03E
と文字参照を用いておくべきです。style
<style>
.cdvPseudo701::before { content: "TEXT --\03E"; }
.cdvPseudo701::after { content: "TEXT \03C/style\03E"; }
</style>
<div class=
"cdvPseudo701"> </div>
HTML文書でのコメントの終わりを示す -->
と、style</style>
の文字列をスタイルシート内で用いる例です。
スタイル情報
スタイル情報を HTML文書に適用させる方法として、link
スタイルシートで装飾するという意図からも、出来るだけ別ファイルに分けるべきです。当サイトでは、一部でCSSの装飾自体が記事である箇所に於いて、意図的にHTMLファイルに書き込む方法を用いています。
尚、理想は別としてlink
CSS には優先度という物があり、style