Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: A 2-pixel line appears below image (IMG) element?

Tags:

html

css

image

Can anyone point out to me why there's a line (of background color, red in this case) below the image and above the navigation bar? Both Firefox and Chrome display the red line, so I assume it is rendered as intended. But I can't seem to find the issue through the developer tools. The borders, paddings and margins are all 0, which is puzzling. Here's the stripped down version of the code, or jsfiddle.net/bvss4/9:

  <body>
    <div id="main-wrapper">
      <header id="main-header">
        <img id="title-image" src="http://i.imgur.com/JaYSY.jpg" />
        <div id="main-navbar">
            STUFF
        </div>
      </header>
    </div>
  </body>

CSS:

* {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

#main-wrapper {
    width: 90%;
    margin: 0 auto;
    border: 3px solid #888;
    margin-top: 20px;
    background: red;
}

#title-image {
    width: 100%;
}

#main-navbar {
    width: 100%;
    background: #333333;
}

bad red line

like image 984
Dan7 Avatar asked Nov 29 '22 17:11

Dan7


2 Answers

Adding a display:block to your #title-image will fix it

 #title-image {
     width: 100%;
     display:block;
 }

JSFiddle here

like image 90
Brett DeWoody Avatar answered Dec 01 '22 07:12

Brett DeWoody


Set vertical-align: bottom on the image's CSS.

The reason it happens is because images are displayed inline with text. This means that they have to allow a small space below the line in case of letters like y, g etc that drop below the baseline and cause the space.

By setting the vertical-align to bottom, you move the image so that it's aligned with the bottom of the text, avoiding this problem.

There is one exception you should be aware of: If the image has less height than a single line of text, it will leave a gap above it to make room for that text, unless you set the containing element to have a line-height that works.

like image 37
Niet the Dark Absol Avatar answered Dec 01 '22 08:12

Niet the Dark Absol