【问题标题】:align label in right and left for input左右对齐标签以进行输入
【发布时间】:2021-01-15 08:33:16
【问题描述】:

我想使用 dl dt dd 列表创建带有标签的输入表单,第一个标签在左边,第二个在右边并在中心输入,我已经花了一个小时来解决这个问题。问题是我可以将第一个标签放在右侧或左侧,但第二个标签不会向左或向右并排输入...

这是一个例子:

这是我目前尝试过的:

dt {
  clear: both;
  width: 25%;
  float: right;
  text-align: left;
}

label {
  font: 11px Tahoma, sans-serif;
  color: #000;
}

dd {
  float: right;
  width: 25%;
  margin: 0 0 15px;
}
<dl class="inline">
  <dt><label for="name">label1</label></dt>
  <dd>
    <input type="text" id="name" class="text" />
    <small>input description</small>
  </dd>
  <dt><label for="name">label2</label></dt>
</dl>

PS:我真的很想继续使用 dl dt dd..

【问题讨论】:

  • input description 怎么样?这不应该被渲染吗?可以从 HTML 中删除吗?

标签: html css responsive-design stylesheet


【解决方案1】:

dl, dd, dt 只是 html 元素,默认情况下是块元素。因此,通过将所有这些元素设置为内联或内联块,它们将位于同一行。

.inline > * {
  display: inline-block;
}
<dl class="inline">
  <dt><label for="name">label1</label></dt>
  <dd>
    <input type="text" id="name" class="text" />
    <small>input description</small>
  </dd>
  <dt><label for="name">label2</label></dt>
</dl>

【讨论】:

    【解决方案2】:

    其中一种解决方案是使用 flex。

    注意:此解决方案可能会有所不同,具体取决于您需要支持的浏览器以及表单是否应该是响应式的,我会说您是否有任何类型的 CSS 架构。

    dl.inline {
      display: flex;
      justify-content: flex-end;
    }
    
    .inline dt {
      width: 25%;
    }
    .inline dt:first-child {
      text-align: right;
    }
    
    .inline dd {
      display: flex;
      flex-direction: column;
      flex-grow: 1;
      padding: 0 15px;
      margin: 0;
    }
    
    label {
      font: 11px Tahoma, sans-serif;
      color: #000;
    }
    <dl class="inline">
      <dt><label for="name">label1</label></dt>
      <dd>
        <input type="text" id="name" class="text" />
        <small>input description</small>
      </dd>
      <dt><label for="name">label2</label></dt>
    </dl>

    【讨论】:

      【解决方案3】:

      只需将所有内容都向左浮动:

      dt,
      dd {
        float: left;
        margin:5px;
      }
      dd small {
        display:block;
      }
      
      /* to center everything*/
      dl {
       display:table;
       margin:auto;
      }
      /**/
      
      label {
        font: 11px Tahoma, sans-serif;
        color: #000;
      }
      <dl class="inline">
        <dt><label for="name">label1</label></dt>
        <dd>
          <input type="text" id="name" class="text" />
          <small>input description</small>
        </dd>
        <dt><label for="name">label2</label></dt>
      </dl>

      【讨论】:

        【解决方案4】:

        我希望这是您所期望的,将 display:flex 和 justify-content:space-between 属性添加到 .inline 类。

        .inline{
        display:flex;
        justify-content:space-between;
        margin:0px 10px;
        }
        

        .inline {
          display: flex;
          justify-content: space-between;
          margin: 0px 10px;
        }
        <dl class="inline">
          <dt><label for="name">label1</label></dt>
          <dd>
            <input type="text" id="name" class="text" />
            <small>input description</small>
          </dd>
          <dt><label for="name">label2</label></dt>
        </dl>

        使用float:

        .inline {
          width: 100%;
        }
        
        .left {
          width: 25%;
          float: left;
          text-align: left;
        }
        
        .right {
          width: 25%;
          float: right;
          text-align: right;
        }
        
        label {
          font: 11px Tahoma, sans-serif;
          color: #000;
        }
        
        dd {
          margin: 0 0 15px;
          width:50%;
          float:left;
          text-align:center;
        }
        <dl class="inline">
          <dt class="left"><label for="name">label1</label></dt>
          <dd>
            <input type="text" id="name" class="text" />
            <small>input description</small>
          </dd>
          <dt class="right"><label for="name">label2</label></dt>
        </dl>

        使用positioning 属性。

        .inline {
          position: relative;
          margin: 0px 10px;
        }
        
        .center {
          position: absolute;
          margin-left: auto;
          margin-right: auto;
          left: 0;
          right: 0;
          text-align: center;
        }
        
        .left {
          position: absolute;
          left: 0;
        }
        
        .right {
          position: absolute;
          right: 0;
        }
        <dl class="inline">
          <dt class="left"><label for="name">label1</label></dt>
          <dd class="center">
            <input type="text" id="name" class="text" />
            <small>input description</small>
          </dd>
          <dt class="right"><label for="name">label2</label></dt>
        </dl>

        【讨论】:

        • 是的,它看起来不错,但是你可以在没有 flex 的情况下做到这一点,就像我尝试的那样......上面并按照我现有的 css 来修复它,如果我遗漏了什么?
        • @Robert checkout my answer 我已经更新了我的答案
        【解决方案5】:

        没有弹性。 内联解决方案。

        dl {
          text-align: center;
        }
        
        dt {
          display: inline-block;
        }
        
        label {
          font: 11px Tahoma, sans-serif;
          color: #000;
        }
        
        dd {
          display: inline-block;
          margin: 0 5px 0 5px;
        }
        
        small {
          display: table-row;
        }
        <dl class="inline">
          <dt><label for="name">label1</label></dt>
          <dd>
            <input type="text" id="name" class="text" />
            <small>input description</small>
          </dd>
          <dt><label for="name">label2</label></dt>
        </dl>

        【讨论】:

        • 是的,我真的需要它而不需要 flex,例如使用相对位置,显示..浮动......如果可能的话,好吗?
        • @Robert,我使用inline-block 做了一个解决方案。请检查。
        【解决方案6】:

        我想你想要这样。

        div {
                display: flex;
                justify-content: center;
                align-items: center;
            }
        <div>
                <label>label1</label>
                <input type="text">
                <label>label2</label>
            </div>

        【讨论】:

        • 谢谢,但我知道如何使用 div,但正如我所说,我想保留 dl dt dd 列表,这可能吗?
        猜你喜欢
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-15
        • 2012-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多