admin管理员组文章数量:1245106
this is probably a dumb question, but I can't figure it out, so I'm asking the community.
Divs are block level elements which force a new line for each element. Spans are inline elements which don't. If I wrap a div with a span, why doesn't it make the div behave like an inline element (like inline-block)?
See example code below. I would have expected the two divs to appear next to each other and not above and below each other (as if the spans were not there).
.box {width: 200px; height: 200px; background: blue;}
<span>
<div class="box">Box1</div>
</span>
<span>
<div class="box">Box2</div>
</span>
this is probably a dumb question, but I can't figure it out, so I'm asking the community.
Divs are block level elements which force a new line for each element. Spans are inline elements which don't. If I wrap a div with a span, why doesn't it make the div behave like an inline element (like inline-block)?
See example code below. I would have expected the two divs to appear next to each other and not above and below each other (as if the spans were not there).
.box {width: 200px; height: 200px; background: blue;}
<span>
<div class="box">Box1</div>
</span>
<span>
<div class="box">Box2</div>
</span>
Note: I know this is not a correct use of span. A student of mine made this mistake and I was surprised that it did not affect the behavior of the divs.
Share Improve this question edited Feb 17 at 13:51 David 219k40 gold badges226 silver badges323 bronze badges asked Feb 17 at 13:43 Peter BPeter B 272 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 0A 'div' is a block element, and a 'span' is an inline element, so you can't place a 'div' inside a 'span'.
Block elements cannot be placed inside inline elements because inline elements are meant to contain only text or other inline elements, not larger block elements. This rule helps maintain a clear webpage structure, improves accessibility, and ensures consistent display across all browsers.
Here are official sources explaining why inline elements cannot contain block elements:
Block-level content Inline-level content
These sources cover the differences between block and inline elements and why inline elements should only contain text or other inline elements.
本文标签: htmlBehavior of child divs with parent spansStack Overflow
版权声明:本文标题:html - Behavior of child divs with parent spans - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740156055a2233561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
display: block
from the user agent stylesheet. – C3roe Commented Feb 17 at 13:51display: inline
from the user agent stylesheet. But it displays Span1 and Span2 on different lines. So the divs seem to convert the spans todisplay: block
, but the spans don't convert the divs todisplay: inline
. – Peter B Commented Feb 18 at 9:17