This is a discussion on "How/when do I use "clear"?" within the Web Page Design section. This forum, and the thread "How/when do I use "clear"? are both part of the Design Your Website category.
|
|
|
|
|
![]() |
||
How/when do I use "clear"?
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
How/when do I use "clear"?
Can someone help me to better understand how (and where/when) to use "clear"? I'm reworking my message board code (again - to eliminate the table wrapper) and I know I'm going to need to use "clear". I could probably get it working via trial and error, but that would just be luck and perseverance - I'd rather own the knowledge.
Here's a mockup of what I'm doing, but if it's not clear, please refer to the attached screenshot.
The wrapper div for all of this is positioned relative. Divs 1 and 6 are exactly 472px wide, and divs 2-5 have their widths set so that they add up to 472px. Divs 2 and 4 use a background image (repeat-y) to create the sides of the neon frame. I'm guessing I'll need to use clear(:left?) on div 2, and clear(:right?) on div 5 and maybe even clear:both on div 1 and/or 6. But I'm not sure about any of that - and I certainly don't have a good understanding of how and why this works. |
|
|
|
#2
|
|||
|
|||
|
Re: How/when do I use "clear"?
Let me give you a short answer. If this doesn't shed light on your problem, I'll be happy to look harder at your diagram! (wow.. can't believe you did that diagram)
Clear is used following a float. If you float a div the next div will either need to be a clear div or another div that has the clear: both in it. What happens is if you float something left, the next element wants to fill in the empty space to the right of that float. You actually in that case would only need clear: left (I think*) but most everyone uses both to be safe. |
|
#3
|
|||
|
|||
|
Re: How/when do I use "clear"?
Thanks for the help. OK, that makes sense. (I suppose it does shed light on my problem, but it sure doesn't make my code work!)
So, what's the difference between using floats and clears vs. display:inline/block? |
|
#4
|
|||
|
|||
|
Re: How/when do I use "clear"?
This is word for word from my book!
Display: Quote:
Quote:
Clear: Quote:
|
|
#5
|
|||
|
|||
|
Re: How/when do I use "clear"?
Generally a clear is used to keep a footer and/or the bottom of a container below floated elemnts regardless of height differences.
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
|
|
#6
|
|||
|
|||
|
Re: How/when do I use "clear"?
I really appreciate all the help!
Quote:
Assuming I want to stack 2 divs (DivA and DivB) side-by-side, using float:left... If I use clear:left on DivB, that doesn't mean that it will line up below DivA - it will still line up to the right of DivA - it just won't wrap below DivA. But that's not right, is it? My own testing disproves that. I tried adding display:inline to DivA, but DivB still lined up below (rather than beside) DivA. But if I use float:left on both and don't use clear on either, DivA doesn't extend to 100% of the height of the container div - which I need it to do (its selector has a background image property with repeat-y.) I should probably add that I can't specify an exact height for the divs with the bgimage, because the message text element (not shown - it's part of the "I'll do this later" section!) will vary in height. I can't figure out how to make this work - with any combination of float/display/clear. I'm attaching a screenshot for further clarification and I'll post my code. I hope someone can help me figure out what I'm doing wrong here. It's making me :crazy: !
|
|
#7
|
||||
|
||||
|
Re: How/when do I use "clear"?
Quote:
Quote:
In my example the only time you should ever need to "clear: both;" will be in the footer. "display: inline;" should be used on list items when they need to be displayed horizontally, not when floating columns. The default display attribute should be fine. I don't think you need to have display in your CSS. Hope this helped! Last edited by BGarner; Apr 12th, 2007 at 02:06. |
|
#8
|
|||
|
|||
|
Re: How/when do I use "clear"?
Then what about "Prevents an element from wrapping around a floated element. Instead the cleared element drops below the bottom of the floated element."
Quote:
Quote:
Last edited by Donny Bahama; Apr 12th, 2007 at 04:10. |
|
#9
|
|||
|
|||
|
Re: How/when do I use "clear"?
OK, BGarner, I tried your code and it worked. (Mostly.) The difference is, I was putting clear:left in DivB's selector - you put it under #DivA.
That made a big difference, and the divs stacked (side-by-side) as I wanted. However... Once I populated DivB with a fair amount of 'lorem ipsum', DivA did not extend to the bottom of DivB - even with 'height:100%'. (I added a dotted border, using a different color for each div, so I could see what was doing what.) Also, the wrapper div did not extend to the bottom of DivB - until I put in an empty div with a class selector with a clear:both property. (This was placed just before the closing </div> tag of the wrapper div.) If I was able to bring the wrapper down below DivB using a clearing div, there ought to be some way of doing the same with DivA. Hmmm... |
|
#10
|
|||
|
|||
|
Re: How/when do I use "clear"?
Oh. I get it. I'm in css-equal-column-height hell (thanks to IE.) Looks like it's back to a table wrapper for me.
|
|
#11
|
|||
|
|||
|
Re: How/when do I use "clear"?
You got it Donny! That's where the faux columns comes into play. Back to the wrapper and throw in your two uneven columns!
You are a good student! hahaha |
|
#12
|
|||
|
|||
|
Re: How/when do I use "clear"?
Gee, thanks!
And the most valuable lesson I learned: When you have something that's working perfectly well, save it aside along with the stylesheet before you start mucking with it! :swear: Why do I always have to learn lessons the hard way? |
|
#13
|
|||
|
|||
|
Re: How/when do I use "clear"?
If it makes you feel better, I've only learned things the hard way too!!!:lolabove:
|
|
#14
|
|||
|
|||
|
Re: How/when do I use "clear"?
CSSEdit has MIlestones =). Handy for tweakage and no need to save multiple files/copies.
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
|
|
#15
|
||||
|
||||
|
Re: How/when do I use "clear"?
I'm glad you worked that out.
I don't know of one person who has not had to learn something the hard way. It's like initiation in the web design world. Welcome to the club! (We have jackets!) |
|
#16
|
|||
|
|||
|
Re: How/when do I use "clear"?
Quote:
Quote:
Really? Ya mean it?! I'm IN?!?!?! WOOT! |
|
#17
|
|
|