If I define a heading like , its width is set to 100% by default.
Is there a way to make sure its width is set to the smallest value possible
It's a block level element, so it follows the rules of display:block. You can set display:inline, which may do what you need or not, depending on the context.
display:block
display:inline