英文:
put label over textfield on hover change the location css
问题
我需要在页面加载时设计这个文本字段,初始状态将如下图所示:
在悬停时,标签将向上移动并更改颜色。
是否有相关解决方案链接?
谢谢。
英文:
I need to design this text field initially on page load it will be like below image
on hover the label will move up and color will change
any solution link for the same.
Thanks
答案1
得分: 1
你可以使用 ~
和 :placeholder-shown
CSS 选择器的组合来实现这个效果。
以下是一个示例:
.container {
width: 300px;
padding: 20px;
margin: 0 auto;
font-family: 'Inter', sans-serif;
}
.floating-label-content {
position: relative;
margin-bottom: 20px;
}
.floating-label {
color: #000;
font-size: 13px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 15px;
top: 11px;
padding: 0 5px;
background: #fff;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
.floating-input {
font-size: 12px;
display: block;
width: 100%;
height: 36px;
padding: 0 20px;
background: #fff;
color: #000;
border: 1px solid #000;
border-radius: 4px;
box-sizing: border-box;
}
.floating-input:focus {
outline: none;
border: 1px solid #3D85D8;
}
.floating-input:focus~.floating-label {
top: -8px;
font-size: 13px;
color: #3D85D8;
}
.floating-input:not(:placeholder-shown) {
color: #3D85D8;
border: 1px solid #3D85D8;
}
.floating-input:not(:placeholder-shown)~.floating-label {
top: -8px;
font-size: 13px;
color: #3D85D8;
}
<div class="container">
<div class="floating-label-content">
<input class="floating-input" type="text" placeholder=" ">
<label class="floating-label">Your Name</label>
</div>
</div>
请注意,这是CSS和HTML的代码示例,用于实现特定效果。
英文:
You can use combination of ~
and :placeholder-shown
css selectors to achieve this.
Here is an example :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.container {
width: 300px;
padding: 20px;
margin: 0 auto;
font-family: 'Inter', sans-serif;
}
.floating-label-content {
position: relative;
margin-bottom: 20px;
}
.floating-label {
color: #000;
font-size: 13px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 15px;
top: 11px;
padding: 0 5px;
background: #fff;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
.floating-input {
font-size: 12px;
display: block;
width: 100%;
height: 36px;
padding: 0 20px;
background: #fff;
color: #000;
border: 1px solid #000;
border-radius: 4px;
box-sizing: border-box;
}
.floating-input:focus {
outline: none;
border: 1px solid #3D85D8;
}
.floating-input:focus~.floating-label {
top: -8px;
font-size: 13px;
color: #3D85D8;
}
.floating-input:not(:placeholder-shown) {
color: #3D85D8;
border: 1px solid #3D85D8;
}
.floating-input:not(:placeholder-shown)~.floating-label {
top: -8px;
font-size: 13px;
color: #3D85D8;
}
<!-- language: lang-html -->
<div class="container">
<div class="floating-label-content">
<input class="floating-input" type="text" placeholder=" ">
<label class="floating-label">Your Name</label>
</div>
</div>
<!-- end snippet -->
答案2
得分: 0
https://css-tricks.com/float-labels-css/
英文:
You can check this example and adapt it for your use:
https://css-tricks.com/float-labels-css/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论