CSS Design: Relative vs Absolute

Posted by Suni on July 31st, 2007

What is the difference between this:

<style>
div {
position: absolute;
left:350px;
top:250px;
}
</style>

and this:

<style>
div{
position: relative;
left:350px;
top:250px;
}
</style>

The placement of these two divs will be rather different. Absolute is easy enough to explain. You will have your div show up in the same place on the page no matter what the size of the browser window. It will always and forever be in the exact same position.

Relative, however, is a bit more complicated than that. You are moving whatever the div contains in relation to where it would have been if you never gave it a position. So if I was to put 2 div boxes up, they would be one right after the other going down the left side of the page. If I then move the 2nd div to relative, and tell it where to land, it will move over in relation to where it USED to be.

The best description I could find as to how these two things work was in this CSS Layout website. I hope it helps to explain what the difference is between the two attributes and how you can use them to your advantage.

CSS Design part 3b: Images in DIVs

Posted by Suni on July 17th, 2007

This is a pretty easy lesson in what to do with DIVs and images. It is all about how you want to place your elements within the page. DIVs are always broken up into layers. Each layer can be displayed in a certain order to make a page look the way you expect it to. Let’s say, for instance, that you have a picture that you want overlain by the text. You can do that with the z-index tag. This tag tells the internet how to display the layers of your page.

Let’s play around with this. I am going to take this picture:
 Which constitutes this code being placed between the body tags:

<div id=”image”>
<img src=”http://test.blogulalmighty.com/wp-content/uploads/2007/07/testtitle.gif”>
</div>

and create some text to go over it. In this case, I am going to use the words “Bloggers R Us”.

<div id=”pictext”>
<h3>Bloggers R Us</h3>
</div>

The code that goes between the style tags looks like this:

div#image {
position:absolute;
left:350px;
top:20px;
}

div#pictext{
position:absolute;
left:350px;
top:20px;
}

This renders a page that looks like:

test 3

Now we are going to take control over where the text shows up. The code between the style tags now changes to look like this:

div#image {
position:absolute;
left:350px;
top:20px;
z-index:2;
}

div#pictext{
position:absolute;
left:350px;
top:20px;
z-index:1;
}

You will notice that the text has disappeared now. It is actually BEHIND the picture. Switch the z-index numbers and see what happens now. The text shows up again. The z-index works up from the bottom. The first layer is the background and it builds up like a tower from there. The higher the number, the higher the precedence in order on the screen.

I was going to talk about the relative attribute, but I will save that for next week. Don’t forget to check out the code we have worked with on the test page. Ctrl-U to see the code.

Happy coding!

Basic CSS part 3a: DIV tags

Posted by Suni on July 10th, 2007

So now we are getting into the fun stuff in CSS. This is a bit more complicated, but once you see how it works, you will never go back to using boring tables again.

If you recall, we are using labels to get things done in the stylesheet. As with the a attribute, div tags are just another label that call to different parts of your website. Think of the div tag as giving you a little box to work with. You can make that box as big or as small as you need it to be. It gives you versatility in where things are placed on your page. You no longer have to rely on the basic left, right, and center attributes. Now you are free to place things pretty much anywhere you want inside a webpage.

For example, we will take our test page and move a few things around. I have uploaded the test page here so that you can view the source (ctrl + u) for yourself.

I have placed our text inside the <div> tag within the page. Now I can manipulate the position and formatting of the elements inside the div box. I have also given the <div> a name or ID. You can name yours whatever you want. It will look something like this:
<body>
<div id=”main”>
<p>your text</p>
</div>

I have also added a new box of text so that you can see how I am moving the boxes around. This new <div> I have called left, so the code looks something like this:

<div id=”left”>
<p> some more text</p>
</div>

Can you guess what we are going to do next? Since we gave these labels ids, it is easier for us to manipulate them individually. We will use the id to call to the different parts and move them around. Right now our page looks like this:

test2.JPG

Now I am going to move the boxes around a bit. I will leave the <div id=”left”> on the left and move <div id=”main”> over some. The stylesheet now changes. There are a few ways you can go about moving things. You can make the position of your box relative to the other elements before it or you can make them absolute, meaning they will stay there no matter how big or small the viewer makes their screen. Those are the two most common ways to place div elements on the screen.

Now we are going to call them from the stylesheet.

<style>
body {
background: black;
color:yellow;
}

a {
color:green;
}

div#main {
position:absolute;
left:475px;
top:345px;
}

</style>

Now we have moved the “main” element over and down the page, so the “left” element has moved up into its place. Why? We did not tell it what to do so it automatically goes into “relative” mode and moves to where the original “main” element was being held.

Now take a look at the test page to see what has happened. Next time I will talk about how to add images in with the div tag and what the relative attribute actually does.

Basic CSS part 2

Posted by Suni on July 3rd, 2007

Last time, we talked about the basic aspects of background color, link color, and text color. This time we are going to go further into the attributes you can change within the text and links.

We open up our test page once again and find our stylesheet coding at the top of the page. Now we are going to change the font style. Right now, it is the basic mundane Times New Roman. That is the default font face. In order to change that, we have to be familiar with other common fonts and their names. We want to make sure that we use a common font so that our page will render similarly on different computers.

The most common font faces are Times New Roman, Verdana, Arial, Georgia, and Courier. Choose one of these to change your font face to and place this code within your styles:

<style>
body {
background: black;
color:yellow;
font-face:Verdana;
}

a {
color:green;
}
</style>

The same style can be placed in with our styles for the link if you want the links to appear in a different font than your text.

a {
text-decoration:none;
}

Then, if you want to get REALLY fancy, you can even take the underline out of your link or make different link phases look different. For instance, if you want to make all the link phases different colors you would do something like this:

a:hover {
color:blue;
}

a:visited {
color:orange;
text-decoration:underline overline;
}

a:active{
color:purple;
text-decoration: dotted;
}

Put that in where your style sheet is, and discover what you have done by hovering over your link, clicking on it, and watching the colors and decorations change.

Happy coding!

Intro to Basic CSS

Posted by Suni on June 26th, 2007

Welcome to the first in a line of tutorials on how to change CSS coding on your own. Fun, right?

What is CSS?

It stands for Cascading Style Sheets. Style sheets are an easier way to maintain and change color, font, and even pictures on your sites. You upload an additional all text file with design rules that will tell your webpages what to do. There are rules that can be applied to all parts of your website, or only a few. It’s up to you how much CSS you want to include into your site.

Okay, so how do we change stuff?

The very first things everyone seems to ask about changing are background color, link color, text color, and fonts. I will go through each one and explain how the CSS translates from HTML so you may better see why this language is so popular.

In HTML, the <body> tag used to house all your rules for background colors and usually the overall text color. It used to look like this in your page:

<body bgcolor=”black” text=”yellow”> which would render a page like this:

black1.JPG

Now we would use something similar, but placed and worded slightly different. The following would be put either in its own file, or at the top of the webpage you are working on. If you are only changing a few aspects, it is easier to leave the styles right on the page you are working with. If you end up having a long style sheet, then it is best to put all the style into their own text file. So now that we have decided to keep our styles within our page, all we need to do is find the right place within the code to add our style list.

Look for this at the top of your HTML coded page:

<html>
<head>
<title>Whatever your webpage is called</title>
</head>
<body>

We will add our style between the <head> </head> tags like this:

<html>
<head>
<title>Whatever your webpage is called</title>
<style>

</style>
</head>
<body>

Now we are ready to add in our styles, removing them one by one from the actual HTML in the <body> tag or wherever else these rules have been placed.

First, let’s change the background color. We will go from <body bgcolor=”black”…> to this:

<style>
body {
background: black;
}
</style>

The body tag tells the stylesheet where to apply the styles. You leave <body> but delete the bgcolor=”black”. Why? The <body> tag is a label. Everything in the stylesheet must match up with a label tag somewhere in your webpage or it will not show up correctly.

Sidenote: I am using the color name, but you can also use the hexadecimal number in its place. Black is #000000. White is #FFFFFF. There are lists of hexadecimal colors all over the internet. My favorite one is here.

Next we will want to change the color of the text within the box. It’s fairly simple. All you need to do is take font=”yellow” and place it in the stylesheet like this:

<style>
body {
background: black;
color: yellow;
}
</style>

Finally, we can change the colors of the links in the document with a simple addition of another rule.

<style>
body {
background: black;
color:yellow;
}

a {
color:green;
}
</style>

The a is derived from the link code <a href=”link.html”> where a is seen as the label.

black2.JPG

Next time we will get more into depth about the different aspects that you can change within these two style rules.

Happy coding!


Copyright © 2007 Bloggers R Us. All rights reserved.