我有一个响应式 SVG 容器,我需要我<pattern>
的出现在每种情况下。
我是说:
<pattern>
三角形应以所有可能的宽度居中- 左边/右边的空间必须重复三角形
<pattern>
这可能吗?我知道可以使用 JS 动态重新计算模式位置。但我想svg
只使用属性来做到这一点。
.mySvg1 {
width: 600px;
height: 50px;
border: 1px dotted red;
margin-bottom: 16px;
}
.mySvg2 {
width: 400px;
height: 50px;
border: 1px dotted red;
margin-bottom: 16px;
}
.mySvg3 {
width: 200px;
height: 50px;
border: 1px dotted red;
margin-bottom: 16px;
}
span {
font-weight: bold;
color: red;
}
.myDiv1 {
margin-bottom: 16px;
}
<div class="myDiv1">
SEE THAT THE TRIANGLE IS <span>NOT</span> CENTERED. I WOULD LIKE IT TO ALWAYS BE CENTERED
</div>
<div>
<svg class="mySvg1">
<defs>
<pattern
id="wave"
width="150"
height="50"
patternUnits="userSpaceOnUse"
>
<path d="M 0 50 l 75 -50 l 75 50" stroke="black" stroke-width="2"/>
</pattern>
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="url(#wave)"/>
</svg>
</div>
<div>
<svg class="mySvg2">
<rect x="0" y="0" width="100%" height="100%" fill="url(#wave)"/>
</svg>
</div>
<div>
<svg class="mySvg3">
<rect x="0" y="0" width="100%" height="100%" fill="url(#wave)"/>
</svg>
</div>
额外的:
很容易做到这一点background-image
。我只想使用相同的方法<svg>
。这可能吗?
div {
margin-bottom: 16px;
}
.myDiv1 {
width: 600px;
height: 50px;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 -2 150 52"><path d="M 0 50 l 75 -50 l 75 50" stroke="black" /></svg>');
background-position: center;
}
.myDiv2 {
width: 400px;
height: 50px;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 -2 150 52"><path d="M 0 50 l 75 -50 l 75 50" stroke="black" /></svg>');
background-position: center;
}
.myDiv3 {
width: 200px;
height: 50px;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 -2 150 52"><path d="M 0 50 l 75 -50 l 75 50" stroke="black" /></svg>');
background-position: center;
}
<div>
SEE THAT THE TRIANGLE IS ALWAYS CENTERED
</div>
<div class="myDiv1">
</div>
<div class="myDiv2">
</div>
<div class="myDiv3">
</div>