div和span之间的差异 [英] Difference between div and span
问题描述
div
与属性 display:inline-block
和之间的区别是什么? span
与 display:inline-block
?
推荐答案
div
和 span
元素之间有两个区别:
There is two differences between div
and span
elements:
-
div
有display:block
code> display:inline 作为默认值。 -
div
可以包含块元素和内联元素,span
是一个内联元素,只能包含其他内联元素。
- The
div
hasdisplay:block
as default, while span hasdisplay:inline
as default. - The
div
is a block element, and can contain block elements and inline elements, whilespan
is an inline element and can only contain other inline elements.
一旦你应用了 display:inline-block
,它们的行为是一样的。
Once you have applied the display:inline-block
they behave the same.
当HTML代码被解析时,不考虑样式。虽然您可以更改显示
样式以使元素的行为相同,但您仍然必须遵循HTML代码中的规则。
When the HTML code is parsed, the style is not considered. While you can change the display
style to make the elements behave the same, you still have to follow the rules in the HTML code.
这例如是有效的:
<div>
<div>
<span></span>
</div>
<span></span>
</div>
例如无效:
<span>
<div>
<span></span>
</div>
<div></div>
</span>
浏览器将尝试重新排列无效代码中的元素,这通常意味着它会移动 span
元素之外的code> div 元素。由于HTML规范(版本5之前)只告诉如何处理正确的代码,每个浏览器都有自己处理不正确代码的方式。
The browser will try to rearrange the elements in the invalid code, which usually means that it moves the div
elements outside the span
elements. As the HTML specification (prior to version 5) only told how correct code should be handled, each browser has its own way of dealing with incorrect code.
这篇关于div和span之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!