Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put display:inline element inside nested display:block element doesn't work?

Tags:

html

css

See this example:

http://jsfiddle.net/aLrfmyqm/

p {
    display: inline;
}
    
em {
    display:block;
}
<p> Outer inline <em>Block <p>Inner inline</p></em></p>   

I expect the <p>Inner inline</p> to be inlined with Block, however, it starts in a new line. Does anyone have ideas about this? Thanks!

like image 408
Hanfei Sun Avatar asked Jun 12 '15 07:06

Hanfei Sun


1 Answers

Your markup is invalid. You are not supposed to nest a p element inside p element and hence the issue.

From W3C :

The P element represents a paragraph. It cannot contain block-level elements (including P itself).

Check the source and you will get it why it behaves differently than what you expect it to be

enter image description here

Your browser will actually separate all the tags out and close the p elements for you.

So how we fix it? Use the <span> element instead of <p>

Demo

like image 96
Mr. Alien Avatar answered Nov 08 '22 21:11

Mr. Alien