2015-10-07 34 views
0

我在頁面的一部分有以下div結構。當頁面呈現時,我看到contentDiv的寬度小於texArea元素的寬度。當我查看Chrome開發人員工具中的元素時,contentDiv的寬度爲239px,即使textArea的寬度爲550px。爲什麼外部div不會擴展到其內容的寬度?

問題:爲什麼會外的div不承擔其內容的寬度時,外層div沒有寬度和高度提到的風格,我將如何確保外層div總是比內部的div尺寸更大?

<div id="contentDiv"> 
<asp:Label ID="lblCodeType" runat="server" Font-Bold="true" Font-Size="Large"></asp:Label> 
    <div> 
    <asp:TextBox ID="txtCode" runat="server" TextMode="MultiLine" Rows="15" Width="550" Height="500" Font-Size="Small" Font-Names="consolas,sans-serif"></asp:TextBox> 
     <div id="runCode" style="width:80%""> 
        <input type="button" value="Run above code" onclick="runCode(); return false;" id="btnRunCode" /><br /> 
        (NOTE: Uncomment the code that you want to run, and comment what you don't want to run. DO NOT run multiple popup code at the same time; for example do not uncomment radalert as well as radconfirm code before running the code, but only have either radalert or radconfirm uncommented.) 
     </div> 
    </div> 
</div> 

UPDATE 1

用於外div的父的CSS是如下。

element.style { 
    height: 516.667px; 
} 
div#radWindow1_C { 
} 
.RadWindow_MetroTouch .rwContent { 
    background-color: #fff; 
    border: 0; 
} 
.RadWindow .rwContent { 
    padding: .41667em .83333em; 
    border-width: 1px; 
    border-style: solid; 
    overflow: auto; 
} 
user agent stylesheetdiv { 
    display: block; 
} 

更新2

經過一番研究,我想出了2個解決方案。我無法弄清楚爲什麼CSS會這樣(可能是因爲在這個ASP.Net頁面中來自其他元素/ div的許多CSS規則的複雜應用),但是這兩個解決方案中的任何一個解決了我的問題。

溶液1contentDiv需要顯示像display CSS屬性必須是一個表或錶行或表小區即或者tabletable-rowtable-cell

<div id="contentDiv" style="display:table"> 
<asp:Label ID="lblCodeType" runat="server" Font-Bold="true" Font-Size="Large"></asp:Label> 
    <div> 
    <asp:TextBox ID="txtCode" runat="server" TextMode="MultiLine" Rows="15" Width="550" Height="500" Font-Size="Small" Font-Names="consolas,sans-serif"></asp:TextBox> 
     <div id="runCode" style="width:80%""> 
        <input type="button" value="Run above code" onclick="runCode(); return false;" id="btnRunCode" /><br /> 
        (NOTE: Uncomment the code that you want to run, and comment what you don't want to run. DO NOT run multiple popup code at the same time; for example do not uncomment radalert as well as radconfirm code before running the code, but only have either radalert or radconfirm uncommented.) 
     </div> 
    </div> 
</div> 

溶液2:使用JavaScript和/或jquery的,所有後代在所有的contentDiv DOM水平需要被解析爲寬度和高度,使得其後代中最大寬度和最大高度可以被確定。然後我們只需要將contentDiv的寬度和高度設置爲這些最大值加上一些邊距。這在下面的代碼中顯示。

function pageLoad() { 
      var max_width = 0; 
      var max_height = 0; 
      var element_width = 0; 
      var element_height = 0; 
       $("#contentDiv").find("*").each(function() { 
        element_width = parseInt($(this).width()); 
        if (element_width > max_width) { 
         max_width = element_width; 
        } 
        element_height = parseInt($(this).height()); 
        if (element_height > max_height) { 
         max_height = element_height; 
        } 
       }); 
       if ($("#contentDiv").width() < max_width) { 
        $("#contentDiv").width(max_width + 10); 
       } 
       if ($("#contentDiv").height() < max_height) { 
        $("#contentDiv").height(max_height + 10); 
       } 
} 
+0

你能提供你的CSS?不能告訴很多隻是HTML。 – Nath

+0

沒有外部div的CSS,因爲它沒有在設計時應用任何樣式,這就是爲什麼我對結果感到驚訝。 – Sunil

+0

外部div的父級和文檔的其餘部分如何?這都是相關的。 – Nath

回答

1

也許,有一組寬一些父元素:

<div style="width: 280px;"> 
 
    <div> 
 
    This div has a width of only 280px, even without having any style vinculated. 
 
    <textarea style="width: 550px"></textarea> 
 
    </div> 
 
</div>

+0

如果外部div沒有寬度的樣式:280px,那麼div會自動展開? – Sunil

+0

這裏可以保證的方法是使用JavaScript來獲取外部的div,然後找到它的所有兒童/孫子的最大寬度/ ...外DIV然後可以給這個最大寬度加上一些填充。 – Sunil

+1

如果外部div的任何父級都設置了寬度,則沒有設置寬度的內部div將假定父級div的寬度。如果沒有父元素設置寬度,則內部div將具有100%的寬度。 – Buzinas

相關問題