单击其他地方后,请重点关注输入文本
我正在研究由YouTube视频制成的输入设计,并且它运行良好,但我想对其进行一些修改,即代码:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
flex-direction: column;
gap: 30px;
background: #1d2b3a;
}
/******************** Input Box - Not Required ********************/
.inputBox{
position: relative;
width: 250px;
}
.inputBox input{
width: 100%;
padding: 10px;
border: 1px solid rgba(255,255,255,0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox span{
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255,255,255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox input:invalid ~ span,
.inputBox input:focus ~ span{
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox input:invalid,
.inputBox input:focus{
border: 1px solid #00dfc4;
}
/******************** Input Box - Required ********************/
.inputBox_r{
position: relative;
width: 250px;
}
.inputBox_r input{
width: 100%;
padding: 10px;
border: 1px solid rgba(255,255,255,0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox_r span{
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255,255,255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox_r input:valid ~ span,
.inputBox_r input:focus ~ span{
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox_r input:valid,
.inputBox_r input:focus{
border: 1px solid #00dfc4;
}
.inputBox_r input:valid ~ span,
.inputBox_r input:focus ~ span{
background: #00dfc4;
color: #1d2b3a;
border-radius: 2px;
}
**HTML:**
<div class="inputBox">
<input type="text" name="">
<span>First Name</span>
</div>
<br>
<div class="inputBox_r">
<input type="text" name="" required>
<span>Last Name</span>
</div>
如您所见,有两个输入不是强制性的,另一个是必须使用必需属性。必需的属性,这正是我想要的第一个输入框。
第一个输入框没有必需的属性,这就是为什么即使我在其中输入文本并单击其他某个地方,它也会脱颖而出,并且它会弄乱我的CSS造型。
因此,当输入文本时,是否有任何方法可以将重点放在第一个输入上,而无需传递必需的属性?
我到处搜索了这个问题,但找不到任何东西。 抱歉,如果我说错了,我是HTML&amp; CSS
I was working on a input design i made from a youtube video and its working very well but i want to modify it a little bit, the code:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
flex-direction: column;
gap: 30px;
background: #1d2b3a;
}
/******************** Input Box - Not Required ********************/
.inputBox{
position: relative;
width: 250px;
}
.inputBox input{
width: 100%;
padding: 10px;
border: 1px solid rgba(255,255,255,0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox span{
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255,255,255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox input:invalid ~ span,
.inputBox input:focus ~ span{
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox input:invalid,
.inputBox input:focus{
border: 1px solid #00dfc4;
}
/******************** Input Box - Required ********************/
.inputBox_r{
position: relative;
width: 250px;
}
.inputBox_r input{
width: 100%;
padding: 10px;
border: 1px solid rgba(255,255,255,0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox_r span{
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255,255,255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox_r input:valid ~ span,
.inputBox_r input:focus ~ span{
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox_r input:valid,
.inputBox_r input:focus{
border: 1px solid #00dfc4;
}
.inputBox_r input:valid ~ span,
.inputBox_r input:focus ~ span{
background: #00dfc4;
color: #1d2b3a;
border-radius: 2px;
}
**HTML:**
<div class="inputBox">
<input type="text" name="">
<span>First Name</span>
</div>
<br>
<div class="inputBox_r">
<input type="text" name="" required>
<span>Last Name</span>
</div>
As you can see there are two inputs one is not mandatory and the other one is mandatory with the required attribute.When you enter text inside the second input box it stas in focus state because of the required attribute and this is exactly what i want with the first input box.
The First input box does not have the required attribute and thats why even if i enter text inside it and click somewhere else it goes out of focus and its messing up my css styling.
So is there any way to keep focus on the first input when text is entered but without passing the required attribute?
I searched for this everywhere but couldnt find anything.
Sorry if i said anything wrong, i am new to HTML & CSS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个简单的JS,可以检查一个字段是否具有值,并相应地添加/删除一类
.field
。然后也调整了CSSHere's a simple bit of JS that will check if a field has a value in it and add/remove a class of
.filled
accordingly. Then the CSS is adjusted as well