英文:
How to add fixed placeholder text to an input box and prevent the user from deleting it on a web page?
问题
I understand that you want a translation of the provided text, excluding the code. Here's the translation:
制作一个新产品的订购表,其中一个列是指定产品的RAL颜色。
我希望有一个固定的文本,用户无法删除,所以他们只需要在之后输入一个与RAL颜色相关的四位数字。
我遇到的问题是输入超过了固定文本而不是在其后 - 请参见下面的截图。
请忽略表格的设计,因为我目前正在处理功能,它看起来有点糟糕。
HTML:
<td class="lst-input-ral-box">
<input value="" id="ral" maxlength="4" autofocus="autofocus">
<span class="ral-placeholder">RAL</span>
</td>
CSS:
.lst-input-ral-box {
position: relative;
}
.input {
display: block;
border: 1px solid #d7d6d6;
background: #fff;
padding: 10px 10px 10px 20px;
width: 195px;
}
.ral-placeholder {
position: absolute;
display: block;
left: 5px;
top: 3px;
z-index: 9;
}
希望这有所帮助,如果您有任何问题,请随时提出。
英文:
Working on an ordering table for a new product and one of the columns is to specify the RAL color of the product.
I want to have fixed text that the user can't delete so all they have to do is enter a four digit number after that which relates to the RAL colour.
The problem I'm having is the input is going over the fixed text and not after it - see below.
Please ignore the design of the table as I'm working on the functionaility for now and its looking a bit rubbish.
HTML:
<td class="lst-input-ral-box">
<input value="" id="ral" maxlength="4" autofocus="autofocus">
<span class="ral-placeholder">RAL</span>
</td>
CSS:
.lst-input-ral-box {
position: relative;
}
.input {
display: block;
border: 1px solid #d7d6d6;
background: #fff;
padding: 10px 10px 10px 20px;
width: 195px;
}
.ral-placeholder {
position: absolute;
display: block;
left: 5px;
top: 3px;
z-index: 9;
}
Any help on this would be greatly appreciated as I'm fairly new to 'complex' coding.
答案1
得分: 4
不要将静态文本放在输入框内部,而是放在输入框之前。您可以通过从输入框中移除边框/轮廓等,并将其添加到包含元素中来样式化它,使其“看起来”像是在输入框内部。例如:
<!-- language: lang-css -->
.lst-input-ral-box {
border: 1px solid #d7d6d6;
}
.lst-input-ral-box:focus-within {
border: 1px solid #000;
}
input {
border: none;
outline: none;
}
<!-- language: lang-html -->
<table>
<tr>
<td class="lst-input-ral-box">
<span class="ral-placeholder">RAL</span>
<input value="" id="ral" maxlength="4" autofocus="autofocus">
</td>
</tr>
</table>
编辑: 为了完整起见,可以通过将 <span>
更改为 <label>
进一步改进,这样激活时将聚焦到 <input>
。我还减小了两个包含元素之间的“间隙”,所以完全可点击的区域应该会激活 <input>
:
<!-- language: lang-css -->
.lst-input-ral-box {
border: 1px solid #d7d6d6;
display: flex;
gap: 0;
}
.lst-input-ral-box:focus-within {
border: 1px solid #000;
}
input {
border: none;
outline: none;
}
<!-- language: lang-html -->
<table>
<tr>
<td class="lst-input-ral-box">
<label for="ral" class="ral-placeholder">RAL</label>
<input value="" id="ral" maxlength="4" autofocus="autofocus">
</td>
</tr>
</table>
请注意,<input>
的 <label>
具有重要的语义含义。在这里,应考虑可访问性,因为并非所有用户都会视觉上观察或关心这种样式上的放置。
这个元素是否为输入框添加了“标签”?从语义上来说,这由您决定。还可以使用 aria-
属性的组合和其他功能来实现合理的设计,同时保持标记的语义清晰。
英文:
Don't put the static text inside the input, put it before the input. You can style it to "appear" to be inside the input by removing the border/outline/etc. from the input and adding it to the containing element. For example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.lst-input-ral-box {
border: 1px solid #d7d6d6;
}
.lst-input-ral-box:focus-within {
border: 1px solid #000;
}
input {
border: none;
outline: none;
}
<!-- language: lang-html -->
<table>
<tr>
<td class="lst-input-ral-box">
<span class="ral-placeholder">RAL</span>
<input value="" id="ral" maxlength="4" autofocus="autofocus">
</td>
</tr>
</table>
<!-- end snippet -->
Edit: For completeness, this can be further improved by changing the <span>
to a <label>
, which will focus the <input>
when activated. I've also reduced the "gap" between the two contained elements, so the full clickable area should activate the <input>
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.lst-input-ral-box {
border: 1px solid #d7d6d6;
display: flex;
gap: 0;
}
.lst-input-ral-box:focus-within {
border: 1px solid #000;
}
input {
border: none;
outline: none;
}
<!-- language: lang-html -->
<table>
<tr>
<td class="lst-input-ral-box">
<label for="ral" class="ral-placeholder">RAL</label>
<input value="" id="ral" maxlength="4" autofocus="autofocus">
</td>
</tr>
</table>
<!-- end snippet -->
Note that a <label>
for an <input>
has an important semantic meaning. One should take accessibility into consideration here, as not all users will visually observe or care about this stylistic placement.
Does this element "label" the input? Semantically that's for you to decide. Combinations of aria-
attributes and other functionality can also be used to achieve reasonable designs while keeping the markup semantically clear.
答案2
得分: 2
I have well understood, you want to add 4 digits after the "RAL" constant right?
If so, you should replace:
<input value="" id="ral" maxlength="4" autofocus="autofocus">
<span class="ral-placeholder">RAL</span>
with:
<label for="ral" class="ral-placeholder">RAL</label>
<input type="text" value="" id="ral" name="ral" maxlength="4" autofocus="autofocus">
PS: you have to know that the name
attribute is a good practice to use correctly in the for=""
from <label>
tag, even if id
is working also.
英文:
if I have well understood, you want to add 4 digits after the "RAL" constant right ?
Is so, you should replace
<input value="" id="ral" maxlength="4" autofocus="autofocus">
<span class="ral-placeholder">RAL</span>
with:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<label for="ral" class="ral-placeholder">RAL</label>
<input type="text" value="" id="ral" name="ral" maxlength="4" autofocus="autofocus">
<!-- end snippet -->
PS : you have to know that the name
attribute is a good practice to use correctly in the for=""
from <label>
tag, even if id
is working also.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论