question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scaling SVG with height/width/viewBox?

See original GitHub issue

I’m trying to scale my SVG, which has viewBox='0 0 24 23', down to 16 x 16. However, when I change the width/height properties, the icon does not scale down, it just gets clipped off outside of the 16x16 box.

Icon with viewBox='0 0 24 23' andwidth=24andheight=23`: image

Icon with viewBox='0 0 24 23' andwidth=16andheight=16`: image

Am I missing something here? Here is my code.

import React from 'react'
import Svg, {
  Path
} from 'react-native-svg'

const AlarmIcon = ({width, height, fill, stroke, viewBox, style}) => {
  return (
    <Svg width={width} height={height} viewBox={viewBox} style={style}>
      <Path stroke={stroke} fill={fill} strokeCap='round' strokeJoin='round' d='M20.5,16.5V11A8.5,8.5,0,0,0,15,3,3,3,0,0,0,9,3,8.49,8.49,0,0,0,3.5,11v5.5a3,3,0,0,1-3,3h23A3,3,0,0,1,20.5,16.5Z'/>
      <Path stroke={stroke} fill={fill} strokeCap='round' strokeJoin='round' d='M15,19.5a3,3,0,0,1-6,0'/>
    </Svg>
  )
}

AlarmIcon.defaultProps = {
  width: 24,
  height: 23,
  stroke: '#fff',
  fill: '#fff',
  viewBox: '0 0 24 23'
}

export default AlarmIcon

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:9

github_iconTop GitHub Comments

28reactions
hedgerhcommented, Jun 6, 2016

I managed to get the icon to scale properly by applying scale={width/24} to both of the path elements. Is there a better way to do this, though?

18reactions
EskelCzcommented, Jan 15, 2018

Also, the viewbox dimensions should be the original dimensions of the shape.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Scale SVG | CSS-Tricks
The SVG Scaling Toolbox. Other images scale because the browser knows the height, width, and aspect ratio of the image, and it adjusts ......
Read more >
Test Scaling Of SVG Images In Fluid Layouts - CodePen
Test Scaling Of SVG Images In Fluid Layouts. Resize your browser so that the images scale down. Browsers IE9+ (desktop & mobile) do...
Read more >
How can I make an svg scale with its parent container?
I first create a viewBox attribute on the SVG element and set the value to "0 0 [original svg width] [original svg height]"....
Read more >
Scaling Up (and Down) SVG - GitHub Pages
Core SVG scaling options; Using CSS media queries ... LIVE DEMO: No height, width, viewBox = no scaling anywhere; LIVE DEMO: No height, ......
Read more >
SVG (basic support) | Can I use... Support tables for ... - CanIUse
Refers to the SVG 1.1 spec. ... 3 IE9-11 & Edge don't properly scale SVG files. Adding height, width, viewBox, and CSS rules...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found