前提
SVGスプライトを用います。
実現したいこと
親要素のサイズに応じて、SVGのサイズが自動で変わるような機能を実現したいです。
SVGスプライトの問題点
SVGスプライトバージョンにおいて、サイズが自動で変わってくれないのが問題です。
該当のソースコード
比較サンプル → https://jsfiddle.net/zn7ctop9/
html
1<!-- 問題点の説明 --> 2<p>上(べた打ちバージョン)<br> 3親要素(.left) の width に応じて、<span class="red">SVGのheight</span>が自動で変わってくれる</p> 4<p>下(SVGスプライトバージョン)<br> 5<span class="red">SVGのheight</span>が自動で変わってくれない</p> 6 7<!-- ベタ打ちバージョン --> 8<div class="flex"> 9 <div class="left"> 10 <svg class="icon icon-plus" viewBox="0 0 24 24"> 11 <path d="M5 13h6v6c0 0.552 0.448 1 1 1s1-0.448 1-1v-6h6c0.552 0 1-0.448 1-1s-0.448-1-1-1h-6v-6c0-0.552-0.448-1-1-1s-1 0.448-1 1v6h-6c-0.552 0-1 0.448-1 1s0.448 1 1 1z"/> 12 </svg> 13 </div> 14 <div class="right"> 15 <p>.right</p> 16 </div> 17</div> 18 19<!-- SVGスプライトバージョン --> 20<svg aria-hidden="true" style="position: fixed; width: 0; height: 0; overflow: hidden;" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 21 <defs> 22 <symbol id="icon-plus" viewBox="0 0 24 24"> 23 <path d="M5 13h6v6c0 0.552 0.448 1 1 1s1-0.448 1-1v-6h6c0.552 0 1-0.448 1-1s-0.448-1-1-1h-6v-6c0-0.552-0.448-1-1-1s-1 0.448-1 1v6h-6c-0.552 0-1 0.448-1 1s0.448 1 1 1z"/> 24 </symbol> 25 </defs> 26</svg> 27 28<div class="flex"> 29 <div class="left"> 30 <svg class="icon icon-plus"><use xlink:href="#icon-plus"></use></svg> 31 </div> 32 <div class="right"> 33 <p>.right</p> 34 </div> 35</div> 36
css
1.flex { 2 display: flex; 3 align-items: center; 4 background: #d1d1d1; 5 margin: 10px; 6} 7 8.left { 9 width : 50px; 10 border: 1px solid red; 11} 12 13.icon { 14 width : 100%; 15} 16 17.red { 18 color : red; 19}
試したこと
SVGスプライトバージョンのstyle
属性を次のように auto にしてみましたが、変化なしでした。
html
1style="position: fixed; width: 0; height: 0; overflow: hidden;"
↓ auto にしてみました
html
1style="position: fixed; width: auto; height: auto; overflow: hidden;"

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/18 05:17
2022/03/18 05:18