css positioning questions

Thread Solved

Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

css positioning questions

 
0
  #1
Feb 20th, 2009
i just read a couple of threads regarding css positioning and i have found something some where a while ago. however, i couldn't find any good explanation of it. would anyone please explain a bit of some of these code? thanks

1. clear:both?
2. margin: 0; padding: 0 // when it is just margin without left or right, how does it work? is padding here just like the table padding?

3. margin-left: auto; margin-right: auto; padding: 0 // why would setting margin-left and margin-right both auto make the whole thing center?

if someone would explain more about how margin work it would be great. i never had any good explanation about css positioning.

thanks thanks
HTML and CSS Syntax (Toggle Plain Text)
  1. .cenx{text-align: center}
  2. .ceni{clear:both}
  3. .bxfix{margin:0; border:none; padding: 0;}
  4.  
  5. .cenx{text-align: center}
  6. .ceni{clear: both}
  7. .bxcen{margin-left: auto; margin-right: auto; border: none; padding:0}
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: css positioning questions

 
0
  #2
Feb 20th, 2009
I was going to be a pompous ass and point you to the W3C site... but it's written in Martian so I'll try to humanize it here. =)


clear: both
If you have a couple of floated elements, say div1, div2, and div3, their arrangement will depend on how much content each div has.
For example, if all divs have a simple "Hello world" in them, most likely, the divs will lie horizontally beside each other on a page. Adding a clear attribute to them specifies that a certain side of that floated div shouldn't have certain floated elements beside it:

HTML and CSS Syntax (Toggle Plain Text)
  1. #div1, #div2, #div3 {display: block; float: left;}
  2. #div2 {clear: left;}

Instead of the divs lying horizontally, div2 and div3 will now lie in a row below div1. This is because you specified that div2's left side shouldn't have left floated elements beside it.

(In truth, the clear: right declaration stumps me. So you need to have someone else explain that)

Specifying clear: both is like saying: I don't want floated elements on BOTH sides of this element. So divs 1, 2, & 3 will lie on different rows altogether.

when it is just margin without left or right
The margin property is a shorthand for the top, right, bottom, left margin properties. It can take a maximum of four values:
HTML and CSS Syntax (Toggle Plain Text)
  1. margin: 1.0em 2.0em 3.0em 4.0em;

Is like saying:
HTML and CSS Syntax (Toggle Plain Text)
  1. margin-top: 1.0em;
  2. margin-right: 2.0em;
  3. margin-bottom: 3.0em;
  4. margin-left: 4.0em;
... but it's shorter. =) The order is important. If you have four values, they are specifying the margins in this order: top-right-bottom-left. It's like a clock starting at 12 and moving clockwise.

I did say it was a maximum of four values... you can actually specify just 1 value. In which case you're saying that all sides are to have that margin:
HTML and CSS Syntax (Toggle Plain Text)
  1. margin: 0;
... which you will often see is a quick and simple way of saying: I want NO margin on ALL sides. It's not limited to setting margins to 0. You can also use it for other values like margin: 2.0em which is like saying all margins are 2.0em.

If you use two values for the margin: margin: 1.0em 5.0em it's equivalent to saying: top and bottom margins are 1.0em --- left and right margins are 5.0em

The three value margin ie margin: 0 1.0em 2.0em specifies that:
top-margin is 0
right-margin is 1.0em
bottom-margin is 2.0em
left-margin is 1.0em


This also works for padding vs padding-top, padding-right, etc.


why would setting margin-left and margin-right both auto make the whole thing center
Ah... the centering trick. =)
It's more known in this form margin: 0 auto which, from the discussion, translates to:
0 margin for top and bottom*
'auto' margin for left and right

*you can totally change this too; it's the left and right values set to auto that's important

Why does that center the element?
Say you have an 800px containing block and an element 400px wide. You have 400px of space between your containing block's edges and the 400px-element. Specifying a value of "auto" for left and right margins is like telling them to be good kids and split up the extra-space between themselves. This will give a (400/2=) 200px margin on BOTH sides (left and right) of your element.

This will work even if you don't know the original width of your containing block since the value of "auto" will just split the remaining space between the left and right sides of your element. (If the container is 1000px, the effective margins will be 600/2 = 300px on each side)

is padding here just like the table padding
Hmm.. I'm not sure what you meant by that but padding SHOULD BE (*hint IE) the "cushion" between your container's boundaries and the content themselves. Whereas margin is the "space" between the boundary of your element and that of another's.

I guess the cellpadding definition kind of fits, except that the padding property is not limited to table cells.
Last edited by kanaku; Feb 20th, 2009 at 1:42 pm. Reason: =P
If you know ASP, you can save other daniweb members from idiots like me by helping out in this forum.

Visit this thread
if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 227
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

Re: css positioning questions

 
0
  #3
Feb 23rd, 2009
thanks kanaku, that cleared up many of my confusions.

very good explanation. = )
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: css positioning questions

 
0
  #4
Feb 23rd, 2009
You're most welcome. =)

(I'm still hoping someone will clear up the issue with clear: right though)
If you know ASP, you can save other daniweb members from idiots like me by helping out in this forum.

Visit this thread
if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,210
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: css positioning questions

 
0
  #5
Feb 23rd, 2009
Originally Posted by kanaku View Post
You're most welcome. =)

(I'm still hoping someone will clear up the issue with clear: right though)
Doesn't this allow other items (such as text) to flow on the left side?
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: css positioning questions

 
0
  #6
Feb 25th, 2009
Doesn't this allow other items (such as text) to flow on the left side?
Thank you for the reply. =)


Yes, it doesn't "clear" the left side, allowing elements to float beside it. But it doesn't always clear the right side either. =(

So I'm wondering what it's for, since it doesn't clear the right side the same way clear: left works for the left.
If you know ASP, you can save other daniweb members from idiots like me by helping out in this forum.

Visit this thread
if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the HTML and CSS Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC