Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two <img> without spacing

Tags:

html

css

image

<html>
 <head>
  <meta name="viewport" content="width=device-width"/>
  <title>HAHAHA</title>
  <STYLE TYPE="text/css" media="screen">
  *
  {
   padding: 0;
    margin: 0;
  }
   body
   {
    padding: 0;
    margin: 0;
   }
   #flexbox
   {
    //display: -webkit-box;
    //-webkit-box-orient: horizontal;
    //-webkit-box-align: stretch;
    //width: auto;
   }
  </STYLE>
 </head>
 <BODY style="padding:0;margin:0;border:0;">
  <!--<div id="flexbox">
   <img src="1.jpg" width="100px" style="padding:0;margin:0;"/>
   <img src="1.jpg" width="100px"/>
   <img src="1.jpg" width="100px"/>
  </div>-->
  <img src="http://is.gd/kjxtj" style="padding:0;margin:0;border:0;"/>
   <img src="http://is.gd/kjxtj" style="padding:0;margin:0;border:0;"/>
 </BODY>
</html>

Why do these images always have a small space in between them, even if i have padding & margin set to 0 on body and all other elements in page?

OK this is the full unedited code.

EDIT: just found out its the line break between the two IMG. SO... i cannot press enter between the two elements? lol... :)

like image 455
denicio Avatar asked Jan 07 '11 18:01

denicio


People also ask

How do I remove the white space between two images in CSS?

HTML. Line Height Property: The CSS line-height-property can be used to set the height of the line, whose value is set to normal, by default. By setting the height of the container at 0%, the white space can be removed.

Why is there a gap between images in HTML?

99% of the time, this is due to your image not having the proper styling tag. Every time images are used next to each other in the cells of a table – no matter the width or height specifications of the cell – there should be a display block style added. This removes the white gap between images.


2 Answers

i cannot press enterin between the two elements? lol... :)

Nothing distinguishes one type of white space from another in most parts of HTML.

You can, however, format your code such:

<img src="..." alt="..."
><img src="..." alt="...">

… to remove the space between the elements.

like image 159
Quentin Avatar answered Sep 28 '22 08:09

Quentin


images are displayed inline by default, which is likely your issue. White-space characters in HTML are condensed, however they will still appear as a space if any exist between HTML elements.

The following HTML will render with a slight space between images.

<div class="images">
    <img src="http://placehold.it/400x300" />
    <img src="http://placehold.it/400x300" />
</div>

If you'd like to make them flush against each other, you could try floating the images:

img {
  float: left;
}

Floating comes with its own issues if you're new to CSS.

An alternative is to adjust the markup to get rid of extra white-space characters. A common way to do this is called "fishy" syntax:

<div class="images"
    ><img src="http://placehold.it/400x300"
    /><img src="http://placehold.it/400x300"
/></div>

The way it works is that the closing > character of each element is moved to just before the beginning < character such that there's no white-space within any HTML element as content. Instead, the white-spaces for indentation are within the HTML tags, where the white-space is ignored completely.

There was a w3c feature request for a new property on white-space, which could theoretically allow CSS to remove all spaces from an element. The orignal proposal was white-space: ignore; however I much prefer (and suggested) white-space: none;.

It appears, however, that updating white-space is not likely to happen, and instead the suggestion was to use flexbox to remove spaces appropriately:

extending off the original HTML example:
.images {
    display: flex;
}

Of course, it will be some time before flexbox has enough cross-browser support to be useful in a commercial environment, so for the time being I recommend sticking with the fishy syntax.

like image 22
zzzzBov Avatar answered Sep 28 '22 07:09

zzzzBov