te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>html - Behavior of child divs with parent spans - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Behavior of child divs with parent spans - Stack Overflow

programmeradmin3浏览0评论

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
  • The divs still have display: block from the user agent stylesheet. – C3roe Commented Feb 17 at 13:51
  • @C3roe Not sure you are correct. Following your logic, changing the divs to spans and the spans to divs in my code example above should display Span1 Span2 on the same line, since span still has display: inline from the user agent stylesheet. But it displays Span1 and Span2 on different lines. So the divs seem to convert the spans to display: block, but the spans don't convert the divs to display: inline. – Peter B Commented Feb 18 at 9:17
  • "But it displays Span1 and Span2 on different lines." - of course it does, because the outer divs cause those two "lines." – C3roe Commented Feb 18 at 9:26
  • 'of course it does, because the outer divs cause those two "lines."' I may be missing something, but if the parent divs cause the two children (spans) to appear on different lines, why don't the parent spans cause the two children (divs) to appear on the same line? – Peter B Commented Feb 18 at 14:14
  • The spans will be as wide, as their content requires. The div inside the span goes over the full width, ergo its parent span also goes over the full width. And two elements that go over the full width, can only be displayed one below the other, not next to each other. – C3roe Commented Feb 18 at 14:20
 |  Show 5 more comments

1 Answer 1

Reset to default 0

A '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.

发布评论

评论列表(0)

  1. 暂无评论