【问题标题】:Styling input label to align inside input box to float right样式化输入标签以在输入框内对齐以向右浮动
【发布时间】:2018-12-29 17:32:59
【问题描述】:

我正在尝试对齐输入框内的输入标签并向右浮动

输入框从b4扩展而来。

这是现在的样子(箭头显示我想要做什么) 这是布局(它在 vue 中但只是忽略。仅显示我如何安排我的元素。

    <label class="input-label" :for="index" v-if="showLabel">{{ label }}

        <span v-if="icon" :class="icon"></span>

        <input v-if="form !== false"
                type="text"
                v-validate="validation"
                v-model="current"
                :id="index"
                :name="index"
               :disabled="disabled"
                :placeholder="label"
                class="form-control"
                :class="{ 'is-invalid': form.errors.has(index) }" @input="triggerEvent">

    </label>

这是我对 css 的尝试:

f.input-icon {
    position: relative;
}
.input-icon input {
    text-indent: 30px;
}
.input-icon span {
    position: absolute;
    top: 50px;
    left: 15px;
    font-size: 20px;
}
.input-label {
    color: blue;
}
label {
    float: right;
    width: 10em;
    position: relative;
    margin-right: 1em;
}

【问题讨论】:

  • 也许尝试在标签样式中添加margin-top: .5em 或其他内容?看起来margin-right 正在被拾取。
  • 所做的只是将输入框向下推,看起来像
  • 啊,也许您也必须相对定位输入,就像您在标签中放置的那样。或者为了简化,只需将输入放在它之外(在 html 中)。
  • HTML 中有 input-label 类,CSS 中有 .input-icon
  • @RoyJ 你是什么意思?它们都被使用。您可以在屏幕截图中看到蓝色的.input-label 正在注册,因为在屏幕截图中标签是蓝色的。 .input-icon 用于图标。我已经让那个工作了

标签: html css vue.js


【解决方案1】:

我创建了这支笔,我相信它可以回答您的问题。见Form input with right aligned label pen on CodePen。我在下面包含了我的精简代码以供快速参考。

HTML

<div class="form-control">
  <label>Test</label>
  <input type="text" placeholder="label">
</div>

CSS

* {
  box-sizing: border-box;
}
.form-control {
  padding: 0.625rem;
  position: relative;
}
label {
  position: absolute;
  top: 1.7rem;
  right: 1.625rem;
  color: blue;
}
input[type="text"] {
  display: block;
  width: 100%;
  padding: 1rem;
}

【讨论】:

  • 完美运行
  • @AndrewRayner 很高兴我能帮上忙。我已经修改了我的 CSS 以确保输入的文本(如果太长)不会与蓝色的绝对定位标签重叠。您的用例需要对您进行一些微调。
猜你喜欢
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
相关资源
最近更新 更多