0

I have an SVG of baseball diamond, and I'm trying to set its width and height via the attributes of its parent container.

Here's my fiddle: https://jsfiddle.net/a5mz90jy/6/

Here's my Javascript:

const svgBaseballField = document.getElementById('diamond');
  console.log(svgBaseballField);
  const svgContainer = document.getElementsByClassName('svg_container');
  console.log(svgContainer);

  const svgContainerHeight = svgContainer.clientHeight;
  console.log(svgContainerHeight);
  const svgContainerWidth = svgContainer.clientWidth;

  svgBaseballField.setAttribute("width", svgContainerWidth);
  svgBaseballField.setAttribute("height", svgContainerHeight);

  svgBaseballField.setAttribute("viewBox", "0 0 100 100");

I know something is wrong, because svgContainerHeight and svgContainerWidth are undefined. I also know I need to set the viewport to something.

I also checked this before: How can I make an svg scale with its parent container?

5
  • basically, I want the svg to scale to the parent container. Commented Mar 30, 2018 at 21:36
  • width="100%" height="100%" Commented Mar 30, 2018 at 21:45
  • I am confused, it is not working for me. Commented Mar 30, 2018 at 21:51
  • You edited the SVG and changed its height and width to 100% and that doesn't produce the rendering you want? Commented Mar 31, 2018 at 6:26
  • editing the SVG directly worked, thanks! Commented Apr 2, 2018 at 1:42

1 Answer 1

1

The SVG you are linking to has a fixed width and height (650 x 500), so you are not going to be able to do it the way you are doing now (ie. via <object>.

You have two options:

  1. Copy the SVG locally and modify it. Add a viewBox attribute and change the width and height to "100%".

    <svg ... width="100%" height="100%" viewBox="0 0 650 500" ... >
    
  2. The other solution is to include the SVG as a background image and take advantage of the CSS background sizing properties.

    .svg_container {
      width: 75%;
      height: 100%;
      background: url(https://upload.wikimedia.org/wikipedia/commons/8/89/Baseball_diamond_clean.svg) blue;
      background-repeat: no-repeat;
      background-size: contain;
    }
    

    https://jsfiddle.net/a5mz90jy/12/

3
  • number one works better for me, thanks! And because I am using the object tag, I still have access to all the XML content, right? (if I wanted to change the fill color of a base in JavaScript, etc) ? Commented Apr 2, 2018 at 1:43
  • I'm having a lot of trouble selecting the elements in the SVG. I'm using D3 to select them, and I can't. do you think you can post an example in the fiddle? Should I be importing the SVG in D3.xml, or is the <object> tag sufficient enough to do it? Commented Apr 3, 2018 at 20:34
  • You can't access the SVG directly, you have to access it via the contentDocument property of the <object>. Are you doing that? See: stackoverflow.com/questions/11434916/… Commented Apr 4, 2018 at 2:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.