02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Defining Margins of HTML elements

In the last lesson we learned how to define the size of HTML elements using CSS. In this lesson we will discover how to control the relation between elements. We do that using margins.

Video Transcript

00:00In the last lecture we control the width
00:02and height of our element.
00:04But our element relates to other things on the screen
00:06or to itself even.
00:08And I wanna talk about two major properties,
00:11the margin and the padding.
00:14We're gonna start with margin and see what that is.
00:16How do we define a margin and how it works?
00:20And in a few lectures we're gonna go to padding
00:21which is very similar, although completely different
00:24and confuses a lot of developers
00:26and that's why I put them one next to each other.
00:28So let's jump right into it
00:29and see what margins are and how to work with them.
00:33After in the last video we saw how to control
00:36the width and height of elements.
00:38I wanna move now into the relationship between elements.
00:43Now there is a spacing between our elements,
00:45between a lot of things or there is no spacing.
00:48How do we control that spacing which in CSS by the way
00:52it is called a margin.
00:54Now as you guessed it, if the name is margin
00:56then that's how we control it.
00:59For example for paragraphs we could go ahead
01:01to the paragraph and define a margin.
01:04Now I'm not gonna do a margin, a specific margin
01:07to an item like in margin top.
01:08Let's start with a global margin.
01:11I could set a global margin such as 10 pixels
01:14basically saying put 10 pixels around each paragraph.
01:18Now if I go ahead and I click on refresh here
01:21we'll see that the layout is going to change a little bit.
01:24Now what I wanna do next is I also wanna go ahead
01:27and add a background to our paragraph
01:31and I'm gonna go ahead and set an RGBA
01:34because I don't want it to be completely solid
01:38because I wanna see through it a little bit.
01:39And I'm gonna go ahead here and just set it to be
01:41with a opacity of 50% and I'm just gonna put here
01:44a random grayish type of color.
01:47I believe it will be a grayish type of color
01:49or a color combination that is a dark gray or not.
01:55Let me just save it.
01:57Give it another try.
02:00Background RGBA.
02:02Let me try and just set it directly.
02:04Oh, because I didn't put here a comma.
02:06A semicolon, big mistake because when I didn't
02:08put a semicolon it treated all this
02:10as if it was one thing.
02:12So I'm gonna go ahead here and just save that.
02:14Go back and refresh and we should have
02:16our background, beautiful.
02:18So now we can see the spacing that we have
02:20inside of our paragraph itself
02:22and notice this 10 pixel gap between the elements.
02:26And if I go ahead here and inspect the element
02:29and I find that specific paragraph
02:31and I go into that margin and change the margin
02:35we'll see that I could bring it all the way down
02:37to zero where they'll be combined.
02:39Notice how my items are flushed together
02:43in the external box.
02:44So there is more padding here that is not ours.
02:47There is more margin in here that is not our margin
02:50but is an external to us.
02:52So our items are completely combined
02:54or I could set them to 10 pixels or more
02:56and the more that I spaced them
02:57the more space in all directions
02:59they will have from each other.
03:02Now the next thing that I wanna do
03:03is I wanna as you probably guessed
03:06we're not gonna go into all the different
03:08properties of margin left, margin top, margin bottom.
03:12Those are all quite intuitive
03:14because we already spoke volumes about those things.
03:18So I'm just gonna go ahead here
03:19and just put them right here
03:20because they should be very intuitive
03:22where you can control only one of the properties
03:25instead of controlling all of the properties.
03:28But what I wanna talk about is
03:30with our margin property I could go ahead
03:32and add here two parameters.
03:35If I put in two parameters I could set here
03:37for example zero PX in the second
03:40what would actually happen the first item
03:42is the top and bottom and the second item
03:45would be the left and right.
03:47So in this case what would happen
03:49is my content would get indented
03:51or the content would have a space
03:53but it would not be indented.
03:54So it's actually vice versa.
03:56So this is what controlled our left and right
04:00while this is what controlled,
04:01well actually this is what controlled
04:03our spacing from top to bottom
04:05while this is what controlled our spacing
04:08of our left and right.
04:09So if I go ahead and save that just one more time
04:11we'll see that this time around
04:12our item will be pushed aside
04:14and be completely connected together.
04:17Another controller that we could set
04:19is if I add another property here
04:22literally the third property
04:23would then control separate
04:25because if this first item was our
04:29left and right or our top and bottom actually
04:32then this would add here for our bottom.
04:36So we have your top,
04:39left and right, bottom.
04:40Kind of confusing but you get used to it.
04:43So that's if you add three properties
04:45and if I go ahead and add also the fourth property
04:47then I could literally control also the last property
04:50which is the bottom or actually is the left in this case
04:55and I could set it to be zero pixels
04:57but really in this case it's kind of invisible to us
05:00because that margin is sitting outside
05:02so we wouldn't really see the gap or the difference
05:04and oh actually left is not, left is right inside here.
05:07So actually we have your,
05:09so the first property was the top,
05:11the second, the first property was our top,
05:15the second property and let's just make it
05:18because it's so confusing when you do this
05:20without actually looking at it.
05:21So let's go ahead to our item.
05:23All right, so let's look at it in action.
05:24So the first property is controlling our top position,
05:28pushing our top as that number grows.
05:31Our third parameter is our bottom.
05:34So notice how only the bottom is moving.
05:36Our second parameter controls our left position
05:40which we can't see anything because of that
05:42because it really doesn't matter how much padding
05:44we're putting there right now
05:45because it's all right here
05:46versus our last item which is our right position
05:50which controls our right.
05:52Now as you subtract items
05:54then it's gonna basically deduce the two together
05:57and compare them as we saw in our sample so far.
06:02This is the first really sophisticated property
06:05where it could have a lot of different combinations
06:07of values and the more comfortable you get with it
06:10the less you have to use all these larger items.
06:13So you could literally play around
06:14with almost most of the combinations,
06:16with all the combinations without the need
06:18to approach the larger items that have larger names.
06:23In the next lecture we're gonna still look into margins
06:26before we walk away from margins.
06:28We're gonna turn our attention again
06:29to this measurement point of pixels
06:31to understand how does it actually work
06:34and take a deeper look, Otto,
06:35into the rules of what type of items could I use?
06:39What type of sizes could I use?
06:41It's not gonna be a very long lecture
06:43but an incredibly interesting one.
06:45So I hope you join me in the next lecture
06:46where we explore different types of measurements on computers.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!