简体   繁体   English

无需使用html和CSS中的表格即可进行文本对齐

[英]Text alignment without using table in html and css

I'm trying to learn HTML and CSS and using the old-school approach by creating a login page with three input controls (two text and one button). 我正在尝试学习HTML和CSS,并通过使用具有三个输入控件(两个文本和一个按钮)的登录页面来使用老式方法。 However, I'm using <table></table> for the alignment issues of textboxes. 但是,我正在使用<table></table>解决文本框的对齐问题。 But I've read somewhere that using tables is not considered a good approach. 但是我在某处读到,使用表并不是一个好方法。 This is what I'm doing: 这就是我在做什么:

Home.html Home.html

<div>
    <table>
        <thead>
            <tr><th><span>Login Page</span></th></tr>
        </thead>
        <tbody>
            <tr>
            <th><label>Username:</label></th>
            <td><input type="text" value="" /></td>
            </tr>
            <tr>
                <th><label>Password:</label></th>
                <td><input type="text" value="" /></td>
            </tr>
            <tr>
                <td><input type="button" value="Submit" /></td>
            </tr>
        </tbody>
    </table>
</div>

However, I'm using some beginner stuff of CSS to align input controls without using tables and this is what I'm trying: 但是,我正在使用一些CSS的初学者来对齐输入控件,而不使用表,这就是我正在尝试的方法:

Home.html Home.html

<div class="box">
    <span>Login Page</span>
    <br />
    <span>
        <label>Username:</label>
        <input type="text" value="" />
        <br />
        <label>Password:</label>
        <input type="text" value="" />
        <br />
        <input type="button" value="Submit" />
    </span>
</div>

style.css style.css

.box {
   margin: 10px 10px 10px 10px;
   border: dotted 2px #fff;
   width: 500px;
}

.box span:first-child {
    border: dotted 1px;
    margin-left: 30%;
    font-family: Arial, sans-serif;
    width: 50%;
}

.box span label:nth-of-type(odd)
{
    color:blue;
    font-family: Arial, sans-serif;
}

.box span label:nth-of-type(even)
{
    color: red;
    font-family: Arial, sans-serif;
    display: inline-block;
}

My concern is using css, I'm able to align the input controls but I have to use multiple break tags ( <br /> ) and also extra code for alignment which is easier by simply using the <table> tag. 我关心的是使用CSS,我能够对齐输入控件,但是我必须使用多个break标记( <br /> )和额外的对齐代码,只需使用<table>标记即可更轻松地对齐。 Can someone suggest me the standard approach and either I'm on the right path or not? 有人可以建议我采用标准方法,而我是否走对了路?

Although tables are a very good approach for forms but I prefer the much shorter and easier method ie CSS tables 尽管表格是表单的一种非常好的方法,但是我更喜欢更短,更容易的方法,即CSS tables

Here is a code: 这是一个代码:

 form { display: table; } p { display: table-row; } label { display: table-cell; } input { display: table-cell; } 
 <form> <p> <label for="a">Short label:</label> <input id="a" type="text"> </p> <p> <label for="b">Very very very long label:</label> <input id="b" type="text"> </p> </form> 

As a small addition to the things that are said already, I would recommend you to consider an option to use a separate container for a single form control and its label. 作为已经说过的话的一小部分补充,我建议您考虑对单个表单控件及其标签使用单独容器的选项。 Like this: 像这样:

<form>
    <div class="input-group">
        <label for="name">Username:</label>
        <input type="text" id="name" value="" />
    </div>
    <div class="input-group">
        <label for="password">Password:</label>
      <input type="text" id="password" value="" />
    </div>
</form>

Perhaps, one might say this is redundant, but from my perspective, it gives you more control during positioning. 也许有人会说这是多余的,但从我的角度来看,它在定位过程中为您提供了更多控制。 On the other hand, Michael Seltenreich is making a good point. 另一方面, Michael Seltenreich提出了一个很好的观点。 I still find tables used for forms in many places, although I'd prefer to keep away from this method. 我仍然在许多地方找到用于表格的表格,尽管我宁愿远离这种方法。

PS In case you want to spread labels and inputs horizontally, you would probably want to use flexboxes. PS如果要水平分布标签和输入,则可能要使用flexbox。

@iSahilSharma Please find following code without using table. @iSahilSharma请在不使用表的情况下找到以下代码。 I hope you were expecting the same. 希望您也一样。 A part from it just a suggestion that start using Bootstrap framework instead of using custom coding. 它的一部分只是一个建议,即开始使用Bootstrap框架而不是使用自定义编码。

 .main_container{ width:100%; } .inner_box { margin: 40px auto; width: 30%; } .inner_box span{ clear: both; display: block; } .inner_box span:first-child{ margin-bottom:10px; } .inner_box span:nth-child(n+2){ margin-bottom:20px; } 
 <div class="main_container"> <div class="inner_box"> <span>Login Page</span> <span> <label>Username:</label> <input type="text" value="" /> </span> <span> <label>Password:</label> <input type="text" value="" /> </span> <span> <input type="button" value="Submit" /> </span> </div> </div> 

Using table is not always good. 使用表格并不总是很好。 One way to do is 一种方法是

   <div class="form-wrap">
     <h2>Login Form</h2>
     <div class="form-field">
       <label>User Name</label>
       <input type="text" value="" />
     </div>
     <div class="form-field">
       <label>Password</label>
       <input type="text" value="" />
     </div>
     <input type="button" value="Submit" />
    </div>

CSS: CSS:

.form-field label {
  width: 80px;
  display: inline-block;
}
.form-field {
  margin-bottom: 10px;
}

link to codepen 链接到Codepen

出于多种原因,表不是一种好的方法,但是表非常适合您要创建的类型的表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM