Defining Backgrounds
Color is probably the thing you think most of when creating pages in CSS. In this chapter as a whole we spend a lot of time adding color into our HTML page. By the end of this lesson, you will know everything you need to about adding backgrounds to html elements.
Video Transcript
00:00Up until now everything we did was very technical but say goodbye to technicalities and say hello to visual fun
00:07From here on we're gonna be focusing on creating beautiful things and we're gonna start off from taking a deeper look into the background
00:14Attribute and we're gonna see a lot of different ways to work with the background and in the process
00:19You're gonna learn a lot about how CSS works
00:21So really what we're doing here, although it's probably a little bit more longer than you would expect from future videos
00:28it's gonna help you kind of grasp the basic concepts in CSS and how
00:32Structures of properties work in CSS. So let's get right to it
00:37Let's start chapter 3 with Augusto and our main focus is gonna be backgrounds and really looking deep into
00:44Properties and seeing how many of the properties in CSS work
00:49So to do that, I'm gonna start off
00:50We took we took a gander at background color ready and we saw how we could add different colors into our
00:57Background now what I want to do is I'm just gonna break a line for the sake of a
01:03Visual pleasure really and in a real world application
01:07You would probably use some sort of condenser to condense all of your code which will talk about that in other courses
01:14But don't worry about it
01:16Especially not now when you don't have massive applications
01:19Try to build your applications to be the most easy for you to read so you could break lines easily
01:25Alright, so now that we've seen the background color
01:27I want to move on to the next item and the next item is going to be background
01:33image
01:34So basically I just type background and I search for the word image or type it in
01:39once I do that the next step is I set a URL by opening typing the word URL and
01:45Opening it and closing a curly. Well, not a curly bracket around bracket
01:49And I always end it with a semicolon because I have more things afterwards to make sure
01:54That the rule will be applied and I'm sending inside a string
01:57So I'm gonna wrap it around with a quote
02:00So I'm putting here a quote to start a quote to end and then I'm gonna put my image itself now
02:06If we take a look into our CSS site itself, we'll see what something very interesting
02:12So first of all in our site itself, we'll see that we have here our CSS that's sitting inside of the style folder
02:18But inside of our images folder
02:20This is where I have a pattern that I want to add right inside. This pattern is actually transparent
02:26so this is a transparent parent a
02:30Transparent Wow, that is a hard word to say
02:33transparent pattern
02:35Maybe it's just hard for me transparent pattern now this transparent pattern, which I'm not gonna say that word again because it's driving me crazy
02:44Will enable me to also not only see that background color than I have but on top of it to put this
02:49Pattern that I want to put above now. It's very easy to put it in but I have to
02:55Naturally on a real website. You might start with a slash now when you start with a slash
03:01It basically means the root of your website in our case
03:04We don't have a root to our website
03:06Well, really the root to our website will be basically no folder because I'm in a local folder in my cert and my website
03:13On my local host, but if I would be working on a real website with the www dot whatever it would be
03:20If I would set my pattern to be starting with a slash I would basically start from the root directory of that site
03:28So if it's zero
03:30To geek calm then it would start from that zero to geek calm root folder
03:34If I don't put anything that means that I'm gonna be positioned right inside the same place that I'm in in this case my style
03:41We're in this the style is inside of the folder CSS
03:45So right now our path assumes that we're gonna find something inside of that path in
03:50Reality, what we want to do is we want to say hey
03:52We want to go back one folder back to do that
03:55I'm gonna type two dots and then a slash which will basically tell me I'm going to the folder that's before you
04:01The next step is I want to go to the image folder and then into the pat dot png. So let's go ahead
04:06It's gonna be image folder pat
04:10dot
04:12Png and there we go. That's all I had to do to create this pattern and connect this image to my CSS
04:18Obviously this image would have to be on our server as well for it to load up
04:23But once I click on refresh
04:25You'll see that now this pattern has been added in to our application
04:29Now by the way while you were gone, I just added a little bit more text
04:34For the sake of the samples if you have not done that yet
04:38Just make sure or if you're working on your own original script make sure that it's scrawl the bull
04:43and if it's not scrawl a bull just go right into your
04:46Index HTML and just add a few more text to make sure that it is so we could talk about our next few topics
04:53All right. So the next thing that I want to talk about is
04:57Right by default when we put our image because it was so small it automatically repeated both to the right and to the both from
05:05Basically to on our x-axis and also our y-axis
05:09To be able to control and say hey
05:11I only want to repeat on the x-axis or only on the y or maybe I don't want to have any repetition at all
05:18That's when we introduce a new property and that property is background
05:24Background but what am I doing?
05:28Repeat background repeat and for our background repeat the default
05:32Which if we don't do anything is repeat and that is the default unless we overrode that default by doing something some CSS that
05:41Overrode it but by default that would be our pattern now if I wanted to have a repeat of our my X
05:47I would just do repeat minus X or dash X and
05:50Repeat repeat that dash X will basically enable me to have a repetition. That's only going in one direction
05:57now on the other hand if I wanted to create a repetition that is only on the y-axis then I would set it to be a
06:05repeat Y
06:06So I'm going to go ahead here and just click on refresh
06:10And you'll see that now only my y-axis has that repeat pattern going on last but not least
06:16what if we don't want to have any repetition at all and for that you'll just set no repeat and
06:22By doing that you're going to set up and make sure that there is no repetition
06:26Now notice everything you see how we only have that square right there now
06:31I'm kind of happy with the repeat X. So I'm going to go ahead here and just set it to be repeat X and again
06:37You don't have to memorize
06:38It's really about knowing it once you know it then just search it up and have a document in front of you of all
06:43the different possibilities and
06:45We'll try to make sure that we add an attachment of all the different options that we covered in the lesson
06:51Alright, so, you know and even to make it a little bit more
06:55It I'll just make sure that I'm gonna add it into the source files right here
06:59So you'll be able to have a reference to all the different options that you have in all these properties that we've been talking about
07:05Alright, so now that we've talked about that and then we set our repeat to be why I want I want to go over and continue
07:10On to another background element that we control now the next element that we could control is basically is
07:18The item scrawlable or not now to be able to show that I'm gonna make sure that it's I'm gonna actually do a repeat X
07:24So we could actually see it
07:25But basically what's going on is if our background is up here and I scroll down on my screen
07:31Then that background is not coming with me. It's staying put it's staying where it's at
07:36Now I'm just gonna make sure that I have a little bit even a little bit more text here
07:40So I could show this sample in a much more vivid way
07:44And I want to showcase the next background option now the next background option is called background attachment
07:52Now the background attachment is simple as well. It has two options one is scroll and
07:58The scroll option what what it does is basically it may basically as the name applies. It basically is
08:07Connected to where it's at and it's not well
08:11technically it's
08:14Connected to where it's at
08:17contrary to that
08:18the option of fixed
08:21So if I set here to be our attachment, well, let me just say this. I might have not saved it actually that could explain it
08:29Alright, so I'm gonna go there and click on and save it with the fixed option instead
08:33And when I set it to be fixed notice the difference now with fixed my position is gonna be on my axis
08:40Exactly in this point where it's at by default
08:43But as I scroll notice what's gonna happen that background remains exactly where it is
08:49This is a perfect scenario where you won't want to have a background that does not scroll that
08:53Stays with the user and continues showing exactly the same thing while the content scrolls, but not the background
09:00The default by the way was that scroll that we had previously so if we wanted to change it
09:06Then we would set it to be fixed and by setting it to fix then that background itself would not scroll
09:11It would be actually connected directly and not moving
09:15In other words as we move our our elements on our screen
09:18The background would remain in the same position that it was at now notice one more thing is when we're scrolling notice
09:24How this background because we set a red background in another background here. These backgrounds are
09:30Overshadowing that back background making it invisible until we cross over and pass that background
09:37All right, we're almost done
09:39We have one more property to cover with the background properties and that background property is
09:44background position now the background position has a lot of different options and
09:49Many some of these options are basically to configure. Where do we want our background to be?
09:55For example, we want our background even though we have here this repetition that's going on
10:00So the repeat repetition of X so I'm gonna go ahead here and set it to be no repeat for a second
10:05just for the sake of showcasing our
10:08Options here and I'm gonna go ahead and I'm gonna say to it. Hey, by the way, I want you to be in
10:1320% and
10:158% in your position or in other words, I want you to be pushed
10:19I want your background to be pushed in this location to move down 8% from the top and
10:2520% from the left now
10:28I could have set it in any values that I wanted so this would control my left and this would control my top
10:34Another option is I could have set it with hard-coded values using pixels
10:38so I could have said 10 pixels 10 pixels giving me a 10 pixel gap in my
10:43Element if I just save this here for a second and go back into my application and click on refresh
10:48We'll see that now it's gonna have your a nice little spacing in my element
10:53Now if I turned on my repetition again, if I had my repeat X notice what would happen
10:59So it's a kind of an important thing to note and also understand is that our our element
11:04Although we're gonna be moved down by 10 pixels. We're seeing that it is flushed all the way to the end anyways
11:10Now the reason is is very simple is because the repetition is happening
11:14But the image original alignment is gonna be aligned a little bit further to make it a little bit more extreme
11:20If I go ahead here and I just I'll just set here even 200 as our position if I click on refresh
11:26We'll see that even though I set it to be 200 still all the repetitions are still happening
11:31The only thing is is that the start point of our image is going to be set to that specific point
11:35so I'm going to go ahead here and set it back to no repeat because I want to continue talking about different options that we
11:41have with our
11:43Background position the last styling
11:47capability is actually typing words and I could literally say I want you to be on the top or
11:52bottom or
11:54center and
11:55The second parameter first parameter will control top center bottom and the second parameter would control again top center
12:04left left center right or
12:07Then go ahead here if I click on refresh
12:09We'll see here that we have here our image is now right smack in the middle if I wanted it to be for example
12:15In the left then I would just set it to be left
12:18Center and if I would go ahead here and click on refresh
12:21We would see that my item would be on the left and in the center
12:24And if I wanted to I could have set oto it to be on the bottom making sure that my bottom left is
12:31Configured so it would always be on my bottom left no matter where I am at
12:36We could close that so we could see it more vividly
12:39So that's it that covers all the different properties of the background and all the ways you could actually work with the background
12:45What I want to talk about is one more small thing is I'm going to go ahead here
12:49And I'm just going to add here a comment and in the process show you how to create CSS comments
12:55So I'm just going to go ahead right into my CSS
12:57I'm going to open up a comment by typing these characters that I just typed on the screen and close it basically
13:05slash star slash star and notice I've just added here
13:10Basically the different types of options that you have for your elements and notice where the default
13:16I'll just put your around bracket so you know, but this is the example of what you could do with it
13:20And this is the default so by the way the default here is repeat. Oh
13:27See the default here is repeat the default here is scroll the default here is basically zero zero
13:34So there's I can't really write it down there
13:36But these are your three different options all your different options of all your configurations
13:41And I'm going to try to make sure that I paste these into samples as we go on from here on now
13:47We're not done talking about the background
13:49But we're going to talk about that in the next video where we talk about the actual background property
13:54Which exists and we're going to see how it works very simple. It'll just take us a minute or two
13:59So we'll see you in the next lecture when we cover that
