之前碰到一個(gè)作業(yè),給了一張圖片,讓按照?qǐng)D片樣式做一個(gè)表單,但在所有功能都實(shí)現(xiàn)后,發(fā)現(xiàn)無(wú)法讓表單居中,一直縮在左上角,看起來(lái)很難看。在經(jīng)過(guò)了各種修改后,終于成功將表單居中,下面分享一下我所經(jīng)歷過(guò)程中的錯(cuò)誤與最終結(jié)果。(因?yàn)樽鲞@部分作業(yè)的時(shí)候還沒(méi)有學(xué) css,所以沒(méi)用)
1、剛做出來(lái)的樣子
<form>
<label for="firstname">名字:</label>
<input type="text" name="firstname" id="firstname" required="required" value="" /><br />
<label for="lastname">姓氏:</label>
<input type="text" name="lastname" id="lastname" required="required" value="" /><br />
<label for="login name">登錄名:</label>
<input type="text" name="login name" required="required" pattern="^\w{4,8}$" id="login name" value="" />(可包含a-z、0-9和下劃線)<br />
<label for="password">密碼:</label>
<input type="password" name="password" required="required" pattern="^[a-zA-Z]\w{5,17}$" id="password" value="" />(至少包含6個(gè)字符)<br />
<label for="password2">再次輸入密碼:</label>
<input type="password" name="password2" required="required" pattern="^[a-zA-Z]\w{5,17}$" id="password2" value="" /><br />
<label for="myEmail">電子郵箱:</label>
<input type="email" name="myEmail" id="myEmail" value="" />(必須包含@字符)
</form>
看起來(lái)很別扭,所以要繼續(xù)改進(jìn)一下。。。
2、經(jīng)過(guò)修改后
看起來(lái)好像更別扭了,但是實(shí)現(xiàn)了居中,到這里時(shí),我也不知道自己用的 < center> 對(duì)不對(duì)。。。
<center>
<form>
`````
`````
</ form>
</ center>
3、使用表格布局后
這就是在經(jīng)過(guò)了一晚上的修改后,最終呈現(xiàn)的結(jié)果。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="icon" type="text/css" href="./img/favicon.png"/>
<title></title>
</head>
<body>
<center>
<form action="Success.html" target="_blank" method="get">
<table border="0" cellspacing="" cellpadding="">
<tr>
<td><label for="firstname">名字:</label></td>
<td><input type="text" name="firstname" id="firstname" required="required" value="" /></td>
</tr>
<tr>
<td><label for="lastname">姓氏:</label></td>
<td><input type="text" name="lastname" id="lastname" required="required" value="" /></td>
</tr>
<tr>
<td><label for="login name">登錄名:</label></td>
<td><input type="text" name="login name" required="required" pattern="^\w{4,8}$" id="login name" value="" />(可包含a-z、0-9和下劃線)</td>
</tr>
<tr>
<td><label for="password">密碼:</label></td>
<td><input type="password" name="password" required="required" pattern="^[a-zA-Z]\w{5,17}$" id="password" value="" />(至少包含6個(gè)字符)</td>
</tr>
<tr>
<td><label for="password2">再次輸入密碼:</label></td>
<td><input type="password" name="password2" required="required" pattern="^[a-zA-Z]\w{5,17}$" id="password2" value="" /></td>
</tr>
<tr>
<td><label for="myEmail">電子郵箱:</label></td>
<td><input type="email" name="myEmail" id="myEmail" value="" />(必須包含@字符)</td>
</tr>
<tr>
<td><label>性別:</label></td>
<td>
<input type="radio" name="sex" id="" value="male" />男<img src="./img/Male.gif" >
<input type="radio" name="sex" id="" value="female" />女<img src="./img/Female.gif" >
</td>
</tr>
<tr>
<td><label>頭像:</label></td>
<td><input type="file" name="myFile" /></td>
</tr>
<tr>
<td><label>愛(ài)好:</label></td>
<td>
<input type="checkbox" name="hobby" id="" value="運(yùn)動(dòng)" />運(yùn)動(dòng)
<input type="checkbox" name="hobby" id="" value="聊天" />聊天
<input type="checkbox" name="hobby" id="" value="玩游戲" />玩游戲
</td>
</tr>
<tr>
<td><label>出生日期:</label></td>
<td>
<input type="text" size="1" name="year"placeholder="yyyy" id="" value="" />年
<select name="month">
<option value ="0">[選擇月份]</option>
<option value ="1">1月</option>
<option value ="2">2月</option>
<option value ="3">3月</option>
<option value ="4">4月</option>
<option value ="5">5月</option>
<option value ="6">6月</option>
<option value ="7">7月</option>
<option value ="8">8月</option>
<option value ="9">9月</option>
<option value ="10">10月</option>
<option value ="11">11月</option>
<option value ="12">12月</option>
</select>
<input type="text" size="1" name="day" placeholder="dd"/>日
</td>
</tr>
</table>
<input type="image" src="img/submit.gif" value="提交" />
<input type="image" src="img/reset.gif" onclick="reset();return false;" value="重填" />
</form>
</center>
</body>
</html>
若覺(jué)得左邊的標(biāo)簽左對(duì)齊不好看,也可以在 < td >標(biāo)簽中加入align=“right”,使文字右對(duì)齊
<td align="right"><label for="firstname">名字:</label></td>
到此這篇關(guān)于HTML中的表單Form實(shí)現(xiàn)居中效果的文章就介紹到這了,更多相關(guān)HTML表單Form居中內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!