css h1 - only as wide as the text

前端 未结 9 479
情深已故
情深已故 2020-12-24 00:08

I have an H1 style for my site:

.centercol h1 {
    color: #006bb6;
    font-weight: normal;
    font-size: 18px;
    padding:3px 3px 3px 6px;
    border-lef         


        
相关标签:
9条回答
  • 2020-12-24 00:54

    You can use display:inline-block to force this behavior

    0 讨论(0)
  • 2020-12-24 00:55
    .h1 {
      width: -moz-fit-content;
      width: fit-content;
    
      // workaround for IE11
      display: table;
    }
    

    All modern browsers support width: fit-content for that.

    For IE11 we could emulate this behavior with display: table which doesn't break margin collapse like display: inline-block or float: left.

    0 讨论(0)
  • 2020-12-24 00:56

    You can use the inline-block value for display, however in this case you will loose the block feature of h1 i.e. the siblings will be displayed inline with h1 if they are inline elements(in which case you can use a line-break
    ).

    display:inline-block; 
    
    0 讨论(0)
提交回复
热议问题