CSS ID 变化

发布于 2024-11-10 11:15:06 字数 671 浏览 5 评论 0原文

嘿,我猜这可能相当微不足道,但我仍然很难找到答案或弄清楚它。

我正在尝试创建一个由彩色方块组成的网格,它们之间的间距任意。 这本身很容易做到,特别是因为我只需要九个正方形。但同时 我看着我完成的代码,我不禁觉得有一种更简单、更有效的方法来完成这个任务。

目前,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叶落知秋 2024-11-17 11:15:06

您可以为共享属性的方块使用一个类:

.square {
    position: absolute;
    width: 80px;
    height: 80px;
}

您绝对定位它们是否有特定的原因?听起来像是一项更适合花车的工作。

div.container {
    width: 240px;
}

.square {
    float: left;
    width: 80px;
    height: 80px;
}

You can use a class for the squares that share a property:

.square {
    position: absolute;
    width: 80px;
    height: 80px;
}

Is there a specific reason you're absolutely positioning them though? Sounds like a job better suited for floats.

div.container {
    width: 240px;
}

.square {
    float: left;
    width: 80px;
    height: 80px;
}
不喜欢何必死缠烂打 2024-11-17 11:15:06

假设内部方块是 div,容器内没有其他 div,并且每个内部 div 应该是相同的 widthheight,这就是我要做的:

.container {
    position: relative;
    left: 50px;
    top: 50px;
    background: rgb(45,45,45);
    height: 300px;
    width: 300px;
}
.container > div {
    position: absolute;
    background-color: rgb(52,82,222);
    height: 80px;
    width: 80px;
}

#square1 {
    left: 20px;
    top: 20px;
}
#square2 {
    left: 110px;
    top: 20px;
}
..

如果您需要为每个 单独的 topleft 属性>div,那么你别无选择,只能使用id

通过使用 .container > ,您可以避免添加 class div,它选择作为 .container 直接子级的所有 div 元素。

HTML 看起来像这样:

<div class="container">
    <div id="square1"></div>
    <div id="square2"></div>
    ..
</div>

Assuming that the inner squares are divs, there are no other divs inside your container, and each inner div should be the same width and height, this is what I'd do:

.container {
    position: relative;
    left: 50px;
    top: 50px;
    background: rgb(45,45,45);
    height: 300px;
    width: 300px;
}
.container > div {
    position: absolute;
    background-color: rgb(52,82,222);
    height: 80px;
    width: 80px;
}

#square1 {
    left: 20px;
    top: 20px;
}
#square2 {
    left: 110px;
    top: 20px;
}
..

If you need separate top and left properties for each div, then you have no choice but to use ids.

You can avoid having to add a class thanks to using .container > div, which selects all div elements that are direct children of .container.

The HTML would look like this:

<div class="container">
    <div id="square1"></div>
    <div id="square2"></div>
    ..
</div>
坏尐絯℡ 2024-11-17 11:15:06

为什么不给所有的方块赋予相同的类并应用类似的东西,

.square
{
   display: inline-block;
   width: 80px;
   height: 80px;
   zoom: 1;
   *display: inline /* for IE */
}

这应该使所有的块都很好地包裹,而不必为每个块添加样式。

Why not give all of the squares the same class and apply something like

.square
{
   display: inline-block;
   width: 80px;
   height: 80px;
   zoom: 1;
   *display: inline /* for IE */
}

That should make all of the blocks wrap nicely without having to add styles for each individual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文