2

I have some responsive inline SVGs I made. Their sizes render as I want them to in Chrome, Firefox and Edge, but in Safari they escape their containers/are cut off. For some reason their containers doesn't stretch to accommodate them. The following screenshot demonstrates the unwanted, cut-off Safari rendering:

SVGS don't fit

There are known issues with SVG rendering in Safari, and I have tried all the fixes out there I have found to the best of my ability (here, here, here, and here), but I can't manage to make the containers fit the SVGs in Safari. It is in part because things are a little complicated with my javascript and I'm still a beginner, forgive me if my codepen is a bit messy.

This is my codepen. :http://codepen.io/ihatecoding/pen/Bzgqqa

What my jquery does: it makes SVGs as large as possible until they take up 1/3 of the screen height, at that point it won't let them get taller.

To help you focus on what matters :

  • The SVGs are all of class .areaSVG
  • The SVG parents/containers are always .ey-col-svg
  • The entire footer is #indexFooter
  • The main background image that is supposed to resize according to the height of the landing footer is #section0img

The current version that is working the best has the following format for the svg container:

.ey-col-svg {
    display: block;
    max-height: calc(30vh - 1vw - 63px);
    text-align: center;
    box-sizing: content-box;
    padding: 0;
    margin: -2vh 0 0 0;
}

This is the css for the SVGs before the javascript makes them visible and adjusts their height:

.areaSVG {
    overflow: visible;
    display: none;
    box-sizing: content-box;
    margin: 0;
    padding: 1.6vh 0 1vh 0;
}

Again, to repeat The javascript I have currently implemented adjusts the height of the SVGs (whose class is areaSVG).

This is the most relevant part of my jQuery script; it controls the size of the the SVGs. As I mentioned above, this script makes the SVGs as large as possible until they take up 1/3 of the screen height, and at that point it won't let them get taller:

  function resizeSVG() {

// the row
 var $linksRow = $('.ey-nav-bar');

// the text
   var $areaText = $('.ey-text-content');

//the entire row below "research Area"



         // the actual svg container

      var $area = $('.areaSVG');

      var scale = 0.6;

         //the wrapper containing the svg div, its height and its width
      var $eyCol = $(".ey-col-svg");
      var eyWidth = $eyCol.width();
      var eyHeight = $eyCol.height();

              //the window
      var winHeight = $(window).height();
      var winWidth = $(window).width();

      //max Height caclulated based on window
      var maxHeight = .33 * winHeight;


        // if the height of the column is less than the width, and below the max height

      if (eyHeight < eyWidth && eyHeight < maxHeight)

                  //make the height of the svg the max heihgt
        $area.height(maxHeight);

          // use the scaling factor times the width of the svg wrapper

      var imageWidth = scale * $eyCol.width();
                // get the hight of the column       

      var imageHeight = $eyCol.height();


          // will be the dimensions used to size lenth and width of the svg

      var tot;

          //apsect ratio of the screen (horizontal/vertical)

      var ratio = winWidth / winHeight;
          // if the screen is landscape or if the user has left the landing section


        tot = imageWidth > imageHeight ? imageHeight: imageWidth;

maxTextHeight = maxHeight * .07;
maxTotHeight = maxHeight * .5;

if (tot < maxTotHeight)

{        $area.css("height", tot);

}
        else
        {


  $area.css("height", maxTotHeight);
  $areaText.css("height", maxTextHeight);

        }

minLinksHeight = maxHeight * .8;

var linksHeight = $linksRow.height();


    }

(Note: the resulting SVG heights are subsequently used by another function, not seen here, to control the size of the main image).

This is my introductory formatting code for each inline svg:

    <svg class="areaSVG notFixed index" viewBox="20 0 37 75" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

Any help would be very appreciated, I really would like to see this to render properly in Safari!

Community
  • 1
  • 1
CoderScissorhands
  • 485
  • 1
  • 6
  • 26
  • 1
    One thing that is immediately noticeable is that your SVG `viewBox` attributes are a lot smaller than you actual SVG contents. And you are relying on setting `overflow: visible` to get the rest of the SVGs to render. I would be suspicious of that myself. I would start by fixing your viewBoxes and see if things improve. – Paul LeBeau Aug 26 '16 at 05:45
  • Thanks your help, but what do you mean fixing the viewboxes. that is just a different way to write 17 by 75, why would that cause issues? Can you maybe fork my codepen and try out your hypothesis? :). One thing I'll mention is that explicit width and height in the inline svg declaration seems to help/change things but I need to play with it more, it also seems to cause new problems. – CoderScissorhands Aug 26 '16 at 16:57
  • Per your suggestion, in my answer, you will see I removed the `overflow: visible` setting. – CoderScissorhands Aug 29 '16 at 16:49

2 Answers2

3

I figured out a combinations of CSS settings that now make the SVGs Render in entirety in Safari (as well as in Chrome, Firefox, and Edge); Safari no longer clips them/cuts them off. My sizing and calculations are not perfect, but the display settings work, you will need to tweak the size according to your own needs adjusting the height of the SVG container/parent. My javascript (which controls other aspects of the page) are wonky, but the SVG settings are more or less correct. I hope this helps someone.

Here is the codepen: http://codepen.io/ihatecoding/pen/zBgqgp

The inline html declaration of the SVG

I adjusted my viewBox and removed my overflow: visible setting according to the suggestions of @Paul Lebeau Note that the preserveAspectRatio is intentionally not specified because it should remain with the default setting, xMidYMid meet (unlike other Safari SVG fixes which change it to none).

Here is the code:

 <svg class="areaSVG notFixed index" viewBox="0 0 80 80"  xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

The CSS

If you are not using my javascript to resize the svgs, use the following css settings for the SVG container (.ey-col-svg) and the SVG itself (.areaSVG):

/* the SVG parent/container */

.ey-col.svg {
    display: block;
    height: auto;
    text-align: center;
    box-sizing: border-box;
    padding: 0;
}


/* the SVG itself */

.areaSVG {
    display: inline-block;
    max-height: 15vh; 
    box-sizing: content-box;
    margin: 0;
}

Notes about my javascript

If you are using my messy javascript, please note both the container and the SVG will both initially have the setting display: none in the CSS, and then the javascript will change both of them to have the same displays settings I have shown above [with the container (.ey-col-svg) set to display: block and the SVG (.areaSVG) set to display: inline-block].

Also, my javascript in my codepen has changed. It now works better if I adjust the height of the container (.ey-col-svg) instead of the of SVG (.areaSVG) itself. Another option that might work better for users is changing the max-height of the container, instead of the height (as I have done).

CoderScissorhands
  • 485
  • 1
  • 6
  • 26
  • `xMidYMid meet` is the default value for `preserveAspectRatio` anyway. – Paul LeBeau Aug 29 '16 at 23:21
  • Do you think I should remove that part from my answer then? Some Safari svg fixes recommend having the `preserveAspectRatio` set to `none`, and my inclusion here clarifies that it should be set to 'XMidYMid meet'. If you think the answer is better without it, I'll remove that bit. – CoderScissorhands Aug 30 '16 at 00:18
  • (I removed the `'preserveAspectRatio' , but mentioned that it needs to remain with the default setting.) – CoderScissorhands Aug 30 '16 at 00:38
1

The viewBox attributes in your SVGs are wrong. I am not sure whether it is the cause of your issues, but you should fix them anyway.

The viewBox is supposed to describe the extent of the elements in your SVG. All of the SVG contents are supposed to fall within the extent of the viewBox. But yours doesn't. At 37x75 it is a lot narrower than the contents.

In the following example I have added a <rect> element the same size as your viewBox so you can see how it compares.

/*this is the container for the  bottom svg */

.areaSVG {
    /* this is the height setting I would like to be a percentage */
    /*height: 30%; <------ there*/
    overflow: visible;
    display: none;
    /* margin: 0 26% 0 26%;*/
    box-sizing: content-box;
    margin: 0;
    padding: 1.6vh 0 1vh 0;
    /* margin:2vh 0;*/
    /*  border: 1px solid Yellow;*/
}

#circle-background {
    /*    filter: "progid:DXImageTransform.Microsoft.Dropshadow(OffX=12, OffY=12,
 Color='#444')";
 filter: url(#f1);*/
    -moz-filter: box-shadow(3px 3px 2px rgba(0, 0, 0, 0.5));
    -webkit-filter: box-shadow(3px 3px 2px rgba(0, 0, 0, 0.5));
    filter: box-shadow(3px 3px 2px rgb(0, 0, 0, 0.5));
    fill: Gainsboro;
}


.fillDark {
    fill: #939598;
    /*DimGray*/
}

.fillWhite {
    fill: White;
}

.strokeDark {
    stroke: #939598;
    /*DimGray*/
}

.strokeWhite {
    stroke: White;
}
<svg class="areaSVG fixed" viewBox="20 0 37 75" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="height: 50px; display: inline-block;">
       <defs>
    <filter id="f1" x="0" y="0" width="200%" height="200%">
      <feOffset result="offOut" in="SourceAlpha" dx="3" dy="3"></feOffset>         
      <feGaussianBlur result="blurOut" in="offOut" stdDeviation="2"></feGaussianBlur>
              <feBlend in="SourceGraphic" in2="blurOut" mode="normal"></feBlend>
    </filter>
  </defs><path id="circle-background" opacity="0.4196" fill="#FFFFFF" enable-background="new    " d="
         M4.193,37.492c0-18.987,15.419-34.38,34.44-34.38c19.021,0,34.439,15.393,34.439,34.38c0,18.987-15.418,34.381-34.439,34.381
         C19.613,71.873,4.193,56.48,4.193,37.492L4.193,37.492z" filter="url(#f1)"></path>
 <path id="sclera" class="fillWhite" fill-rule="evenodd" clip-rule="evenodd" stroke-width="0.25" stroke-miterlimit="8" d="
         M11.41,38.895c27.619-31.029,41.313-9.542,49.646-2.012c-4.306,6.07-12.69,27.49-46.392,9.919c0,0-5.375-3.548-5.641-4.75
         C12.787,37.379,11.41,38.895,11.41,38.895z"></path>
 <ellipse id="iris" class="fillDark" fill-rule="evenodd" clip-rule="evenodd" cx="38.196" cy="36.63" rx="16.202" ry="15.686"></ellipse>
 <ellipse id="pupil" class="fillWhite" fill-rule="evenodd" clip-rule="evenodd" cx="38.529" cy="36.954" rx="5.628" ry="5.449"></ellipse>
 <path id="eyelid" class="fillDark" fill-rule="evenodd" clip-rule="evenodd" stroke-width="0.25" stroke-miterlimit="8" d="
         M56.955,26.227c5.438,2.787,12.803,9.595,12.803,9.595s-2.338,3.235-5.677,2.588c-4.027,3.396-13.345,29.705-49.417,8.393
         c33.702,17.571,42.086-3.849,46.392-9.919c-8.333-7.53-22.026-29.018-49.646,2.012c0,0-2.94,1.806-4.112-1.456
         c-1.172-3.261,2.481-0.477,4.009-2.911c1.527-2.434,3.674-3.557,7.682-6.792c-4.008,0.646-7.348,3.558-7.348,3.558
         c10.521-10.835,31.379-17.498,53.107-4.205C64.748,27.089,59.404,26.119,56.955,26.227z"></path>
         
<rect x="20" y="0" width="37" height="75" fill="none" stroke="red"/>
</svg>

A better viewBox that covers the eye design and its drop shadow would be:

viewBox="0 0 80 80"

If you update all the SVGs you get this:

http://codepen.io/anon/pen/KrOVoJ"

I can't tell if it now works in Safari, because I don't have a Mac.

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • Thank you, that information helped, but they still wouldn't render in Safari. I was able to get it to work if I changed a few other things. You can see my answer to find out what worked for me. – CoderScissorhands Aug 29 '16 at 16:39