How do I center an inline-block/floated header with no extra markup?

前端 未结 3 1838
南笙
南笙 2021-01-05 06:06

I have a variable width header that must have a background color that is as wide as the text (no wider). The only way I can think of doing this (with no ext

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-05 06:46

    you can give text-align:center; to the body as a global arrtibute & give display:inline-block to your header div. So,it's center your dynamic width in center like this :

    CSS:

    body{margin:0; padding:0;text-align:center}
    p{text-align:left;}
    .header{background:red;display:inline-block;}
    

    Check this example http://jsfiddle.net/vpcXS/

提交回复
热议问题