LoginSignup
34
29

More than 5 years have passed since last update.

インラインSVGをレスポンシブ対応にする方法

Last updated at Posted at 2015-10-30
  1. svgタグのwidth,height属性を削る
  2. viewBox属性に"0 0 [width] [height]"をセットする。

レスポンシブ対応前のコード

<svg width="640" height="400">
 <g>
  <title>background</title>
  <rect fill="#fff" id="canvas_background" height="402" width="642" y="-1" x="-1"/>
  <g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid">
   <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/>
  </g>
 </g>
 <g>
  <title>Layer 1</title>
  <rect id="svg_1" height="208" width="469" y="101.5" x="93.5" stroke-width="1.5" stroke="#000" fill="#fff"/>
 </g>
</svg>

レスポンシブ対応後のコード

<svg viewBox="0 0 640 400">
 <g>
  <title>background</title>
  <rect fill="#fff" id="canvas_background" height="402" width="642" y="-1" x="-1"/>
  <g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid">
   <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/>
  </g>
 </g>
 <g>
  <title>Layer 1</title>
  <rect id="svg_1" height="208" width="469" y="101.5" x="93.5" stroke-width="1.5" stroke="#000" fill="#fff"/>
 </g>
</svg>

サンプル

※QiitaはSVGタグ使えないのね。残念。。

34
29
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
29