@RobertLongson's answerは、これがなぜ起こっているのかを説明しています。
circle
のr="29.3"
は実際にはマークアップで「高さ/幅を指定してください」ということはありません。そこに値を置くことができれば、私はあなたが実際にのsvgの幅または高さで同じ値を想像するでしょう。これを念頭に置いて別のアプローチをとることができます。 viewbox
は変更されませんが、0 0 100 100
はpolygon
のpoints
のパーセント値を使用するだけです。それらを計算するために、私はyourpointvalue/yourradiusを実行しました(たとえば、21.9/58.6)。円の要素の代わりにsvg
のボーダーを使用すると、軽量化され、読みやすくなります。 CSSで幅を指定しましたが、インラインでも可能です。あなたはまた、唯一の高さを指定、または親、などなどへの、それは相対的な可能性があり
(ブラウザのインスペクタでsvg
をチェックすると、あなたはそれが円と同じ幅と高さです表示されます)
#play-button {
fill: #010101;
border: 1px solid #000;
border-radius: 50%;
display: block;
/* specify a width or a height either here or inline */
width: 58.6px;
}
<svg id="play-button" viewbox="0 0 100 100">
<polygon points="37.3,26.8 79.5,50 37.3,73.2" />
</svg>
あなたが本当にviewbox
、width
、またはheight
を使用し、すべてのそのマークアップを維持する必要がない場合は、JavaScriptでこれを達成することができます。これはa solution by @Almis(但し、@PaulLeBeau's take on the issue参照)から適合しています。 circle
のストロークがsvg
の範囲を少し超えて伸びるため、overflow: visible
が必要です。 (最終的にはspecify where a stroke is drawnになるかもしれませんが、まだありません。)
var playButton = document.getElementById('play-button');
var boundingRect = document.getElementById('button-border').getBoundingClientRect();
playButton.style.height = boundingRect.height + 'px';
playButton.style.width = boundingRect.width + 'px';
#play-button {
border: 1px dashed gray;
overflow: visible; /* added */
}
<svg id="play-button">
<style type="text/css">
.st0 {
fill: none;
stroke: #010101;
stroke-miterlimit: 10;
}
.st1 {
fill: #010101;
}
</style>
<circle id="button-border" class="st0" cx="30" cy="29.9" r="29.3" />
<polygon id="play-triangle" class="st1" points="21.9,15.7 46.6,29.9 21.9,44.1 " />
</svg>
あなたにロバートをありがとうございます。このポストはしばらくするとかなり人気があると思う。 – Persijn