更改输入字段中的字体,并在每个字符下添加下划线

Lev*_*evo 2 html css django sass

我想在 Django 中创建完整的文本。间隙应显示丢失了多少个字母。例如:这是一个例子。我会告诉你_ _你就是我_ _。

我在codepen上发现了一些有用的 css-snippet 。我尝试使用它并使用字母而不是数字并将字体更改为 Segoe UI。我遇到的问题是,当我使用 Segoe UI 时,下划线不再与字母同步。我必须如何更改配置才能使其再次适合?

我的SCSS代码:

$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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 HTML 代码:

<input maxlength='7' value=''/>
Run Code Online (Sandbox Code Playgroud)

Jak*_*b E 6

发生这种情况的原因是您使用的字体不是monospaced(字母具有单独的宽度)。如果你改变字体就font-family: monospace;可以了。

这是一个使用 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;
}
Run Code Online (Sandbox Code Playgroud)
Lorem ipsum <input maxlength='7' value=''/> dolor sit amet
Run Code Online (Sandbox Code Playgroud)