Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative Margins, Hack or Not?

Tags:

I've come across various posts where people are stating that negative margins are hacks. Thus I've strayed away from using them with exception to particular cases when I can justifiably use them, like throwing text off the screen with margin:-9999px

Question, are negative margins a hack and when is it right to use negative margins? I've seen them used in the 960 Grid System with, .push and .pull, any other cases where they are justifiably usable?!?!

like image 523
Alex Avatar asked Aug 20 '10 19:08

Alex


People also ask

Is it good to give margin in negative?

It is possible to give margins a negative value. This allows you to draw the element closer to its top or left neighbour, or draw its right and bottom neighbour closer to it.

Is it OK to give negative margins CSS?

Negative paddings are not allowed (and don't work) in css, only negative margins.

Can negative values be allowed in Box property?

Negative values for margin properties are allowed, but there may be implementation-specific limits. These properties set the top, right, bottom, and left margin of a box.

Can we give negative values in margin & padding?

No. Padding only takes positive values. Negatives are ignored or treated as 0, which would have the same effect: none. Margins can have negative values, as can other position related properties, but not padding.


2 Answers

Negative margins are allowed based on W3C specifications. I have used them to let things overhang their container.

like image 133
Dustin Laine Avatar answered Sep 20 '22 22:09

Dustin Laine


I would argue it's not a hack: It's defined, logical behaviour; it's supported in all browsers; it can be expected to be supported in future versions of browsers (the absence of future support is the most important argument against hacks).

Also, as @durilai points out, they are officially sanctioned (although with limits) by the W3C.

This Smashing Magazine article supports the case.

like image 41
Pekka Avatar answered Sep 22 '22 22:09

Pekka