Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Margins and negative margins

Tags:

html

css

margin

I've always thought I understood margins and negative margins but apparently I don't! I've just started a new design and running into problems already.

I have a div (hill3Cont) and another div (hill3Hill) nested inside, and this is the CSS for them.

#hill3Cont
{
    width: 100%;
    background-image: url("images/grass.jpg");
}
#hill3Hill 
{
    margin: -100px 0 0 0;
    height: 600px;
    width: 100%;
    background: url("images/hill3.png") no-repeat center top;
}

I have applied a negative margin to the top of the child div in the hope it will push this content up outside the boundaries of the parent div. But it doesn't, it doesn't move. If I apply a positive margin it pushes the parent div down along with the child inside it.

The only way I can make it behave properly is use a border or overflow hidden on the parent div, but the design won't allow for either of these (I don't want a border and overflow hidden hides the child).

Maybe it's just been a long day and I'm missing something common! Many thanks in advance.

Edit: I have got a solution, I've put a single padding top on the parent div, padding: 1px 0 0 0. It works for my design so I'm happy but still keen to find out what's happening.

like image 894
Chris Avatar asked Apr 21 '11 21:04

Chris


People also ask

What is a negative margin used for?

Typically, negative margins are used to make visual adjustments, to manage workarounds for centering liquid designs in layout, or to offset specific elements outside the box in which they are contained (see Example 11-3).

How do you calculate a negative profit margin?

You can calculate a negative profit margin using the same equation as the profit margin: If the percentage is negative, you have a negative profit margin. To calculate, follow these steps: 1. Find your net income Before calculating profit margin, it's important to identify your net income.

What is the difference between bottom and top margin?

Setting the bottom margin indicates what distance you want below the current block. Setting a negative top margin indicates that you want negative spacing above your block. Negative spacing may in itself be a confusing concept, but just the way positive top margin pushes content down, a negative top margin pulls content up.

What are negative margins in Dreamweaver?

Negative margins are wholly supported across all modern browsers (and IE6 in most cases). It reacts differently when floats are applied. Negative margins are not your everyday CSS so they should be applied with care. Dreamweaver doesn’t understand it Negative margins don’t show up in the Design View of DW.


1 Answers

For child inside parent element use position relative and negative top instead.

http://jsfiddle.net/GEwj3/3/

#hill3Cont
{
    margin-top: 50px;
    width: 200px;
    height: 200px;
    background-color: red;
}
#hill3Hill 
{
    height: 50px;
    width: 100px;
    background: blue;
    position: relative;
    top: -20px;
}
like image 173
mrtsherman Avatar answered Sep 22 '22 23:09

mrtsherman