How to implement an SVG?

2

Well, SVG is very important to the web since it's vector and I can resize it at will. It is an image format written in xml and is one of the image standards for web. But when I try to implement it on a web page, it just does not obey me! It sticks to the top left of the page. Can anyone tell me how do I manipulate it?

Sample code for an SVG image:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Creator: CorelDRAW X6 -->
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="210mm" 
        height="297mm" version="1.1" shape-rendering="geometricPrecision" 
        text-rendering="geometricPrecision" image-rendering="optimizeQuality" 
        fill-rule="evenodd" clip-rule="evenodd" viewBox="0 0 21000 29700"
        xmlns:xlink="http://www.w3.org/1999/xlink">
 <g id="Camada_x0020_1">
  <metadata id="CorelCorpID_0Corel-Layer"/>
  <circle fill="red" cx="10092" cy="13553" r="1508"/>
 </g>
</svg>

The above code can be seen in a browser, it's a simple circle. The problem is that I can not position this type of image the way I want it. How do I do this?

    
asked by anonymous 24.04.2014 / 14:01

2 answers

4

Actually the SVG you posted is not a simple circle . It is an object that contains a circle. It is important to distinguish this because if you are trying to position the circle , it will not work unless you understand the object it is contained in.

It's important to understand the attributes you're using to describe SVG. I'll remove those that are irrelevant to this question and keep only the others:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" 
     width="210mm" height="297mm" 
     viewBox="0 0 21000 29700"> ... </svg>

You are using the attributes height and width in mm , so this unit will be valid for the values you are using within SVG. viewBox is your coordinate system. You're associating 210mm with 21000, and 297mm with 29700. I'm going to split this by 100 to make it easier to work with:

<svg xmlns="http://www.w3.org/2000/svg" width="210mm"  height="297mm" version="1.1" viewBox="0 0 210 297">
   <g id="Camada_x0020_1">
      <circle fill="red" cx="100.92" cy="135.53" r="15.08"/>
   </g>
</svg>

What you have here is a 210x297mm screen with a circle more or less in the middle.

I made a JSFiddle where I changed mm to px so SVG displayed on the screen. The fiddle contains a block of HTML before, which pushes the SVG to a different position (see that it does not get stuck at the top of the page).

<h1>Texto em HTML</h1>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
     width="210px"  
     height="297px" 
     viewBox="0 0 210 297">
    <g>
        <circle fill="red" cx="100.92" cy="135.53" r="15.08"/>
    </g>
</svg>

You can position this SVG on the page as you like using CSS. I put a border around it to distinguish the SVG (invisible rectangle of 210x297px) from the circle (red, which is 15.08px radius):

svg {
    border: solid blue 1px;
}

If you want to use just the circle, you can set a viewBox that has exactly the dimensions of it:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
     viewBox="0 0 200 200">
    <circle fill="red" cx="100" cy="100" r="100"/>
</svg>

And do not use height and width , leaving the control to do in CSS or use the default size (which will be defined by the parent element):

<div id="circulo">
   <svg> ... </svg>
</div>

So you set the dimensions you want. viewBox is a relative coordinate system . It will fit the absolute coordinates (in cm , mm , px ) that you set through attributes, CSS or context. If you use a CSS containing:

#circulo {
    width: 150px;
    height: 150px;
}

The div will have dimensions 150 x 150px and will reduce the SVG proportionally. The default behavior is not to distort the image, so the smaller dimension will determine the size of the circle. Try varying the dimensions and see what happens.

See JSFiddle no. 2

You can now treat SVG like any other DOM component, change its placement with CSS, access internal components identified with ID via CSS or JavaScript, etc.

I used an SVG example built into the page, but everything works for SVG included via tag <img> (except the possibility of accessing internal elements from SVG via DOM). See JSFiddle no. 3 . Remember that if you use an external SVG in this way, you can not omit the namespace and version attributes (which HTML5 tolerates and defaults if you do not use).

    
28.04.2014 / 23:20
0

Look at this example I made in JSFiddle , the link has the reference of where I produced the image, just click preview HTML, and then on the console pick up the source code.

    
15.12.2015 / 22:20