Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display span over input with HTML+CSS

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.

like image 885
EasyDevop Avatar asked Dec 18 '13 08:12

EasyDevop


People also ask

Can I put a span inside an input?

Wrap both your input and your span inside a container, position this container as relative, and the span as absolute. You can now do whatever you like with the span.

How do you span input fields in HTML?

The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.

Can you style span in HTML?

The span tag is just like a div, which is used to group similar content so it can all be styled together.


1 Answers

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

like image 120
Morpheus Avatar answered Sep 19 '22 06:09

Morpheus