Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 7th, 2008, 05:35 PM
freerider2007@hotmail.com
Guest
 
Posts: n/a
Default 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!

  #2  
Old July 7th, 2008, 06:05 PM
Ben C
Guest
 
Posts: n/a
Default Re: Adapt width of DIV to content

On 2008-07-07, freerider2007@hotmail.com <freerider2007@hotmail.comwrote:
Quote:
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.
Quote:
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.
  #3  
Old July 7th, 2008, 06:45 PM
freerider2007@hotmail.com
Guest
 
Posts: n/a
Default Re: Adapt width of DIV to content

You need to give the DIV shrink-to-fit width, which you can get by
Quote:
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?
Quote:
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.
  #4  
Old July 7th, 2008, 06:45 PM
freerider2007@hotmail.com
Guest
 
Posts: n/a
Default 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.
  #5  
Old July 7th, 2008, 07:55 PM
Ben C
Guest
 
Posts: n/a
Default Re: Adapt width of DIV to content

On 2008-07-07, freerider2007@hotmail.com <freerider2007@hotmail.comwrote:
Quote:
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:

http://netweaver.com.au/alt/shrinkTo...rinkToFit.html

dorayme may have a more up-to-date URL, I thought I remembered seeing
more about centring somewhere. Anyway that URL still works.
  #6  
Old July 7th, 2008, 07:55 PM
Ben C
Guest
 
Posts: n/a
Default Re: Adapt width of DIV to content

On 2008-07-07, freerider2007@hotmail.com <freerider2007@hotmail.comwrote:
Quote:
Quote:
>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.
  #7  
Old July 8th, 2008, 03:25 AM
dorayme
Guest
 
Posts: n/a
Default Re: Adapt width of DIV to content

In article <slrng74pfa.cn1.spamspam@bowser.marioworld>,
Ben C <spamspam@spam.eggswrote:
Quote:
On 2008-07-07, freerider2007@hotmail.com <freerider2007@hotmail.comwrote:
Quote:
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:
>
http://netweaver.com.au/alt/shrinkTo...rinkToFit.html
>
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.au/centring/>

--
dorayme
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles