【问题标题】:Placing static text at the end of an input field using HTML使用 HTML 将静态文本放置在输入字段的末尾
【发布时间】:2014-04-08 15:46:22
【问题描述】:

我要做的是在输入字段的末尾放置一段静态文本。不是占位符,想法是在使用 HTML 和 CSS 的输入元素字段的末尾添加类似“@gmail.com”的内容。我的问题是如何做到这一点?我从引导程序中得到了这个想法validation states icons.

例如,输入字段看起来像..

我希望这是有道理的。如果您有任何问题,请随时提出。

此文本应在用户键入时保持静态,这是将其与输入字段分开的原因。

这是我的一些代码:

<form id="loginForm">
    <fieldset>
        <legend>Login</legend>
        <div class="form-group">
            <input id="usernameField" class="loginField form-control" name="username" type="text" placeholder="john.doe" required>
        </div>
        <br>
        <div class="form-group">
            <input id="passwordField" class="loginField form-control" name="password" type="password" placeholder="password" required>
        </div>
        <br>
        <span class="visible-lg">
            <button class="btn btn-danger loginBtn"><i class="fa fa-signin"></i> Login <i class="fa fa-spinner icon-spin icon-white loginSpinner"></i></button>
        </span>
        <span class="hidden-lg">
            <button class="btn btn-danger loginBtn btn-large"><i class="fa fa-signin"></i> Login <i class="fa fa-spinner icon-spin icon-white loginSpinner"></i></button>
        </span>
        <br>
    </fieldset>
</form>

我的 CSS:

.loginSpinner{
    display: none !important;
}

#badLogin{
    display: none;
}

#badRequest{
    display: none;
}

#createAccountForm{
    display: none;
}

.createSpinner{
    display: none !important;
}

#forgotPassword{
    display: none;
    color: red;
}

#usernameField:before{
    content:"@gmail.com";
    background-color:yellow;
    color:red;
    font-weight:bold;
}

JSFiddle:http://jsfiddle.net/UMBRd/

【问题讨论】:

  • 一些 CSS 技巧怎么样?使用 :beforecontent: '@gmail.com'
  • @luk3thomas CSS 似乎不起作用:\ 这是我的 jsfiddle:jsfiddle.net/UMBRd

标签: javascript html css twitter-bootstrap


【解决方案1】:

这里有一些东西可以帮助您,基本上您需要将输入包装在一个 div 中,并将其宽度调整为与输入相同(我通过浮动 div 来做到这一点,但还有其他方法)。这将为您提供一个坐标系,以便在您想要的文本中放置一个伪元素。

HTML

<div class="input-container">
    <input type="text"/>
</div>

CSS

.input-container {
    position: relative;
    float: left;
}

.input-container:after {
    position: absolute;
    right: 0;
    top: 0;
    content: '@gmail.com';
}

this fiddle

请注意,带有伪元素的纯 CSS 方式无法像 this post 中讨论的那样工作。

【讨论】:

    【解决方案2】:

    我唯一能想到的就是使用absoluterelative 定位将input 覆盖在span 之上。这并不完美,我不得不稍微调整一下 line-height,但这可能足以让你开始:

    HTML

    <div>
        <span>@gmail.com</span>
        <input type="text" />
    </div>
    

    CSS

    div {
        position: relative;
    }
    span {
        display: inline-block;
        width: 200px;
        position: relative;
        line-height:95px;
        text-align:right;
    }
    input {
        position: absolute;
        left: 10px;
        top: 35px;
        background-color: transparent;
        width:200px;
        height: 20px;
    }
    

    http://jsfiddle.net/pemep/

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      相关资源
      最近更新 更多