How to add static text inside an input form

2020-06-03 07:06发布

问题:

How can I put this static text in an input form?

It's there all the time.

This is my code:

<label for="subdomain">Subdomain:</label>
<input type="text" placeholder="ExampleDomain" id="subdomain"/>

回答1:

HTML

<label for="subdomain">Subdomain:</label>
<input type="text" placeholder="ExampleDomain" id="subdomain" />
<input type="text" id="subdomaintwo" value=".domain.com" disabled/>

CSS

input[type="text"]#subdomaintwo{
    -webkit-appearance: none!important;
    color: red;
    text-align: right;
    width: 75px;
    border: 1px solid gray;
    border-left: 0px;
    margin: 0 0 0 -7px;
    background: white;
}
input[type="text"]#subdomain{
    -webkit-appearance: none!important;
    border: 1px solid gray;
    border-right: 0px;
    outline: none;
}

JS Fiddle for this



回答2:

You can achieve this with the following approach:

  • place the <input> in a <label> with position:relative
  • give the <label> an ::after pseudo-element with position:absolute
  • set box-sizing of the <input> to border-box
  • give the <input> a padding-right equal to the width of the ::after pseudo-element

Working Example:

label, input {
    position: relative;
    display: block;
    padding-right: 76px;
    width: 170px;
    box-sizing: border-box;
}

label::after {
    content: '.' attr(data-domain);
    position: absolute;
    top: 4px;
    left: 94px;
    font-family: arial, helvetica, sans-serif;
    font-size: 12px;
    display: block;
    color: rgba(0, 0, 0, 0.6);
    font-weight: bold;
}
<label data-domain="domain.com">
<input type="text" placeholder="exampledomain" />
<label>



回答3:

The readonly property should be used:

<label for="subdomain">Subdomain:</label>
<input type="text" placeholder="ExampleDomain" id="subdomain" />
<input type="text" id="subdomaintwo" value=".domain.com" readonly="readonly" />

Because disabled controls that do not receive focus and are ignored in tabbing navigation, are not posted. The readonly property only can't be changed by the user.



回答4:

How about wrapping your input inside a div (let's call it div.wrapper for ease), and then you can add some text in the div.wrapper with ".domain.com" aligned to the right? Like this for example:

<div class="wrapper"> <input type="text" name="subdomain"/> <p class="input-text">.domain.com</p> </div>

You can style your div to make it look like your input and can make sure the actual input has no border, etc., so it's indistinguishable from div.wrapper.

It is a bit of a hack, but why not?



回答5:

I'm not sure if this can be accomplished using just one input form.

Maybe what you are seeing there is not a single input form, but an input form next to a static text.

So my idea here is to put an input form (where the user can write) followed by a static text (.domain.com). Then you can put both them inside a container and style the container to look like a input form.

This will do the trick.



回答6:

In angular you can do this:

In the html file add placeholder attribute to input with text variable: <input type="time" class="timepicker" placeholder="{{text_variable}}">

In the css:

.timepicker:before {
    content:'.' attr(placeholder);
    margin-right:5px;
    color:#9d9d9d;
}


回答7:

I´ve written an span and given it the bootstrap class .form-control, a grey background and filled in that span the static word.

<span class="form-control bg-grey">Exampledomain</span>

But I think, the Answer of nerkn solves it´s best.