Display span over input with HTML+CSS

会有一股神秘感。 提交于 2019-12-07 08:18:55

问题


I want to display a span element over an input element with CSS. How can I do this. My current code:

<input type="url" placeholder="e.g. www.google.com" />
<span>http://</span>

How can I display the span element on the input element so that the users know there is no need to enter http:// so it would look like if there's already a value in but then it is the span element on the input? I assume I can do that with CSS positioning.

I cannot use placeholder as I don't want it to be removed.


回答1:


As you have mentioned you need to use positioning and wrap your input with span into div or some other element.

.wrapper {
    position: relative;
}

input {
    padding-left: 48px;
}

.wrapper span {
    position: absolute;
    left: 2px;
}
<div class="wrapper">
    <input type="url" placeholder="e.g. www.google.com" />
    <span>http://</span>    
</div>

Example




回答2:


This is a bit of an old post, but there is a more simple way of doing this and without using positioning. Wrap each element of the form in a list item and set the display to 'inline-block' and the display of the span to 'block' like this -

li{
   display: inline-block;
}

li span{
   display: block;
}

You would then need to swap the order of the html elements like so -

<span>http://</span>
<input type="url" placeholder="e.g. www.google.com" />

You could however leave it the same if you wanted the span to display on the bottom of the input element.




回答3:


something like this: fiddle

* {
    font-family: Arial, sans-serif;
    font-size: 12px;
}
.row {
    position:relative;
}
.row span {
    position:absolute;
    left:5px;
    top:5px;
}
.row input {
    padding-left: 40px;
    line-height: 16px;
}
<div class="row">
    <span>http://</span>
    <input type="url" placeholder="e.g. www.google.com" />
</div>


来源:https://stackoverflow.com/questions/20653207/display-span-over-input-with-htmlcss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!