【发布时间】:2015-06-25 16:52:12
【问题描述】:
如何使用 CSS 制作输入文本,底部虚线/线条的笔划长度等于每个字符的长度?
这是一个例子:
【问题讨论】:
如何使用 CSS 制作输入文本,底部虚线/线条的笔划长度等于每个字符的长度?
这是一个例子:
【问题讨论】:
dashed 边框样式没有给出确切的效果,因为我们cannot control the length of strokes。相反,我们可以通过在输入后面使用绝对定位的伪元素来制作虚线边框,并通过letter-spacing 属性指定笔划之间的空间。
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>
【讨论】:
尝试使用border-bottom: 1px dashed black;
【讨论】:
您还可以为每个字母使用一个跨度并获得相同的结果(仅使用边框底部并设置固定的宽度、高度和边距):
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> </span>
<span> </span>
<span> </span>
<span> </span>
【讨论】:
我最近遇到了同样的问题,并找到了解决该问题的另一种方法:
Hashem Qolami 提供了a link,我发现this answer by Balthazar 指向dashed border generator tool by kovart。这个工具很棒,让我们的生活更轻松!
我决定只需要稍微修改一下,并在生成的 SVG 代码中使用 line 而不是 rect。
之后,选择 SVG vs font-size vs letter-spacing 的正确参数就可以了。
我只需要担心数字,所以我不必使用monospace 字体来使其工作,但text 输入的情况并非如此。
请查看 sn-p(输入整页以使其看起来像预期的那样):
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">
【讨论】: