2

when I use display = inline-block to my div, there is a gap between these 2 boxes. can anyone tell me why it is like this, and how can I remove the gap?

* {
  margin: 0;
  padding: 0;
}
.first {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block;
}
.second {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block;
}
<div class="first"></div>
<div class="second"></div>
Chao Wang
  • 49
  • 4

6 Answers6

3

Use float: left;

* {
  margin: 0;
  padding: 0;
}
.first {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block; float:left
}
.second {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block;float:left
}
Sowmya
  • 26,684
  • 21
  • 96
  • 136
2

set font-size: 0 for parent element.

.parent-element {  /* apply to the parent element */
  font-size: 0;
}

.first, .second {
     font-size: 13px; /* default value, change as per your need */
}
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
Suresh Gogu
  • 359
  • 5
  • 12
1

Change your html like blow

other way add Comment like blow

* {
  margin: 0;
  padding: 0;
}
.first {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block;
}
.second {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block;
}
<div class="first"></div><div class="second"></div>

<div class="first"></div><!--
--><div class="second"></div>
Lalji Tadhani
  • 14,041
  • 3
  • 23
  • 40
0

@Chao Wang for removing that gap you have to use left float in bot the div

<style type="text/css">
*{
  margin: 0;
  padding: 0;
}
.first {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block;
  float:left;
}
.second {
  height: 100px;
  width: 100px;
  border: solid 1px black;
  display: inline-block;
  float:left;
}

</style>
<div class="first"></div>
<div class="second"></div>

It will resolve your problem (y)

lazyCoder
  • 2,544
  • 3
  • 22
  • 41
0

The "inline-block" elements has this space because of font size of the parent. Here you can find more details and ways how to remove the space. One of the easiest is this to add a div parent with font-size=0:

<div style="font-size: 0;">
<div class="first"></div>
<div class="second"></div>
</div>
Corina Gheorghe
  • 198
  • 1
  • 9
0

This happen beacause it use line height

.inline-parent{
  display:inline-block;
  width:100%;
  line-height:0;
  font-size:0;
}
.inline1{
  display:inline-block;
  width:50%;
  background:#333;
  line-height:1;
  font-size:15px;
}
.inline2{
  display:inline-block;
  width:50%;
  background:#999;
  line-height:1;
  font-size:15px;
}
<div class="inline-parent">
<div class="inline1">
text text
</div>
<div class="inline2">
text text
</div>
</div>

It is all because of line-height and font-size

ma_dev_15
  • 1,176
  • 7
  • 18