2014-01-29 38 views
0

我想在頁面的左側添加背景顏色。我有主div,其中心和固定寬度。我試過背景圖片,但它不適合我,因爲我在頁面上有無限滾動。下面是圖片,顯示我想達到的目標:如何在頁面左側添加背景顏色?

enter image description here

這裏是我的方法:

<div class="row"> 
    <div class="left"></div> 
    <div class="container"> 
     my custom HTML code 
    </div> 
</div> 

CSS

.container {width: 1000px; margin: 0 auto} 
.left {background-color: red; position: absolute; height: 100%; width:100%; top: 0px; left: -100%; } 
+0

你有正確的想法,但爲什麼你有沒有':-100%;'?把它改爲正值,你就會得到你想要的。並將你的寬度設置爲小於100% – stackErr

+0

@stackErr這對我不起作用。 – Maximus

+0

看看這個:http://jsfiddle.net/FeGFE/1/ – stackErr

回答

1

如果你想在中心和左側單獨的容器,這裏是它 http://jsfiddle.net/V6JaX/

.row { width: 100%; position: relative; } 
.left { width: 20%; height: 100%; top: 0; left: 0; position: absolute; background: red; } 
.container { width: 600px; margin: 0 auto; background: #FFF; } 
4

Check out the fiddle here

HTML

<div class="row"> 
    <div class="left"> 
     your left code 
    </div> 
    <div class="container"> 
     your custom HTML code 
    </div> 
</div> 

CSS

.container { 
    background-color: gray; 
    width: 80%; 
    margin: 0 auto; 
    float: left; 
} 

.left { 
    background-color: red; 
    float: left; 
    width: 20%; 
} 
+0

它不會工作。我正在使用固定寬度... – Maximus

+0

好吧,請嘗試用固定值替換寬度。爲什麼它不工作? –