Integrating SVG into HTML5
The process of creating SVG within HTML is very simple. In this lesson we will learn about the process, see the relationship between styling and rules and position our SVG exactly where we want it to be.
Keynotes:
- The rules of integration.
- No need to declare.
- Styling and SVG.
Video Transcript
00:00SVG is natively supported in the world of HTML5.
00:04With that said, the process of integrating SVG into
00:08HTML5 is a little bit different than creating a file standalone.
00:13In this lecture, we'll learn about those rules of integration and with it,
00:17declare that we don't need to declare a version.
00:20The reason is because our HTML will all render it if it supports it or not if it doesn't.
00:25It's not going to actually care if your SVG is version one or zero, I don't know what.
00:31Just like with HTML,
00:33with the new interpretation,
00:34the new concepts of HTML5 is don't declare in advance,
00:37the browser will be in charge of making sure that it
00:40continues to support older versions theoretically speaking,
00:43at least we'll see if that really happens in 20 years from now.
00:46With that said, we're going to also talk about styling in SVG.
00:49We're going to talk about a lot of the integration points between
00:53SVG, HTML5, and CSS.
00:56Let's jump right into it and let's start integrating an SVG image into HTML5.
01:02Now that you know how to create SVG elements,
01:05it's time for us to learn how to integrate the creation inside of our HTML.
01:11Let's go ahead and we're going to learn about creating more SVG stuff,
01:14but just go ahead into your HTML and find your 3D object around line 23,
01:20and just break the line inside of the front because
01:22inside of the front we're going to go ahead and create a new SVG element.
01:27Now, contrary to the process of creating
01:30an SVG element outside of HTML where we have to declare that it's SVG,
01:35and we have to declare a version,
01:37and we have to declare, well,
01:39that's all we have to declare.
01:40In HTML5, we don't need to declare anything because the tag SVG itself is
01:46then automatically recognized inside of HTML as SVG.
01:51That's it. This is the definition of SVG.
01:53We just created an SVG element.
01:55Now, before we continue onwards,
01:58we were talking previously about the idea that SVG on its own
02:02has the capability of using CSS.
02:06We saw that in the previous lesson.
02:08As we're integrating SVG inside of an element,
02:12it actually is going to basically inherit some properties.
02:15I want to show you how that happens.
02:17If I'm going to go ahead here and set a width of,
02:20let's say, 100, and I'm going to set a height of 100 as well.
02:26Let me go ahead O2 and go right into my CSS,
02:29into my styling, and I'm going to go ahead and just go to
02:32my article canvas wrapper and define an SVG rule here.
02:37I'm literally just going to go ahead and just set my background to be red,
02:40just to override its actual color.
02:43Now, remember, our SVG now should have
02:45a red background because we're using CSS.
02:47This is pure HTML, CSS relationships,
02:50and the SVG element itself has been defined to be 100 by 100.
02:55But notice what happens when I go into my browser and I click here on refresh.
02:59I'm getting an SVG element that is way larger than 100 by 100,
03:04and it's also positioned in the absolute wrong place.
03:06The reason this is happening is because the width and height is
03:12automatically inherited from our CSS.
03:16In that CSS is already defined because we have here basically a rule up here,
03:20if we find it that we've created earlier,
03:22that define that the width and height of each element would be 100 percent and 100 percent.
03:27In other words, our CSS items basically they come
03:33before whatever things we put inside of our SVG.
03:37In general, I recommend don't define a width and height in your SVG inside of HTML,
03:41but instead of that, go to your CSS and define it there.
03:44Now, beyond that, we also want to position our items,
03:47so I'm going to also make sure that I'm going to position it in the right spot.
03:51I'm going to go ahead and define here.
03:53I want to make sure that this element has a relative positioning to the item,
03:58the object that it's within,
03:59so I'm going to go ahead here and define that.
04:01Instead of it having a position that is absolute,
04:04I'm going to set a position that is relative,
04:07and I'm going to go and say,
04:08by the way, I want it to be to the left to be at zero pixels.
04:12If I go ahead here and click on save and click on refresh,
04:15my SVG should be now sitting exactly where I want it to be.
04:19Now that I know that it's taking all the bound that I want it to be in,
04:23I could go ahead here and now also maybe get rid of
04:26that red coloring because I want it to be invisible.
04:29I want to only see through whatever we create.
04:32That's it. We've just created and integrated an SVG file.
04:36It is now invisible as we want it to be,
04:39but the next step will be to actually add something into our SVG,
04:42which we'll do in the next lesson and learn about more SVG things that we can do.
04:47In this lecture, we integrated SVG into HTML5.
04:51In the next lecture, we're going to go ahead and continue drawing an SVG
04:55when we'll create a polygon.
