How to add static text inside an input form

2020-06-03 07:18发布

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

It's there all the time.

Enter image description here

This is my code:

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

7条回答
我只想做你的唯一
2楼-- · 2020-06-03 07:44

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

查看更多
萌系小妹纸
3楼-- · 2020-06-03 07:44

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.

查看更多
爷的心禁止访问
4楼-- · 2020-06-03 07:45

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.

查看更多
5楼-- · 2020-06-03 07:46

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?

查看更多
劳资没心,怎么记你
6楼-- · 2020-06-03 07:56

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.

查看更多
太酷不给撩
7楼-- · 2020-06-03 07:58

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>

查看更多
登录 后发表回答