5px extra margin getting added to the bottom of each div
Asked Answered
T

4

12

I am trying to design a simple header to a page in css. I planned to stack two divs on top of each other. The top one has many tabs and the bottom one is a plain solid single image div. But when rendering i see that an extra 5px is getting added to the the heights of both these divs. So i am not able to place the bottom on exactly on top of the other one.

There is a 5px bottom margin automatically. I tried negative margins, reset the global margins and paddings to zero. Still no use.

Heres the code.

<div class ="main_nav">
 <div class="first_tab">
 <img src ="images/startup/tab1_brown.png" height="25" width="90" alt="Temp" /> 
 </div>
 <div class = "ind_tab">
 <img src ="images/startup/tab1_orange.png" height="25" width="90" alt="Temp"/>
 </div>
 <div class = "ind_tab">
 <img src ="images/startup/tab1_brown.png" height="25" width="90" alt="Temp" />
 </div>
</div>
<div class="lock">
 <img src ="images/startup/divbg_new.png" alt="Temp" />
</div>

CSS:

 *{ margin:0; padding:0; }
ul.master_navigation
{
    font-size: 125%;
    font-weight: bold;
    text-align: center;
    list-style: none;
    margin: 0.5em 0;
    padding: 0;
}

ul.master_navigation li
{
    display: inline-block;
    padding: 0 1%;
}

a
{
    color: #08f;
    font-weight: bold;
    text-decoration: none;
}

a:visited
{
    color: #88f;
}

a:hover
{
    color: #f00;
}

p
{

    text-align: justify;
}


p.cent
{
    text-align: left;
}

div.header
{    
    height: 200;
}

div.main_nav
{ 
    display: inline-block;
    height: 25;

    width: 900;
    padding: 0;
    margin: 0;
    vertical-align: bottom;

}

div.first_tab
{    
    height: 25;
    float: left;


}
div.ind_tab
{    

    height: 25;
    float: left;
    margin-left: -10px;
    z-index: -5;

}

div.lock
{
    margin-top: -100;
    height: 91;
    width: 900;
    padding: 0;
    margin: -5;

}

body {

    width:900px;
    max-width: 100%;
    margin: auto;
    padding: 0;
    background-image:url(images/startup/bg_2.gif);
    background-repeat:repeat-x;
}

.pad 
{
    padding: 0;
    margin: 0;

}

Here's the link to the page

http://net4.ccs.neu.edu/home/pradep/

Ive been spending too much time on this. Please help.

Thebaid answered 19/1, 2012 at 2:46 Comment(1)
next time whn u post a question organise your code well. The div with its appropriate CSS under it. No need to paste the entire CSS. Welcome to stackoverflow .Kaciekacy
O
6

You need to specify units on your CSS declarations.

http://jsfiddle.net/m7YCW/

div.main_nav
{ 
    display: inline-block;
    height: 25px;       /* set px */
    width: 900;
    padding: 0;
    margin: 0;
    vertical-align: bottom;    
}

Learn to make use of your developer tools in Chrome, if you had right mouse buttoned on the elements and chosen -> inspect it would bring up the dev tools. You can then view the 'metrics' and 'computed styles' areas to see that main_nav was rendering as 30px instead of 25px and also that there was a warning symbol next to the 25 css declaration and it was being explicitly dropped.

Orchard answered 19/1, 2012 at 2:53 Comment(3)
It worked. That was such a basic thing to do. But caused too much of a problem . Thanks a lot. Ill never forget to add px from now on.Thebaid
*You need to specify units on your CSS declarations. * - You have the following CSS code: width: 900; :)Metathesis
@DaveRook - it's a cut/paste of his own code. He needs to set units on all his declarations. I only pointed out the first one.Orchard
C
38

Ege's answer was very useful for me. I have spent hours to find the reason of a bottom padding of some images in div's. It was the line-height. Set it to 0px on the interesting areas:

.divclass {
    line-height: 0px; /* crucial for bottom padding 0 !!! */
}
Conscionable answered 7/2, 2012 at 22:41 Comment(2)
Such an easy solution, but such an annoying CSS aspect!Ossifrage
I had an image with a height of 47px, but wrapping div was 52px, with the extra 5px added to the bottom. This was the solution that worked for me - nw the div is also 47pxFunderburk
P
7

I think your problem is the line-height. Yup, there it is. Just added line-height:0, on firebug and they stuck together.

The thing about inline-blocks is that they behave just like any inline text, you also have a similar issue on the navigation below, because you pressed enter in your code, it will render it as a non-breaking space and add extra x margins to the right and left sides as well. X here will depend on the font size.

In order to solve this, you can either close and open tags on the same line like below:

<div>
.
.
.
</div><div>
.
.
.
</div>

or you can set the font-size and line-height to 0, but thats not always possible if you don't have other selectors inside that div.

Philipps answered 19/1, 2012 at 3:4 Comment(0)
O
6

You need to specify units on your CSS declarations.

http://jsfiddle.net/m7YCW/

div.main_nav
{ 
    display: inline-block;
    height: 25px;       /* set px */
    width: 900;
    padding: 0;
    margin: 0;
    vertical-align: bottom;    
}

Learn to make use of your developer tools in Chrome, if you had right mouse buttoned on the elements and chosen -> inspect it would bring up the dev tools. You can then view the 'metrics' and 'computed styles' areas to see that main_nav was rendering as 30px instead of 25px and also that there was a warning symbol next to the 25 css declaration and it was being explicitly dropped.

Orchard answered 19/1, 2012 at 2:53 Comment(3)
It worked. That was such a basic thing to do. But caused too much of a problem . Thanks a lot. Ill never forget to add px from now on.Thebaid
*You need to specify units on your CSS declarations. * - You have the following CSS code: width: 900; :)Metathesis
@DaveRook - it's a cut/paste of his own code. He needs to set units on all his declarations. I only pointed out the first one.Orchard
A
5

Try setting vertical-align:bottom on the images.

Ahner answered 19/1, 2012 at 2:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.