Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block Level Elements inside Inline elements [duplicate]

The W3C Validator tells me I can't put block-level elements inside inline elements. Makes sense...

but what if I'm using CSS to change that block-level element into an inline element?

And what if I'm using CSS to convert an inline element into a block-level element (when inside another inline element)?

The Validator doesn't pick up on this obviously, but is it wrong?

(I'm not actually doing this, I'm just wondering about best-practice)

like image 471
aidan Avatar asked Nov 11 '09 09:11

aidan


People also ask

Can block elements be inside inline elements?

Note: An inline element cannot contain a block-level element!

Do block level elements stay inline with other elements?

Generally, block-level elements may contain inline elements and (sometimes) other block-level elements. Inherent in this structural distinction is the idea that block elements create "larger" structures than inline elements.

Can block elements be nested inside block elements?

Yes, you can have nested block elements. You may need to use floats or positioning to keep them from stacking though. I think you may be confusing having a block element child of a block element (good) with a block element that is a child of an inline element (will probably render fine; but violates standards).

How does a block level element differ from an inline element?

By default, inline elements do not force a new line to begin in the document flow. Block elements, on the other hand, typically cause a line break to occur (although, as usual, this can be changed using CSS).


2 Answers

An element defined as a block element in the HTML specification is not the same as a element defined as display: block; in CSS.

With this I mean: Even if you say <p style="display: inline;">Lorem...</p>. The <p> will still be a block element in the eyes of HTML.

HTML, CSS and JavaScript if used right is said to have loose coupling toward each other and that one should not be dependent of another and that the absence of one should not make the others (or the system as whole) fail.

like image 57
anddoutoi Avatar answered Oct 14 '22 11:10

anddoutoi


An element that is inline elements should not contain block elements. Block elements can contain block and/or inline elements while inline elements can only contain other inline (including inline-block, such as <img>) elements.

You can of course do it anyways, since the graphical representation will be pretty consistent across browsers. It's still not something I'd recommend though, and can't really think of a reason to do it either.

like image 23
Blixt Avatar answered Oct 14 '22 11:10

Blixt