Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
Applying borders to elements is a fairly simple process, but the Border property syntax can be a little bit cumbersome. Let's explore the options we have when applying borders to our elements, and how to write the syntax efficiently. In this exercise, we will be working with the borders.htm file You can find that in the 05_05 directory. Again, very simple structure here, if I scroll down, I can see that I have the heading 1 and that's what we're going to initially apply a border to. And then I have all these paragraphs down here and they have classes that basically correspond to the different styles you can apply to borders, and we are going to take a look at those after we're done playing around with the syntax of the border declaration. Okay.
So I'm going to scroll up to my Styles here, and after the paragraph, I am going to go ahead and create a selector for h1. The first thing that I am going to do is I want to explore the verbose syntax of borders. There are actually three different ways that you can apply borders in terms of their syntax; one is a very verbose, the second one is sort of a shorthand notation for a single side, and then we have a uniform shorthand notation that is extremely efficient. So we are going to do the inefficient way first just so you can see it, and then we are going to refine the syntax to maybe more of an efficient way.
So the first thing that you can do is you can declare that you want a border, and you can declare that border on all the different sides of your elements. So you can do top, right, bottom and left just like you can with margins and padding. But with border, you're always declaring a width, a style, and a color. Using this syntax, you can declare them individually. So if I say border-top-width, I can pass that a value. So in this case, I am just going to give it a border-top-width of 1 pixel. Then I can say border-top-color, and I can do these in any order that I want basically.
We will just go ahead and do #000 which is black. Then I can do border-top-style. Now, we are going to experiment with styles and all the different styles that are available to us in just a moment. So for right now I am just going to do solid. So now, that would apply a top border to this h1 that is 1 pixel, black and solid. If you use this verbose syntax, you have to do this to every single side. So we would come down here and say border-right-width: 1px; border-right-color:#000 and then border-right-style: solid.
That's fun right? So I'm betting you guys don't want to go and complete the rest of this. So if you were going to go ahead and complete it out, you would do the same thing for border-bottom and border-left. You would declare individual width, colors and styles for each one of those. You end up with 12 separate selectors that are all really just applying a border to one element. So it's not very efficient. So let's take a look at maybe a little bit more efficient way to do this. Instead of going ahead and declaring the width, color, and style their own separate rules, I can use the sort of directional shorthand notation.
So I could just say border-top and when I say border top, I can pass-in all three of those properties. So I can say 1px solid #000. So after border-top, I can do border-right: 1px solid #000, border-bottom: 1px solid #000, and finally, border-left: 1px solid #000.
So that's much more efficient than what we were doing before. Rather than having 12 separate properties, we only have 4 and we're declaring basically border all the way around this to each individual side. Now using the syntax, the nice thing about this, let me just go ahead and save this and we will go ahead and preview this in one of our browsers. There we go. So I am getting a border of equal width all the way around this. The thing that I like about this syntax is that if I wanted to do something a little bit different here. So, for example, if I decided that I wanted a really thick top border and then 1 pixel border for everybody else, I could save that, preview it, and you can see it sort of gives me that nice top border.
So using this sort of directional shorthand notation, you can declare individual borders for each edge of the element which is very nice. But if we want to do a solid single width border all the way around the element, this also is not very efficient. So I can take this one step further and just use what we call the uniform shorthand notation and I could just say border. Here I can just do border: 1px solid black; If save this, and test this in the browser, I get that nice 1 px border all the way around it again.
So using this shorthand notation, you're essentially passing the same border to each of the sides. So I really recommend using one of those two syntaxes depending upon whether you want your border to be uniform or not. I mention that we would take a closer look at the border styles that were available to us, and I'm going to do that through the use of these class selectors. You can see, for example, that I have got all these different paragraphs and I have these classes applied to them, and I just want to come up here and I am going to do each one of these as border and then 2 px solid, and I am going to do #f00 just as something a little bit different, save that.
I am going to do f00 rather than black because I want you guys to see how color is applied to a few of these border styles, some of them don't get quite the color applied to it the way that you would think, some of them use color almost as a highlight if you will. Okay. I am going to keep going. Here, I am going to do border: 2px, and this time, I am going to use dotted; so dotted for the line style, then #f00. Going down to the next line, you can probably guess what we are going to do here. I am going to do border: 2px-#f00.
Now, I'm going to change up a little bit because the remaining borders down here, the double, the groove, the ridge, the inset and the outset often require thicker borders in order for you to be able to see the style because some of these have sort of a three-dimensional styling to them, and if you try to do them with a 1-pixel border, you really wouldn't see the formatting or the styling of that particular border. So I am going to up the width of these. For double, I am going to do border: 4px and then double #f00. For groove, we are going to do border: 4px groove #f00, for ridge, border: 4px groove#f00.
We have two more to do, border: 4px inset #f00, and finally, and yes, feel free to copy and paste, it probably would have been a little quicker; border: 4px outset #f00. Okay, let's take a look at all these different styles, I am going to save this. Again, I am going to go up to my browser and I'm going to preview this again.
So you can see these first three the solid, dotted, and dash give you pretty consistent styling throughout all of your browsers; the double, groove, ridge, inset, and outset these require a little bit of a thicker border in lot of cases at least 3 pixels or higher, in this case I am using 4. Now, the color that I used here f00 is a red and depending upon which browser you're looking at in, some of them apply colors a little bit differently, especially some of the older browsers. So I am just going to open up another browser here real quick.
In this case, I have opened it up in Chrome, and you can see there's not too much of a difference between that. Now, I am going to open it up in Firefox. Now, here in Firefox, we see a little bit of a difference. Let me compare this again to Opera. So take a look at the outset and the inset values. Now look at those in Firefox, so that's a lot lighter so that color is being used as a much brighter highlight, it's also being used as a much brighter highlight on the ridge and the groove. Let me try this in Safari and you can see that you get the darker red in Safari like you're in the other browsers.
So it's up to the browser itself to go ahead and display that style the way that, that style is basically just defaulting in that browser. It's when you throw a color in the mix, you are going to see some slight variations these days, especially with some of the older browsers that you might be working with. So another thing I want to mention here before we close is that with the advent of CSS3 and the adoption of some of those properties, you are going to have even more options than you currently have in regards to borders. CSS3 introduces border images which allow you to use images as decorative border.
So you can specify images for the sides and the tops and basically decide how you wanted the tile between the sides as well. Now it's not fully implemented across all of the browsers yet, but if you want to see it in action, I recommend going and checking out my CSS3 First Look title for more information on border images.
There are currently no FAQs about CSS: Core Concepts.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.