Adapt width of DIV to content

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • freerider2007@hotmail.com

    Adapt width of DIV to content

    Hi!

    I have a HTML table which width will vary (since it's dynamically
    built). Above the table I want to align a select box with the table's
    right edge.

    A simple solution is to add an extra row (and use colspan) and put the
    select box there.

    Is it possible to solve the problem using a container DIV and CSS?

    My idea is to use a container DIV to hold both the select box and the
    table. The table will always be wider than the select box. If it is
    possible to set the width of the DIV to adapt to the width of the
    table, then the problem is solved since it's easy to right align the
    input box to the DIV.

    Thanks!

  • Ben C

    #2
    Re: Adapt width of DIV to content

    On 2008-07-07, freerider2007@h otmail.com <freerider2007@ hotmail.comwrot e:
    Hi!
    >
    I have a HTML table which width will vary (since it's dynamically
    built). Above the table I want to align a select box with the table's
    right edge.
    >
    A simple solution is to add an extra row (and use colspan) and put the
    select box there.
    >
    Is it possible to solve the problem using a container DIV and CSS?
    You need to give the DIV shrink-to-fit width, which you can get by
    giving it display: inline-block, display: table, display: table-cell or
    float: left. Float: left is probably the most widely supported.
    My idea is to use a container DIV to hold both the select box and the
    table. The table will always be wider than the select box. If it is
    possible to set the width of the DIV to adapt to the width of the
    table, then the problem is solved since it's easy to right align the
    input box to the DIV.
    Are you putting the select box in the div too? Then you could float the
    table to get it to the left of the select. But the select will end up
    below the table if there isn't enough horizontal room.

    Comment

    • freerider2007@hotmail.com

      #3
      Re: Adapt width of DIV to content

      You need to give the DIV shrink-to-fit width, which you can get by
      giving it display: inline-block, display: table, display: table-cell or
      float: left. Float: left is probably the most widely supported.
      This worked just fine in FF but not in IE7 (both Windows). Is it
      possible to get it to work in most browsers?
      Are you putting the select box in the div too? Then you could float the
      table to get it to the left of the select. But the select will end up
      below the table if there isn't enough horizontal room.
      The select box is above the table inside the same container div.

      Comment

      • freerider2007@hotmail.com

        #4
        Re: Adapt width of DIV to content

        Another problem is that the div should be horizontally centered on the
        page. I use margin-left: auto; margin-right: auto; to accomplish that
        but adding the rules you suggested makes the div left aligned.

        Comment

        • Ben C

          #5
          Re: Adapt width of DIV to content

          On 2008-07-07, freerider2007@h otmail.com <freerider2007@ hotmail.comwrot e:
          Another problem is that the div should be horizontally centered on the
          page. I use margin-left: auto; margin-right: auto; to accomplish that
          but adding the rules you suggested makes the div left aligned.
          You're out of luck then-- you can't centre a float and IE doesn't
          support display: table.

          See also:



          dorayme may have a more up-to-date URL, I thought I remembered seeing
          more about centring somewhere. Anyway that URL still works.

          Comment

          • Ben C

            #6
            Re: Adapt width of DIV to content

            On 2008-07-07, freerider2007@h otmail.com <freerider2007@ hotmail.comwrot e:
            >You need to give the DIV shrink-to-fit width, which you can get by
            >giving it display: inline-block, display: table, display: table-cell or
            >float: left. Float: left is probably the most widely supported.
            >
            This worked just fine in FF but not in IE7 (both Windows). Is it
            possible to get it to work in most browsers?
            I thought IE7 did support float up to a point, but I can't really help
            you as I'm not familiar with many of its bugs and quirks.

            Comment

            • dorayme

              #7
              Re: Adapt width of DIV to content

              In article <slrng74pfa.cn1 .spamspam@bowse r.marioworld>,
              Ben C <spamspam@spam. eggswrote:
              On 2008-07-07, freerider2007@h otmail.com <freerider2007@ hotmail.comwrot e:
              Another problem is that the div should be horizontally centered on the
              page. I use margin-left: auto; margin-right: auto; to accomplish that
              but adding the rules you suggested makes the div left aligned.
              >
              You're out of luck then-- you can't centre a float and IE doesn't
              support display: table.
              >
              See also:
              >

              >
              dorayme may have a more up-to-date URL, I thought I remembered seeing
              more about centring somewhere. Anyway that URL still works.
              Yes, I won't move that one. It is just that it is also nearly identical
              to page 4 of another more comprehensive piece I made on centring:

              <http://netweaver.com.a u/centring/>

              --
              dorayme

              Comment

              Working...