Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

html - What is the difference between inline-block and inline-table?

As far as I can tell, these display selectors seem to be identical.

From the Mozilla CSS documentation:

inline-table: The inline-table value does not have a direct mapping in HTML. It behaves like a <table> HTML element, but as an inline box, rather than a block-level box. Inside the table box is a block-level context.

inline-block: The element generates a block element box that will be flowed with surrounding content as if it were a single inline box (behaving much like a replaced element would).

It seems that whatever could be done with inline-table can be done with inline-block.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Both inline-block and inline-table have an inline outer display role. That means

The element generates an inline-level box.

The difference is that

However, in most cases, inline-table will behave like inline-block because of anonymous table objects:

Generate missing child wrappers:

  • If a child C of a 'table' or 'inline-table' box is not a proper table child, then generate an anonymous 'table-row' box around C and all consecutive siblings of C that are not proper table children.
  • If a child C of a 'table-row' box is not a 'table-cell', then generate an anonymous 'table-cell' box around C and all consecutive siblings of C that are not 'table-cell' boxes.

Therefore, if your inline-table element has non-tabular content, that content will be wrapped in an anonymous table-cell.

And table-cell has a flow-root inner display model, just like inline-block.

But if the inline-table has tabular content, it will behave differently than inline-block.

Some examples:

  • Inside an inline-block, cells with non-tabular separator will generate different table anonymous parents, so they will appear at different lines. Inside an inline-table, it will be the separator who will generate a table-cell parent, so they all will appear at the same row.

    .itable {
      display: inline-table;
    }
    .iblock {
      display: inline-block;
    }
    .cell {
      display: table-cell;
    }
    .wrapper > span {
      border: 1px solid #000;
      padding: 5px;
    }
    <fieldset>
      <legend>inline-table</legend>
      <div class="itable wrapper">
        <span class="cell">table-cell</span>
        <span class="iblock">inline-block</span>
        <span class="cell">table-cell</span>
      </div>
    </fieldset>
    <fieldset>
      <legend>inline-block</legend>
      <div class="iblock wrapper">
        <span class="cell">table-cell</span>
        <span class="iblock">inline-block</span>
        <span class="cell">table-cell</span>
      </div>
    </fieldset>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...