0

I am trying to get input box like below:

enter image description here

I am new to CSS so I just want to achieve how to put text in border of input box OR I have to make a div with border and put text into border of div.

Here is my current go, I know it not good one but in between

    input {
  display:block;
  width:100%;
  margin:10px 0;
  padding:10px;
  
}

I did that but unable to wrap text with border, any suggestion would be appreciable.

4
  • Show at least what you have so far. Your best try. Commented Aug 13, 2020 at 7:06
  • @RokoC.Buljan I am still trying, but unable to pick right approach. Just created above for css. Commented Aug 13, 2020 at 7:09
  • 1
    @SandeepTiwari May be doing some research would have got you the results - its takes more time to write a question and less time searching on google. Just FYI. Thank you! Commented Aug 13, 2020 at 7:10
  • @AlwaysHelping great you are rock, -ve voters are every where among them someone like you Commented Aug 13, 2020 at 7:14

2 Answers 2

5

Try this

<fieldset>
    <legend>Name</legend>
    <input type="text" name="name" id="name" placeholder="Enter your Name">
</fieldset>


<style>
    fieldset > input{
        border: none;
        outline: none;
        width: 100%;
    }
</style>

Edit:

The fieldset by itself add the border, the legend tag add the name text above the input, and then you will just edit the input element removing the border and the outline properties, to don't ruin the design when focused showing multiple borders, and adjust the width to fit the fieldset and don't look so weird when adding text (see below).

<fieldset>
    <legend>Name</legend>
    <input type="text" name="name" id="name" placeholder="Enter your Name" value="a thousand of values hehe, see the text won't fit the fieldset">
</fieldset>


<style>
    fieldset > input{
        border: none;
        outline: none;
    }
</style>

Sign up to request clarification or add additional context in comments.

3 Comments

@David great its working, but unable to get it please explain little bit
I tried to add some explanation there
2

label{
  display:block; 
  position:relative; 
  bottom:-10px; 
  width:fit-content; 
  left:10px; 
  padding:0px 5px; 
  background-color:white;
}
input{
  border:2px solid gray; 
  height: 30px; 
  width:300px;
  padding:5px 10px;
  font-weight:bold;
}
    <form >
        <label for="name">Name</label>
        <input type="text" placeholder="Enter Your Name" name="name" >
    </form>

3 Comments

thanks for your answer but I need text in border like image. I have done this like text then input box
This will not work if you have for example a form on a gradient background or over an image.
yes your right this wont work on gradient background. I did not even think about it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.