鱼C论坛

 找回密码
 立即注册
查看: 4774|回复: 8

[已解决]HTML,如何将表单对齐

[复制链接]
发表于 2020-9-18 12:35:15 | 显示全部楼层 |阅读模式
10鱼币
屏幕截图 2020-09-18 122738.png
如上图所示,只用html的情况下,怎么将用户名、密码、手机号这些对齐?还有后面的框框也对齐?
代码如下:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>登录</title>
  7.    <style>
  8.        .top{
  9.            text-align: center;
  10.        }

  11.        .center{
  12.            text-align: center;
  13.        }
  14.    </style>
  15. </head>
  16. <body>
  17.     <div class="top">
  18.         <h1>注册界面</h1>
  19.     </div>
  20.     <hr/>

  21.     <div class="center" >
  22.         <form>
  23.             用户名:<input type="text" name="username" placeholder="zhangsan"><br/><br/>
  24.             密码:<input type="password" name="pwd" placeholder="123456"/><br><br>
  25.             手机号:<input type="tel" name="tel" placeholder="+86"/><br><br>
  26.             邮箱:<input type="email" name="email" placeholder="@qq.com"><br><br>
  27.             性别: 男:<input type="radio" name="sex"/> 女:<input type="radio" name="sex"/><br><br>
  28.             爱好:<input type="checkbox" />唱歌
  29.                 <input type="checkbox" /> 睡觉
  30.                 <input type="checkbox" /> LOL
  31.                 <br>
  32.                 <input type="checkbox" /> 旅游
  33.                 <input type="checkbox" /> 高尔夫
  34.                 <input type="checkbox" /> 篮球

  35.                 <br><br>
  36.             籍贯:<select >
  37.                     <option>--请选择</option>
  38.                     <option>中国</option>
  39.                     <option>俄国</option>
  40.                     <option>法国</option>
  41.                     <option>德国</option>
  42.                   </select>

  43.             <br><br>
  44.             个人介绍:<textarea rows="10" cols="10" ></textarea>
  45.             <br><br>
  46.             <input type="checkbox" />是否同意协议
  47.             <br><br>
  48.             <input type="submit" value="提交"/>
  49.         </form>
  50.     </div>
  51. </body>
  52. </html>
复制代码
最佳答案
2020-9-18 12:35:16

另一种方法是使用CSS的Flexbox布局来实现对齐。以下是一个示例代码:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6.   display: flex;
  7.   flex-direction: column;
  8.   align-items: flex-start;
  9. }
  10. .label {
  11.   margin-right: 10px;
  12. }
  13. .input {
  14.   width: 200px;
  15.   margin-bottom: 10px;
  16. }
  17. .button {
  18.   margin-top: 10px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23.     <div class="container">
  24.         <h1>注册界面</h1>
  25.         <div>
  26.             <label class="label" for="username">用户名:</label>
  27.             <input class="input" type="text" id="username" name="username">
  28.         </div>
  29.         <div>
  30.             <label class="label" for="password">密码:</label>
  31.             <input class="input" type="password" id="password" name="password">
  32.         </div>
  33.         <div>
  34.             <label class="label" for="phone">手机号:</label>
  35.             <input class="input" type="text" id="phone" name="phone">
  36.         </div>
  37.         <!-- 其他表单项 -->
  38.         <button class="button" type="submit">注册</button>
  39.     </div>
  40. </body>
  41. </html>
复制代码

这个示例使用了一个包含所有表单元素的容器(`<div class="container">`),并通过CSS的Flexbox布局将其子元素在垂直方向上从上到下排列,同时使标签和输入框对齐。`.label`和`.input`类定义了标签和输入框的样式,你可以根据需要进行调整。最后一个按钮被设置为居中对齐。

通过修改和扩展这个示例,你可以实现更复杂的表单布局。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。

最佳答案

查看完整内容

另一种方法是使用CSS的Flexbox布局来实现对齐。以下是一个示例代码: 这个示例使用了一个包含所有表单元素的容器(``),并通过CSS的Flexbox布局将其子元素在垂直方向上从上到下排列,同时使标签和输入框对齐。`.label`和`.input`类定义了标签和输入框的样式,你可以根据需要进行调整。最后一个按钮被设置为居中对齐。 通过修改和扩展这个示例,你可以实现更复杂的表单布局。 以上回复来自 -- ChatGPT(FishC官方接口), ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-18 12:35:16 | 显示全部楼层    本楼为最佳答案   

另一种方法是使用CSS的Flexbox布局来实现对齐。以下是一个示例代码:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6.   display: flex;
  7.   flex-direction: column;
  8.   align-items: flex-start;
  9. }
  10. .label {
  11.   margin-right: 10px;
  12. }
  13. .input {
  14.   width: 200px;
  15.   margin-bottom: 10px;
  16. }
  17. .button {
  18.   margin-top: 10px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23.     <div class="container">
  24.         <h1>注册界面</h1>
  25.         <div>
  26.             <label class="label" for="username">用户名:</label>
  27.             <input class="input" type="text" id="username" name="username">
  28.         </div>
  29.         <div>
  30.             <label class="label" for="password">密码:</label>
  31.             <input class="input" type="password" id="password" name="password">
  32.         </div>
  33.         <div>
  34.             <label class="label" for="phone">手机号:</label>
  35.             <input class="input" type="text" id="phone" name="phone">
  36.         </div>
  37.         <!-- 其他表单项 -->
  38.         <button class="button" type="submit">注册</button>
  39.     </div>
  40. </body>
  41. </html>
复制代码

这个示例使用了一个包含所有表单元素的容器(`<div class="container">`),并通过CSS的Flexbox布局将其子元素在垂直方向上从上到下排列,同时使标签和输入框对齐。`.label`和`.input`类定义了标签和输入框的样式,你可以根据需要进行调整。最后一个按钮被设置为居中对齐。

通过修改和扩展这个示例,你可以实现更复杂的表单布局。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-18 12:51:47 | 显示全部楼层
搞个盒子包住,然后text-align: left;
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-18 13:37:44 | 显示全部楼层
ZSniubi000 发表于 2020-9-18 12:51
搞个盒子包住,然后text-align: left;

不行,它变成屏幕靠左对齐了,怎么居中然后对齐?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-18 16:18:40 | 显示全部楼层
大仙爱吃糖 发表于 2020-9-18 13:37
不行,它变成屏幕靠左对齐了,怎么居中然后对齐?


再把父盒子居中  方法很多 比如margin auto
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-18 16:56:46 | 显示全部楼层
kogawananari 发表于 2020-9-18 16:18
再把父盒子居中  方法很多 比如margin auto

我试试
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-26 03:07:25 | 显示全部楼层
只用html的情况下,怎么将用户名、密码、手机号这些对齐?还有后面的框框也对齐?

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>登录</title>
  7.    <style>
  8.        .top{
  9.            text-align: center;
  10.        }

  11.        .center{
  12.            text-align: center;
  13.        }
  14.    </style>
  15. </head>
  16. <body>
  17.     <div class="top">
  18.         <h1>注册界面</h1>
  19.     </div>
  20.     <hr/>

  21.     <div class="center" >
  22.         <form>
  23.             用户名:<input type="text" name="username" placeholder="zhangsan"><br/><br/>
  24.             密码:<input type="password" name="pwd" placeholder="123456"/><br><br>
  25.             手机号:<input type="tel" name="tel" placeholder="+86"/><br><br>
  26.             邮箱:<input type="email" name="email" placeholder="@qq.com"><br><br>
  27.             性别: 男:<input type="radio" name="sex"/> 女:<input type="radio" name="sex"/><br><br>
  28.             爱好:<input type="checkbox" />唱歌
  29.                 <input type="checkbox" /> 睡觉
  30.                 <input type="checkbox" /> LOL
  31.                 <br>
  32.                 <input type="checkbox" /> 旅游
  33.                 <input type="checkbox" /> 高尔夫
  34.                 <input type="checkbox" /> 篮球

  35.                 <br><br>
  36.             籍贯:<select >
  37.                     <option>--请选择</option>
  38.                     <option>中国</option>
  39.                     <option>俄国</option>
  40.                     <option>法国</option>
  41.                     <option>德国</option>
  42.                   </select>

  43.             <br><br>
  44.             个人介绍:<textarea rows="10" cols="10" ></textarea>
  45.             <br><br>
  46.             <input type="checkbox" />是否同意协议
  47.             <br><br>
  48.             <input type="submit" value="提交"/>
  49.         </form>
  50.     </div>
  51. </body>
  52. </html>
复制代码

@FishC_GPT
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-26 03:07:44 | 显示全部楼层
小甲鱼的二师兄 发表于 2023-11-26 03:07
只用html的情况下,怎么将用户名、密码、手机号这些对 ...

可以使用HTML的表格(table)来实现对齐。以下是一个示例代码:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>注册界面</title>
  5.     <style>
  6.         .top {
  7.             text-align: center;
  8.         }
  9.         .center {
  10.             text-align: center;
  11.         }
  12.     </style>
  13. </head>
  14. <body>
  15.     <div class="top">
  16.         <h2>注册界面</h2>
  17.     </div>
  18.     <div class="center">
  19.         <table>
  20.             <tr>
  21.                 <td>用户名:</td>
  22.                 <td><input type="text" name="username"></td>
  23.             </tr>
  24.             <tr>
  25.                 <td>密码:</td>
  26.                 <td><input type="password" name="password"></td>
  27.             </tr>
  28.             <tr>
  29.                 <td>手机号:</td>
  30.                 <td><input type="text" name="phone"></td>
  31.             </tr>
  32.             <tr>
  33.                 <td>邮箱:</td>
  34.                 <td><input type="text" name="email"></td>
  35.             </tr>
  36.             <tr>
  37.                 <td>性别:</td>
  38.                 <td>
  39.                     <label><input type="radio" name="gender" value="male">男</label>
  40.                     <label><input type="radio" name="gender" value="female">女</label>
  41.                 </td>
  42.             </tr>
  43.             <tr>
  44.                 <td>爱好:</td>
  45.                 <td>
  46.                     <label><input type="checkbox" name="hobby" value="唱歌">唱歌</label>
  47.                     <label><input type="checkbox" name="hobby" value="睡觉">睡觉</label>
  48.                     <label><input type="checkbox" name="hobby" value="LOL">LOL</label>
  49.                     <label><input type="checkbox" name="hobby" value="旅游">旅游</label>
  50.                     <label><input type="checkbox" name="hobby" value="高尔夫">高尔夫</label>
  51.                     <label><input type="checkbox" name="hobby" value="篮球">篮球</label>
  52.                 </td>
  53.             </tr>
  54.             <tr>
  55.                 <td>籍贯:</td>
  56.                 <td>
  57.                     <select name="birthplace">
  58.                         <option value="">--请选择--</option>
  59.                         <option value="中国">中国</option>
  60.                         <option value="俄国">俄国</option>
  61.                         <option value="法国">法国</option>
  62.                         <option value="德国">德国</option>
  63.                     </select>
  64.                 </td>
  65.             </tr>
  66.             <tr>
  67.                 <td>个人介绍:</td>
  68.                 <td><textarea name="introduction"></textarea></td>
  69.             </tr>
  70.             <tr>
  71.                 <td></td>
  72.                 <td><label><input type="checkbox" name="agreement">是否同意协议</label></td>
  73.             </tr>
  74.         </table>
  75.         <br>
  76.         <input type="submit" value="注册">
  77.     </div>
  78. </body>
  79. </html>
复制代码

在示例代码中,使用了一个表格来布局,并通过`<td>`元素对齐各个输入框和标签。注意,这只是一种简单的布局方法,你可以根据实际需求进行调整和美化。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-26 03:08:11 | 显示全部楼层
FishC_GPT 发表于 2023-11-26 03:07
可以使用HTML的表格(table)来实现对齐。以下是一个示例代码:

使用另外一种方法
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-4 20:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表