TextMate snippet for clearing floats
update: in the midst of internet connection problems, I managed to double post. Please check out the first post.
Liquid error: undefined method `source' for nil:NilClass Liquid error: undefined method `url' for nil:NilClass
Is this your first visit? You may want to subscribe to the feed.
For those pesky times when whole numbers just won’t cut it, but you only want some precision.
7.7.round(0.5) #=> 7.5
7.95.round(0.5) #=> 8
8.2.round(1.5) #=> 7.5
8.3.round(1.5) #=> 9
The magic, courtesy of Daniel Morrison:
class Float
def round(round_to = 1.0)
mod = self % round_to
rounded = self - mod + (mod >= round_to/2.0 ? round_to : 0)
rounded % 1 == 0 ? rounded.to_i : rounded
end
end
Note that do to some quirks with Ruby’s handling of floats, you won’t get what you expect in some situations:
3.5.round(0.2) #=> 3.4, instead of 3.6
update: in the midst of internet connection problems, I managed to double post. Please check out the first post.
I often use positioniseverything.net’s CSS method for clearing floats without structural markup. The code for this technique is not something that I’ve committed to memory yet, so I’m referencing the article all the time (I could just copy it from another project, but it’s just so much quicker to type “css clear float” into google).
Well, positioniseverything.net is about to get several less page hits per week, because I finally threw the code into a TextMate snippet and assigned it the keyword “clearfix”.
${1:.clearfix}:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
$1 {display: inline-block;}
/* Hides from IE-mac \*/
* html $1 {height: 1%;}
$1 {display: block;}
/* End hide from IE-mac */
$0
Update: DRYed up based on Jacob’s comment.