Subscribe to RSS

Using negative margin in CSS

There are a lot of times when you want something to just be a few pixels higher than it is. Unfortunately for you the container of that element starts to soon. So you need to move it outside of the box. Your options in CSS? You can either use relative positioning or you can use negative margin. I’ll go over in another article so for now we will discuss the handy ability to use negative margin on objects.Using negative margin definitely isn’t complicated but most people don’t even know it is possible. The idea is simple, instead of using a positive number to assign the margin of an element you can use negative margin to take that element outside of its container. This can be handy for all kinds of things, especially if you are just moving an object a few pixels.

1
#headerContainer {margin-top: -4px;}

It is as simple as that and you can adjust an object to be nearly anywhere you want it to be. The only issue you may run in to is that the object you are trying to place may not be a block-level element and therefore may have trouble getting it to work cross-browsers without floating it or assigning its display attribute to block.

1
#headerContainer {margin-top: -4px;display: block; width: 800px;}

Sorry for the short and sweet post, spent too much of the weekend migrating my blog over to Wordpress and testing everything to make sure it works. I’ll try and post another CSS article later today, everything is frozen here in Oklahoma so I think I’ll be staying home to avoid car wrecks and curb accidents.

Popularity: 7%

Whats Next?

  • Save to Delicious! Save the page

One Response to “Using negative margin in CSS”

  1. dustin brewer - web design news and style » Blog Archive » Using relative positioning in CSS Says:

    […] any cross browser issues that may develop from its use. This article is a complimentary article to using negative margin to position objects. With both of these ideas in hand you become a much more powerful designer and can move towards […]

Leave a Reply


Recent comments

I’ve been on a vacation, normality will happen next week

On August 28, 2008, holiday travel wrote:

well this is useful… (at least for me) very thanks ————&# 8212;———R 12;—–...

Top 10 search engine optimization techniques

On August 26, 2008, ибп wrote:

А что, неплохо!

Styling the first post of your Wordpress blog

On August 21, 2008, cheap website design wrote:

good point you have in there! thanks for sharing with us

CSS Hack:Getting Safari to behave

On August 21, 2008, Glass wrote:

You can also target Safari with the following: body:first-of-type .class {} That is: just prepend css the rule with body:first-of-type. Simple.

Styling the first post of your Wordpress blog

On August 20, 2008, jeeremie wrote:

Hi, This solution didn’t work for me. Instead, I use this one »