How can I make a div not larger than its contents?

前端 未结 30 3656
青春惊慌失措
青春惊慌失措 2020-11-21 07:35

I have a layout similar to:

I would like for the div to only exp

相关标签:
30条回答
  • 2020-11-21 07:51
    <table cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td>
                <div id="content_lalala">
                    this content inside the div being inside a table, needs no inline properties and the table is the one expanding to the content of this div =)
                </div>
            </td>
        </tr>
    </table>
    

    I know people don't like tables sometimes, but I gotta tell you, I tried the css inline hacks, and they kinda worked in some divs but in others didn't, so, it was really just easier to enclose the expanding div in a table...and...it can have or not the inline property and still the table is the one that's gonna hold the total width of the content. =)

    0 讨论(0)
  • 2020-11-21 07:52

    Tampering around with Firebug I found the property value -moz-fit-content which exactly does what the OP wanted and could be used as follow:

    width: -moz-fit-content;
    

    Although it only works on Firefox, I couldn't find any equivalent for other browsers such as Chrome.

    0 讨论(0)
  • 2020-11-21 07:53

    OK, in many cases you even don't need to do anything as by default div has height and width as auto, but if it's not your case, applying inline-block display gonna work for you... look at the code I create for you and it's do what you looking for:

    div {
      display: inline-block;
    }
    <div>
      <table>
        <tr>
          <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ultrices feugiat massa sed laoreet. Maecenas et magna egestas, facilisis purus quis, vestibulum nibh.</td>
          <td>Nunc auctor aliquam est ac viverra. Sed enim nisi, feugiat sed accumsan eu, convallis eget felis. Pellentesque consequat eu leo nec pharetra. Aenean interdum enim dapibus diam.</td>
          <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ultrices feugiat massa sed laoreet. Maecenas et magna egestas, facilisis purus quis, vestibulum nibh.</td>
        </tr>
      </table>
    </div>

    0 讨论(0)
  • 2020-11-21 07:53
    <div class="parentDiv" style="display:inline-block">
        // HTML elements
    </div>
    

    This will make parent div width same as the largest element width.

    0 讨论(0)
  • 2020-11-21 07:55

    You can try fit-content (CSS3):

    div {
      width: fit-content; 
      /* To adjust the height as well */ 
      height: fit-content;
    }
    
    • Browser support
    • Specification
    0 讨论(0)
  • 2020-11-21 07:55

    Try display: inline-block;. For it to be cross browser compatible please use the below css code.

    div {
      display: inline-block;
      display:-moz-inline-stack;
      zoom:1;
      *display:inline;
      border-style: solid;
      border-color: #0000ff;
    }
    <div>
      <table>
        <tr>
          <td>Column1</td>
          <td>Column2</td>
          <td>Column3</td>
        </tr>
      </table>
    </div>

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