Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DIV without padding "higher" than its contents

Tags:

html

css

height

I have a div containing only an image with a height of 400px. The div has no padding but it's height is 406px causing an ugly grey 6px horizontal stripe below its img.

The reason for the grey background is that comparable divs may contain a caption beneath their img.

What causes the extra 6px and how can I get rid of it?

P.s. I'm aware the HTML markup is not semantic/HTML5 but I'd rather not change it.

The basic markup is

<body>
<div>
    <div class='img w960'>
        <img src='timg-960-480.png' alt=''>
    </div>
</div>
</body>

The CSS for this example is

body>div{
font-size:20px;
width:26em;
margin:5em auto;
text-align:justify;
}

div.img{
border:0px solid #fff;
border-radius:.5em;
background:#ddd;
margin:1em 0;
width:1px;
overflow:hidden;
display:table;
}

div.w960{
position:relative;
left:-7em;
}

div.w960 img{
width:40em;
}


div.img h3{
margin:0;
padding:1em;
font-size:20px;
font-style:italic;
}

enter image description here

like image 923
RubenGeert Avatar asked Jan 11 '23 22:01

RubenGeert


1 Answers

Set line-height: 0 on your div.img. This will affect the image caption h3 but you can correct that with an extra CSS line. The image is set inline and sits on the text baseline.

body>div{
font-size:20px;
width:26em;
margin:5em auto;
text-align:justify;
}

div.img{
border:0px solid #fff;
border-radius:.5em;
background:#ddd;
margin:1em 0;
width:1px;
overflow:hidden;
display:table;
line-height: 0;
}

div.w960{
position:relative;
left:-7em;
}

div.w960 img{
width:40em;
}


div.img h3{
margin:0;
padding:1em;
font-size:20px;
font-style:italic;
}
like image 96
michaelward82 Avatar answered Jan 18 '23 07:01

michaelward82