Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2012, 01:07 AM   PM User | #1
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 860
Thanks: 68
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Floating vs positioning

Hello, I did search the forum before creating this topic but the thread I found was over 1 year old and there was a message saying its frowned upon to revive threads over 1 year old, so for that reason I created my own.

I am wondering whether I should be using floats or using position: relative/absolute to position things?

I float pretty much all my content but I wanted to know if positioning things make a site more flexible/compact. Not sure of the pro's and con's of each. I've always been advised to steer clear of positioning things absolutely, so I kinda dropped relative and never bothered to learn it.

Can anyone shed some light for this issue?

Kind regards,

LC.
__________________
Carewizard - http://www.carewizard.co.uk
LearningCoder is offline   Reply With Quote
Old 12-09-2012, 12:59 PM   PM User | #2
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Hmm, well it's one of those questions that is.. not the right question. All elements are positioned, the real seperation is between static (default) & relative positioning, in which elements must respect each-other in the element flow, and absolute & fixed positioning, in which they are removed from the element flow. All elements are following one positioning rule or another, so floating elements are positioned as well. However, you can't float absolutely positioned elements (I'm thinking fixed elements too, I've never checked).

It is like compairing a nail and a screw in a lot of cases, when trying to decide on how to position, or if you should float, since it depends on the job at hand more than anything. A good page will respect the element flow, don't abuse absolute positioning as the lazy option. If something seems like it should be part of the element cascade, then it probably should be! I dare say most elements should be, with the odd exception. If you take your time to fit the puzzle of elements together nicely in relation to each-other you will end up with efficient code, your pages will be less likely to fall appart, and of course it makes dynamic layout a lot simpler. Floating elements is still respecting the flow.
Custard7A is offline   Reply With Quote
Users who have thanked Custard7A for this post:
LearningCoder (12-09-2012)
Old 12-09-2012, 03:41 PM   PM User | #3
tempz
Regular Coder

 
Join Date: Jul 2012
Location: London
Posts: 436
Thanks: 4
Thanked 80 Times in 80 Posts
tempz is an unknown quantity at this point
Each to their own I guess. If the website looks great in all resolutions, browsers; just do what is easiest for yourself really.
tempz is offline   Reply With Quote
Old 12-09-2012, 03:51 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
The answer is: "it depends"! Here is a good article to read.

But to attempt to summarise..in VERY broad terms:

(always) give 1st preference to "static";
consider "float" to achieve desired layout or, for example, to float images within a larger textual-area;
then "relative", but mainly to supply a content for "absolute" and other positioned elements;
avoid "absolute" but, if used, tend to do so in the context of another (relatively) positioned element;
"fixed" is used rarely, but occasionally is used to fix a small element at one side of the browser window.

Specific layouts are mainly achieved using floats, but some layouts can also be achieved with a combination of relative and absolute positioning. Percentage and/or fixed widths on elements are also used to achieve a desired layout and behaviour. cf liquid layouts.

People sometimes resort to positioning of elements when they haven't fully explored the use of margins and padding to separate elements.

And avoid DIVITIS!

I may have confused rather than helped?? but the links are good
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-09-2012, 06:17 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You need to use position:relative and position:absolute when you want elements to overlap. The rest of the time floats generally work better.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-09-2012, 09:22 PM   PM User | #6
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 860
Thanks: 68
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Thank you very much for your replies.

After reading the link from Andrew, it still seems very much a gray area. I think the best way to learn would be putting it into practice and creating a simple site using positioning for everything, just to get a feel for it and see what the results are. It's definitely an area of CSS which I am lacking so i'd like to close the gap.

I'll keep updating the thread with any issues which arise but again thank you all very much for the replies. Seems there are many different opinions on it so it's definitely worth taking a stab at it for future projects. I can seem to get things where I need them using floats with ease but I'd like to try different approaches to gain the same effect.

Kind regards,

LC.
__________________
Carewizard - http://www.carewizard.co.uk
LearningCoder is offline   Reply With Quote
Old 12-09-2012, 10:49 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Seems there are many different opinions
I don't think this is the case, we've just emphasised different aspects of this topic.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-09-2012, 11:43 PM   PM User | #8
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 860
Thanks: 68
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Ok, so it's really down to my preference? I'm floating anything I need to at the minute to position it then just clearing the next element and floating, or leaving static, depending on what I need to do. Use fixed for things you want to be 'fixed' on the page or absolute when you need to do something a bit different, say overlapping it over another element?

Regards,

LC.
__________________
Carewizard - http://www.carewizard.co.uk

Last edited by LearningCoder; 12-09-2012 at 11:48 PM..
LearningCoder is offline   Reply With Quote
Old 12-10-2012, 12:11 AM   PM User | #9
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by LearningCoder View Post
Ok, so it's really down to my preference? I'm floating anything I need to at the minute to position it then just clearing the next element and floating, or leaving static, depending on what I need to do. Use fixed for things you want to be 'fixed' on the page or absolute when you need to do something a bit different, say overlapping it over another element?

Regards,

LC.
Yeah, kinda I think you will only obtain some clarity by exploring - putting in the time
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-10-2012, 10:25 AM   PM User | #10
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 860
Thanks: 68
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
When creating a site I generally go for that approach, creating a main container but I center it using margin: auto instead. Within there I usually create a heading div, either put the nav inside or create a separate div for a nav. Content div below and either the footer div inside or just outside the content div.

Got the full day now to create a simple page using the relative and absolute stuff so I'ma give it a go.

I feel I have a bit of a better understanding from before I started this thread so thanks for the replies!

I'll most likely run into issues so I'll probably post later. Will give that site another thorough read.

Regards,

LC.
__________________
Carewizard - http://www.carewizard.co.uk
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:03 PM.


Advertisement
Log in to turn off these ads.