嗨,我正在try 实现自定义material 轮廓输入,其中包括标签浮动的凹槽.

我知道Angular Metal现有组件,但我需要有自己的特殊定制,因此构建自己的组件.

我在创造浮动方面已经走到了这一步,但在创造实际的缺口方面遇到了困难.我见过一些解决方案,其中标签后面只有白色背景,但这在我的解决方案中行不通,因为输入背景是不同的 colored颜色 .

任何最好的帮助/策略都将是伟大的.我可以使用JS,只要它在多个浏览器中保持一致,这是最重要的.提前感谢.

这是我的堆栈闪电战 https://stackblitz.com/edit/angular-u5nxde-xdq3qy?file=src%2Fapp%2Fexample.component.html,src%2Fapp%2Fexample.component.ts,src%2Fapp%2Fexample.component.css,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.css

Outline expected:

Google design input outline

代码 :

     HTML 
<div class="material-textfield">
      <input placeholder=" " type="text" value="test" />
      <label class="label-container">Label</label>
    </div>

CSS
    .material-textfield {
  position: relative;
}

.label-container {
  position: absolute;
  font-size: 1rem;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  background-color: transparent;
  color: gray;
  padding: 0 0.3rem;
  margin: 0 0.5rem;
  transition: 0.1s ease-out;
  transform-origin: left top;
  pointer-events: none;
}
input {
  font-size: 1rem;
  outline: none;
  border: 1px solid gray;
  border-radius: 5px;
  padding: 1rem 0.7rem;
  color: gray;
  transition: 0.1s ease-out;

  background-color: beige;
}
input:focus {
  border-color: #6200ee;
}
input:focus + .label-container {
  color: #6200ee;
  top: 0;
  transform: translateY(-50%) scale(0.9);
  border-color: #6200ee; /* Color on focus */
}

input:not(:placeholder-shown) + .label-container {
  top: 0;
  transform: translateY(-50%) scale(0.9);
  border-color: #6200ee; /* Color when input is not empty */
}

推荐答案

我试图用最少的代码来重现有缺口的轮廓行为,我使用了我在之前的 comments 中描述的技术:

带缺口的边框由三个html元素组成,它们包含在实际输入后面的绝对Flex dis中,

名为"trailing"的第一个名称存在于输入之前添加的固定填充中.

名为notch的第二个元素包含标签,因此notch就是标签的大小.

最后一个名为trailing的元素占用了标签后面的剩余空间.

这是实现:

.form-field {
  --input-pading: 16px;
  --border: solid 1px black;
  --border-radius: 4px;

  padding-left: var(--input-pading);

  min-width: 0;
  max-height: 56px;
  max-width: 248px;

  position: relative;
  box-sizing: border-box;
}

.form-field:focus-within {
  --border: solid 2px black;
}

.notched-outline {
  display: flex;
  position: absolute;

  top: 0;
  right: 0;
  left: 0;
  width: 100%;
  max-width: 100%;
  height: 100%;

  box-sizing: border-box;
  pointer-events: none;
}

.notched-outline .leading {
  border-radius: var(--border-radius) 0 0 var(--border-radius);
  border-left: var(--border);
  border-top: var(--border);
  border-bottom: var(--border);
  width: var(--input-pading);
}

.notched-outline .notch {
  border-bottom: var(--border);
}
.notched-outline .notch .label {
  position: relative;
  transform: translateY(-50%);
  padding-left: 4px;
  padding-right: 4px;
}

.notched-outline .trailing {
  border-radius: 0 var(--border-radius) var(--border-radius) 0;
  border-right: var(--border);
  border-top: var(--border);
  border-bottom: var(--border);
  width: 100%;
}

.form-field-infix {
  width: 100%;
  padding-bottom: var(--input-pading);
  padding-top: var(--input-pading);
}

.form-field-infix input {
  width: 90%;
  outline: none;
  border: none;
  background-color: transparent;
}
<div class="form-field">
  <div class="notched-outline">
    <div class="leading"></div>
    <div class="notch">
      <div class="label">Label</div>
    </div>
    <div class="trailing"></div>
  </div>
  <div class="form-field-infix">
    <input placeholder="test" type="text"/>
  </div>
</div>

Css相关问答推荐

当滚动到页脚时,是否可以隐藏固定元素?

如何在移动屏幕尺寸上包装元素?

可折叠/可展开的数据表行:如何使Shadcn数据表中的可折叠内容全宽?

如何根据屏幕宽度覆盖SCSS变量?

Next.js中的TailWind CSS类名称疑难解答

CSS 内联 Flex 和段落换行

CSS Select 器仅 Select 具有特定类名称的每行的第一个单元格

同时实现渐变文本和闪光文本效果是否可行?

如何使用 Tailwind CSS 稳定 React 中的浮动标签?

为什么我的 CSS 转换在 React 18 中不起作用

CSS 仅过渡背景 colored颜色

Material UI Modal 因背景而变暗

如何让我的 CSS 代码响应小屏幕?

CSS 媒体查询不为桌面版的圣杯响应式布局包装 Flexbox 容器元素

左右卡片的 OwlCarousel 导航:react js

对齐视图中的复选框(RN)

包装器内两个跨度之一上的文本溢出省略号

CSS3 变换:旋转;在 IE9 中

BEM 块、命名和嵌套

我应该如何组织我的 CSS 文件的内容?