2014-05-19 44 views
-1

HTML:將透明div層放置在另一個具有100%高度的div層之上(粘性頁腳!)?

<div id="mainWrapper"> 
    <div id="headerContainer">Header</div> 
     <div class="contentPage"> 
      <div id="content"> 
         <p>Content Page</p> 
         <p>Content Page</p> 
      </div> 
     </div> 
    <div id="footerContainer">Footer</div> 
</div> 

CSS:

body { 
    height: 100%; 
    margin: 0; 
    padding: 0; 
} 

#mainWrapper { 
    margin: 0 auto; 
    min-height: 100%; 
    position: relative; 
    width: 100%; 
    background: red; 
} 

#headerContainer { 
    background: none repeat scroll 0 0 #4ED0AA; 
    height: auto; 
    width: 100%; 
} 

.contentPage { 
    background: url("http://www.sammt.net/Baum.jpg") repeat-y scroll 0 0/100% auto rgba(0, 0, 0, 0); 
    padding-bottom: 120px; 
    width: 100%; 
} 

#content { 
    background: none repeat scroll 0 0 #6288A1; 
    opacity: 0.8; 
    padding: 20px 20%; 
} 

#footerContainer { 
    background-color: #4ED0AA; 
    bottom: 0; 
    height: 120px; 
    position: absolute; 
    width: 100%; 
} 

結果:

enter image description here

應該發生什麼: 與背景圖像的層以及與層藍色透明背景應該直到頁腳開始。問題是粘頁腳我想......

貌似是:

enter image description here

+0

您需要對您需要的行爲更精確:1)內容是否始終具有相同的高度? 2)如果內容在底部溢出,您希望使用scollbar,會發生什麼情況? 3)頁腳和頁眉的高度是否固定? –

+0

1)#內容總是不同的高度(這是主要問題)。 2)如果#內容爲高度,滾動條應位於瀏覽器窗口中。 3)#footerContainer固定高度,你可以在CSS中看到,標題高度不是固定的。 – user1711384

回答

2

只有兩段頁面Here is a demo,並another demo有大量的內容進行滾動。

我已經做了佈局有點重排,所以現在看起來是這樣的:

<div id="transparent-bg"></div> 

<div id="mainWrapper"> 
    <div id="headerContainer">Header</div> 

    <div class="contentPage"> 

     <div id="content"> 
      <p>Content Page</p> 
      <p>Content Page</p> 
     </div> 
    </div> 
</div> 
<div id="footerContainer"> 
    Footer 
</div> 

基本上,背景圖像和透明色被呈現在一個單獨的div,因爲這是造成一些呈現問題,並且footer已成爲mainWrapper之後的兄弟姐妹。

的CSS代碼如下:

html { 
    height:100%; 
} 
body { 
    height: 100%; 
    margin: 0; 
    padding: 0; 
} 

#transparent-bg { 
    position:fixed; 
    top:0; 
    left:0; 
    right:0; 
    bottom:0; 
    margin:auto; 
    background: url("http://www.sammt.net/Baum.jpg") no-repeat; 
    background-size:cover; 
    z-index:-1; 
} 

#transparent-bg:after { 
    background:#6288A1; 
    opacity:.8; 
    position:absolute; 
    width:100%; 
    height:100%; 
    top:0; 
    left:0; 
    content:''; 
} 

#mainWrapper { 
    margin: 0 auto -120px; 
    width: 100%; 
    min-height:100%; 
    height: auto !important; 
} 

#headerContainer { 
    background: #4ED0AA; 
    height: auto; 
    width: 100%; 
} 

.contentPage { 
    width: 100%; 
    margin: 0 auto -120px; 
} 

#content { 
    padding: 20px 20% 120px; 
    box-sizing: border-box; 
} 

#footerContainer { 
    background-color: #4ED0AA; 
    height: 120px; 
    width: 100%; 
} 

我希望這能解決您的問題!

+0

感謝van100j這個熟練的答案。你的代碼工作完美。我的問題是,我不能改變HTML,因爲其他的依賴關係。 .contentPage中的所有內容都是完全適應性的,在更高層次上更改HTML是不可能的: - /你有任何想法如何以這種方式解決它? – user1711384

+0

如果有幫助,我們可以通過# – user1711384

+0

的方式給#headerContainer一個固定的高度。您可以在'.contentPage'容器中添加'#transparent-bg' div [如本演示中所示](http:// jsbin.com/yiqafuzo/4) – van100j

0

嘗試將background-size:cover在藍色透明背景。

+0

不工作。似乎背景大小隻適用於圖像,不是嗎? – user1711384

0

試試這個代碼

#content { 
    background: none repeat scroll 0 0 #6288A1; 
    opacity: 0.8; 
    padding: 20px 20%; 
    height: 100%; 
} 

Live Demo

+0

此代碼使背景高度的層(但太高度,你必須滾動),藍色層仍然只包括contentPage中的文本... – user1711384

+0

也許我誤解了你的問題和想法在屏幕上,你錯過了我寫信給你的事實,以及屬性background-size:cover;應用爲了鋪平Choate圖像的選定部分 – AlexPrinceton

+0

剛剛添加了一個圖像它應該如何看起來像 – user1711384

1
// text write your css code 

#mainWrapper { 
    background: none repeat scroll 0 0 #FF0000; 
    height: 1000px; 
    margin: 0 auto; 
    position: relative; 
    width: 100%; 
    } 
#headerContainer { 
    background: none repeat scroll 0 0 #4ED0AA; 
    width: 100%; 
} 
.contentPage { 
    height: 100%; 
    margin: 0; 
    padding: 0; 
    position: relative; 
    width: 100%; 
} 
#content { 
    background: url("http://www.sammt.net/Baum.jpg") 
    repeat-y scroll 0 0/100% auto rgba(0, 0,0,0); 
    bottom: 0; 
    left: 0; 
    opacity: 0.8; 
    position: absolute; 
    right: 0; 
    top: 0; 
} 
#footerContainer { 
    background-color: #4ED0AA; 
    height: 120px; 
    width: 100%; 
} 
+0

感謝您的回答,但現在如果只有少量內容,您必須滾動。你不能給mainWrapper一個固定的高度,因爲內容總是可變的。 – user1711384

相關問題