css - 径向渐变中心与跨度中心不匹配

标签 css

我将径向渐变中心距顶部 200 像素,距左侧 200 像素。我对跨度女巫所做的同样包含字母“a”。但据我所知,gradient center doesn't mach the span center. 为什么会发生这种情况?

<body>
    <div class="box"><span>a</span></div>
</body>

CSS

.box {
position:relative;
width: 300px;
height: 300px;
background: -webkit-gradient(radial, 200 200, 20, 200 200, 30,  from(#FCFCFC), to(#CF0C13));
}

span {
position: absolute;
left: 200px;
top: 200px;
}

enter image description here

最佳答案

你说得对。

问题:

您看到的问题是因为 span 开始X = 200px Y = 200 像素。而径向渐变则将其中心设置在该点。这是因为默认字体字形为上升部分和下降部分留下了空间。这将针对您拥有的每个字体系列而改变。

这在此片段中可见,请参阅标记为蓝色的范围:

.box {
    position: relative;
    width: 300px; height: 300px;
    background: -webkit-radial-gradient(200px 200px, circle, #fcfcfc 10%, #cf0c13 15%, #cf0c13 100%);
}
    
span {
    position: absolute; 
    left: 200px; top: 200px;
    border: 1px solid blue;
}
 <div class="box"><span>a</span></div>

解决方案:

只需将径向原点在 Y 轴上向下移动 10px 即可。

像这样:-webkit-radial-gradient(200px 210px....

片段:

.box {
    position: relative;
    width: 300px; height: 300px;
    background: -webkit-radial-gradient(200px 210px, circle, #fcfcfc 10%, #cf0c13 15%, #cf0c13 100%);
}
    
span {
    position: absolute; 
    left: 200px; top: 200px;
}
 <div class="box"><span>a</span></div>

或者,如果您的 span 内容要发生变化,那么最好使用 translate(-50%, -50%) 技巧将其反向移动其大小的一半。

关于css - 径向渐变中心与跨度中心不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753534/

相关文章:

html - 关于焦点问题的自定义 html 输入框

html - 如何将此 HTML 表格布局解决方案转换为 float div 解决方案?

html - scriptaculous 下拉菜单在 IE 中不起作用

css - 额外的 Twitter Bootstrap 标签/按钮/警告/徽章颜色

css - 如何删除 Firefox 在按钮和链接上的虚线轮廓?

javascript - HTML 表格标题中的垂直(旋转)文本

ios - z-index 不适用于 iPad

asp.net - 在列表中设置 asp 文本框的样式?

html - ul 列表不在屏幕中心

html - <li> 中哪些子元素有效?