2016-01-12 512 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>