Wednesday, 19 February 2014

CSS: Simple top and bottom margin hack.

Most of the web designers use very brilliant frameworks for setting up the UI, like Bootstrap, Foundation, etc. These frameworks save your great time, no doubt. Few days ago, working with Bootstrap, I observed that, though it has smart fluid grid, sometimes you have to set an element to an unusual position. You may want to leave a little top or bottom margin to a div, for example.

Bootstrap comes up with an offset option, which allows you to set the position of an element by few left or right margins. But by top and bottom margin? Nope! Bootstrap doesn't give you anything for top and bottom margins. Obviously, it is not much important when your elements are getting stacked nicely.

So, within few free minutes of my so-called "relaxing" web design work schedule (which my boss thinks I  have), I wrote a CSS, which you may find stupid, which includes 200 classes. Hundred for margin-top:0 to margin-top:100px, and same for margin-bottom

Sample Code

@charset "utf-8";
/* CSS Docu.ment */

Yes, you can see how stupid that is to write some line 200 times. But later this helped me, as I went on calling these classes into my templates, so that I don't need to write individual margin property in respective CSS.

Download margin.css

Also Read