Now let's take a look back at what we've got on our web site and see how we can use some of the knowledge 1
that we learnt about positioning in order to make this web site look the way that we want it to. 2
Now the first thing that we want to achieve is we want to center all of these elements, the image, the 3
mountain image, as well as the text here in the h1, and also the subtitle. 4
So the easiest way of centering elements is by tapping into a property called text align and that property 5
has to be set inside the parent container, 6
so in this case either in the top container or inside the body. 7
So I'm just going to go ahead and center everything on screen by tapping into the body selector and 8
saying text-align center. 9
If we hit save and we head back and we hit refresh then you'll notice that all of our images, our h1 10
and our paragraph is now centered. 11
Now this works as long as we've got an inline block element such as our images or if we've got these 12
full width block elements like our h1 and our p tag, which are going from left edge to the right edge. 13
Now if I go into my code and I change the h1 to change its width to say only, I don't know, let's 14
say 10 percent, 15
then you'll notice that it's no longer being center aligned despite the fact that it's still inside 16
the body which is asking for text align to be center. 17
So what do we do in this case? 18
Well the other way of centering an element is by using the margin. 19
So there is a value called auto which when applied will center the element either vertically or horizontally. 20
So in this case if we're looking for horizontal centering then we can say that the margin top is 0, 21
the margin right is auto, 22
the margin at the bottom is also 0 and the margin on the left is again auto. 23
So I'm going to delete this margin top here and hit save. 24
And if I refresh you'll see that our element despite being only 10 percent of the width of the screen 25
is now centered in the middle of our web page. 26
And when you look at the box model you can see that the top and bottom margins were set to be 0 by us 27
and the left and the right are set automatically to whatever amount it needs to be in order to keep it 28
in the center. 29
So very often when you find yourself trying to center elements you'll probably end up using one of these 30
methods. 31
So just to recap, text-align center inside the container, or the parent element, will center everything 32
inside that doesn't have a width set. 33
Now if it is a block element and it has a width set, then you're going to have to center it using this 34
auto value in the margin. 35
So in this case we can also shorten this by saying that the top and bottom needs to be zero and the 36
left and right needs to be auto. 37
And that will not change this at all. 38
And this is just one of those circle shorthands that you will begin to learn. 39
And if you've forgotten how that worked or if you're interested in learning more then you can always 40
check out the CSS Docs on MDN for the margin, where they go through each of these, where if you only 41
have a single value of the margin then that's all four sides, 42
if you have two then that's top and bottom and left and right, 43
if you have three then it's the top left and right and bottom, and if you have all four then that's just 44
all four sides in a clockwise direction. 45
So if you get confused or if you forgot how that works then have a look over there. 46
Now I'm actually going to undo and I'm going to revert back to margin-top being 0 and text-align 47
being centered and you'll see that this is what it will now look like. 48
Now the next problem we need to tackle is that these two images are acting as if they are inline blocks, 49
and the cloud, because it's in the flow of the HTML file, is affecting the layout of the mountain. 50
More specifically, I want this mountain to be right in the middle, and at the moment it’s being pushed to 51
the right by that cloud. 52
So in order to fix this we have to take this cloud out of the HTML flow, and once we've done that 53
then the mountain will go back to being centered. 54
So can you remember how we can take an element out of the HTML flow by changing one particular CSS 55
property? That is of course the position property that we're going to change. 56
And we're going to change it from its current value which is static to absolute and that is how we're 57
going to remove it from the HTML flow. 58
So inside our index.html I'm going to give a class to our bottom cloud and I'm going to call 59
it bottom-cloud. 60
And while I'm here I'm going to give a class to our top cloud and I'm going to hit save and I’m going to head back 61
over to all styles.css, and we're going to target that bottom-cloud, and I'm going to set 62
its position to absolute. 63
Now if I hit refresh you can see that that cloud has been taken out of the flow and it's kind of being 64
placed more or less randomly on the screen. 65
But more importantly our mountain is now bang in the middle of the file, 66
and this is exactly the layout that I want for it. 67
So the next part is a challenge and this is what you want to end up with if you manage to complete the 68
challenge successfully. 69
We've got our top cloud that's on the right and a little bit above the h1, and we've got our bottom 70
cloud that's on the left and just between the text and the mountain image, and they are both taken out 71
of the flow of the HTML document. 72
So pause the video and give that a go now. Now how did that go? 73
Firstly, our bottom cloud at the moment has an absolute position. 74
Now in order to position it relative to something, one of its parents has to have its position set as 75
relative. 76
And if we don't then it will be relative to the body. 77
So in our case I want us to be able to position our bottom cloud relative to this entire top container. 78
So I'm going to set the position of our top container to relative. 79
And I'm going to start off by giving our top container just a little bit of padding at the top so that 80
we can push down this cloud and all of these contents just down by a little bit so that it's not all 81
stuck at the top of the page. So to do that we can add a padding top of 100 pixels, and that just pushes 82
everything down a little bit, and then I'm going to add our top cloud as well. 83
And I'm also going to take it out of the flow by changing its position to absolute as well. 84
Now if we hit refresh you can see our two clouds are kind of just floating around without doing very much. 85
So now let's position them properly. 86
So relative to our top container here I want the top cloud to have a right margin of 300 pixels and 87
a top margin of 50 pixels. 88
And I want our bottom cloud to have a position left margin of 300 pixels and and a bottom margin of 300 89
pixels from the bottom of its parent which is of course the top container. 90
So it will be from this line. 91
And if we hit save and refresh then this is what we will end up with. 92
Now before we do any further positional tweaking we should really format the text because by changing 93
the font, the font size etc., we’re going to affect the positioning of our elements as well. 94
So in the next lesson we're going to look at how we can style our text using CSS. 95
So for all of that and more, I’ll see you on the next lesson.