scriptygoddess

31 Mar, 2004

CSS: What's the difference

Posted by: Jennifer In: Lessons learned

Sorry for the placeholder, my plan is to fill in the answers. (If I get the answer wrong, please correct me! As well, feel free to answer any of them in the comments – if you do, I'll move your comment into the body of the post – with credit (and then delete the comment to clean up) (Actually there was an answer by Tom (anonymous) in the comments – I'll leave his there, and try to put it in my own words – maybe then I'll get it! LOL!)

Question
What's the difference between static, relative, absolute, fixed positioning. And what scenario would be a good example for use of each?

static
I'm guessing this is the default. It's "position" is as it is in the html. If it's the first thing in the html, it'll show up at the top. If it's the last thing in the html, it'll show up at the bottom… (barring any other styling of other elements around it)
Uses? got me. Doesn't seem like you WOULD use this specifically.
relative
This block still shows up where it is in the html – but you can modify it's positioning… move it over a little, using top, right, bottom, and left elements.
Those additional elements move it over based on it's "container" – which could be the page itself – or another layer. If that "parent" positioned over to the right of the page 300 pixels. Then you give this relative positioned layer left: 20px…technically it would bee 300 + 20 over from the left of the page. It's position is relative to that container.
absolute
This removes the layer from the rest of the flow of the page and pastes it where you specifically want it to go. It's position is determined with height, width, top, right, bottom, and left elements.
fixed
Would be cool if IE supported it. This fixes an element on the page – much like absolute, but with the addition of it not scrolling. I don't see my using it much un less IE magically learns what it is

Question
what's the difference between inline vs block display (and some of the other options for display)? good examples for use? (I know that display: none hides stuff. That's about it)

block
An example of a well-known "block" element – <p> – there's space before and after it – once you put that </p> – carriage return – block ends.
inline
Doesn't generate a carriage return at the end – (<span> tag is typically displayed inline, same for <b> etc..) If you wanted to really mess with the world you could go and specify p { display: inline; } and then suddenly p tags would cease to have a carriage return. (Note: I'm not recommending you do this. I would imagine reading paragraphs that bleed one into another would be just a TAD difficult to read.
uses: got me!

Question
Float?? What's the best use for this – doesn't this fight against positioning?

(Incidentally – I just tried to find the answers to those questions in this book, and I DO believe my eyes are starting to bleed… The definitions made NO sense. Don't get me wrong – Eric Meyer is great… except for those particular definitions)

14 Responses to "CSS: What's the difference"

1 | Tom

March 31st, 2004 at 9:44 pm

Avatar

Hope this helps…

Question
What's the difference between static, relative, absolute, fixed positioning. And what scenario would be a good example for use of each?
static is just normal. It's there. Relative is relative to it's static position. It can overlap things. Absolute is pulled out of the flow of content, and overlaps things. Fixed stays in it's position on the page when you scroll. Otherwise, it acts like absolute positioning. Careful, doesn't work in IE because of its poor support of standards.

Question
what's the difference between inline vs block display (and some of the other options for display)? good examples for use? (I know that display: none hides stuff. That's about it)
inline is in a line. :) what that means, is if there are multiple inline elements, they will be next to each other. Ex: strong and em. Block will return the next element to the next line. block fills up the whole containing element unless you specify a width. Also, you cannot specify a width or height for an inline element (but you can do line-height, I think).

Question
Float?? What's the best use for this – doesn't this fight against positioning?
Best use for this is alignment.

Fight against positioning? I'm not sure what you mean, but it does make the element pulled out of the flow of content. Sometimes you need to clear your floats. Floats can be very troublesome, and problems get worse when you test out your site in IE. Here's more information about floats: http://www.complexspiral.com/publications/containing-floats/

2 | Jon

March 31st, 2004 at 10:02 pm

Avatar

You should read the HTML and CSS guides at http://www.htmldog.com/

I know I mentioned it before, but I really think it will help you. Read them all in order. Obviously the Beginning HTML you can get through really quickly. But I would still read it because the author makes a point of talking about which tags are no longer good to use.

You can probably read all 6 tutorial in a couple hours. It doesn't give the depth that Eric's book does but I don't remember any sudden onsets of eye bleeding. :)

3 | wcoco

March 31st, 2004 at 11:06 pm

Avatar

I've been wanting to know this! There's *nowhere* i could find a proper explanation and not causing futher confusion! Thank you for this.

4 | Jennifer

April 1st, 2004 at 8:07 am

Avatar

I ran out of time last night – but my point about "float fighting against positioning" is if you specify position AND float … what happens? When I get more time to night, I'll experiement and see if I understand what's happening enough to explain it in a "definition" :)

5 | jayseae

April 1st, 2004 at 8:10 am

Avatar

Your position stuff is just about right. Personally, I don't think that I use any of them.

I also don't really use inline or block – but they can be useful. Say you want to display a heading as the beginning of your paragraph – but then continue text as normal, instead of adding space around it. Change your heading (say, <h1>) to display: inline, and it won't break when it hits it. You could also use a span, styled with the right size information, to do this, and it might be more semantically correct – this is why I don't use the block/inline options. But you can do it that way.

Contrary to the others, I find float very useful. It allows you to "float" an element to the left or right of other elements. Take a look at my page. The main entires have a float (of left). That means that it stays on the left side of the enclosing container, and other elements in the same container, like the sidebar, slides up next to it on the right.

Float indicates where the element ends up in the display – in this case, on the left of my page.

When you use float, make sure you pay attention to your widths. For instance, if you have a page that is 800px wide, and a float: right element that is 300px wide, your remaining elements cannot be more than 500px (actually something like 490px is probably accurate, with padding and margins and borders) or it won't look like it floats. The float moves an element to the side, and allows space for the other elements to move up the page next to it. But if the other elements aren't wide enough, they won't.

The biggest problem with floats is that browsers support width differently – seems to mostly be because of margins and padding and such. So make sure you test it!

Using borders helps, as it can display the border of each element, so you see exactly how the interact with one another.

6 | Vincent

April 1st, 2004 at 10:01 am

Avatar

Other uses for inline:

You can make lists inline. This can be useful in creating standard templates for pages, and then making the same code for your main menu either go down a left hand collum or across the top of the page.

float:

I use float all the time for blocks. Lets say you have 5 or 6 blocks of text (say, each of them have the last 5 posts in a particular category) if you float each one left (or right) then they'll fit as many as the can across the page, then move down below. As the user resizes the page, they'll refloat.

7 | Billy

April 1st, 2004 at 5:47 pm

Avatar

A small point about relative positioning: In your example, your element will appear 320px from the left of the page, but there will still be a hole on the left side of your page where that element fits into the flow of the document.

Basically, a relatively positioned element is positioned statically first, then repositioned according to the rules in your CSS. But, this does not effect any other elements on the page. They stay exactly where they are.

8 | Jennifer

April 2nd, 2004 at 8:24 am

Avatar

One thing I noticed looking at some code – a useful display:inline; usage is on the form tag (depending on your layout) it's driven me crazy in some layouts that form is a block element…

9 | Jesus Piña

April 3rd, 2004 at 10:28 am

Avatar

I was in the same predicament as you are and I found thsi tutorial about positioning that I think could be very helpful. In my opinion this the one tutorial that really gets me closer to understanding positioning.
Just my 2 cents.

10 | Jesus Piña

April 3rd, 2004 at 10:29 am

Avatar

Sorry I forgot to put the link.
http://brainjar.com/css/positioning/
Take a look at it and see what happens.

11 | sosa

April 5th, 2004 at 2:03 pm

Avatar

i've found float position to be so much useful than absolute. But it can be so hard to understand in the first time. Floatutorial is an very good step-by-step resource for learning floats http://css.maxdesign.com.au/floatutorial/.
Also you could check Listamatic and Listutorial to see how can block and inline display property work in making unordered lists in navigation bars. 😀

12 | Julik

April 15th, 2004 at 11:34 am

Avatar

To onderstand the difference between 'display:block' and 'display:inline' you have to know a few things.
Basically, HTML elements are divided into 'inline' and 'block' elements. Block elements include DIVs, lists, list items, paragraphs, headings, blockquotes etc.
If you put, say, 2 consecutive block elements in a page they will reside one UNDER each other (this is what happens if you put a few headings one after another. If you assign a border to them, you will see that they span from one side of the containing block (body is a block too BTW :) to the other. This is the behaviour for blocks.
Inline elements, in contrast, are positioned like words in a paragraph – one after the other, this is what happens if you put, say, 15 links one after the other in your code – they will follow each other and flow to the next line when there is little space.
The 'display' property allows you to override the default display of a particular element – for example, if you make the <li> tag inline – the list items will go one after the other like words in a paragraph.
And vice-versa – if you make the a tag block – it will occupy it's own line in your layout.
Overriding these properties is very useful for configuring custom backgrounds of elements (for example, left-padding property for an inline element gives you padding ONLY in the first line of this element, while padding-left on block element gives you padding for the whole box.

13 | Code Novice

April 8th, 2004 at 12:46 am

Avatar

CSS Defs & Lorem Ipsum
Hey, who knew there was Imitate Jenni Week? 😉 I…

14 | Position important text logically using CSS » SEO News, Articles & Opinion

November 22nd, 2004 at 4:23 pm

Avatar

[…] Related External Links: How to's: absolute positioning Mimicking Magazine Layouts in CSS Lessons learned: CSS: What's the difference Floating Points […]

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements