Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

css - Why centering with margin 0 auto works with display:block but does not work with display:inline-block ?

Just a quick question that was bugging my mind : Why centering with

margin:0 auto

does work fine with

display:block

but does not center a div when display is set to

display:inline-block

Thanks for answers

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

My understanding is as follows (though I am happy to be corrected).

  • Inline elements do not have a width property, and so the "auto" cannot be calculated.
  • Block elements have a width property, so the width of the "auto" can be calculated.
  • Inline-block elements have an outside which acts inline, but an inside which acts like a block. As such, the width set acts more like the width of a word in an inline element.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...