3

How do I make an input text using CSS, with a dashed bottom border/line that the length of strokes are equal to the length of each character?

Here's an example:

enter image description here

Alessandro Incarnati
  • 7,018
  • 3
  • 38
  • 54
Nuno Almeida
  • 31
  • 1
  • 2

4 Answers4

2

Try using border-bottom: 1px dashed black;

Akshay Gundewar
  • 864
  • 11
  • 30
2

dashed border style doesn't give the exact effect because we cannot control the length of strokes. Instead, we can make the dashed border by using an absolutely positioned pseudo-element behind the input, and specify the space between strokes by letter-spacing property.

input[type="text"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: 0;
  outline: 0;
  background: transparent;
  width: 12.70em;
  font-size: 1em;
}

.input-wrapper, input[type="text"] {
  font-family: monospace;
  font-weight: bold;
  letter-spacing: .3em;
}

.input-wrapper {
  display: inline-block;
  position: relative;
  overflow: hidden;
  padding-bottom: .2em;
  font-size: 200%;
}

.input-wrapper:after {
  content: "——————————————————————————————";
  line-height: .3em;
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: -1;
}
<div class="input-wrapper">
  <input type="text" value="Hello World" maxlength="15">
</div>
Community
  • 1
  • 1
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
0

You could also use a single span per each letter and achieve the same result (using just border-bottom and setting fixed width, height and margin):

span{ 
    display:inline-block;
    border-bottom:5px solid black;
    margin-right:2px; 
    width:20px;
    text-align:center;
    height:30px; 
    font-family:Verdana; 
    font-weight:bold; 
    font-size:24px; 
    /* Add padding-bottom if changing the font-size */
}
<span>H</span>
<span>E</span>
<span>L</span>
<span>L</span>
<span>O</span>
<span>&nbsp;</span>
<span>&nbsp;</span>
<span>&nbsp;</span>
<span>&nbsp;</span>            
Alessandro Incarnati
  • 7,018
  • 3
  • 38
  • 54
0

I had the same problem recently and found another way around the issue:

Hashem Qolami provided a link, and I found this answer by Balthazar pointing at the dashed border generator tool by kovart. The tool is awesome and makes our lives a lot easier!

I decided I only need to modify it a little and use line instead of rect in the resulting SVG code. After that, selecting the correct parameters of the SVG vs font-size vs letter-spacing did the trick.

I only had to worry about digits, so I didn't have to use monospace font to make it work, but it's not the case for text inputs.

Please have a look at the snippet (enter Full Page to make it look as it was intended to):

input {
  width: 100%;
  padding-left: 19px;
  border: 0;
  outline: none;
  font-size: 26px;
  letter-spacing: 27px;
}

/*
Background image from here (slightly adjusted – `line` instead of `rect`):
   https://kovart.github.io/dashed-border-generator/

x2: cut the beginning of the dashes
stroke-dasharray: change the width of the dashes and the blanks
stroke-dashoffset: change the starting point of the dashes
stroke-width: change the thickness of the dashes
*/
input[type="text"] {
  font-family: monospace; /* That's the trick here */
  background-image: url("data:image/svg+xml,%3csvg width='100%' height='100%' xmlns='http://www.w3.org/2000/svg'%3e%3cline x1='100%' y1='100%' x2='4' y2='100%' fill='none' stroke='black' stroke-width='4' stroke-dasharray='33%2c10' stroke-dashoffset='11' stroke-linecap='square'/%3e%3c/svg%3e");
}

input[type="number"] {
  width: 274px;
  letter-spacing: 28.5px;
  background-image: url("data:image/svg+xml,%3csvg width='100%25' height='100%25' xmlns='http://www.w3.org/2000/svg'%3e%3cline x1='263' y1='100%' x2='11' y2='100%' fill='none' stroke='black' stroke-width='4' stroke-dasharray='33%2c10' stroke-dashoffset='39' stroke-linecap='square'/%3e%3c/svg%3e");
}

/*
Just hiding the browser up/down arrows
The keyboard arrows still work, of course
*/
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
<input type="text" value="Hello">

<br/>
<br/>
<br/>
<br/>

<!-- Here's a 6-digit input example -->
<input type="number" temp="" pattern="\\d{6}" max="999999" oninput="validity.valid ? this.temp = value : value = this.temp" value="123456">
Dharman
  • 30,962
  • 25
  • 85
  • 135
victorpolko
  • 359
  • 3
  • 4