这是我在 CSS 中创建的按钮的一个单独示例。它具有带渐变的 1px 边框和背景渐变。背景渐变被实现为伪元素,以允许其不透明度在悬停时褪色。
https://codepen.io/anon/pen/wbYoeo?editors=1100 https://codepen.io/anon/pen/wbYoeo?editors=1100
.Button
{
width: 200px;
height: 30px;
cursor: pointer;
padding: 0.8rem;
border-style: solid;
border-image: linear-gradient(
to right,
green 0%,
blue 100%);
border-image-slice: 1;
border-width: 1px;
position: relative;
margin-top: 10px;
transition: color 0.2s;
}
.Button::before
{
content: '';
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-image: linear-gradient(
to right,
green 0%,
blue 100%);
opacity: 0.5;
transition: opacity 0.2s;
}
该按钮在不同 DPI 的显示器之间呈现的效果并不相同。在 Windows 上的 Chrome 中以不同的 DPI 比例呈现的按钮的屏幕截图:
100% DPI 缩放显示器,正确渲染,无间隙。 https://i.stack.imgur.com/ooK61.png
150% DPI 缩放显示器,显示背景和边框之间的间隙。 https://i.stack.imgur.com/iZhCt.png
175% DPI 缩放显示器,显示背景和边框之间的间隙。 https://i.stack.imgur.com/Ny7zT.png
200% DPI 缩放显示器,正确渲染,无间隙。 https://i.stack.imgur.com/xFPnR.png
我尝试了几种渲染按钮的策略,但它们都会导致间隙:
- 尝试使用image https://i.stack.imgur.com/dKjDi.png用渐变代替
linear-gradient
在两个border-image
and background-image
.
- 尝试使用显式 div 作为背景渐变而不是伪元素。
- 尝试使用显式 div 作为背景渐变而不是伪元素,并且还使用实心左边框和右边框以及 ::before 和 ::after 伪元素,以及顶部和底部边框的线性渐变背景。