Use the text type input so that its completion is similar to the figure below. The input will accept one character per "square". It is not allowed to use multiple inputs for this field.
Is there a way to recreate the following input without any additional inputs?
The input should be at most 20 characters, each box representing one character.
The input will accept one character per box.
This is what I’ve done so far but with this approach, if I increase the font size everything falls apart
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
padding: 2rem;
}
.input-container {
display: flex;
gap: 1rem;
}
.input-container input {
height: 1rem;
letter-spacing: 8px;
font-size: 15px;
border: none;
outline: none;
background-color: transparent;
font-family: monospace;
}
.box-container {
display: flex;
position: absolute;
top: 50%;
transform: translate(0, -50%);
z-index: -1;
}
.box {
width: 16px;
height: 16px;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 1);
}
<body>
<div class="input-container">
<label for="name">Nume</label>
<div style="position: relative">
<input id="name" type="text" size="40" maxlength="20" />
<div class="box-container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
</div>
</body>
2
Answers
Like j08691 suggested, use relative units for your font and sizing. I’ve applied it with a custom property for easy updates.
I’ve also shifted the characters to center them with some padding on the input.
Demo using
1 rem
:Demo using
2 rem
:To get your code easy to scale you have to define the values in a way to depend on each other. They have to be linked mathematically.
This can be achieved by using variables in the CSS:
Just fit these values to your needs:
Unfortunately, your concept lacks in one central point: "monospace" is a local resource.
Which font will be used at the client depends on the operating system, the web browser and user settings. As fonts have different spacing, each font may need to be justified (variable
--input-letter-relation
) to make it run synchronous with the boxes in the background.The only solution to solve this (using your concept) is to provide the font used.
I decided to "Noto Sans Mono". It is widely spread due to the "Noto" universal fonts, is free to download and can be addressed at googleapis.com, so this demo should run properly.
To justify a font
Now tune the variable
--input-letter-relation
to fit the first an last zero into their field identically.