3

I'm new to css. When I was learning I found something which made me confused.

I know if I not set the height of div,it will decide by what it contains.

Sample code:

div {
  border: solid red;
}
div.a {
  border: solid green;
  height: 10px;
}
<div>
  <div class="a">
    123<br/>
    123<br/>
    123<br/>
  </div>
</div>

Here Outermost div'height is up-to div.a's height,which is 10px.

But When I set div.a css as { display: inline-block;}

div {
  border: solid red;
}
div.a {
  border: solid green;
  height: 10px;
  display: inline-block
}
<div>
  <div class="a">
    123<br/>
    123<br/>
    123<br/>
  </div>
</div>

The height changed. It is up to div.a's content event it is overflow.

What's the display: inline-block effect.

Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
qslcna
  • 107
  • 1
  • 10

1 Answers1

0

I think your issue here is the combination of giving it a specific height and the overflowing content.

I have done some examples here that might help.

DEMO https://jsfiddle.net/r6n12325/:

HTML:

<div class="box1">
  <div class="box2">
    123<br>
    123<br>
    123<br>    
  </div>
</div>

<hr>

<div class="box3">
  <div class="box4">
    123<br>
    123<br>
    123<br>    
  </div>
</div>

<hr>

<div class="box5">
  <div class="box6">
    123<br>
    123<br>
    123<br>    
  </div>
</div>

CSS:

.box1 {
  border:1px solid #333;
}
.box2 {
  border:1px solid red;
}

.box3 {
  border:1px solid #333;
}
.box4 {
  height: 10px;
  display:inline-block;
  border:1px solid red;
}

.box5 {
  border:1px solid #333;
}
.box6 {
  height: 10px;
  display:inline-block;
  border:1px solid red;
  overflow:auto; /* could also use hidden */
}
Brad
  • 8,044
  • 10
  • 39
  • 50