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

前端 未结 3 1770
南笙
南笙 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条回答
  • 2021-01-05 06:24

    Solved using display: table; on the heading (with margin: 0 auto;).

    0 讨论(0)
  • 2021-01-05 06:28

    replace the p tag with center as my markup is

    <div id="header">
        <center>hello</center>
    </div>
    

    and it's CSS is

    body{
        width:100%;
    }
    #header{
        text-align:center;
    }
    #header center{
        display:inline-block;
        background:red;
    }
    
    0 讨论(0)
  • 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/

    0 讨论(0)
提交回复
热议问题