【发布时间】:2019-05-30 01:40:15
【问题描述】:
这是我在 CSS 中创建的按钮的一个孤立示例。它具有带渐变的 1px 边框和背景渐变。背景渐变被实现为伪元素,以允许在悬停时淡化其不透明度。
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 上以不同 DPI 比例在 Chrome 中呈现的按钮的屏幕截图:
100% DPI-scaled monitor, rendering correctly with no gap.
150% DPI-scaled monitor, showing a gap between the background and border.
175% DPI-scaled monitor, showing a gap between the background and border.
200% DPI-scaled monitor, rendering correctly with no gap.
我尝试了几种渲染按钮的策略,但它们都导致了差距:
- 尝试在
border-image和background-image上使用带有渐变的image 而不是linear-gradient。 - 尝试对背景渐变使用显式 div 而不是伪元素。
- 尝试使用显式 div 作为背景渐变而不是伪元素,并且还使用实心左右边框和 ::before 和 ::after 伪元素,上下边框具有线性渐变背景.
【问题讨论】:
标签: css google-chrome retina-display dpi