About the doubts encountered by css in layout

<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="renderer" content="webkit">
  <title>Document</title>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
    }

    .item {
      display: inline-block;
    }
    .bk1 {
      display: inline-block;
    }
    .box {
      border: 1px solid -sharp000;
    }
  </style>
</head>

<body>
  <div class="box">
      <span class="bk1"></span><div><span class="item">1</span><span class="item">2</span><span class="item">3</span><span class="item">4</span></div>
  </div>
</body>

</html>

the screenshot rendered according to the above page structure and style is as follows

, .bk1 block inline



I don"t know why a blank space is left at the top of the dispaly: inline-block
the size of this element is also 0 * 0 through the console, and there is no margin.
I don"t know how to explain this

.
Oct.09,2021

when the child element is inline-block, the height of the parent element is always higher. For a normal height, you can use it with float, or set font-size:0


on the parent element.

this is a bit confusing to explain. Here is my experiment and summary: block put inline-block in inline-block regardless of whether this inline-block has content or not, it will align this inline-block with baseline of block .

  • experiment 1:

    • Code:

        <div style="border:1px solid">
          <span style="display:inline-block"></span>
        </div>
    • effect

      clipboard.png


    • span0
  • 2:

    • :


    • :

      clipboard.png


    • span3xspanxbaselineinline-blockvertical-aline
  • 3:



    • clipboard.png
    • :
      blockinline-blockblockbaseline
  • 4:

    • :



    • clipboard.png
    • set font-size of container block to 0, then font-size of span is also 0, and baseline alignment is on the other side, but it also affects other child elements
    • .

Summary: inline-block is placed in block , regardless of whether this inline-block has content or not, it will align this inline-block with baseline of block .


this height is caused by line-height.
is like: an empty div, has a height of 0, but if you write a word in it, you will have a height; but this height is not supported by the text, but determined by the line height.

Menu