Results 1 to 7 of 7

Thread: How to get div to resize to width of contents?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    How to get div to resize to width of contents?

    I have the following code:

    Code:
        <div style="background-color: Blue;">Line 1</div>
        <div>Line 2</div>
    What style information would I add to the first div so that the blue background is only as wide as needed for the text?

    Currently, the div is expanding to the full width of the browser window.

    Thanks.

    Visual Studio 2010

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to get div to resize to width of contents?

    then you want a span tag...
    Code:
        <span style="background-color: Blue;">Line 1</span>
        <div>Line 2</div>
    or

    Code:
        <div><span style="background-color: Blue;">Line 1</span></div>
        <div>Line 2</div>
    depending on your needs....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: How to get div to resize to width of contents?

    This is due to elements having either block (div) or inline (span) display properties by default. Relevant reading.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to get div to resize to width of contents?

    That doesn't quite work because if the content in the span extends multiple lines (see example code below), it doesn't render correctly. Problem 1 is that there is a visible gap between the lines and Problem 2 is that the background is not a square. It wants to fit tight around the text. I want a solid box around the content. It seems like span is not what I'm looking for.

    Code:
        <span style="background-color: Blue;">Line 1<br />Line 1a</span>
        <div>Line 2</div>

    Visual Studio 2010

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: How to get div to resize to width of contents?

    I think I'm getting close. The following code almost works. I have the div fitting the contents correctly, but Line 2 is not going underneath the Line 1, it is floating to the right. How do I modify this to get Line 2 to be underneath Line 1?

    Code:
        <div style="background-color: Blue; white-space: nowrap; float: left;">Line 1<br />Line 1a</div>
        <div>Line 2</div>

    Visual Studio 2010

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to get div to resize to width of contents?

    well, had you mentioned that right from the start, you would have gotten a more appropriate response.

    Line 2 isn't floating to the right... it's rendered to the right, line 1 is floated to the left... it's like the drop case letter in older books, where the first letter is illustrated... it's "floating" to the left, allowing the text to flow around it...

    Anyways... I'm not sure the float is appropriate anyways... what you really want to be manipulating is the width... but since div tags are block elements... you need to know what width you want...
    remove the float and try margin:auto ... that might do it... not sure... I'm not used to dealing with flexible width divs...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How to get div to resize to width of contents?

    display: inline-block

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width