2011-09-11 112 views
2

我需要一些關於如何在HTML表格中設置列寬的建議。 我有一個簡單的表,跨越conatining div的整個寬度。 (使用表格的寬度=「100%」)HTML表格:設置列的寬度以適應浮動內容

這裏的渲染: enter image description here

我想要實現的是,最後一列「重量」是正是所包含元素的大小。 包含的元素是滑塊和文本輸入。滑塊向左浮動。

首先,我想了解如何爲最後一列設置小於所需的大小(例如width =「1%」),以便列自動調整爲最小大小。 但問題是,內容包裝:

enter image description here

因此,如何能我指定的最後一列是完全滑蓋+輸入內容的大小固定大小,明知滑塊有固定像素大小,但在「EM」輸入已經給出大小,即它取決於字體設置等

EDIT1
(從阿圖爾古普塔見註釋)。
使用<td style="white-space:nowrap; width:1%">...</td>
enter image description here

EDIT2
如果這可能是重要的,滑塊控件實際上是摘自: Unobtrusive Slider Control V2
我從來沒有想通了的細節,但它動態地添加到文本<input>
我可以通過添加一個額外的類float:left左移它;

一些更多的信息:
滑塊和文字輸入的實例是這樣的:

<input name="w-e2-09" id="w-e2-09" type="text" title="Range: 0 - 100" class="fd_slider fd_jump fd_slider_cn_extraclass" value="50" maxlength="3" size="3" /> 

fd_slider_cn_extraclass使滑塊在內部使用此:

div.extraclass 
{ 
    width:120px; 
    float:left; 
} 

如果我省略浮動離開,文本盒子會像第二個屏幕截圖一樣跳到下一行(即使有空間可用)。這就是爲什麼我添加了浮動。

EDIT3
嗨再次,
這裏的錶行(滑塊JS插入後)的全功能HTML的詳細信息,由螢火蟲給出:

<tr> 
      <td><img src="images/buildings/city.png"></td> 
      <td>Urban Infrastructure</td> 
      <td class="numeric em">13 <i>(+2)</i></td> 
      <td class="numeric">10</td> 
      <td> 
       <div class="fd-slider extraclass" id="fd-slider-w-e2-09" aria-disabled="false"><span class="fd-slider-inner"></span><span class="fd-slider-bar"></span><button type="button" class="fd-slider-handle" role="slider" aria-valuemin="0" aria-valuemax="100" aria-valuenow="50" aria-valuetext="50" style="left: 50px;">&nbsp;</button></div><input type="text" size="3" maxlength="3" value="50" class="fd_slider fd_jump fd_slider_cn_extraclass" title="Range: 0 - 100" id="w-e2-09" name="w-e2-09"> 
      </td> 
</tr> 

滑塊相關CSS是通過這個鏈接可供選擇: http://www.frequency-decoder.com/demo/slider-v2/slider-v2.zip

Edit4
以下是一個演示的問題,並可以作爲實驗室的最小HTML樣本:
我用一個簡單的固定大小div替換了滑塊。結果是一樣的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Test Table</title> 
</head> 
<body> 
    <div style="width:40em";> 
    <table width="100%" border="1"> 
     <thead> 
     <tr><th>Sector</th><th width="1%">Weight</th></tr> 
     </thead> 
     <tbody> 
      <tr> 
      <td>Urban Infrastructure</td> 
      <td style="display:inline; white-space:nowrap;"> 
       <div style="width:120px; background-color:#BBB; float:left;">&nbsp;</div> 
       <input type="text" value="50" size="3" /> 
      </td> 
      </tr> 
     </tbody> 
    </table> 
    </div> 
</body> 
</html> 
+0

爲什麼不總結所包含元素的寬度,然後將最後'​​'的寬度設置爲固定值? – StefanNch

+0

@StefanNch: slider size =「120px」 input size =「3」(characters) 那麼請問這是多少錢? – Scrontch

+0

no no no ...給輸入一個大小,使用css;下面所有的解決方案都是黑客行爲,如果他們是可以避免的,並且在我看來他們是,你應該使用一個更清潔和可維護的解決方案......給維度,總和和瞧!更進一步,如果你想/需要用戶可訪問性,使用「em」單位。 – StefanNch

回答

3

我不是100%確定如果這個工程沒有你提供一個工作的例子,我修改(也許jsfiddle或類似的東西)。但是,也許你可以嘗試刪除滑塊和文本框中的float: left;並設置display: inline-block;。如果這不起作用,請在單元格上嘗試white-space: nowrap;

+0

您的建議無效。如上所述,儘管我沒有對滑塊的完全編輯訪問權限。 – Scrontch

+0

好的,你可以用任何你想要的方式設置輸入/滑塊的樣式,只要你使用一個足夠具體的選擇器並且使用!important關鍵字;我個人不喜歡使用它,但有些情況需要時。使用Chrome,你應該能夠看到確切的來源,一旦它被創建和風格。用它來決定如何定位它。 – ClarkeyBoy

+0

我剛纔看了一下滑塊的網站和我只是喜歡它是如何訪問它是;如果您點擊向上,向上翻頁或插入鍵並增加這些鍵的相對位置,它會減少。 :D – ClarkeyBoy

0

我不會給你的方式來speficy「滑塊+輸入內容的準確大小」,但也許這將幫助你(?):
- >如果你把你養你指定width =「1%」的第二個解決方案,並在相應的td中添加關鍵字nowrap?

+0

與Edit1中的截圖結果相同。 – Scrontch

1
  1. 嘗試使用<td style="white-space:nowrap; width:1%">...</td>
  2. 如果存在問題,與&nbsp;
  3. 更換space, tab & enter如果還有問題,請嘗試在滑塊

去除float:left添加display:inline-blockclass="fd-slider-handle" role="slider"之間即class="fd-slider-handle" style="display:inline-block" role="slider"

或者你可以編輯css文件;改變display:blockdisplay:inline-block在類.fd-slider-handle

+0

見編輯1。 1. =>糟糕。 2.沒有空格。實際上,滑塊包含在標籤 中3.與第二個截圖相同。 – Scrontch

+0

在'class =「fd-slider-handle」role =「slider」'ie ie class =「fd-slider-handle」style =「display:inline-block」role =「slider之間添加'display:inline-block' 「' –

+0

或者你可以編輯css文件;在'.fd-slider-handle'類中將'display:block'更改爲'display:inline-block'。 –

0

嘗試在扇區列上放置'width = 100%',並在所有其他列上添加'nowrap'。這(我希望)會迫使部門專欄佔用儘可能多的空間,將其他專欄壓扁到最小。如果這樣做,你可能需要添加一些填充到每個列以獲得漂亮的外觀。