www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Beginner question - why use <body> and <html> tags?

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Beginner question - why use <body> and <html> tags?

    Hi!

    Even though I've been working with IT for many years now, I'm relatively new at HTML scripting. So please bear with if this is a stupid question.

    Why do I need the <html> and <body> tags when writing a html script?

    I understand their uses to the extend that <body> is the "body"-part of the page, and usually comes right after <head> - and <html> is just a tag to specify which kind of language I'm using. But what happens if I leave them out? What difference do they make?

    If I make a very simple index.html containing the following:

    Code:
    <title>Page title</title>
    <h1>Headline of page</h1>
    <p>Text on page</p>
    ...it works just fine. What difference would it do to to add <body> and <html> tags to the code?

    Thanks in advance!

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    There are probably a few edge cases where it matters, but by and large you're right, you can leave off those tags, plus the <head> tag as well, and not suffer any consequences.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Mar 2011
    Posts
    1,133
    The state of the web is such that browser software, search engine robots, and other "user agents" tend to be reasonably tolerant of errors because they encounter so many of them. That said, it doesn't pay to be intentionally sloppy with your coding. It can make it more difficult to maintain your site, diagnose rendering problems, and can potentially impair your site's performance in the search engines.

    Proper coding not only gives you the best chance of having your web pages seen and interpreted as you intend them to be, it can help overcome issues caused by improper coding at the server level. In the end, you'll save yourself a lot of frustration by following the nitpicking little rules that come with web design.
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,458
    Hi there lleroy.

    and a warm welcome to these forums.

    Why do I need the <html> and <body> tags when writing a html script?
    Well, personally, you are totally free to code as you heart's desire dictates,
    but if you were employed to work for a company then it would be necessary
    for you to strictly follow coding conventions so that all team members were
    'singing from the same song sheet'.


    coothead

  5. #5
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by rtrethewey View Post
    The state of the web is such that browser software, search engine robots, and other "user agents" tend to be reasonably tolerant of errors because they encounter so many of them.
    Though, in this case, it wouldn't be an error. The spec explicitly says the HTML, HEAD and BODY tags are optional.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  6. #6
    Join Date
    Feb 2013
    Posts
    80
    I just like to add, while most browsers will call most errors they DON'T catch them all. I remember one time my website didn't look right, so I spent hours trying to figure it out. Then it dawn on me to check my html with w3c validator and load & behold I forgot to close a tag </a>, as a side not I felt stupid spending all the hours when it only took a few seconds to check my code. Moral of the story while you might get away it most of the time, it will eventually come around and bite you.

  7. #7
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,458
    Hi there lleroy,

    Quote Originally Posted by Jeff Mott View Post
    The spec explicitly says the HTML, HEAD and BODY tags are optional.
    If you are planning on referring to them with javascript, then their inclusion, of course, will become essential.

    coothead

  8. #8
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by coothead View Post
    Hi there lleroy,


    If you are planning on referring to them with javascript, then their inclusion, of course, will become essential.

    coothead
    Nope, still optional.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  9. #9
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,458
    Hi there Jeff,
    Nope....
    Well, in all my test browsers, this code...
    Code:
    
    <script type="text/javascript">
    
       document.body.style.backgroundColor='#000';
    
    </script>
    ...fails to turn the page black.

    Do you have a magical browser in your set.

    coothead

  10. #10
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    This code below also fails to turn the background color black.

    HTML Code:
    <!doctype html>
    <html>
        <head>
            <script type="text/javascript">
               document.body.style.backgroundColor='#000';
            </script>
        </head>
        <body>
        </body>
    </html>
    In both cases, the reason is because the body doesn't exist yet. Normally, we wait for the DOM ready event or the window load event.

    This works:

    HTML Code:
    <!doctype html>
    <script type="text/javascript">
        onload = function () {
            document.body.style.backgroundColor='#000';
        };
    </script>
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  11. #11
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,458
    Hi there Jeff,

    You are right and I am wrong.

    As long as there is some sort of content above the script....

    Code:
    
        Jeff Mott
    
    <script type="text/javascript">
    
       document.body.style.backgroundColor='#000';
    
    </script>
    
    ...it will work.

    coothead

  12. #12
    Join Date
    Apr 2013
    Posts
    135
    Hi lleroy
    <html> is the element that begins and ends each and every web page. Its sole purpose is to hold each web element nicely in position and serves the role of book cover; all other HTML elements are encapsulated within the <html> element. and
    The <body> tag defines the document's body.
    The <body> element contains all the contents of an HTML document, such as text, hyperlinks, images, tables, lists, etc.
    that's why <html> and <body> tags are important for writing a html script.

  13. #13
    its very good work

  14. #14
    Join Date
    Apr 2013
    Posts
    135
    thanks

  15. #15
    Join Date
    Apr 2013
    Location
    USA
    Posts
    1
    <HTML> tag is used to provide the information to the users how to display the things in browsers and <Body> tag used to defines all the content of the HTML document.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles