Levo
Levo

Reputation: 239

Changing Font in input field with underline under each character

I want to create a complete text in Django. The gaps should show how many letters are missing. For example: This is an example. I will sh_ _ you what i me_ _.

I found some usefull css-snippet on codepen. I tried to play around with it and use letters instead of numbers and change the font to Segoe UI. The problem i have is that the underlines are not synchronized with the letters anymore when i use Segoe UI. How do i have to change the configurations so it can fit well again?

My SCSS code:

$char-w: 1ch;
$gap: .5*$char-w;
$n-char: 7;
$in-w: $n-char*($char-w + $gap);

input {
    display: block;
    margin: 2em auto;
    border: none;
    padding: 0;
    width: $in-w;
    background: repeating-linear-gradient(90deg, 
        dimgrey 0, dimgrey $char-w, 
        transparent 0, transparent $char-w + $gap) 
        0 100%/ #{$in-w - $gap} 2px no-repeat;
    font: 5ch Segoe UI; /*That's the attribute i changed*/
    letter-spacing: $gap;

    &:focus {
        outline: none;
        color: dodgerblue;
    }
}

my HTML code:

<input maxlength='7' value=''/>

Upvotes: 2

Views: 2597

Answers (1)

Jakob E
Jakob E

Reputation: 4936

The reason this happens is that the font you are using is not monospaced (letters have individual widths). If you change the font to font-family: monospace; it will work.

Here is an example using Ubuntu:

@import url("https://fonts.googleapis.com/css?family=Ubuntu|Ubuntu+Mono");
body {
  font: 1.2rem 'Ubuntu', sans-serif;
}

input {
  display: inline-block;
  margin: 1.2rem auto;
  border: none;
  padding: 0;
  width: 10.5ch;
  background: repeating-linear-gradient(90deg, dimgrey 0, dimgrey 1ch, transparent 0, transparent 1.5ch) 0 100%/ 10ch 2px no-repeat;
  font: 1.2rem 'Ubuntu Mono', monospace;
  letter-spacing: 0.5ch;
}
input:focus {
  outline: none;
  color: dodgerblue;
}
Lorem ipsum <input maxlength='7' value=''/> dolor sit amet

Upvotes: 6

Related Questions