メインコンテンツへスキップ

body要素


1.仕様

BODY要素 は、
HTML文書の
内容(コンテンツ)を
表します。

body

HTML文書のコンテンツ (document Body)

種類

通常要素

カテゴリ

なし (セクショニングルートは廃止されました)

使用できるコンテキスト
html要素の 2番目

html要素の 2番目のコンテンツとして、使用できます。

ただし HTML4.01 Frameset の場合は、html要素には使用できず、noframes要素に 1つのコンテンツとして、使用できます。

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

開始タグは、要素が空の場合は省略してもよいし、body要素内の先頭が meta要素, link要素, style要素, script要素, template要素でない場合なら省略できます。

終了タグは、body要素の直後にコメントがない場合は、省略できます。

コンテンツ属性
グローバル属性 他
onafterprint属性
印刷後に発生
onbeforeprint属性
印刷前に発生
onbeforeunload属性
ページがアンロードされようとしている時に発生
onhashchange属性
ドキュメントのURLのフラグメント部分が変更された時に発生
onlanguagechange属性
ユーザーの優先言語が変更された時に発生
onmessage属性
サーバーからメッセージを受信した時に発生
onmessageerror属性
デシリアライズできないメッセージを受信した時に発生
onoffline属性
ネットワーク接続に障害が発生した時に発生
ononline属性
ネットワーク接続が戻った時に発生
onpagehide属性
セッション履歴内のページのエントリが現在のエントリでなくなった時に発生
onpageshow属性
セッション履歴内のページのエントリが現在のエントリになった時に発生
onpopstate属性
ユーザーがセッション履歴をナビゲートした時に発生
onrejectionhandled属性
promise拒否がreject関数で処理される時に発生
onstorage属性
対応するlocalStorageまたはsessionStorageストレージ領域が変更された時に発生
onunhandledrejection属性
promise拒否がreject関数で処理されなかった時に発生
onunload属性
ウィンドウがコンテンツとリソースをアンロードする時に発生

alink属性, background属性, bgcolor属性, bottommargin属性, leftmargin属性, link属性, marginheight属性, marginwidth属性, rightmargin属性, text属性, topmargin属性, vlink属性がありましたが、廃止されました。代わりに CSS を使用してください。

DOM インターフェイス
HTMLBodyElement
  1. [Exposed=Window]
  2. interface HTMLBodyElement : HTMLElement {
  3. [HTMLConstructor] constructor();
  4.  
  5. // also has obsolete members
  6. };
  7.  
  8. HTMLBodyElement includes WindowEventHandlers;
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. };
WindowEventHandlers
  1. interface mixin WindowEventHandlers {
  2. attribute EventHandler onafterprint;
  3. attribute EventHandler onbeforeprint;
  4. attribute OnBeforeUnloadEventHandler onbeforeunload;
  5. attribute EventHandler onhashchange;
  6. attribute EventHandler onlanguagechange;
  7. attribute EventHandler onmessage;
  8. attribute EventHandler onmessageerror;
  9. attribute EventHandler onoffline;
  10. attribute EventHandler ononline;
  11. attribute EventHandler onpagehide;
  12. attribute EventHandler onpagereveal;
  13. attribute EventHandler onpageshow;
  14. attribute EventHandler onpageswap;
  15. attribute EventHandler onpopstate;
  16. attribute EventHandler onrejectionhandled;
  17. attribute EventHandler onstorage;
  18. attribute EventHandler onunhandledrejection;
  19. attribute EventHandler onunload;
  20. };
ARIA in HTML
暗黙的="generic" ARIAロール="No" aria-* 属性="GlobalAny"

暗黙的な ARIAセマンティクスは、role=generic です。

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

グローバル aria-* 属性と、許可されたロールに適用される任意の aria-* 属性が記述できます。
aria-hidden=true を指定してはなりません。

レンダリング
ブロックボックス
ページ
  1. body {
  2. display: block;
  3. margin: 8px;
  4. }

CSS を大雑把に読むと、 ブロックレベルのブロックコンテナボックス(ブロックボックス)を生成します。 などになります。

説明
body要素は、文書の内容(コンテンツ・content)を表します。

body要素は、適合ドキュメントでは、body要素は1つだけです。body要素は、イベントハンドラのコンテンツ属性として、Windowオブジェクトの多数のイベントハンドラを公開します。また、イベントハンドラーのIDL属性もミラーリングします。

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>body要素の例 - (Example of body element) | 離珠のHTMLでしぃ!</title>
  6. </head>
  7. <body>
  8. <h1>body要素の例</h1>
  9. <p>この例の body要素は、文書の内容を表しています。</p>
  10. </body>
  11. </html>

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

参照元
参照元
JSONデータ
JSON形式での、データのイメージです。
  1. {
  2. "body": {
  3. "check": "2024-09-01",
  4. "group": "Page",
  5. "element": "O O (%Flow)* -- document body. (Normal elements.) [Sectioningroots] --",
  6. "attlist": {
  7. "%Global;": "",
  8. "%Eventhandler;": "",
  9. },
  10. "comment": "HTML文書のコンテンツ (document Body)",
  11. "point": "文書の内容(コンテンツ・content)を表します。",
  12. "description": "適合ドキュメントでは、body要素は1つだけです。body要素は、イベントハンドラのコンテンツ属性として、Windowオブジェクトの多数のイベントハンドラを公開します。また、イベントハンドラーのIDL属性もミラーリングします。",
  13. "kind": "Normal",
  14. "categories": "Sectioningroots",
  15. "used": "HTML (HEAD,BODY)",
  16. "usedsupplement": "ただし HTML4.01 Frameset の場合は、html要素には使用できず、noframes要素に 1つのコンテンツとして、使用できます。",
  17. "contentmodel": "BODY (%Flow)*",
  18. "tag": [
  19. "O",
  20. "O",
  21. ],
  22. "tagsupplement": [
  23. "開始タグは、要素が空の場合は省略してもよいし、body要素内の先頭が meta要素, link要素, style要素, script要素, template要素でない場合なら省略できます。",
  24. "終了タグは、body要素の直後にコメントがない場合は、省略できます。",
  25. ],
  26. "attributes": {
  27. "onafterprint": "印刷後に発生",
  28. "onbeforeprint": "印刷前に発生",
  29. "onbeforeunload": "ページがアンロードされようとしている時に発生",
  30. "onhashchange": "ドキュメントのURLのフラグメント部分が変更された時に発生",
  31. "onlanguagechange": "ユーザーの優先言語が変更された時に発生",
  32. "onmessage": "サーバーからメッセージを受信した時に発生",
  33. "onmessageerror": "デシリアライズできないメッセージを受信した時に発生",
  34. "onoffline": "ネットワーク接続に障害が発生した時に発生",
  35. "ononline": "ネットワーク接続が戻った時に発生",
  36. "onpagehide": "セッション履歴内のページのエントリが現在のエントリでなくなった時に発生",
  37. "onpageshow": "セッション履歴内のページのエントリが現在のエントリになった時に発生",
  38. "onpopstate": "ユーザーがセッション履歴をナビゲートした時に発生",
  39. "onrejectionhandled": "promise拒否がreject関数で処理される時に発生",
  40. "onstorage": "対応するlocalStorageまたはsessionStorageストレージ領域が変更された時に発生",
  41. "onunhandledrejection": "promise拒否がreject関数で処理されなかった時に発生",
  42. "onunload": "ウィンドウがコンテンツとリソースをアンロードする時に発生",
  43. },
  44. "attributessupplement": [
  45. "alink属性, background属性, bgcolor属性, bottommargin属性, leftmargin属性, link属性, marginheight属性, marginwidth属性, rightmargin属性, text属性, topmargin属性, vlink属性がありましたが、廃止されました。代わりに CSS を使用してください。",
  46. ],
  47. "dominterface": "HTMLBodyElement",
  48. "rendering": "Page",
  49. "reference": [
  50. {
  51. "title": "HTML Living Standard. 4.3.1 The body element.",
  52. "href": "https://html.spec.whatwg.org/multipage/sections.html#the-body-element",
  53. "heading": "HTML Living Standard. 4.3.1 The body element",
  54. "sub": "Last Updated 27 August 2024",
  55. },
  56. {
  57. "title": "HTML Living Standard. 15 Rendering 15.3.2 The page.",
  58. "href": "https://html.spec.whatwg.org/multipage/rendering.html#the-page",
  59. "heading": "HTML Living Standard. 15 Rendering 15.3.2 The page",
  60. "sub": "Last Updated 27 August 2024",
  61. },
  62. {
  63. "title": "HTML 5.2 W3C Recommendation. 4.3.1 The body element. W3C Recommendation, 14 December 2017.",
  64. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/sections.html#the-body-element",
  65. "heading": "HTML 5.2 W3C 勧告. 4.3.1 The body element",
  66. "sub": "W3C勧告、2017年12月14日",
  67. },
  68. {
  69. "title": "HTML 5.2 W3C Recommendation. 10.3.2. The page. W3C Recommendation, 14 December 2017.",
  70. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/rendering.html#the-page",
  71. "heading": "HTML 5.2 W3C 勧告. 10.3.2. The page",
  72. "sub": "W3C勧告、2017年12月14日",
  73. },
  74. {
  75. "title": "HTML 5.1 W3C Recommendation. 4.3.1 The body element. W3C Recommendation, 1 November 2016.",
  76. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/sections.html#the-body-element",
  77. "heading": "HTML 5.1 W3C 勧告. 4.3.1 The body element",
  78. "sub": "W3C勧告、2016年11月1日",
  79. },
  80. {
  81. "title": "HTML 5.1 W3C Recommendation. 10.3.2. The page. W3C Recommendation, 1 November 2016.",
  82. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/rendering.html#the-page",
  83. "heading": "HTML 5.1 W3C 勧告. 10.3.2. The page",
  84. "sub": "W3C勧告、2016年11月1日",
  85. },
  86. {
  87. "title": "HTML 5 W3C Recommendation. 4.3.1 The body element. W3C Recommendation, 28 October 2014.",
  88. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/sections.html#the-body-element",
  89. "heading": "HTML 5 W3C 勧告. 4.3.1 The body element",
  90. "sub": "W3C勧告、2014年10月28日",
  91. },
  92. {
  93. "title": "HTML 5 W3C Recommendation. 10.3.2. The page. W3C Recommendation, 28 October 2014.",
  94. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/rendering.html#the-page",
  95. "heading": "HTML 5 W3C 勧告. 10.3.2. The page",
  96. "sub": "W3C勧告、2014年10月28日",
  97. },
  98. {
  99. "title": "HTML 4.01 Specification W3C Recommendation. 7.5.1 The BODY element. W3C Recommendation 24 December 1999.",
  100. "href": "https://www.w3.org/TR/html401/struct/global.html#h-7.5.1",
  101. "heading": "HTML 4.01 Specification W3C 勧告. 7.5.1 The BODY element",
  102. "sub": "W3C勧告1999年12月24日",
  103. },
  104. {
  105. "title": "HTML 3.2 Reference Specification. The BODY element. W3C Recommendation 14-Jan-1997.",
  106. "href": "https://www.w3.org/TR/2018/SPSD-html32-20180315/#body",
  107. "heading": "HTML 3.2 Reference Specification. The BODY element",
  108. "sub": "W3C勧告1997年1月14日",
  109. },
  110. {
  111. "title": "Hypertext Markup Language - 2.0. November 1995.",
  112. "href": "https://www.ietf.org/rfc/rfc1866.txt",
  113. "heading": "Hypertext Markup Language - 2.0",
  114. "sub": "November 1995",
  115. },
  116. {
  117. "title": "Hypertext Markup Language (HTML). June 1993.",
  118. "href": "https://www.w3.org/MarkUp/draft-ietf-iiir-html-01.txt",
  119. "heading": "Hypertext Markup Language (HTML)",
  120. "sub": "June 1993",
  121. },
  122. {
  123. "title": "MDN Web Docs. HTML <body>: The Document Body element.",
  124. "href": "https://developer.mozilla.org/ja/docs/Web/HTML/Element/body",
  125. "heading": "MDN Web Docs. HTML <body>: 文書本体要素",
  126. },
  127. ],
  128. "version": {
  129. "first": "HTML",
  130. "latest": "HTML Living Standard",
  131. },
  132. }
  133. }

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

最終確認日

2024-09-01


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

body要素 は、
html要素の
2番目の要素として
使用できます。

body要素が使用できるコンテキスト(Context文脈)は、html要素の2番目の要素としてです。

と言うよりhtml要素の2番目以外には配置できないという事です。head要素でもタグが省略できるので、html要素の最初にあるものとして処理されます。body要素もタグが省略できるので、html要素の2番目にあるものとして処理されます。
尚、HTML4.01 Frameset は例外です。HTML4.01 Frameset ではhtml要素には使用できません。noframes要素に使用できます。

document
html
head
body
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>サンプル</title>
  6. </head>
  7. <body>
  8. HTMLを用いると、テキストデータの表示等ができます。
  9. </body>
  10. </html

html要素の2番目の要素として、body要素を配置できます。
ただし、HTML4.01 Frameset を除きます。

document
html
head
frameset
frame
frame
noframes
body
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd" >
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>サンプル</title>
  6. </head>
  7. <frameset cols="50%, 50%">
  8. <frame src="./menu.htm">
  9. <frame src="./header.htm">
  10. <noframes>
  11. <body>
  12. フレームに関しての、テキストデータの表示等の例示です。
  13. </body>
  14. </noframes>
  15. </frameset>
  16. </html

HTML4.01 Frameset でnoframes要素のコンテンツとして、body要素を使用できます。


3.コンテンツモデル

body要素 は、
フローコンテンツを
配置できます。

body要素子や子孫としてどのようなコンテンツ(Content内容) が含まれなければならないかというと、0 以上のフローコンテンツです。

body
Flow
content
  1. <body>
  2. <p>フローコンテンツ</p>
  3. </body>

body要素のコンテンツの一部として、フローコンテンツであるp要素を配置している例です。


4.コンテンツ属性

body要素 は、
グローバル属性と、
イベントハンドラー
コンテンツ属性を
設定できます。

body要素に指定することができる属性は、グローバル属性イベントハンドラーコンテンツ属性を、設定できます。

alink属性, background属性, bgcolor属性, bottommargin属性, leftmargin属性, link属性, marginheight属性, marginwidth属性, rightmargin属性, text属性, topmargin属性, vlink属性がありましたが、削除されました。代わりに CSS を使用してください。


5.DOM インターフェイス

documentの
プロパティである
bodyは、
body要素または
frameset要素を
返します。

body要素を JavaScript などで操作するには、documentのプロパティであるbodyを用います。これは、要素であるbody要素またはframeset要素を返します。この時に取得できる値の形式はHTMLBodyElement インターフェイスです。

documentquerySelector()メソッド等を用いる事も考えられますが、このプロパティで充分でしょう。

body要素のclassは、 "〇〇"です。

  1. <p>body要素のclassは、
  2. "<code id="icdJs501">〇〇</code>"です。</p>
  3. <script>
  4. document.addEventListener("DOMContentLoaded", function() {
  5. document.getElementById("icdJs501").textContent = document.body.className;
  6. });
  7. </script>

body要素class属性の値を取得する例です。例示で表示する為に、HTML文書の読み込みと解析が終了した時に処理するコードとしています。


6.レンダリング

  1. body {
  2. display: block;
  3. margin: 8px;
  4. }

body要素ユーザーエージェントレベルスタイルシートデフォルトでは、displayプロパティの値は block であり、ブロックレベルブロックコンテナボックス(ブロックボックス) を生成します。

body要素包含ブロックhtml要素です。html要素ブロックフォーマットコンテキストを確立しているので、body要素はそこに参加します。

body要素の既定値では、heightプロパティの既定値は auto であり、min-height: 100vhと指定する事により画面いっぱいの高さを最小値にする事ができます。その上でフッターなどを画面の下側に置きたい場合は、displayプロパティの値に flex を指定して、フレックスコンテナボックスを生成して、フレックスコンテナの主軸を、ブロック軸と同じと指定して(flex-direction: column;)各アイテムを配置コンテナの中で主軸方向に均等に配置するように指定します。(justify-content: space-between;)

ブロックフォーマット
コンテキスト
ブロック
フォーマット
コンテキスト
(継続)
コンテンツ
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>サンプル</title>
  6. </head>
  7. <body>
  8. <div>ブロックレベルのボックスのみの例。</div>
  9. </body>
  10. </html

html要素が存在して、その中にbody要素が存在して、その中にdiv要素がある例です。

html要素は、ブロックレベルブロックコンテナであるボックスを生成します。html要素はルート要素であり、それを含むブロックフォーマットコンテキストがない為、新しいブロックフォーマットコンテキストを確立します。html要素は、ブロックレベルボックスを生成するbody要素だけを配置します。head要素は、ボックスやテキストシーケンスを生成しません。

body要素は、ブロックコンテナボックスを生成します。div要素は、ブロックレベルボックスを生成します。この例ではブロックレベルボックスを生成する要素だけを配置しています。なので、ブロックフォーマットコンテキストを継続します。

尚、この説明は各要素のレンダリングに関する指定が既定値での話で、個々にスタイルを指定した場合はこの限りではありません。

ブロックフォーマット
コンテキスト
インライン
フォーマット
コンテキスト
コンテンツ
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>サンプル</title>
  6. </head>
  7. <body>
  8. <span>インラインレベルのボックスのみの例。</span>
  9. </body>
  10. </html

html要素が存在して、その中にbody要素が存在して、その中にspan要素がある例です。

html要素は、ブロックレベルブロックコンテナであるボックスを生成します。html要素はルート要素であり、それを含むブロックフォーマットコンテキストがない為、新しいブロックフォーマットコンテキストを確立します。html要素は、ブロックレベルボックスを生成するbody要素だけを配置します。head要素は、ボックスやテキストシーケンスを生成しません。

body要素は、ブロックコンテナボックスを生成します。span要素は、インラインレベルボックスを生成します。この例ではインラインレベルボックスを生成する要素だけを配置しています。なので、インラインフォーマットコンテキストを新たに確立します。次に、その要素は、すべてのインラインコンテンツを包むルートインラインボックスも生成します。

尚、この説明は各要素のレンダリングに関する指定が既定値での話で、個々にスタイルを指定した場合はこの限りではありません。

ブロックフォーマット
コンテキスト
ブロック
フォーマット
コンテキスト
(継続)
コンテンツ
インライン
フォーマット
コンテキスト
コンテンツ
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>サンプル</title>
  6. </head>
  7. <body>
  8. <div>ブロックレベルのボックスとインラインレベルのボックスの混在の例。</div>
  9. <span>ブロックレベルのボックスとインラインレベルのボックスの混在の例。</span>
  10. </body>
  11. </html

html要素が存在して、その中にbody要素が存在して、その中にdiv要素span要素がある例です。

html要素は、ブロックレベルブロックコンテナであるボックスを生成します。html要素はルート要素であり、それを含むブロックフォーマットコンテキストがない為、新しいブロックフォーマットコンテキストを確立します。html要素は、ブロックレベルボックスを生成するbody要素だけを配置します。

body要素は、ブロックコンテナボックスを生成します。span要素は、インラインレベルボックスを生成します。この例ではブロックレベルボックスインラインレベルボックスを混在して配置しています。なので、匿名のブロックボックスを生成してインラインレベルボックスを囲みます。これにより、body要素が生成したブロックコンテナボックスには、ブロックレベルボックスだけとなります。なので、ブロックフォーマットコンテキストを継続します。

匿名のブロックボックスには、インラインレベルボックスだけですから、インラインフォーマットコンテキストを新たに確立します。次に、その要素は、すべてのインラインコンテンツを包むルートインラインボックスも生成します。

尚、この説明は各要素のレンダリングに関する指定が既定値での話で、個々にスタイルを指定した場合はこの限りではありません。

ブロックフォーマット
コンテキスト
インライン
フォーマット
コンテキスト
テキスト
コンテンツ
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>サンプル</title>
  6. </head>
  7. <body>
  8. ブロックコンテナに直接含まれるテキストと
  9. <span>インラインレベルのボックスの混在の例。</span>
  10. </body>
  11. </html

html要素が存在して、その中にbody要素が存在して、その中にテキストとspan要素がある例です。

html要素は、ブロックレベルブロックコンテナであるボックスを生成します。html要素はルート要素であり、それを含むブロックフォーマットコンテキストがない為、新しいブロックフォーマットコンテキストを確立します。html要素は、ブロックレベルボックスを生成するbody要素だけを配置します。

body要素は、ブロックコンテナボックスを生成します。span要素は、インラインレベルボックスを生成します。テキストはこの例ではインラインレベルボックスと混在する為、書式の定義を簡単にする為に匿名のインラインボックスを生成します。(ブロックコンテナボックスにテキストのみ存在する場合も、匿名のインラインボックスが生成されます。)これにより、body要素が生成したブロックコンテナボックスには、インラインレベルボックスだけとなります。なので、インラインフォーマットコンテキストを新たに確立します。次に、その要素は、すべてのインラインコンテンツを包むルートインラインボックスも生成します。

尚、この説明は各要素のレンダリングに関する指定が既定値での話で、個々にスタイルを指定した場合はこの限りではありません。

リセットCSS

  1. body {
  2. margin: 0;
  3. }

これは、リセットCSSnormalize.cssbody要素の部分を抜き出したものです。

すべてのブラウザでマージンを 0 にします。

小さいページ

  1. .cdvSample601 {
  2. display: flex;
  3. flex-direction: column;
  4. justify-content: space-between;
  5. min-height: 8em;
  6. border: dotted 1px;
  7. }
  8. .cdvSample601 > *:last-child {
  9. margin-top: auto;
  10. }
  11. .cdvSample601 > * {
  12. margin: 3px;
  13. border: solid 1px #0000ff;
  14. }

6行目と11行目以降は、見やすくする為の装飾です。包含ブロックを画面いっぱいにするには、min-height: 100vhと指定する事でできます。min-heightプロパティは、高さの最小値を指定できます。最小値なので、要素のコンテンツによって、それ以上の高さになる事もあります。heightプロパティautoで、max-heightプロパティnoneの既定値のままという想定です。例示では、提示の都合でmin-height: 8em(高さの最小値が8文字分)と指定しています。その上でdisplay: flex, flex-direction: column, justify-content: space-between を指定します。フレックスコンテナの最後のアイテムにmargin-top: autoと指定する事で、最後のアイテムを画面の下側に置くことができます。.cdvSample601 が body で、フッターがbody要素の最後の子要素にあたります。

属性をCSSで置き換える

  1. body {
  2. color: #000000; // text attributes.
  3. background-color: #ffffff; // bgcolor attributes.
  4. background-image: url("../Img/G00/demo.gif"); // background attributes.
  5. margin-top: 8px; // topmargin attributes. marginheight attributes.
  6. margin-bottom: 8px; // bottommargin attributes. marginheight attributes.
  7. margin-right: 8px; // rightmargin attributes. marginwidth attributes.
  8. margin-left: 8px; // leftmargin attributes. marginwidth attributes.
  9. }
  10. :link {
  11. color: #0000ee; // link attributes.
  12. }
  13. :visited {
  14. color: #551abb; // vlink attributes.
  15. }
  16. :link:active, :visited:active {
  17. color: #ff0000; // alink attributes.
  18. }

これは廃止された属性の、CSS での代用例です。


7.気を付ける点

タグの省略の扱い

body要素タグは省略可能です。 開始タグは、要素が空の場合は省略してもよいし、body要素内の先頭がmeta要素, link要素, style要素, script要素, noscript要素, template要素でない場合なら省略できます。
終了タグは、body要素の直後にコメントがない場合は、省略できます。

包含ブロック

body要素の包含ブロックは、html要素が確立します。包含ブロックを基準とした割合を指定する時に、この包含ブロック自体の指定が auto だったりして思うように効果が表れない事があるので注意が必要です。 また、body要素は、html要素のコンテンツとして唯一のボックスを生成する要素なので、親子のボックスでのマージンの扱いにも注意が必要です。

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>html要素の高さ - [離珠のHTMLでしぃ!]</title>
  6. <style>
  7. html { border: solid 1px #ff0000; box-sizing: border-box; }
  8. html { border: solid 1px #0000ff; box-sizing: border-box; }
  9. </style>
  10. </head>
  11. <body>
  12. <a href="" target="_parent">html要素</a>のページに戻るリンクです。
  13. <h1 title="Height of html element.">html要素の高さ</h1>
  14. <p>Height of html element.</p>
  15. <hr>
  16. <form onchange="document.documentElement.style.height = document.getElementById('iseJs001').value;">
  17. このhtml要素の高さは、<select id="iseJs001">
  18. <option value="auto">auto</option>
  19. <option value="100%">100%</option>
  20. <option value="100vh">100vh</option>
  21. </select>です。
  22. </form>
  23. <form onchange="document.body.style.height = document.getElementById('iseJs002').value;">
  24. このbody要素の高さは、<select id="iseJs002">
  25. <option value="auto">auto</option>
  26. <option value="100%">100%</option>
  27. <option value="100vh">100vh</option>
  28. <option value="calc(100% - 16px)">calc(100% - 16px)</option>
  29. <option value="calc(100vh - 18px)">calc(100vh - 18px)</option>
  30. </select>です。
  31. </form>
  32. <form onchange="document.documentElement.style.fontSize = document.getElementById('iseJs003').value + 'em';">
  33. この文字の大きさは、<input id="iseJs003" type="number" max="5.0" min="0.5" step="0.5" value="1.0">emです。
  34. </form>
  35. </body>
  36. </html>

この例では、<iframe src="./heighthtml.htm">HTMLファイルを埋め込んで提示しています。

html要素の高さについて体感する為に、cssでhtml要素, body要素の枠を視覚化して、JavaScriptで cssを動的に設定して表示するというものです。ついでに余白とか線の幅など、幅や高さを扱う時の注意点も、表しています。

使用している外部ファイルへのリンクは、「./heighthtml.htm」です。

CSSリセット

  1. body {
  2. margin: 0;
  3. }

normalize.css というリセットCSS の一部です。すべてのブラウザでマージンを 0 にします。
詳細は理解していないのですが、いろいろとバグ等の対応も含んでいる様なので、リセットCSSを利用する事をお勧めします。


8.付記