tags:

views:

44

answers:

5
+1  Q: 

css margin problem

My css looks like this

* {
     margin: 0;
     padding: 0;
}
body {
    background-color: #FFFFFF;
}
div#header {
    background-color: #969696;
    height: 80px;
}
div#mid-bar {
    background: url(images/home.jpg) left no-repeat #6f6565;
    height: 200px;
}

#searchbox {
    background-color: #c1c1c1;
    width: 200px;
    height: 180px;
    margin: 10px 20px 10px 350px;
}

and my html

    <div id="header">

    </div>
    <div id="mid-bar">
        <div id="searchbox">

        </div>
    </div>

preview

you can see the problem. the space between header and mid-bar which is created due to the margin given in the searchbox div. i want this margin for searchbox within the mid-bar div... and not from header div.

+1  A: 

Give padding to #mid-bar instead of searchbox margin

Chinmayee
+4  A: 

I's a known bug: would use padding instead of margin. so:

div#mid-bar {
    background: url(images/home.jpg) left no-repeat #6f6565;
    height: 200px;
padding-top: 10px;
}


#searchbox {
    background-color: #c1c1c1;
    width: 200px;
    height: 180px;
    margin: 0px 20px 10px 350px;
}
pixeline
that has filled in the gap... till now so good... but sth else gone wrong. background: url(images/home.jpg) left no-repeat #6f6565the image home.jpg is 200px in height. due to background color when padding applied to mid-bar, i have 6f6565 color and my image is cut in 200px of height. rest 10px height of padding is filled with the background-color :(
booota
give searchbox's height : 190px;
pixeline
+1  A: 

I have seen this happen when you don't give margins to parents and the first element, even a child that you give margin to, causes gaps in the parents by creating margins. One way I've overcome this is by using paddings on the parent containers instead of margins.

See your example here with paddings: http://jsbin.com/ememi3

Moin Zaman
+1  A: 

If you are intent on using margins, try setting margin:0; in #mid-bar. Otherwise give #mid-bar a padding-top:10px; and remove top margin from #searchbox.

Freyr
A: 

Everyone seems to agree on this one, padding will work much better then margins will. I looked into it a little and it seems Pixeline is right, it's a known bug in Firefox, hopefully they will fix it in 4.

Blake
padding tweak working fine. Though there was a problem with the image specified in mid-bar. i.e. background: url(images/home.jpg) left no-repeat #6f6565;This image was 200px high, I changed it into 220px of height, so that the space created by padding can be filled. This was my approach to tweak the problem.
booota
That should work, glad your problems fixed.
Blake