2016-01-12 359 views
-2

我用多個div創建網格,每個div都有不同的顏色。但是,下面的代碼顯示所有div灰色。我怎樣才能改變div的顏色?如何填充不同顏色的div?

#container{ 
 
    width: 400px; 
 
    height: 300px; 
 
} 
 

 

 
.Rect{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 

 
} 
 

 
.Rect1{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 100px; 
 
    top: -300px; 
 
} 
 

 
.Rect2{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 200px; 
 
    top: -600px; 
 
} 
 

 
.Rect3{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 300px; 
 
    top: -900px; 
 
} 
 

 
#rectYellow{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: yellow; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -1125px; 
 
    left: 100px; 
 
} 
 

 
#rectGreen{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: green; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -1125px; 
 
    left: 200px; 
 
} 
 

 
#rectBlue{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: blue; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -1200px; 
 
    left: 0px; 
 
} 
 

 
#rectWhite{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: White; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -1425px; 
 
    left: 200px; 
 
} 
 

 
#rectOrange{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: Orange; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -1275px; 
 
    left: 100px; 
 
}
<div id=container> 
 
    
 
    <!--1th row --> 
 
    <div class="Rect"></div> 
 
    <div class="Rect"></div> 
 
    <div id="rectBlue"></div><!--BLUE --> 
 
    <div class="Rect"></div> 
 
    <!--2th row --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectYellow"></div><!--YELLOW --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectOrange"></div><!--ORANGE --> 
 
    <!--3th row --> 
 
    <div id="rectWhite"></div><!--WHITE --> 
 
    <div class="Rect2"></div> 
 
    <div id="rectGreen"></div><!--GREEN --> 
 
    <div class="Rect2"></div> 
 
    <!--4th row --> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    </div>

我卡發生了什麼事,道歉的代碼塊。我無法使用小提琴。

+0

工作對我來說很好 - http://jsfiddle.net/0a8adkt0/和@Paulie_D存在相應小提琴,你會看看你是否編輯它,但我不知道我應該如何編輯: – Anonymous

+0

那裏應該有白色,黃色,藍色,綠色和橙色的形狀,它們分別命名爲rectColor。一個空格,他們應該在那裏 – wundersoy

+0

他們消失的唯一原因是因爲你沒有給出適當的頂部和左側絕對位置,這就是導致元素移出文檔的原因。 – AmJustSam

回答

0

您的top計算結果已關閉。我已經更新了它們(爲了演示將白色轉換爲粉紅色),以便它們排列在片段中。

這就是說,我不確定最終結果應該是什麼。使用position:absolute;可能會更容易,併爲容器定義特定的維度,而不是計算每個元素的relative位置。或者讓每個元素display: inline-block;自然包裝。

#container{ 
 
    width: 400px; 
 
    height: 300px; 
 
} 
 

 

 
.Rect{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 

 
} 
 

 
.Rect1{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 100px; 
 
    top: -300px; 
 
} 
 

 
.Rect2{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 200px; 
 
    top: -600px; 
 
} 
 

 
.Rect3{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 300px; 
 
    top: -900px; 
 
} 
 

 
#rectYellow{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: yellow; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -300px; 
 
    left: 100px; 
 
} 
 

 
#rectGreen{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: green; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -600px; 
 
    left: 200px; 
 
} 
 

 
#rectBlue{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: blue; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: 0px; 
 
    left: 0px; 
 
} 
 

 
#rectWhite{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: pink; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -600px; 
 
    left: 200px; 
 
} 
 

 
#rectOrange{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: Orange; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -300px; 
 
    left: 100px; 
 
}
<div id=container> 
 
    
 
    <!--1th row --> 
 
    <div class="Rect"></div> 
 
    <div class="Rect"></div> 
 
    <div id="rectBlue"></div><!--BLUE --> 
 
    <div class="Rect"></div> 
 
    <!--2th row --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectYellow"></div><!--YELLOW --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectOrange"></div><!--ORANGE --> 
 
    <!--3th row --> 
 
    <div id="rectWhite"></div><!--WHITE --> 
 
    <div class="Rect2"></div> 
 
    <div id="rectGreen"></div><!--GREEN --> 
 
    <div class="Rect2"></div> 
 
    <!--4th row --> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    </div>

0

您的代碼無效top位置。它應該是這樣的:

#container{ 
 
    width: 400px; 
 
    height: 300px; 
 
} 
 

 

 
.Rect{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 

 
} 
 

 
.Rect1{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 100px; 
 
    top: -300px; 
 
} 
 

 
.Rect2{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 200px; 
 
    top: -600px; 
 
} 
 

 
.Rect3{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 300px; 
 
    top: -900px; 
 
} 
 

 
#rectYellow{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: yellow; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -300px; 
 
    left: 100px; 
 
} 
 

 
#rectGreen{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: green; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -600px; 
 
    left: 200px; 
 
} 
 

 
#rectBlue{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: blue; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: 0px; 
 
    left: 0px; 
 
} 
 

 
#rectWhite{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: White; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -1425px; 
 
    left: 200px; 
 
} 
 

 
#rectOrange{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: Orange; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -300px; 
 
    left: 100px; 
 
}
<div id=container> 
 
    
 
    <!--1th row --> 
 
    <div class="Rect"></div> 
 
    <div class="Rect"></div> 
 
    <div id="rectBlue"></div><!--BLUE --> 
 
    <div class="Rect"></div> 
 
    <!--2th row --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectYellow"></div><!--YELLOW --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectOrange"></div><!--ORANGE --> 
 
    <!--3th row --> 
 
    <div id="rectWhite"></div><!--WHITE --> 
 
    <div class="Rect2"></div> 
 
    <div id="rectGreen"></div><!--GREEN --> 
 
    <div class="Rect2"></div> 
 
    <!--4th row --> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    </div>

0

的唯一原因,它消失了,因爲你沒有給適當的topleftabsolute position,這是什麼原因造成的元素遷出的文件。但現在它的固定和工作....

#container{ 
 
    width: 400px; 
 
    height: 300px; 
 
} 
 

 

 
.Rect{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 

 
} 
 

 
.Rect1{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 100px; 
 
    top: -300px; 
 
} 
 

 
.Rect2{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 200px; 
 
    top: -600px; 
 
} 
 

 
.Rect3{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: grey; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    left: 300px; 
 
    top: -900px; 
 
} 
 

 
#rectYellow{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: yellow; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -150px; 
 
    left: 100px; 
 
} 
 

 
#rectGreen{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: green; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -600px; 
 
    left: 200px; 
 
} 
 

 
#rectBlue{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: blue; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -150px; 
 
    left: 200px; 
 
} 
 

 
#rectWhite{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: #ECECEC; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -526px; 
 
    left: 100px; 
 
} 
 

 
#rectOrange{ 
 
    width: 100px; 
 
    height: 75px; 
 
    background: Orange; 
 
    border-radius: 25px; 
 
    position: relative; 
 
    top: -375px; 
 
    left: 0px; 
 
}
<div id=container> 
 
    
 
    <!--1th row --> 
 
    <div class="Rect"></div> 
 
    <div class="Rect"></div> 
 
    <div id="rectBlue"></div><!--BLUE --> 
 
    <div class="Rect"></div> 
 
    <!--2th row --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectYellow"></div><!--YELLOW --> 
 
    <div class="Rect1"></div> 
 
    <div id="rectOrange"></div><!--ORANGE --> 
 
    <!--3th row --> 
 
    <div id="rectWhite"></div><!--WHITE --> 
 
    <div class="Rect2"></div> 
 
    <div id="rectGreen"></div><!--GREEN --> 
 
    <div class="Rect2"></div> 
 
    <!--4th row --> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    <div class="Rect3"></div> 
 
    </div>