2013-09-16 55 views
0

考慮下面的代碼:獲得非固定寬度div以完美契合父母而不會溢出?

<div class='wrapper'> 
    <div class='right-panel'>Here is the article</div> 
    <div class='left-panel'> 
     <div class='left-panel-contents'> 
      <div class='headline'> 
       <h1>HEADLINE</h1> 
      </div> 
     </div> 
    </div> 
</div> 

.wrapper { 
    height: 200px; 
    min-width: 960px; 
    max-width: 1060px; 
    background: gray; 
} 
.right-panel { 
    float: right; 
    height: 200px; 
    width: 760px; 
    background: blue; 
} 
.left-panel { 
    background: green; 
    height: 200px; 
} 
.left-panel-contents { 
    position: relative; 
    background: pink; 
    height: 100%; 
    width: 15%; 
    // how do I make this fill the width of the left panel 
} 
.headline { 
    background: black; 
    color: white; 
    line-height: 45px; 
    width: 100%; 
    position: absolute; 
    top: 0; 
    left: 0; 
    z-index: 1000; 
} 

h1 { 
float: right; 

} 

http://jsfiddle.net/duw4G/

我試圖讓標題文字擴大一路右側面板中。如果左側面板的內容完美地填充其父項,則可能。如果我將它設置爲100%,溢出:隱藏它不能解決問題(左側面板內容填充整個包裝div寬度)

有沒有什麼辦法可以調整我的技術來使它工作?

+0

你'寬度:15%;'的確切元素你想成爲100%的寬度它的父?這沒有多大意義?!爲什麼不把它刪除? – Yoshi

回答

0
.wrapper { 
    height: 200px; 
    min-width: 960px; 
    max-width: 1060px; 
    background: gray; 
} 
.right-panel { 
    float: right; 
    height: 200px; 
     width:75%; 
    padding:0px; 
    margin:0px; 
    background: blue; 
} 
.left-panel { 
    background: green; 
    height: 200px; 
    width:25%; 
    padding:0px; 
    margin:0px; 

} 
.left-panel-contents { 
    position: relative; 
    background: pink; 
    height: 100%; 
    width: 100%; 
    // how do I make this fill the width of the left panel 
} 
.headline { 
    background: black; 
    height: 45px; 
    width: 100%; 
    position: absolute; 
    top: 0; 
    left: 0; 
    z-index: 1000; 
float:left; position:relative; 
} 
+0

是的..如果右側面板不是固定寬度,效果很好。我需要將寬度固定爲760px – fansonly

+0

,那麼您必須修復左側面板px。只要父母具有固定的寬度,孩子可以使用100%的寬度。 它需要一個參考點。 – FlemGrem

0

.headline將根據與非靜態位置(即相對的或絕對的)的最近的父被定位,或到視如果沒有這樣的母體中找到。

如果不需要其他用途,請從.left-panel-contents刪除position:relative,但將其添加到.wrapper。請參閱:http://jsfiddle.net/duw4G/9/

+0

是的..我意識到我可以做到這一點。我試圖讓標題填充左側面板並僅填充左側面板。這樣,當我將標題文本正確浮動時,它會排列在正確的內容面板旁邊。 – fansonly