开发者

Is it neccessary to add HTML doctype at the starting of the web page

开发者 https://www.devze.com 2023-02-22 20:05 出处:网络
Is it neccessary to write doctype at the starting of the HTML page? E.g. If I use this doctype and in the HTML file :

Is it neccessary to write doctype at the starting of the HTML page? E.g.

If I use this doctype and in the HTML file :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body >
    <table width="95%" height="100%"  border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#99FF66">
    <tr>
    <td>
<form id="login" name="loginForm" action="" method="post">    
          <table align="center" border="0" cellspacing="0">

               <tr><td>User Name:</td><td><input class="inputtext" name="username" type="text" id="un"/></td></tr>
               <tr><td>Password:</td>  <td><input class="inputtext" name="password" type="password" id="pwd"/></td>   </tr>

               <tr><td style="padding: 0 0 0 3px"> <input name="Submit" type="submit" class="sbutton" tabindex="4" value="Login" /></td></tr>
         </table>
 </form>
</td>
</tr>
</table>

The table or the form is n开发者_如何学运维ot coming at the center (horizontal and vertical) of the page. It is just horizontally centered. I want both. If I remove the above doctype, then the table is centered on both the axis. Please tell where I am wrong.


Technically, no, you dont need a doctype to get something to display on the page. In practice however, you definitely should.

A doctype is essentially a protocol that dictates how each element on the page should be displayed, which tags are valid etc. etc.

The existence of doctypes, means that web browsers can be developed against this doctype, to ensure they render all elements correctly.

When the doctype is not present, the browser resorts to interpreting the HTML itself (often called quirks mode). While most modern browsers make a fairly good attempt at this (provided the html is relatively well formed), there can still be inconsistencies the way browsers choose to display elements.

So in short, the use of a doctype means that you can rely (for the most part) on your page rendering the same in every browser, making development significantly easier.


The reason for your layout changing with and without a doctype is because without a doctype a browser goes into quirks mode which doesn't render your layout as consistently as it would with a doctype across the multitude of browsers, most notably Internet Explorer.

A full rundown of the history and rendering differences can be found at QuirksMode


The DOCTYPE tag tells the browser in wich version of the marckup (in this case XHTML 1.0 Transitional) is the document written.

So the proper way is to use it and then write the documment following the dtd indicated by DOCTYPE.

I'd suggest to check the valid DOCTYPEs ( http://www.w3.org/QA/2002/04/valid-dtd-list.html ) and validate your document afterwards ( http://validator.w3.org/ ).

To place the tables on the document you can use CSS ( http://www.w3.org/Style/CSS/ ) and you can also validate your CSS ( http://jigsaw.w3.org/css-validator/ ).

Hope it helps :)


Is it neccessary to write doctype at the starting of the HTML page?

Yes, it is necessary. See DOCTYPE by W3Schools

The table or the form is not coming at the center (horizontal and vertical) of the page.

Use CSS to specify locations of elements in the page.

0

精彩评论

暂无评论...
验证码 换一张
取 消