CSS Basics

Basics of CSS Part - #8

0

Today we're going to continue our journey into the Box Model. If you aren't familiar with this box model, you should read about it!

Padding

Padding — defines the space between the element border and element content and will be the same color as the background color of the element.
If we recall that in the box model we have margins > borders > padding > content so Padding is tertiary. Let's assume you have an HTML paragraph element:

<!--No Padding-->
<p style="background-color:#fff;">Lorem ipsum dolor sit amet, 
consectetur adipiscing elit.</p>
<!--With padding-->
<p style="background-color:#fff; padding:10px;">Lorem ipsum dolor 
sit amet, consectetur adipiscing elit.</p>
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.

So what is different from the first line and the second line? The first line is without padding so the white background only fills to the end of the text instead the width of the box. The second line, however, spreads across the entire width of the box and it is a padding of 10px on all sides.

Padding works in very similar ways as the margin property except the margins are always invisible and the padding is based on the background of the element you are adding padding to.

Properties

Like margins, there are five possible properties we can use. They are the following:

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

The first four properties define each side for padding. For example, if you wanted to adjust the bottom and right padding for a certain element you could use the following:

p.element {
     padding-bottom:3px;
     padding-top:7px;
}

This will change the bottom padding of the element by 3px and the top padding of the element by 7px. Example:

  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Remember that padding is basically an extension of the background of an element and you also need to take into account any other elements (whether above, below, left or right) that might get in the way of this padding. In theory, you could make an entire HTML and CSS website using only margins, borders, padding, and content.

Values

  1. em, px, pt - numeric values e.g. 1em, 15px, 2pt
  2. percent % - percent of the containing element e.g. 10% of 500px is 50px.
span.padding {
     padding-top:2em;
     padding-bottom:10px;
     padding-right:16%;
}

All of the above are valid ways to define padding of our span element and class of padding.

Padding - All Inclusive

The all-inclusive padding property works the same way as the all-inclusive margin property. This singular padding property will define padding how ever we need it to using one property instead of writing out each side.

padding:15px 30px 45px 60px;
  • top padding - 15px
  • right padding - 30px
  • bottom padding - 45px
  • left padding - 60px

This example has four values for the padding property. The values start with padding-top and then go clockwise to padding-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.

padding:15px 30px 45px;
  • top padding is 15px
  • right and left padding is 30px
  • bottom padding is 45px

This might come as a surprise to us because you would think they would group the [top and right] and [bottom and left] segments separately, however; we can see in this example that the padding-top is 15px and the padding-bottom is 45px. The left and right padding is 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.

padding:15px 30px;
  • top and bottom padding 15px
  • right and left padding 30px

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

padding:15px;
  • top, right, bottom, and left padding is 15px

One single value for the padding property will set all padding to that value, in our case 15px.

Basics of CSS - Part #7

1

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:

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:

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.

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:

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

2

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:

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.

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.

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 right] 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.

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 bottom] 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.

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

box-model

Basics of CSS - Part #5

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:

<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..

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:

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

1

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:

#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:

<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:

<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...

<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!

<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
Go to Top