CSS: 正当中一个宽度为auto的块元素 (width:auto;margin:auto) center an auto-width div
CSS: 居中一个宽度为auto的块元素 (width:auto;margin:auto) center an auto-width div
for parent block or body - text-align:center
; for centerd block- display:inline-block
;
<style> body{text-align:center} .center{display:inline-block;background:red} </style> <div class="center"> <p contenteditable="true">write text</p> </div>
DEMO http://jsfiddle.net/RXP4F/
参考: http://stackoverflow.com/a/18524791/4484798
关于 inline-block 的用法, 请参考文章: 详解CSS display:inline-block的应用
本文转自: CSS: 居中一个宽度为auto的块元素 (width:auto;margin:auto) center an auto-width div