<div>:内容划分元素

HTML <div> 元素 (或 HTML 文档分区元素) 是一个通用型的流内容容器,

作为一个“纯粹的”容器,<div> 元素在语义上不表示任何特定类型的内容。它被用来对其它元素进行分组,以及样式相关的需求(借助 classid 特性加以区分),或是用于对具有相同特性的一组元素进行分组 (比如 lang)。

Content categories Flow content, palpable content.
Permitted content Flow content.
Or (in WHATWG HTML): If the parent is a <dl> element: one or more <dt> elements followed by one or more <dd> elements, optionally intermixed with <script> and <template> elements.
Tag omission 不允许,开始标签和结束标签都不能省略。
Permitted parents Any element that accepts flow content.
Or (in WHATWG HTML): <dl> element.
Permitted ARIA roles Any
DOM interface HTMLDivElement

属性

该元素支持所有全局属性

注意: align 属性已废弃;不要再使用它了。你应当使用 CSS GridCSS Flexbox 来对齐、定位页面上的 <div> 元素。

使用备注

  • <div> 元素应当仅在没有任何其它语义元素(比如 <article><nav>)可用时使用。

示例

一个简单的例子

<div>
  <p>这里可以是任何内容,比如 &lt;p&gt;,
  &lt;table&gt;,一切由你作主!</p>
</div> 

结果看起来像这样:

这里可以是任何内容,比如 <p>, <table>,一切由你作主!

Live Sample:

A styled example

This example creates a shadowed box by applying a style to the <div> using CSS. Note the use of the class attribute on the <div> to apply the style named "shadowbox" to the element.

HTML

<div class="shadowbox">
  <p>Here's a very interesting note displayed in a
  lovely shadowed box.</p>
</div>

CSS

.shadowbox {
  width: 15em;
  border: 1px solid #333;
  box-shadow: 8px 8px 5px #444;
  padding: 8px 12px;
  background-image: linear-gradient(180deg, #fff, #ddd 40%, #ccc);
}

结果

规范

规范 状态 备注
HTML Living Standard
<div>
Living Standard No changes since the latest snapshot
HTML5
<div>
Recommendation Obsoleted align
HTML 4.01 Specification
<div>
Recommendation

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
divChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
align
DeprecatedNon-standard
Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

参见