CSS: Adding Borders to Elements

Styling the Border of any Element. Add Border-Width, Border-Color and even different Border-Styles.

FREELevel: Beginner3:31 mins
Course content
Lessons #1: Understanding HTML FREE
6:35 mins
Lessons #2: Understanding Basic Markup FREE
3:56 mins
Lessons #3: Textual Tags: Common Elements Used to Write Text in HTML FREE
14:45 mins
Lessons #4: Layout Tags: Common Elements Used to Layout an HTML Page FREE
8:08 mins
Lessons #5: CSS: Getting Started with CSS FREE
7:21 mins
Lessons #6: CSS: Common CSS Properties to Style Text FREE
4:48 mins
Lessons #7: CSS: Common CSS Properties to Style Background of Element FREE
4:42 mins
Lessons #8: CSS: Additional CSS Properties to Style Text FREE
3:12 mins
Lessons #9: CSS: Adding Borders to Elements FREE
3:31 mins
Lessons #10: CSS Pseudo Class Selectors FREE
6:27 mins
Lessons #11: CSS Layout (Part 1 of 3) FREE
16:08 mins
Lessons #12: CSS Layout (Part 2 of 3) FREE
6:35 mins
Lessons #13: CSS Layout (Part 3 of 3) FREE
17:21 mins
Lessons #14: Creating a Blog Design (Part 1 of 3) FREE
5:29 mins
Lessons #15: Creating a Blog Design (Part 2 of 3) FREE
10:20 mins
Lessons #16: Creating a Blog Design (Part 3 of 3) FREE
11:01 mins
Lessons #17: Form Elements (Part 1 of 2) FREE
7:54 mins
Lessons #18: Form Elements (Part 2 of 2) FREE
7:18 mins
Lessons #19: Table: All About Creating Simple to Complex HTML Tables FREE
13:04 mins
Lessons #20: Embeds: Video, Audio and iFrame Elements FREE
5:43 mins
Lessons #21: External Libraries FREE
17:04 mins

Adding Borders to Elements

In this lesson, we are going to learn, how to add borders to elements, so let's get started. In our previous lessons, we learned to set a background image. If you want to change the certain background color and keep the background image for rest of the webpage,  you can accomplish that by selecting whatever area which you want to change the color of, and wrap it inside of a generic div. I'm going to set the class for that particular div element and call it a wrapper, you can name whatever you want. In the style.css sheet, write .wrapper with a dot at the start, and set the background color to white like such:

.wrapper{
  background-color: white;
}

The div with a class of wrapper is now going to have a background color of white.

In the above image, you see the certain background color changed to white, while some area still shows the background image, now it looks pretty. You can play around and make changes as you see fit according to your preference.

You can also add borders around the webpage by using the border-width: property and set the width of the borders to 4px like such 

border-width: 4px; this property will not take effect, we need to add another property called border-style: tells us what kind of borders do we want. You can select from dashed, bottled, hidden and so forth, it really depends on your 

preference. For the sake of demonstration, I am going to go with the solid one.  border-style: solid; you can also change the border color by using this property  border-color: blue;

.wrapper{
 background-color: white;
 border-width: 4px;
 border-style: solid;
 border-color: blue;
}

Now if you refresh the screen you will notice that it has a solid border of blue

You can change the color by writing the name of the color or you can write the HTML color code like such #ffffff

We learned in the last lesson that instead of writing so many attributes, you can define all the parameters in one particular attribute. So instead of writing so many lines of code, we can simple write border: 4px solid #3399cc; and it will still work the same way.

.wrapper{
 background-color: white;
 border: 4px solid #3399xx;
}

If you want your border to be only at the top of the webpage and not on the right or the left side or bottom, you can simply do

border-top: and it will only show at the top of the webpage. 

.wrapper{
 background-color: white;
 border-top: 4px solid #3399xx;
}

You notice, only the top border is showing and its color is also changed

you can also have two different borders as well, you can have a solid border at the top and a dotted border at the bottom, you can accomplish that like such

.wrapper{
 background-color: white;
 border-top: 4px solid #3399xx;
 border-bottom: 4px dotted #3399xx
}

Refresh the screen and you see, the top border is solid while the bottom border is dotted

I hope you enjoyed this lesson, If you have any question, please leave your comments below, I'll talk to you in the next lesson. Good bye :)

Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login