html - 边框上的 CSS 渐变

标签 html css background border gradient

在下面的代码中,渐变的宽度为 100%,并越过左侧的边框。

JSFiddle

input {
    background: transparent;
    font-family:'Open Sans';
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-sizing: border-box;
    padding: 5px;
    color: rgb(164, 164, 164);
}
input:focus {
    outline: none;
    border: 1px solid rgba(255, 255, 255, 0.5)
}
#demo {
    background : transparent linear-gradient(to right, rgba(109, 179, 242, 0) 0%, rgb(24, 189, 70) 100%, rgba(54, 144, 240, 0) 0%, rgba(58, 107, 182, 0) 100%);
}
body {
    background: hsla(0, 5%, 5%, 1) linear-gradient(to right top, rgba(10%, 0%, 0%, 1), rgba(0%, 0%, 0%, 1)) no-repeat fixed;
}
<input type="text" id="demo" />
<input type="text" />

为什么会发生这种情况,我该如何避免这种情况?

最佳答案

那是因为背景默认会延伸到边框边缘。您可以使用 background-clip 来避免这种情况。属性,通过设置 background-clip: padding-box;

工作示例:

input {
    background: transparent;
    font-family:'Open Sans';
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-sizing: border-box;
    padding: 5px;
    color: rgb(164, 164, 164);
}
input:focus {
    outline: none;
    border: 1px solid rgba(255, 255, 255, 0.5)
}
#demo {
    background : transparent linear-gradient(to right, rgba(109, 179, 242, 0) 0%, rgb(24, 189, 70) 100%, rgba(54, 144, 240, 0) 0%, rgba(58, 107, 182, 0) 100%);
    background-clip: padding-box;
}
body {
    background: hsla(0, 5%, 5%, 1) linear-gradient(to right top, rgba(10%, 0%, 0%, 1), rgba(0%, 0%, 0%, 1)) no-repeat fixed;
}
<input type="text" id="demo" />
<input type="text" />

关于html - 边框上的 CSS 渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443765/

相关文章:

javascript - 单击项目名称时打开/关闭子菜单

jquery - 为什么 jQuery 不返回 thead 元素的正确边框属性,有解决方法吗?

css - 如何定位选择框的选项

java - 为桌面手机(android-java)的背景设​​置图库中的图像

javascript - 管理动态生成的表的元素

html - 手机上的固定按钮闪烁

jquery - 我可以在 IE 8 中使用 CSS 更改 SELECT 元素的边框颜色/样式吗?

android - 图片背景根据屏幕大小不使用九 fragment

html - 如何通过 CSS 为按钮设置背景图片?

html - 如何按组布置列表项