(Css Spec) Clarification on containing block definition

Guys I have a little question about containing block, By definition containing block is the nearest ancestor that is a block container(containing only block-level or inline-level boxes) or which establishes a formatting context but here in this code :
<DIV id="div1">
<P id="p1">This is text in the first paragraph...</P>
<P id="p2">This is text
<EM id="em1"> in the
<STRONG id="strong1">second</STRONG>
paragraph.</EM>
</P>
</DIV>
<DIV id="div1">
<P id="p1">This is text in the first paragraph...</P>
<P id="p2">This is text
<EM id="em1"> in the
<STRONG id="strong1">second</STRONG>
paragraph.</EM>
</P>
</DIV>
The spec says that the containing block for strong element is the second paragraph(id=p2), but if we follow the definition shouldnt the 'em' element be the containing block since it contains only inline-level elements therefore being the nearest block container? Anyone can enlighten me on this 🧐
18 Replies
dysbulic 🐙
dysbulic 🐙9mo ago
Not sure I understand the question, but <em> is an inline element & you're saying it has to be block?
Axim
Axim9mo ago
No, em here have inline elements in it which are all inline and by definition it should have been the containing block of the strong element but in the spec it doesnt say that, they consider the paragraph #p2 to be it's containing block. Here's the link to the spec : https://www.w3.org/TR/2016/WD-CSS22-20160412/visudet.html at the very start until the example
b1mind
b1mind9mo ago
yes cause <p> is the parent containing block by changing the position of em to absolute* it would become a block and then it would be the containing block Not sure why you are reading the specs but seems a convoluted way to learn >.>;; least for me. Just make sure you understand block/inline. As well as parent/child relationships.
Axim
Axim9mo ago
yeah agreed but shouldnt em be the containing block in the first place instead of the p2 for the strong element?
b1mind
b1mind9mo ago
no because its inline fun experiment for ya ... make <em style="display:block;"> and you will see what happens to that <p>
Axim
Axim9mo ago
ok let me test
b1mind
b1mind9mo ago
or even the <strong> do both seperately that might help break the glass for ya.
Axim
Axim9mo ago
Ok I see but inline elements can be block containers,right so this means it theres only inline elements in it it can be containing block or am I missing something?
b1mind
b1mind9mo ago
no inline elements cannot be block containers
Axim
Axim9mo ago
ohhh like regular one cant and only few specific like inline-block,etcc... I thought it was all of them
b1mind
b1mind9mo ago
the next block ancestor would be the containing for a bunch of inline elements.
Axim
Axim9mo ago
Ok got it, what confused me was this from the spec:
Not all block container boxes are block-level boxes: non-replaced inline blocks and non-replaced table cells are block containers but are not block-level.
Not all block container boxes are block-level boxes: non-replaced inline blocks and non-replaced table cells are block containers but are not block-level.
And so I assumed that all inline can be block containers
b1mind
b1mind9mo ago
yea idk I don't get hung up on stuff like that I just ignore it >.>;; Learn behavior by using not reading, is more my way.
Axim
Axim9mo ago
Ok I see thx again for the little push, that’s true I’m giving too much attention to this
b1mind
b1mind9mo ago
yea way to much attention imo
Axim
Axim9mo ago
Already built some projects so thought it was a good idea to see what’s happening inside
b1mind
b1mind9mo ago
Just make sure you learn Grid (learn it before Flex if you can lol, but def don't stop with Flexbox) Grid for layout is 2023 happy path >.>;;
Axim
Axim9mo ago
Ok got it, I’m already good with flex and grid is next on the list