CSS ID 变化
嘿,我猜这可能相当微不足道,但我仍然很难找到答案或弄清楚它。
我正在尝试创建一个由彩色方块组成的网格,它们之间的间距任意。 这本身很容易做到,特别是因为我只需要九个正方形。但同时 我看着我完成的代码,我不禁觉得有一种更简单、更有效的方法来完成这个任务。
目前,我在 CSS 中声明了九个不同的 ID,每个方块一个。
div.container{
position:relative;
left:50px;
top:50px;
background-color:rgb(45,45,45);
height:300px;
width:300px;
}
#square{
position:absolute;
background-color:rgb(52,82,222);
left:20px;
top:20px;
height:80px
width:80px
#square2{
position:absolute;
background-color:rgb(58,82,22);
left:110px;
top:20px;
height:80px;
width:80px;
etc etc etc
我想做的是找到一种更有效的方法来做到这一点。
感谢您的帮助!
Hey, I am guessing that this is probably fairly trivial, but I am having difficulty finding an answer or figuring it out nonetheless.
I'm trying to create a grid of colored squares with arbitrary spacing between them.
This in itself is easy to do, especially because I need only nine squares. But while
I look at my completed code, I cannot help but feel there is a far more simple and efficient way to accomplish this.
At the moment, I have nine different IDs declared in my css, one for each square.
div.container{
position:relative;
left:50px;
top:50px;
background-color:rgb(45,45,45);
height:300px;
width:300px;
}
#square{
position:absolute;
background-color:rgb(52,82,222);
left:20px;
top:20px;
height:80px
width:80px
#square2{
position:absolute;
background-color:rgb(58,82,22);
left:110px;
top:20px;
height:80px;
width:80px;
etc etc etc
What I would like to do is find a more efficient way to do this.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为共享属性的方块使用一个类:
您绝对定位它们是否有特定的原因?听起来像是一项更适合花车的工作。
You can use a class for the squares that share a property:
Is there a specific reason you're absolutely positioning them though? Sounds like a job better suited for floats.
假设内部方块是
div
,容器内没有其他div
,并且每个内部div
应该是相同的width
和height
,这就是我要做的:如果您需要为每个
单独的
,那么你别无选择,只能使用top
和left
属性>divid
。通过使用
.container > ,您可以避免添加
,它选择作为class
div.container
直接子级的所有div
元素。HTML 看起来像这样:
Assuming that the inner squares are
div
s, there are no otherdiv
s inside your container, and each innerdiv
should be the samewidth
andheight
, this is what I'd do:If you need separate
top
andleft
properties for eachdiv
, then you have no choice but to useid
s.You can avoid having to add a
class
thanks to using.container > div
, which selects alldiv
elements that are direct children of.container
.The HTML would look like this:
为什么不给所有的方块赋予相同的类并应用类似的东西,
这应该使所有的块都很好地包裹,而不必为每个块添加样式。
Why not give all of the squares the same class and apply something like
That should make all of the blocks wrap nicely without having to add styles for each individual.