1

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.

3
  • Which browser are you testing in? This is a strange problem. Commented Oct 27, 2010 at 11:29
  • Yeah. If we add even space [&nbsp;] in #mid-bar before #searchbox in renders correctly in firefox atleast. Commented Oct 27, 2010 at 11:32
  • @Kyle Sevenoaks... firefox and IE both showing same Commented Oct 27, 2010 at 11:44

5 Answers 5

4

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

that has filled in the gap... till now so good... but sth else gone wrong. background: url(images/home.jpg) left no-repeat #6f6565 the 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 :(
give searchbox's height : 190px;
1

Give padding to #mid-bar instead of searchbox margin

Comments

1

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

Comments

1

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.

Comments

0

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.