views:

1154

answers:

4

I want to understand what happens when an element whose CSS is display:block is a DOM child of an element whose CSS is display:inline (so that the block element is a child of an inline element).

This scenarios is described in the Anonymous block boxes section for the CSS 2.1 specification: the example includes the following rules ...

body { display: inline }
p    { display: block }

... and the accompanying text says ...

The BODY element contains a chunk (C1) of anonymous text followed by a block-level element followed by another chunk (C2) of anonymous text. The resulting boxes would be an anonymous block box around the BODY, containing an anonymous block box around C1, the P block box, and another anonymous block box around C2.

If you have a display:inline parent element, and if this parent has a child that is display:block, then the existence of that child seems to make the parent nearly behave like display:block, and ignore the fact that it's defined as display:inline (in that the parent now contains nothing but anonymous and non-anonymous block children, i.e. it no longer contains any inline children)?

My question is, in this scenario (where there's a display:block child) then what are the differences between the parent's being defined display:inline instead of display:block?


Edit: I more interested in understanding the CSS 2.1 standard than in how and whether various browser implementations behave in practice.


2nd Edit:

There's one difference noted in the spec. In the following document, the border for the 2nd 'block' paragraph surrounds the whole paragraph and the whole width of the page; whereas the border for the 1st 'inline paragraph is around each line (even when there are several lines) within the paragraph and no more than the exact width of each line (with each line being shorter than the page width).

<html>
<head>
<style type="text/css">
p.one 
{
border-style:solid;
border-width:1px;
display: inline;
}
p.two 
{
border-style:solid;
border-width:1px;
}
</style>
</head>
<body>
<p class="one">Some text. <b>Note:</b> The "border-width"
property does not work if it is used alone. Use the
"border-style" property to set the borders first.</p>
<p class="two">Some text.</p>
</body>
</html>

If I add the following style rule ...

b
{
display: block;
}

... then the "Note:" in the first inline paragraph becomes a block, which splits the paragraph (according to the specs, the first and last part of the paragraph are now in an anonymous block). However, the border around the first and last part of the paragraph are still 'inline'-style borders: and so, still not the same as if p.one had been declared display:block in the first place.

There's a quote from the spec, which says,

Properties set on elements that cause anonymous block boxes to be generated still apply to the boxes and content of that element. For example, if a border had been set on the BODY element in the above example, the border would be drawn around C1 (open at the end of the line) and C2 (open at the start of the line).

Is the "border-style" property the only type of property where the difference is visible?

+1  A: 

It sometimes depends on the exact css defined or browser.

Most commonly, I've seen two behaviors:

  • The display:block element inside display:inline element makes the display:inline act like a display:block with width:100%.

  • A display:inline element containing only display:block float:left or float:right elements takes no space, and is as if there were no elements inside of it. The display:block elements act as if they where inside another display:block element, sometimes having funky actions depending on position.

Both position and float make the child elements have sometimes bizarre behaviors, but avoiding them make them work generally as if they were inline.

voyager
+4  A: 

An inline container cannot contain a block container. The usual result when such a thing occurs is that the inline container is converted to block in order to accommodate its contents. If you need a container that appears inline to contain something that appears to be a block use the following:

display: inline-block;

inline-block property is a display mode that positions the container in an inline fashion with the immediately properties and definitions of an inline container applied to only the container itself without limiting its children to such constraints. The result is that a block container child of a inline-block parent is confined to the dimensions of the parent if the parent has defined definitions or may cause the parents dimensions to stretch to accommodate a larger child. A container set to inline-block can receive properties only afforded to blocks, such as width or height, without loosing the default positioning associated with an inline container.

That property is not supported by FF2, and as a result is not supported by Ice Weasel browser. Nearly every other browser supports that property including IE6, so you should be fine to use it since almost nobody is using FF2 or Ice Weasel except for a minor of users confined to out of the box Linux distributions.

"The usual result when such a thing occurs is that the inline container is converted to block in order to accommodate its contents." -- My question is: whenever that happens, it is exactly the same as if it had been defined as display:block in the first place? If it's not exactly the same, what are the differences?
ChrisW
When a block child is placed within an inline parent the declaration that defines that parent as inline is overwritten. This means that if the browser is self-aware of changes to the structure of the DOM then by removing the block child dynamically should result in the parent automatically retaining its inline definition. If a browser is not so self-aware then the parent is permanently presented as a block until either the child becomes statically inline or until the child is removed from the HTML.
Just want to add that IE6 only supports inline-block for elements that are already inline. But other than that: +1
Borgar
+7  A: 

when i read the spec, i find your question actually quite well answered:

When an inline box contains a block box, the inline box [...] [is] broken around the block. The [in]line boxes before the break and after the break are enclosed in anonymous boxes, and the block box becomes a sibling of those anonymous boxes.

<BODY style="display: inline; ">
This is anonymous text before the P.
<P>This is the content of P.</P>
This is anonymous text after the P.
</BODY>

The resulting boxes would be an anonymous block box around the BODY, containing an anonymous block box around C1, the P block box, and another anonymous block box around C2.

or, visually:

+- anonymous block box around body ---+
| +- anonymous block box around C1 -+ |
| |                                 + |
| +---------------------------------+ |
|                                     |
| +- P block box -------------------+ |
| |                                 + |
| +---------------------------------+ |
|                                     |
| +- anonymous block box around C2 -+ |
| |                                 + |
| +---------------------------------+ |
+-------------------------------------+

now to your question: is this different from <BODY style="display: block; ">?

yes, it is. while it is still 4 boxes (anonymous block box around body now being BODY block box), the spec tells the difference:

Properties set on elements that cause anonymous block boxes to be generated still apply to the [generated anonymous block] boxes and content of that element. For example, if a border had been set on the BODY element in the above example, the border would be drawn around C1 (open at the end of the line) and C2 (open at the start of the line):

+--------------------------------------
| This is anonymous text before the P. 
+--------------------------------------
  This is the content of P.
 --------------------------------------+
  This is anonymous text after the P.  |
 --------------------------------------+

this is different to <BODY style="display: block; ">:

+--------------------------------------+
| This is anonymous text before the P. |
|                                      |
| This is the content of P.            |
|                                      |
| This is anonymous text after the P.  |
+--------------------------------------+
ax
Yes, you're right. Is the "border-style" property the only type of property where the difference is visible?
ChrisW
no - it is also visible with margins, paddings, text-align, text-decoration, etc.
ax
Using "text-decoration: underline" I see text (only text, not whitespace) being underlined regardless of the element's 'display'. What do you think the difference ought to be? [I'm using IE8 and FF3.0.14 fwiw]
ChrisW
with inline parent, only the generated anonymous block boxes are underlined, not the original child block box (FF3.5.3 win). note this part of the spec: "As CSS1 and CSS2 did not define this behavior, CSS1-only and CSS2-only user agents may implement this alternative model and still claim conformance to this part of CSS 2.1. This does not apply to UAs developed after this specification was released."
ax
I observe that with 'display: inline' the border affects every line (e.g. there's horizontal border between lines); but the padding and margin properties don't affect every line (e.g. the inter-line spacing doesn't change). Can you please cite anywhere which describes how the properties including padding and margin are supposed to behave when they're applied to a 'display: inline' element?
ChrisW
"with inline parent, only the generated anonymous block boxes are underlined, not the original child block box" -- I don't understand this sentence (also I've upgraded to FF3.5.3 to try to see what you're talking about).
ChrisW
what i mean with "with inline parent, ...": underlined are "This is anonymous text before the P." and "This is anonymous text after the P.". "This is the content of P." is not underlined.
ax
the behavior of padding and margin on inline elements is detailed in the spec, chapter 10, Visual formatting model details. for the others i'm not sure.
ax
When 'text-decoration: underline;' is added to the body element I see (using FF3.5.3 and using IE8) that all 3 sentences (including "This is the content of P") are underlined, both when 'display:block' and when 'display:inline' (and that surprises me, because 'text-decoration' is specified as not inherited).
ChrisW
do you use the exact example from the spec, incl. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">? with "display: inline" on body, i get underlines on the anonymous blocks and no underline on the <p>. */* gotta go now - good night.
ax
Ok: I now see that that there is a difference with the padding and margin properties, and I even understand why that difference exists. The reason why there's a difference is that when `<BODY>` is `display:block` then the padding is a property of the containing block which contains the `<P>`. Whereas when the `<BODY>` is `display:inline` then there is no containing block for the `<P>` (instead the `<P>` is contained in the top-level viewport or document block), and the padding is a property of the contents of the anonymous blocks.
ChrisW
"incl. `<!DOCTYPE `..." -- Ok now I see the difference with the text-decoration too. Thanks for your help.
ChrisW
A: 

I think I've understood the difference, finally, and there is a fundamental difference.

When the top-level element (e.g. <BODY>) is defined with display:block, then:

  • There's a block associated with the element

  • This block contains (i.e. it acts as the containing block for) anonymous blocks (e.g. text nodes) and for non-anonymous child element (e.g. <P> blocks)

  • The top-level element's style attributes, e.g. padding, are associated with this containing block

When the top-level element (e.g. <BODY>) is defined with display:inline, then:

  • There's no single block associated with the element

  • The element's contents (text nodes in an anonymous block, and child elements in a non-anonymous block) do not have a containing block which is associated with the top-level element

  • The top-level element's style attributes, e.g. padding, are associated with its inline content

ChrisW