Clever Geek Handbook

Random 5 px space at the bottom of the div

I have the following code in this fiddle :

Html

<div id="overview">
    <div id="banner">
        <img src="http://www.placehold.it/1200x600"/>
        <div id="info">
            <div id="identity">
                <span id="name">Name</span><br/>
                <span id="title">Title of Name g</span>
            </div>
        </div>
    </div>
</div>

      

CSS

* {
    box-sizing:border-box;
}
#banner {
    width:100%;
    position:relative;

}
#banner > img {
    width:100%;
}
#info {
    position:absolute;
    bottom:0;
    width:100%;
    text-align:center;
}
#identity {
    display:inline-block;
    text-align:left;
}
#name {
    font-size:2em;
    font-weight:bold;
}
#title {
    font-size:1em;
    font-weight:bold;
}

      

I have not tested this in any browsers besides Chrome yet, however added 5px is added to the height banner

. When you remove styles and elements, so it's only an image and the containing banner

whitespace is still present. I've included g in title

to demonstrate how problematic it is.

At first I thought it had something to do with the nature of inline elements and the ww browser considering line / tab breaks in the code as a space. However, condensing down to just one line, the problem remained.

Can someone please explain what is happening and how to fix it?

+3
html css


sharf May 07 '15 at 11:26 PM
source to share


2 answers


Make your image a box or inline:

#banner > img {
    width:100%;
    display: block;
}

      

See: http://jsfiddle.net/audetwebdesign/cf6vwy5h/

By default, images are inline elements with their bottom edge on the baseline. There is a small amount of space (leading) below the baseline to provide space for descenders of certain letters, such as "y" or "j".

* {
  box-sizing: border-box;
}
#banner {
  width: 100%;
  position: relative;
  border: 1px dashed blue;
}
#banner > img {
  width: 100%;
  display: block;
}
#info {
  position: absolute;
  bottom: 0;
  width: 100%;
  text-align: center;
  border: 1px dotted blue;
}
#identity {
  display: inline-block;
  text-align: left;
}
#name {
  font-size: 2em;
  font-weight: bold;
}
#title {
  font-size: 1em;
  font-weight: bold;
}
      

<div id="overview">
  <div id="banner">
    <img src="http://www.placehold.it/1200x600" />
    <div id="info">
      <div id="identity">
        <span id="name">Name</span>
        <br/>
        <span id="title">Title of Name g</span>
      </div>
    </div>
  </div>
</div>
      

Run codeHide result




Reference:

If you look at the CSS2 spec:

http://www.w3.org/TR/CSS21/visudet.html#line-height

read the section related to vertical-align

, in particular baseline

, which reads:

Align the baseline with the base of the original field. If the box does not have a baseline, align the bottom edge with the original baseline.

Images are replaced by elements and have no baseline; hence, their bottom edge is aligned with the baseline of the containing block, which gives a little vertical space below the image and the edge of the parent block.

+3


Marc audet 07 May '15 at 23:30
source to share


This issue is related to the layout img

, which is similar inline

, you can change it to block

to solve the problem:

#banner > img {
  width:100%;
  display:block;
}

      

JSFiddle



Or, you can also change the default img

vertical alignment from baseline

to top

, suppressing the top padding:

 #banner > img {
  width:100%;
  vertical-align:top;
}

      

JSFiddle

+3


potashin 07 May '15 at 23:30
source to share






More articles:

  • Android Custom Adapter - won't return a view? - android
  • Android 5.0 - Declaring custom permissions in a module - android
  • Docker on Mac: Unable to View Ports - docker
  • how to maintain transparency for JButtons (java) - java
  • Π Π°Π±ΠΎΡ‚Π° с нСсколькими ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°ΠΌΠΈ JSON Π² Ρ‚ΠΎΠΌ ΠΆΠ΅ Ρ„Π°ΠΉΠ»Π΅ с нСизвСстными ΠΈΠΌΠ΅Π½Π°ΠΌΠΈ ΠΊΠ»ΡŽΡ‡Π΅ΠΉ с использованиСм Jackson - java
  • How to execute a button click inside an Android web browser - android
  • https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/2222402/azkaban-pass-parameters-to-underlying-job-code&usg=ALkJrhhFU7NAGpXvynv7NkQVUZGbUYjIeA
  • Why is the HTML tag width smaller when serving with a local server? - html
  • Какая Ρ€Π°Π·Π½ΠΈΡ†Π° ΠΌΠ΅ΠΆΠ΄Ρƒ "Π΄Π²ΠΎΠΉΠ½Ρ‹ΠΌ ΠΏΠΎΠ΄Ρ‡Π΅Ρ€ΠΊΠΈΠ²Π°Π½ΠΈΠ΅ΠΌ" ΠΈ "ΠΏΠΎΠ΄Ρ‡Π΅Ρ€ΠΊΠΈΠ²Π°Π½ΠΈΠ΅ΠΌ Ρ…"? - syntax
  • Wildcard search with spaces

All Articles

Daily Blog | 2020

Green Geek Media (GGM)
1298 Despard Street
GA 30344 East Point, USA
404-763-3837