Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Now in the last lesson we looked at how we could change the display property using CSS in order to
change the way that our elements stacked against each other.
Now in this lesson we're going to explore CSS positioning in more detail.
But before we do so, the first thing we have to realize is that even without CSS, your HTML element
already has predefined rules for how it should be displayed on your web page even if you don't have
any CSS.
And we have to understand how things get positioned by default before we can go on to change it, to bend
it to our will, without getting confused and getting stuck.
So let's take a look at these rules.
So let's go through those rules quickly.
The first rule is that content is everything.
Now we know that inline elements only take up as much width and as much height as the content.
So if you have a span that contains a long word then it has a large width, if you have a span that contains
a short word then it has a short width. And with block elements, even though they take up 100 percent of
the width, the height is still determined by the content.
So if you have a really super large font for your h1 or for your paragraph, then that box model is going
to take up a lot more height than if it was a smaller font.
So your content is the first thing that determines how large things gets displayed and what the height
and the width will be.
And this is despite any CSS.
The next thing to remember is that the order of the elements on screen comes from your HTML code.
So in this case we have an h1, three paragraphs, then an image.
This is how we've written our code in our HTML file and this is how it gets rendered, in that order.
Now if we change the order and put the image at the top of our code file, then our web site is going to
display in a corresponding way.
So the default layout order is determined by your code.
The third thing we need to know is that children sit on top of parents.
What do I mean by that?
So here on the left I've got a div that's colored in red.
Now if inside this div I add an h1 with some text, then that h1 is going to be on top of the
div.
So it's more towards the viewer and away from the screen.
So this kind of introduces the concept of the Z index.
We know that on screen we have an X and a Y axis.
But there's also a Z axis, and that determines which element will be displayed on the other one, because
if in this case the div was in front of the h1, then we wouldn't see the h1 at all.
So by default our HTML elements that are children, they will sit on top of the parent element.
So if we modify this code even further, and we add a span nested inside that h1, then the word inside
the span is going to be on top of the h1.
So we have three elements here on screen so far, a div an h1 and a span, and going from the screen towards
the viewer, we first have the div, then the h1, then the span. From top to the bottom of the hierarchy,
it comes closer and closer towards the viewer.
So these three are the default rules for how things get rendered on screen just based on your HTML code.
Now you can also set a CSS property which is the position property in order to position elements on
screen the way that you want it to
instead of just going along with the default layout, and in most cases you're going to want to make changes
to the default layout.
So let's look at some of the ways in which we can change this position property.
Now these are the four that I'm going to talk about.
And the first one, the static position, we've actually already talked about it. All HTML elements are static
in their position by default, and static just means go along with the HTML rules and keep to the default
HTML flow, and that is what we see when we just have HTML without any CSS, or if we
don't change this position property at all.
Now the next one is called the relative positioning.
And what this allows us to do is to position the element that we select relative to how it would have
been positioned had it been static.
So in this case on the left we've got our Bacon Fansite, and this is just the normal HTML flow.
The image is being shown to the left and it's below all the other elements,
abiding by those three rules for HTML flow that we mentioned before.
Now if we select that image element and we change its position property to relative instead of static,
which is the default remember, and in addition to that we say it should be pushed 30 pixels from the
left of the previous position,
this is how our image will end up being positioned.
So whereas the left edge of our image used to be here and that would have been its natural position,
abiding by the HTML flow,
now we're saying, give that image 30 pixels of space between its left edge and where the left edge used
to be.
And there's four of these coordinate properties:
the top, the bottom, the left and the right.
And we can set values for them in order to determine how we want to move our element.
So let's say that we've got a image that is positioned using relative layer.
Then we set the top coordinate property to 20 pixels.
That means it'll get moved down by 20 pixels. And if we change the left property 20 pixels, then it'll
get pushed right 20 pixels.
If we change the right property 20 pixels, it'll get pushed left
20 pixels, and the bottom property as well.
Now there's two important things that you have to remember when you're dealing with relative positioning.
The first thing is that when you move an element that has a relative position,
say in this case we're pushing it down by 50 pixels,
it doesn't affect the position of anything else on screen.
It's as if the old position was kept and everything else just flows around it as if it was never moved.
It's almost like this element left the ghost of itself where it used to be in order for all the other
elements to keep their own positions.
Now the other thing to remember is that when we change the coordinate properties, for example in this
case we're saying make the top corner property for this relatively positioned image 50 pixels,
what that really means is that we're taking the top of where that image used to be and we're giving it
a 50 pixel margin from the top of our current image.
So if you think back to the box model when we spoke about margins, this is the best way of thinking about
these coordinates. Because very often students tend to ask, 'If it's top 50, why is it not going up by 50
and why is it instead going down,
and what is it top relative to?'.
So if you get confused
have a look back at this video.
So inside this code pen I've created three divs that are colored red, blue and yellow, and they each
have a height of 100 pixels and a width of 100 pixels,
so essentially three squares inside a body element.
I want to show you what changing the red div's position from static, which it currently is by default,
to relative does. Now by itself changing this position property does nothing.
It only starts doing something when you use one of the coordinate properties like left.
So let's push it 20 pixels from the left you can see that it just got nudged 20 pixels from the left
of where it used to be.
Now I can also move it all the way to the right by saying something like left 600 pixels.
So it's now got a margin between the left edge of where it used to be and the current left edge of
600 pixels.
Now if I say right 200 pixels,, can you predict what will happen to my red square?
So let's take a look, see if you got it right.
So all of our red square has completely disappeared off the screen.
Now why is that?
Well, because the previous right edge was here, and to have a margin between the current right edge and
the previous right edge of 200 pixels pushes our red square all the way over to the left that you can't
see it anymore.
Now some people might have thought that by saying right 200 pixels, then it should be somewhere around
here, because this is the right edge of the screen.
But as we mentioned the relative positioning is relative to where the element would have been displayed
if it was just following the natural HTML flow.
So it would have been here and now there's a margin between its previous right edge and its current
right edge of 200 pixels.
But remember as we said that the relative positioning doesn't affect any other element on screen.
So if I said top 50 pixels it pushes it down by 50 pixels but it doesn't affect the layout of any other
element on screen.
It doesn't for example push this blue square downwards or any other element.
It's as if the ghost of the red square's past has stayed where it used to be,
and it's only this newer version that's being moved around.
So there's a lot of new concepts here.
And before we move on I just want to make sure that you're comfortable with this idea of relative positioning.
So as a challenge I want you to change the display property of the blue and the yellow squares so that
they will show up in a row where we've got the red square, the blue square and the yellow square,
and then I want you to move the red square to the right of the yellow square.
And this is what you should end up with.
Now don't worry too much about the gaps between the squares.
It's more important that you get the order right.
So they're all on the same line and it's blue, then yellow, then red, and you should be able to see most of
the square.
All right.
So did you manage to do it?
So the first thing that we needed to change was the display property of all three elements. So we're
going to change it to inline block so that we can still change its width but also have them show up
inline.
So let's add that line of code to all three divs.
So now they're in the same line.
So I want to get rid of this top and make them line up properly.
But I now want to nudge the blue square to the position of the red square.
So I'm going to make its position relative to where it should be.
And I'm going to give it a right margin of 100 pixels and I'm going to do the same thing to our yellow square.
So I'm going to change its position to relative and also give it a right margin from its previous position
to 100 pixels and that nudges it left.
Now the final thing I have to doing is to move the red square to the right by 200 pixels. So I can change
its left margin to 200 pixels, and that moves the red square over to the right.
Now the reason why the spaces between our squares are inconsistent is because by making it an inline
block there's actually a little space that gets added in by the browser.
So we can see that if we get rid of all the position code there's just a slight smudge of a space between
each of the inline block elements.
And that's what's making our gap slightly inconsistent.
But the important thing is that you managed to get the order of the blocks correct.
So if you managed to get the squares to line up in this order then that means you've successfully completed
the challenge. In order to get the position more fine tuned,
we're going to look at different ways in the future.
Now in the next lesson I want to show you another way of positioning your elements by using absolute
positions.
So for all of that and more.
I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.