CSS Basics

Basics of CSS – Part #7

Today we’re going to continue our adventure about the box model. We covered the box model and the margin property but we still need to cover padding and border properties!

If we recall that margin > border > padding > content… Margins are always invisible, Borders can be any color we’d like them to be, and padding will be the same color as the background-color of the element.

Border Property

The border property defines the border around an element. Using this border property we can have a border around anything we’d like. We can have the following properties:

  1. border-top
    • border-top-color
    • border-top-style
    • border-top-width
  2. border-right
    • border-right-color
    • border-right-style
    • border-right-width
  3. border-bottom
    • border-bottom-color
    • border-bottom-style
    • border-bottom-width
  4. border-left
    • border-left-color
    • border-left-style
    • border-left-width
  5. border – all-inclusive

Consider the following code:

1
2
3
4
5
6
7
8
9
p.top-border {
     border-top-color:#ff9300;
     border-top-style:solid;
     border-top-width:1px;
}

So this would only apply to p elements with the class attribute of “top-border”, we know that much! This particular element would have a top border of #ff9300 (Atomicpages orange!), a border-style of solid (there are no breaks or spaces), and a border width of 1px since we want it to be thin.

This is how it would look like if we tried this. The more we type the longer the border becomes. It almost appears as if the text above is really being underlined but this is the border-top property.

For every border property there exists a short-hand way of adding styles to our borders. We can very well type border-top-width, border-top-style, border-top-color every instance we want to use this. We know exactly what the border is doing without having to guess or anything. This can take time, however. We can shorten the example above by doing the following:

1
2
3
p.border-ex {border-top:width style color;} /*Syntax*/
p.top-border {border-top:1px solid #ff9300;}

This is far more efficient and a much faster way of adding border styles to our elements! It is more confusing and might take some time to get used to. If you’re comfortable using the long-hand method then stick to what you’re comfortable with. If you prefer short-hand methods then use those, bear in mind that the short-hand method does exist and it readily available.

border-width

The first value/property we should define in our border should be the width of our border. There are built in values that we can use or we can define our own. Here are the values we can have:

  1. thin — approx. 1px
  2. medium — default value
  3. thick
  4. px, em, pt, etc

This is a thin border, This is a medium border, This is a thick border,

This is a border with 15px as the value..

Why we would want a border with 15px is beyond me, but for the sake of this example it is fine.

border-style

There are a lot of styles for borders, here are the styles you can have:

Values Description
none Specifies no border
hidden Specifies a hidden border
solid A solid border
inset 3D inset border, effects depends on border-color value
outset 3D outset border, effects depends on border-color value
dotted a dotted border
dashed a dashed border
groove 3D groove border, effects depends on border-color value
double a double border

That is a lot of styles indeed! We won’t go over every single style in this tutorial. I will, however, add a downloadable that will contain these styles! Note: the 3D style borders, it depends all on the color you pick. Some colors will look nice and some color will not look nice at all!

border-color

The border color property works the same as the background-color property. You can choose any color you would like your border to be. You may use built in colors (e.g. reg, green, blue), HTML Color Codes, or RGB Values to use your colors.

1
2
3
4
5
border-color:red; /*Color value*/
border-color:#ff9300; /*Hex Value*/
border-color:rgb(51, 255, 0); /*Decimal Value*/

All-Inclusive

The all-inclusive border property works the same as most other all-inclusive properties. It saves time but makes the code a little more confusing. The syntax you should use is like the following:

1
2
3
border:width style color;
border:1px solid #ff9300;

If you want to see these examples in action then click on the green download button below!

border.zip
border.zip
Size: 3.25 KB

Basics of CSS – Part #6

Today we’re going to talk about the margin property in CSS! You should familiarize yourself with the box model if you aren’t already.

Margin Property

As we see in the box model, the margins are always transparent and always surround the padding which surrounds the border which surrounds the content — margin > border > padding > content.

Properties

There are five possible properties you can use with margin:

  1. margin-left
  2. margin-right
  3. margin-top
  4. margin-bottom
  5. margin

The first four options allow you to have different values for any side with the code being clear as day. Consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
p.indent {
color:#ff9300;
border:1px solid #fff;
margin-left:25px;
margin-top:10px;
margin-bottom:30px;
margin-right:15px;
}

We have a class called “indent” which is turning the text the color #ff9300 (AtomicPages Orange), this element will also have a border that is 1px in width, solid, and pure white. This will also have a left margin of 25px, a top margin of 10px, a bottom margin of 30px, and a right margin of 15px. That means the p element will be confided to those specifications. These values can be almost anything you would like them to be.

Values

As with most if not all CSS properties. The margin property can contain multiple values.

  1. auto — the browser determines the margin. Different browsers means different results!
  2. em, px, pt
  3. %

In the above code example we used pixels as the unit. You can use em, px, pt, or any other unit you really want to use. The most common units are px and em.

Margin — All Inclusive

Using the single margin property will allow you to define all values of a margin that you want instead of typing out margin-top, margin-left, etc. This all-inclusive property is pretty nifty if you’re working and you’re on a roll and whatnot. It can contain one to four values.

1
margin:15px 30px 45px 60px;
  • top margin – 15px
  • right margin – 30px
  • bottom margin – 45px
  • left margin – 60px

This example has four values for the margin property. The values start with margin-top and then go clockwise to margin-left. So if you think of clock and a new day, which always starts at 12 or the top of the clock, and then rotate clockwise the next 90° angle is at the right, then the 180° is at the bottom, and then the 270° is at the left. So it’s like looking at the clock at every 90° angle you get top, right, bottom, left.

1
margin:15px 30px 45px;
  • top margin 15px
  • right and left margin 30px
  • bottom margin 45px

This might come as a surprise to us because you would think they would group the Top and [bottom and left] segments separately, however; we can see in this example that the margin-top is 15px and the margin-bottom is 45px. The left and right margins are grouped together because they just belong together. If someone asks, “when I say a word you think of something that is opposite to that” and they ask, “up” you’ll probably say “down” off the bat.

1
margin:15px 30px;
  • top and bottom margins 15px
  • right and left margins 30px

This might be more clear since there are only two values here. Naturally, we’d think Top and [right and left] so the top and bottom margins will have a value of 15px, whereas the right and left margins will have a value of 30px.

1
margin:15px;
  • top, right, bottom, and left margins 15px

One single value for the margin property will set all margins to that value, in our case 15px. If you wish to see this property and the values in action click on the download button below!
margins.zip
margins.zip
Size: 3.12 KB

Basics of CSS – Part #5

So the Basics of CSS saga continues! In this basics tutorial, we’re going to talk about the very interesting box model!

The Box Model

All HTML elements can be considered to have an invisible box around them (assuming you have no borders or padding). Consider the following:

1
2
3
<p style="background-color:green; color:#fff;">Some text here</p>
<p style="color:#fff;">Some more text here</p>

Let’s try it! This is some text and this is some more text.

So where’s this box?! Well… This HTML element is actually surrounded by margins, borders, padding, and the actual content. This box model is illustrated by the background we created. We can see the “box” around the content portion of the box.

box-model

As we can see by this picture, we have our margins, borders, padding, and content. That is what every HTML element is surrounded by. Most browsers, by default, will automatically keep the margins, borders, and padding of content at zero..

1
2
3
4
5
6
7
8
9
body, html {
     margin:0;
     border:0;
     padding:0;
}

So those three properties are values are essentially understood by some browsers. Now, to clarify for every browser, you should include these first and foremost in your CSS document — before everything else.

What is what?

  1. Margins — can be thought as an always invisible area around the border.
  2. Border — can be different styles, colors, and thicknesses and it is the area that surrounds the padding.
  3. Padding — is the area around the content and it is relative to the background color of the box. So if your box is set to blue then the padding will also be blue.
  4. Content — the content is well, content. Text, images, links, lists, all of that good stuff!

Great! Now we know all about this box model but what use is it? Well in order to correctly set the size of an element you actually need to be aware of this box model.

Height and Width

So why is it so crucial to be aware of this box model? Well, when you define the height it width of an element you’re only defining the height and/or width of the content of the box model. So if you decide you want to add margins and some padding to an HTML element you can calculate the real width and real height.
Consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
selector {
     width:500px;
     height:250px;
     padding:20px;
     margin:25px;
     border:5px solid #470b0b;
}

So let’s calculate the real width and real height!

Width

500px (width) + 40px (left and right padding) + 50px (left and right margin) +
10px (left and right borders) = 600px total width

Height

250px (height) + 40px (top and bottom padding) + 50px (top and bottom margin) +
10px (top and bottom border) = 350px total height.

So why does this matter in the end? It’s good to know the exact height and width of an element because if your webpage or a particular area within a webpage is dependent on being a certain height then you’ll want/need to know the exact height of other elements to make sure your web page is exactly how you intend. Sometimes elements can overlap, move in extreme places, and other assorted nasty things when you don’t know this real width and real height.

ID v. Class

As covered in the Basics of CS Tutorials, there are two main references ways we can essentially “link” a particular style to an HTML document, and PHP document, ASP, etc… They all operate in the same general fashion.

ID Selector

ID is called the “unique identifier element” and it is used to define a special case for an element. For example:

1
2
3
4
5
6
7
8
9
10
11
#selector {property:value;}
#footer-example {
     margin:auto;
     background: url("images/footerbg.gif") repeat-x bottom center;
     height:67px;
}

We are defining a special case for our footer

We can “call” this id by doing something like the following:

1
2
3
4
5
<div id="footer">
<p>Copyright &copy; 2009 AomicPages</p>
</div>

Why ID?

The reason why they chose ID is because an ID is unique to one person so two ID selectors CANNOT exist within the same html document. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div id="header">
<a href="#">Login</a>
</div>

<div id="header"> <!--INCORRECT-->
<ul style="float:left;">
     <li><a href="#">home</a></li>
</ul>
</div>

In the above example, you must choose one or the other to exist within the same document! If you wish to have multiple selectors within a document then you use the class selector. Click here to download a very small (2.98KB) zip archived example that I put together for the ID Selector!

Class Selector

A Class Selector is used when you have many elements that you want to apply styles to! You can technically create a web page with only class or ID selectors (although a combination of both is best).

So let’s say we have a cool looking box that we’re going to use multiple times on our web page. Obviously we don’t want to create a separate ID Selector for every instance that we use this nifty box. ID is the “Unique Identifier” and it can only be used once so…

1
2
3
4
5
6
7
8
9
10
11
12
<div id="nifty-box">
<p>Some text here</p>
</div>

<div id="nifty-box">
<p>Some more text here is not valid</p>
</div>

So two of the same ID Selector within the same page is NOT valid! Our solution is by using the Class Selector!

1
2
3
4
5
6
7
8
9
10
11
12
<div class="nifty-box">
<p>Some text here</p>
</div>

<div class="nifty-box">
<p>Some more text here IS valid!</p>
</div>

That is perfectly fine to do! In fact, this make things significantly easier when creating a web page because you can easily implement styles across your entire web site with just a few keystrokes. Instead a few dozen ID Selectors you can get the job done with one Class Selector! Click to download a very small file to see this in action!

class.zip
class.zip
Size: 3.3 KB

All about Inline CSS

I feel like we’ve been neglecting Inline CSS… We’ve talked about classes and id’s and all sorts of good stuff but we’ve left good old Inline CSS out of the equation! So we’re going to talk all about Inline CSS today!

So as we covered in the Basics of CSS there are three different types of CSS that we can use. If you don’t know a whole lot about CSS then you can still read on!

  1. External Style Sheets
  2. Internal Style Sheets
  3. Inline CSS

If we recall, an External Style Sheet is a separate document called something like “mystyle.css” and it is something that we need to link the HTML document to.
An Internal Style Sheet is when CSS is within the head tags of an HTML document and Inline CSS is CSS that is within than HTML element. CSS is inserted into an HTML element via the style attribute. It will look something like the following:

1
2
3
<tag attribute="CSS">content</tag>
<span style="color:#ff0000;">Red text</span>

Inline CSS is a blend of HTML and CSS, which means the rules for both languages applies! We need to write proper (X)HTML and proper CSS. If you are unfamiliar with the syntax rules for either of these then click here for XHTML OR here for CSS.

How exactly can we use Inline CSS?

The answer rests with how many properties you know how to use. You can use advanced positioning methods such as…

Note: if you aren’t familiar with these properties, needn’t worry. These are advanced techniques of positioning and they will be covered in later tutorials. This is purely for an exploratory purpose..

1
2
3
4
5
6
7
<a href="home.php">
<img src="images/home.png" height="50" width="100" alt="Home Page"
style="position:absolute; top:15px; bottom: 35px;" />
</a>

So we can have insane positioning properties and Inline CSS with a dozen or so properties if we wanted. It all really depends on what you need/want to accomplish. Keep in mind that most of these tasks can be accomplished by using ID or class elements so Inline CSS is usually a last resort. Also, remember that ORDER COUNTS! External Style Sheets < Internal Style Sheets < Inline CSS. So External Style Sheets have the last priority when applying styles. Internal Style Sheets will override External Style Sheets and Inline CSS will override both External and Internal Style Sheets.

One excellent example of using Inline CSS is if you have a blog such as wordpress or blogger! If you’re writing posts then you’re using HTML and CSS whether you are aware of it or not! If you’re using wordpress and you’re using the visual tab for new posts and you just want to change the color of some text then you’re using Inline CSS (check the HTML tab to see)! Let’s say, for instance, you wish to change a certain amount of text to pure white. You could write the following:

1
2
3
<p style="color:white;">White text</p>
<span style="color:#ffffff;">White text</span>

Remember that “P” stands for “Paragraph” and “span” doesn’t do anything unless you set it otherwise… So pure white text would look like this entire sentence!

1
2
3
<span style="color:#ffffff;">This was used to make
pure white text</span>

Inline CSS and Links

What if you want to have a unique link on our page for some reason? Then we could easily use Inline CSS of course! How about this unique link?

Even though that link is extremely overdone, we can see how it differs from the other links on the page. Let’s examine this link we created.

1
2
3
4
5
<a href="http://atomicpages.net/blog/2009/10/25/all-about-inline-css/"
target="_blank" style="font-weight:bold; text-decoration:overline;
font-size:16pt; color:blue;">this unique link</a>?

So we have our link and we can see it is linking to a page (this post actually) and it is opening a new window OR tab (depending on your web browser settings) via target=”_blank”. Then we have our Inline CSS that is making the font bold, overline, large, and then finally blue in that order.

So we can see there are multiple ways we can use Inline CSS and use it effectively. Generally, Inline CSS is used for one-time occasions such as that fancy link we created. Perhaps you need that image to be up a pixel higher and you don’t want to impact everything else. Example:

1
2
3
4
5
6
7
<div id="img">
<img src="images/img.gif" height="16" width="16" alt="Img"
style="position:relative; bottom:1px;" />
</div>

This will move this one particular image up by 1 pixel AFTER applying the id=”img” properties in the External Style Sheet OR Internal Style Sheet (remember order). It would be wise to keep the use of Inline CSS to a minimum and do as much as you can in an External or Internal Style Sheet; however, don’t be afraid to use it if you’re in a situation that calls for it! All about Inline CSS.