在鼠标悬停时在文本字段上放置标签,更改位置的CSS。

huangapple go评论57阅读模式
英文:

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
在鼠标悬停时在文本字段上放置标签,更改位置的CSS。

on hover the label will move up and color will change
在鼠标悬停时在文本字段上放置标签,更改位置的CSS。

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: &#39;Inter&#39;, 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 -->

&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;floating-label-content&quot;&gt;
&lt;input class=&quot;floating-input&quot; type=&quot;text&quot; placeholder=&quot; &quot;&gt;
&lt;label class=&quot;floating-label&quot;&gt;Your Name&lt;/label&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- 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/

huangapple
  • 本文由 发表于 2023年5月11日 19:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定