Html 5 Strange Img Always Adds 3Px Margin At Bottom

HTML 5 strange img always adds 3px margin at bottom

This problem is caused by the image behaving like a character of text (and so leaving a space below it where the hanging part of a "y" or "g" would go), and is solved by using the vertical-align CSS property to indicate that no such space is needed. Almost any value of vertical-align will do; I'm fond of middle, personally.

img {
vertical-align: middle;
}

jsFiddle: http://jsfiddle.net/fRpK6/1/

Three extra pixels at the bottom of the page unaccounted for and not sure how to get rid of them?

Set your img's style to include display: block; or vertical-align: middle;

http://jsfiddle.net/85a6a52t/1/ (See very bottom of CSS panel)

#slideshow img { display: block; }

or

#slideshow img { vertical-align: middle; }

Reference to this good answer that explains issue

Image inside div has extra space below the image

By default, an image is rendered inline, like a letter so it sits on the same line that a, b, c and d sit on.

There is space below that line for the descenders you find on letters like g, j, p and q.

Demonstration of descenders

You can:

  • adjust the vertical-align of the image to position it elsewhere (e.g. the middle) or
  • change the display so it isn't inline.

div {  border: solid black 1px;  margin-bottom: 10px;}
#align-middle img { vertical-align: middle;}
#align-base img { vertical-align: bottom;}
#display img { display: block;}
<div id="default"><h1>Default</h1>  The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""></div>
<div id="align-middle"><h1>vertical-align: middle</h1> The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div> <div id="align-base"><h1>vertical-align: bottom</h1> The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""> </div>
<div id="display"><h1>display: block</h1> The quick brown fox jumps over the lazy dog <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/VangoghStarry-night2.jpg/300px-VangoghStarry-night2.jpg" alt=""></div>

Strange padding between image and a span

There is a strange padding because there is a space between the <img> and <span> in the html source.

<div>
<img src="http://placehold.it/100x100" />
<span>hello</span>
</div>

Removing the space would eliminate the padding.

<div>
<img src="http://placehold.it/100x100" /><span>hello</span>
</div>

But that's probably not what you are after (read on). As per your second question, the reason 100% doesn't work is because the <div> isn't given a height and there is nothing to base the percentage height on. The <div> height here is the result of the height of its contents. It takes the height of the taller element so that it can accommodate both.

So what you are actually looking for is display: table. Placing the image and text side by side is very easily achieved with tables.

div {
display: table;
}

img {
display: table-cell;
padding:20px;
background: red;
}

span {
display: table-cell;
width: 300px;
background-color: blue;
vertical-align: top;
height: 100%;
}

See demo here.

HTML 5 strange img always adds 3px margin at bottom

This problem is caused by the image behaving like a character of text (and so leaving a space below it where the hanging part of a "y" or "g" would go), and is solved by using the vertical-align CSS property to indicate that no such space is needed. Almost any value of vertical-align will do; I'm fond of middle, personally.

img {
vertical-align: middle;
}

jsFiddle: http://jsfiddle.net/fRpK6/1/

Cant remove padding between nav-bar and banner css

You need to do two things. 1) set the vertical alignment on your image to top so it removes the gap reserved for descender text elements, and 2) remove the margin on your <ul>

@import 'https://fonts.googleapis.com/css?family=Work+Sans:400,500,900';nav {  padding-top: 0px;  position: fixed;  width: 100%;  transition: top 0.2s ease-out;}
.banner { text-align: center; width: 100%; box-shadow: inset 0px -1px 0px 0px rgba(0, 0, 0, 0.13);}
nav ul#menu { padding-left: 0; display: flex; padding-top: 0px;}
nav ul li { flex-grow: 1;}
.nav-bar { /* Rectangle 1: */ background: #FFFFFF; box-shadow: 0px 2px 2px 0px rgba(0, 0, 0, 0.11), 0px 4px 6px 0px rgba(0, 0, 0, 0.11); width: 100%; text-align: center;}
//-------------------------------------------------------
/*Strip the ul of padding and list styling*/
ul { list-style-type: none; margin: 0; position: absolute;}

/*Create a horizontal list with spacing*/
li { display: inline-block; float: center; margin-right: 1px;}

/*Style for menu links*/
li a { display: block; min-width: 140px; height: 50px; text-align: center; line-height: 50px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; color: #fff; background: #2f3036; text-decoration: none;}

/*Hover state for top level links*/
li:hover a { background: #19c589;}

/*Style for dropdown links*/
li:hover ul a { background: #f3f3f3; color: #2f3036; height: 40px; line-height: 40px;}

/*Hover state for dropdown links*/
li:hover ul a:hover { background: #19c589; color: #fff;}

/*Hide dropdown links until they are needed*/
li ul { display: none;}

/*Make dropdown links vertical*/
li ul li { display: block; float: none;}

/*Prevent text wrapping*/
li ul li a { width: auto; min-width: 100px; padding-left: 0px; padding-right: 0px;}

/*Display the dropdown on hover*/
ul li a:hover+.hidden,.hidden:hover { display: block;}

/*Style 'show menu' label button and hide it by default*/
.show-menu { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; text-decoration: none; color: #fff; background: #19c589; text-align: center; padding-left: 0px; padding-right: 0px; display: none;}

/*Hide checkbox*/
input[type=checkbox] { display: none;}

/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked~#menu { display: block;}

/*Responsive Styles*/
@media screen and (max-width: 760px) { /*Make dropdown links appear inline*/ nav ul#menu { position: static; display: none; } /*Create vertical spacing*/ li { margin-bottom: 1px; } /*Make all menu links full width*/ ul li, li a { width: 100%; } /*Display 'show menu' link*/ .show-menu { display: block; }}
.hero-image { /* The image used */ /* Set a specific height */ height: 50%; width: 100%; /* Position and center the image to scale nicely on all screens */ background-position: center; background-repeat: no-repeat; background-size: cover; position: relative; vertical-align:top;}
#menu {margin:0;}
<nav>  <div class="banner animated"><img class="hero-image" src="https://picsum.photos/1080/200/?random"></div>  <div class="nav-bar"> <label for="show-menu" class="show-menu">Show Menu</label>    <input type="checkbox" id="show-menu" role="button">    <ul id="menu">      <li><a href="#">Home</a></li>      <li>        <a href="#">About</a>      </li>      <li>        <a href="#">Portfolio</a>      </li>      <li><a href="#">News</a></li>      <li><a href="#">Contact</a></li>    </ul>  </div></nav>

strange spaceing under svg in html

You can visit this link to know why img tag always has a white space at bottom.

In this case, just add style="display:block;overflow:hidden;background:white" attribute to your svg tag.

<a href="/firm-profilepl/homepl/indexpl" style="    padding: 0;    margin: 0;    overflow: hidden;    background: red;    display: block;    height: 100%;">  <svg style="display:block;overflow:hidden;background:white" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" enable-background="new 0 0 512 512" height="512px" id="Layer_1" version="1.1" viewBox="0 0 512 512" width="512px" xml:space="preserve" style="background: #FFF"><path d="M497.913,497.913c-18.782,18.782-49.225,18.782-68.008,0l-84.862-84.863c-34.889,22.382-76.13,35.717-120.659,35.717  C100.469,448.767,0,348.312,0,224.383S100.469,0,224.384,0c123.931,0,224.384,100.452,224.384,224.383  c0,44.514-13.352,85.771-35.718,120.676l84.863,84.863C516.695,448.704,516.695,479.131,497.913,497.913z M224.384,64.109  c-88.511,0-160.274,71.747-160.274,160.273c0,88.526,71.764,160.274,160.274,160.274c88.525,0,160.273-71.748,160.273-160.274  C384.657,135.856,312.909,64.109,224.384,64.109z"/></svg></a>

IMG has 5px extra padding at bottom of div

use the following css: working jsFiddle

img{display:block;}

Images are by default displayed inline - which causes the padding below the image. (because of line-height)



Related Topics



Leave a reply



Submit