![](https://i-blog.csdnimg.cn/blog_migrate/3cbb459049a2af758922cf75556b0e61.jpeg)
这是一个右端的输入框对齐,文字向右对齐的登录表单,在DIV+CSS中控制它们还费了一点时间来折腾,下面整理出来让大家了解,其中CSS要注意的几个地方就是文中红字的地方。
body { font:12px/19px Arial, Helvetica, sans-serif; color:#666;}
input.required{border:#999 1px solid; height:16px; width:120px;}
label{ text-align:right; float:left; width:60px;}
.sub{margin-left:50px;}
.int{margin:5px auto;}
下面是具体的编码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>表单对齐</title>
<style type="text/css">
body {font:12px/19px Arial, Helvetica, sans-serif; color:#666;}
input.required{border:#999 1px solid; height:16px; width:120px;}
label{text-align:right; width:60px; float:left;}
.sub{margin-left:50px;}
.int{margin:5px auto;}
</style>
</head>
<body>
<form method="post" action="#">
<div class="int">
<label for="username">用户名:</label>
<input type="text" id="username" class="required" />
</div>
<div class="int">
<label for="email">邮 箱:</label>
<input type="text" id="email" class="required" />
</div>
<div class="int">
<label for="pefsoninfo">个人资料:</label>
<input type="text" id="pefsoninfo" class="required" />
</div>
<div class="sub">
<input type="submit" value="提交" id="send" />
<input type="reset" value="重置" id="res" />
</div>
</form>
</body>
</html>
下面再把一个复杂点的加进来:
![](https://i-blog.csdnimg.cn/blog_migrate/f2432b629b5a20d9f5342aa9709a6b89.jpeg)
基本的xhtml:
- <h3>已注册用户登录</h3><span id="more-25"></span>
- <form action="" method="post" id="Login">
- <fieldset>
- <legend>用户登录</legend>
- <div>
- <label for="Name">用户名</label>
- <input type="text" name="Name" id="Name" size="18" maxlength="30" /><br />
- </div>
- <div>
- <label for="password">密码</label>
- <input type="password" name="password" id="password" size="18" maxlength="15" /><br />
- </div>
- <div class="cookiechk">
- <label><input type="checkbox" name="CookieYN" id="CookieYN" value="1" /> <a href="#" title="选择是否记录您的信息">记住我</a></label>
- <input name="login791" type="submit" class="buttom" value="登录" />
- </div>
- <div class="forgotpass"><a href="#">您忘记密码?</a></div>
- </fieldset>
- </form>
看了代码,发现标单描述文字都在< label >< /label >中,只要让< label >< /label >标签浮动左对齐,fieldset包含的< div >清除浮动,就可以实现我们常见的那类布局。
下面是基本的CSS:
- fieldset label {
- float:left;
- width:120px;
- text-align:right;
- padding:4px;
- margin:1px;
- }
- fieldset div {
- clear:left;
- margin-bottom:2px;
- }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Form demo</title>
<style type="text/css">
<!--
body {
font-family: Arial, Helvetica, sans-serif;
font-size:12px;
color:#666666;
background:#fff;
text-align:center;
}
* {
margin:0;
padding:0;
}
a {
color:#1E7ACE;
text-decoration:none;
}
a:hover {
color:#000;
text-decoration:underline;
}
h3 {
font-size:14px;
font-weight:bold;
}
pre,p {
color:#1E7ACE;
margin:4px;
}
input, select,textarea {
padding:1px;
margin:2px;
font-size:11px;
}
.buttom{
padding:1px 10px;
font-size:12px;
border:1px #1E7ACE solid;
background:#D0F0FF;
}
#formwrapper {
width:450px;
margin:15px auto;
padding:20px;
text-align:left;
border:1px solid #A4CDF2;
}
fieldset {
padding:10px;
margin-top:5px;
border:1px solid #A4CDF2;
background:#fff;
}
fieldset legend {
color:#1E7ACE;
font-weight:bold;
padding:3px 20px 3px 20px;
border:1px solid #A4CDF2;
background:#fff;
}
fieldset label {
float:left;
width:120px;
text-align:right;
padding:4px;
margin:1px;
}
fieldset div {
clear:left;
margin-bottom:2px;
}
.enter{ text-align:center;}
.clear {
clear:both;
}
-->
</style>
</head>
<body>
<div id="formwrapper">
<h3>已注册用户登录</h3>
<form action="" method="post" id="Login">
<fieldset>
<legend>用户登录</legend>
<div>
<label for="Name">用户名</label>
<input type="text" name="Name" id="Name" size="18" maxlength="30" />
<br />
</div>
<div>
<label for="password">密码</label>
<input type="password" name="password" id="password" size="18" maxlength="15" /><br />
</div>
<div class="cookiechk">
<label><input type="checkbox" name="CookieYN" id="CookieYN" value="1" /> <a href="#" title="选择是否记录您的信息">记住我</a></label>
<input name="login" type="submit" class="buttom" value="登录" />
</div>
<div class="forgotpass"><a href="#">您忘记密码?</a></div>
</fieldset>
</form><br />
<h3>未注册创建帐户</h3>
<form action="" method="post" id="apForm">
<fieldset>
<legend>用户注册</legend>
<p><strong>您的电子邮箱不会被公布出去,但是必须填写.</strong> 在您注册之前请先认真阅读服务条款.</p>
<div>
<label for="Name">用户名</label>
<input type="text" name="cr_Name" id="cr_Name" value="" size="20" maxlength="30" />
*(最多30个字符)<br />
</div>
<div>
<label for="Email">电子邮箱</label>
<input type="text" name="Email" id="Email" value="" size="20" maxlength="150" /> *<br />
</div>
<div>
<label for="password">密码</label>
<input type="password" name="cr_password" id="cr_password" size="18" maxlength="15" />
*(最多15个字符)<br />
</div>
<div>
<label for="confirm_password">重复密码</label>
<input type="password" name="confirm_password" id="confirm_password" size="18" maxlength="15" />
*<br />
</div>
<div>
<label for="AgreeToTerms">同意服务条款</label>
<input type="checkbox" name="AgreeToTerms" id="AgreeToTerms" value="1" />
<a href="#" title="您是否同意服务条款">先看看条款?</a> * </div>
<div class="enter">
<input name="create791" type="submit" class="buttom" value="提交" />
<input name="Submit" type="reset" class="buttom" value="重置" />
</div>
<p><strong>* 在提交您的注册信息时, 我们认为您已经同意了我们的服务条款.<br />
* 这些条款可能在未经您同意的时候进行修改.</strong></p>
</fieldset>
</form>
</div>
</body>
</html>