Skip to content Skip to sidebar Skip to footer

How Do I Center An Inline-block/floated Header With No Extra Markup?

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 extra markup) is to set the

Solution 1:

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

Solution 2:

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/

Solution 3:

replace the p tag with center as my markup is

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

and it's CSS is

body{
    width:100%;
}
#header{
    text-align:center;
}
#header center{
    display:inline-block;
    background:red;
}

Post a Comment for "How Do I Center An Inline-block/floated Header With No Extra Markup?"