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