html - 尝试重新创建 Chrome 时出现奇怪的轮廓位置大纲
问题描述
我正在制作一个包含 的自定义项,我的目标是从 中删除自然轮廓/边框并将其放置在包含对象上,这样在聚焦时它仍然看起来很自然,但是,我似乎没有得到自然轮廓外观,而是看起来轮廓被放置在 div 的外部......
自然输入元素
我的自定义 div 包含输入
有没有什么办法可以实现自然的轮廓外观?
我已经outline: 5px auto -webkit-focus-ring-color;
用于<div>
.
.container {
display: flex;
flex-direction: column;
width: 300px;
}
input, .two {
font-size: 20px;
border: 2px solid grey;
border-radius: 10px;
}
.one {
margin-bottom: 12px;
}
.two {
height: 25px;
}
.two:focus-within {
outline: 5px auto -webkit-focus-ring-color;
}
.three {
border: 0;
outline: 0;
width: 100%;
background: 0;
}
<div class="container">
<input class="one" />
<div class="two">
<input class="three"/>
</div>
</div>
解决方案
您可以尝试给它一个否定的outline-offset
,例如outline-offset: -1px;
它不是像素完美的,但它看起来更像是 Chrome 上的内置样式大纲:
但请记住,它可能因不同的设备而异。对我来说-1px
看起来最好。
.container {
display: flex;
flex-direction: column;
width: 300px;
}
input, .two {
font-size: 20px;
border: 2px solid grey;
border-radius: 10px;
}
.one {
margin-bottom: 12px;
}
.two {
height: 25px;
}
.two:focus-within {
outline: 5px auto -webkit-focus-ring-color;
outline-offset: -2px;
}
.three {
border: 0;
outline: 0;
width: 100%;
background: 0;
}
<div class="container">
<input class="one" />
<div class="two">
<input class="three"/>
</div>
</div>
正确的解决方案可能是使用box-sizing: border-box;
,边框宽度也计入宽度和高度。所以你不需要做任何事情来使它们看起来相同。
但它似乎不适用于 Safari。
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
width: 300px;
}
input, .two {
font-size: 20px;
border: 2px solid grey;
border-radius: 10px;
}
.one {
margin-bottom: 12px;
}
.two {
height: 30px;
}
.two:focus-within {
outline: 5px auto -webkit-focus-ring-color;
}
.three {
border: 0;
outline: 0;
width: 100%;
background: 0;
}
<div class="container">
<input class="one" />
<div class="two">
<input class="three"/>
</div>
</div>
推荐阅读
- javascript - 从jquery中的div中删除特定单词
- c++ - Why does putting const on the left side of an ampersand valid when referring to a pointer but not on the right?
- flutter - How can i listen to a stream and then pass it to Streambuilder?
- javascript - Assigning uploaded files from google form in appropriate google drive folder
- node.js - Properly handling Google OAuth with separated frontend and backend
- javascript - 为什么 Javascript 不能与 Django HTML 一起使用?
- python - docker中的容器路径是什么?
- c# - 如何获取所有超过 N 分钟的文件?
- python - “图像尺寸太大”错误在 python 中保存颤动图并剪裁
- python - 返回日期之间的行仅间歇性地工作