【发布时间】:2019-12-22 15:15:51
【问题描述】:
我的问题是,对于绝对定位的 SVG 元素,应该显示在 div 元素上方,Firefox 71.0 (64-bit) 中会显示一个间隙。
以下最小示例概述了问题:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Example</title>
<style>
html {
height: 100%;
}
body {
display: flex;
flex-direction: column;
margin: 0;
min-height: 100%;
}
body > div {
flex-grow: 1;
}
#special {
position: relative;
background-color: #000;
color: #FFF;
}
#special svg, #special div {
position: absolute;
width: 100%;
bottom: 100%;
}
#special div {
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div>
Box one
</div>
<div id="special">
<svg color="#6A4A3C" viewBox="0 0 100 2">
<polygon
points="0 0, 100 0, 100 2, 20 0, 0 2"
transform="scale(1 -1) translate(0 -2) scale(-1 1) translate(-100 0)"
></polygon>
</svg>
<!--<div>
Magic div
</div>-->
Box two
</div>
</body>
</html>
您可以在here 中运行测试此代码。当您增加或减少宽度时,间隙会发生变化:
我正在寻找一种解决方案,它不只是将 SVG 向下推一个像素,因为分隔线与另一个 div 接触的地方看起来很难看。另外增加一个部分的高度并使其重叠会带来困难,因为很难将这种重叠包含在网站的设计中。
由于这种重叠,很难将上方框的内容居中,所以看起来并不奇怪。
奇怪的是,Chrome 并没有这样做,但它在两侧会这样做。我怎样才能防止这个问题?
【问题讨论】:
-
comment1:你有 2 个
id="special"comment2:我没有看到差距,但请尝试给 svgdisplay:block或inline-block -
注意:
<meta>标签在 HTML 中不使用也不需要结束斜杠,而且从来没有。 -
@enxaneta 感谢您指出我在结束标签中带有 id 的错字。请看截图。
-
用 SVG 绘制整个东西(或者用 HTML 绘制)作为一个单一的形状。否则,抗锯齿总是会成为一个问题。