不用 table 的 form 表单效果

作者: Dynamic Drive     来源: DynamicDrive.com     时间: 2006-08-22 20:11:39
摘要: 以前做 form 时,通常是用两列表格,一列显示名称,一列放置表单元素。而本文介绍的是用 css 来实现的方法,便于控制全站风格,代码简洁。
归类: (X)HTML/CSS,

关键词: 表单, tableless, css, form,

以前做 form 时,通常是用两列表格,一列显示名称,一列放置表单元素。而本文介绍的是用 css 来实现的方法,便于控制全站风格,代码简洁。


---------------------------------------------------
点此查看示例文件
---------------------------------------------------


CSS:
  1.  
  2. .cssform p{
  3. width: 300px;
  4. clear: left;
  5. margin: 0;
  6. padding: 5px 0 8px 0;
  7. padding-left: 155px; /*width of left column containing the label elements*/
  8. border-top: 1px dashed gray;
  9. height: 1%;
  10. }
  11.  
  12. .cssform label{
  13. font-weight: bold;
  14. float: left;
  15. margin-left: -155px; /*width of left column*/
  16. width: 150px; /*width of labels. Should be smaller than left column (155px) to create some right margin*/
  17. }
  18.  
  19. .cssform input[type="text"]{ /*width of text boxes. IE6 does not understand this attribute*/
  20. width: 180px;
  21. }
  22.  
  23. .cssform textarea{
  24. width: 250px;
  25. height: 150px;
  26. }
  27.  
  28. /*.threepxfix class below:
  29. Targets IE6- ONLY. Adds 3 pixel indent for multi-line form contents.
  30. to account for 3 pixel bug: http://www.positioniseverything.net/explorer/threepxtest.html
  31. */
  32.  
  33. * html .threepxfix{
  34. margin-left: 3px;
  35. }
  36.  



HTML:
  1.  
  2. <form id="myform" class="cssform" action="">
  3.  
  4. <p>
  5. <label for="user">Name</label>
  6. <input type="text" id="user" value="" />
  7. </p>
  8.  
  9. <p>
  10. <label for="emailaddress">Email Address:</label>
  11. <input type="text" id="emailaddress" value="" />
  12. </p>
  13.  
  14. <p>
  15. <label for="comments">Feedback:</label>
  16. <textarea id="comments" rows="5" cols="25"></textarea>
  17. </p>
  18.  
  19. <p>
  20. <label for="comments">Sex:</label>
  21. Male: <input type="radio" name="sex" /> Female: <input type="radio" name="sex" /><br />
  22. </p>
  23.  
  24. <p>
  25. <label for="comments">Hobbies:</label>
  26. <input type="checkbox" name="hobby" /> Tennis<br />
  27. <input type="checkbox" name="hobby" class="threepxfix" /> Reading <br />
  28. <input type="checkbox" name="hobby" class="threepxfix" /> Basketball <br />
  29. </p>
  30.  
  31. <p>
  32. <label for="terms">Agree to Terms?</label>
  33. <input type="checkbox" id="terms" class="boxes" />
  34. </p>
  35.  
  36. <div style="margin-left: 150px;">
  37. <input type="submit" value="Submit" /> <input type="reset" value="reset" />
  38. </div>
  39.  
  40. </form>
  41.  




推荐链接:(联系 QQ :326801485)