css - CSS边框与文本间隙而不改变背景
问题描述
您如何制作带有一些间隙的边框,以便可以输入任意文本(无需对背景颜色/图像进行硬编码)。也无需更改 HTML
到目前为止我的尝试
https://codepen.io/anon/pen/ZZVMBx#anon-login
$primary-color: blue;
$border-color: blue;
$base-font-size: 16px;
label {
position: relative;
span {
position: absolute;
display: block;
color: $border-color;
left: 10px;
right: 0;
top: -8px;
font-size: 14px;
}
input {
position: absolute;
border: 2px solid $border-color;
border-radius: 4px;
padding: 8px;
z-index: -1;
font-size: $base-font-size;
}
}
<label>
<span>label</span>
<input type="text" value="hello">
</label>
类似于边框 CSS HTML 中的文本,只是我注意到我不希望更改背景。接受的答案和提供的答案将背景设置为白色。
解决方案
只需为您的跨度添加填充和背景即可
background:white;
padding-left: 10px;
padding-right: 40px;
label {
position: relative;
}
label span {
position: absolute;
display: block;
color: blue;
left: 10px;
right: 0;
top: 8px;
font-size: 14px;
background: white;
padding-left: 10px;
padding-right: 40px;
}
label input {
position: absolute;
border: 2px solid blue;
border-radius: 4px;
top: 15px;
padding: 8px;
z-index: -1;
font-size: 16px;
}
<label>
<span>label</span>
<input type="text" value="hello">
</label>
推荐阅读
- r - 应用 distHaversine 并生成最小输出
- python - 为 pandas 单元格设置或添加值 // 优化
- azure - 有没有办法在 Azure DevOps Pipelines YAML 中参数化/动态设置变量组名称?
- go - 如何使用 http.HandleFunc 以“/”结尾或以“/”结尾的模式映射到相同的函数
- css - 无法定位引导程序?
- r - 如何根据R中的另一列获取列的平均值
- sql - 如何使用 count 函数计算一列中不同元素的实例
- ansible - Ansible 使用带有变量的评估表达式设置事实
- ios - 如何将二维数组变量分配给 Swift 中的大浮动矩阵?
- node.js - 为什么 node.js 在调试过程中会给出“Invalid Debug Argument Error”?