html - 如何修改 div contenteditable 以使用 css 在焦点上显示从中心到边框的底线

标签 html css

我想在 this page 的文本字段上实现效果使用 css,但我需要将它应用到一个 contenteditable div 上。

这是我到目前为止所做的:

<style>
.container {
      height: 35px;
      width: 100%;
      border: solid 1px rgba(0, 0, 0, .2);
      margin-bottom: 5px;
      overflow-x: auto;
      padding-bottom: 10px;
      transition-duration: 0.2s;
      transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    .container:focus {
      border-bottom: solid 2px black;
    }
</style>

这是 HTML:

<div id="editor" contenteditable="true" class="container" />

它在聚焦时将底线从无变为黑色,在失去聚焦时它从黑色变为淡色。我想要的是显示完全相同的线,但是从 div 的中心到焦点的边界和焦点丢失我希望它从边界到中心,如链接中所示。

有人可以帮忙吗? 提前致谢。

最佳答案

你不能只用一个边框来实现这一点。为此使用 :before:after。确保你给你的元素一个 position: relative; 这样你就可以给你的 :before:after 一个 position: absolute;

.container {
  position: relative;
  height: 35px;
  width: 100%;
  border: 0;
  margin-bottom: 5px;
  overflow-x: auto;
  padding-bottom: 10px;
}

.container:before,
.container:after {
  position: absolute;
  bottom: 0;
  content: '';
}

.container:before {
  left: 0;
  height: 1px;
  width: 100%;
  background: #e0e0e0;
  border-bottom: solid 1px rgba(0, 0, 0, .2);
  z-index: 1;
}

.container:after {
  position: absolute;
  left: 50%;
  height: 3px;
  width: 0;
  background: blue;
  border: 0;
  transition-duration: 0.2s;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); 
  transform: translateX(-50%);
  z-index: 2;
}

.container:focus {
  outline: 0;
}

.container:focus:after {
  width: 100%;
}

示例 https://jsfiddle.net/skeurentjes/3brebpug/

关于html - 如何修改 div contenteditable 以使用 css 在焦点上显示从中心到边框的底线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44070450/

相关文章:

html - CSS:垂直居中水平菜单

html - 如果文本溢出 Div,则显示内容

html - 这个 CSS3 发生了什么?我找不到它

html - 响应式 CSS : Organizing images issue

html - 如何在一行中显示三个文本字段

打印时 CSS 元素在 Chrome 中不起作用

html - 使用 Bootstrap 在移动 View 中将单个表分成 2 个表

html - 是否可以保存另一个网站的修改后的 css/html?

html - 具有 flex 的流体粘性页脚

JQuery 按钮 - 删除文本时保持高度