Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<HTML> and <BODY>: Why are there no new lines?

So I read that <html> and <body> are block level elements, just like <div> and <p>.

I understand that block level elements start a new line.

For example aaa<div>b</div>ccc looks like this:

aaa
b
ccc

So, why don't <html> and <body> add two lines to the top of your html page?

like image 740
Lebowski156 Avatar asked Feb 28 '13 04:02

Lebowski156


2 Answers

Block level elements don't "start new lines"... they simply expand to both sides indefinitely until the hit a container element or the sides of the display (width:100%)... because of this, they have the effect of "pushing" any other content down below them, or dropping below any inline content that immediately precedes them. It also means that block-level elements will only "push down" sibling-level elements.

<html> and <body> elements have no siblings, only children, so they don't need to displace anything.

Here's a graphical representation of what's happening: enter image description here

Given this markup:

<html>
<head></head>
<body>
  <div>&nbsp;</div>
  <div>&nbsp;</div>
  <div style='width:45%; float:left;'>
    <div>&nbsp;</div>
  </div>
  <div style='width:45%; float:left;'>&nbsp;</div>
</div>
</body>
</html>
like image 160
Ben D Avatar answered Oct 17 '22 15:10

Ben D


Think of it this way:

<div>
    <div>Text</div>
</div>

There is only one line of text:

Text

This is just like the case when you have any text in the body:

<html>
    <body>Text</body>
</html>

When the text is in the child element, no new lines are introduced.

like image 2
Antony Avatar answered Oct 17 '22 15:10

Antony