| | |
css positioning questions
Please support our HTML and CSS advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 227
Reputation:
Solved Threads: 0
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
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)
.cenx{text-align: center} .ceni{clear:both} .bxfix{margin:0; border:none; padding: 0;} .cenx{text-align: center} .ceni{clear: both} .bxcen{margin-left: auto; margin-right: auto; border: none; padding:0}
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. =)
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
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
Specifying
The
Is like saying:
... 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:
... 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
If you use two values for the margin:
The three value margin ie
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.
Ah... the centering trick. =)
It's more known in this form
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)
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.
•
•
•
•
clear: both
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)
#div1, #div2, #div3 {display: block; float: left;} #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
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)
margin: 1.0em 2.0em 3.0em 4.0em;
Is like saying:
HTML and CSS Syntax (Toggle Plain Text)
margin-top: 1.0em; margin-right: 2.0em; margin-bottom: 3.0em; margin-left: 4.0em;
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)
margin: 0;
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.0emThe 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
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
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.
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.
You're most welcome. =)
(I'm still hoping someone will clear up the issue with
(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.
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.
•
•
•
•
Doesn't this allow other items (such as text) to flow on the left side?
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.
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.
![]() |
Similar Threads
- Positioning advise & help (HTML and CSS)
- javascript css background-image (JavaScript / DHTML / AJAX)
- CSS questions (HTML and CSS)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- starting the first page (Site Layout and Usability)
- Hyperlink a div (JavaScript / DHTML / AJAX)
- fancy borders with images... ? (HTML and CSS)
Other Threads in the HTML and CSS Forum
- Previous Thread: Flash Movie Clip Problem! Simple help needed
- Next Thread: pages
| Thread Tools | Search this Thread |
appointments asp background backgroundcolor beta browser bug calendar cart cgi code codeinjection corporateidentity css design development displayimageinsteadofflash dreamweaver emailmarketing epilepsy explorer firefox flash form format google griefers hackers hitcounter hover html ide ie7 ie8 iframe image images internet internetexplorer intranet iphone javascript jpeg layout macbook maps marketshare microsoft mozilla multimedia navigationbars news offshoreoutsourcingcompany opacity opera optimization pnginie6 positioning problem scroll seo shopping studio swf swf. textcolor timecolor titletags url urlseparatedwords visual visualization web webdevelopment webform website windows7






