如何重构一些重复的 HTML 和 CSS?

发布于 2024-10-25 00:34:00 字数 431 浏览 3 评论 0原文

如何重构以下重复的 HTML 和 CSS?

代码大约是10个房间。房间 i 的顶部位于 40*i 像素处。

我使用冒号来表示我删除的行。

HTML:

<div id="room1">Room 1</div>
<div id="room2">Room 2</div>
:
<div id="room10">Room 10</div>

CSS:

#room1,
#room2,
:
#room10
{
:
    top: 40px;
:
}

#room2 {top: 80px}
#room3 {top: 120px}
#room4 {top: 160px}
:
#room10 {top: 400px}

How can I refactor the following repetitive HTML and CSS?

The code is about 10 rooms. The top of Room i is at 40*i pixels.

I used colons to indicate lines that I deleted.

HTML:

<div id="room1">Room 1</div>
<div id="room2">Room 2</div>
:
<div id="room10">Room 10</div>

CSS:

#room1,
#room2,
:
#room10
{
:
    top: 40px;
:
}

#room2 {top: 80px}
#room3 {top: 120px}
#room4 {top: 160px}
:
#room10 {top: 400px}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

橘虞初梦 2024-11-01 00:34:00

如果您需要相对/绝对定位,则没有更简洁的方法来为n不同的top指定不同的top div

抱歉,您只需完全写出所有内容,就像您的方式一样。我自己做了这个之后,我想我的东西比你的稍微短一些。


作为参考,如果我需要定位,我会这样做:

现场演示< /em>

CSS:

#roomContainer {
    position: relative
}
#roomContainer div {
    position: absolute;
    background: #ccc;
    width: 100px;
    height: 16px;
    padding: 10px;
    text-align: center;
    outline: 1px solid blue
}

#room1  { top: 0px    }
#room2  { top: 40px    }
#room3  { top: 80px    }
#room99 { top: 9120px  }

HTML:

<div id="roomContainer">
    <div id="room1">Room 1</div>
    <div id="room2">Room 2</div>
    <div id="room3">Room 3</div>
</div>

If you require the relative/absolute positioning, there is no cleaner way to specify a different top for n different divs.

Sorry, but you just have to write it all out exactly similar to how you have it. After making this myself, I think I have something slightly shorter than yours.


For reference, this is how I'd do it if I needed the positioning:

Live Demo

CSS:

#roomContainer {
    position: relative
}
#roomContainer div {
    position: absolute;
    background: #ccc;
    width: 100px;
    height: 16px;
    padding: 10px;
    text-align: center;
    outline: 1px solid blue
}

#room1  { top: 0px    }
#room2  { top: 40px    }
#room3  { top: 80px    }
#room99 { top: 9120px  }

HTML:

<div id="roomContainer">
    <div id="room1">Room 1</div>
    <div id="room2">Room 2</div>
    <div id="room3">Room 3</div>
</div>
演多会厌 2024-11-01 00:34:00

这必须是绝对定位吗?为什么不这样做呢?

<div id="room1" class="roomHeight">Room 1</div>
<div id="room2" class="roomHeight">Room 2</div>
:
<div id="room10" class="roomHeight">Room 10</div>

.roomHeight {
    height: 40px;
}

每个房间仍然会堆叠,因为它们自然地在块显示下,使每个房间高 40 像素将获得与使用绝对定位并声明每个 div 顶部相同的效果。

Does this have to be in absolute positioning? Why not do this?

<div id="room1" class="roomHeight">Room 1</div>
<div id="room2" class="roomHeight">Room 2</div>
:
<div id="room10" class="roomHeight">Room 10</div>

.roomHeight {
    height: 40px;
}

Each room will still stack as they are naturally under block display, making each one 40px tall will get the same effect as using absolute positioning and declaring the top of each div.

烟─花易冷 2024-11-01 00:34:00

给每个相同的类并使类正确地分开每个类,例如:

<div id="room1" class="room">Room 1</div>
<div id="room2" class="room">Room 2</div>
:
<div id="room10" class="room">Room 10</div>

css:

.room {
   margin-top: 40px
}

我不确定为什么你使用 top 属性,它们都是绝对定位的吗?

give each the same class and make the class separate each correctly like:

<div id="room1" class="room">Room 1</div>
<div id="room2" class="room">Room 2</div>
:
<div id="room10" class="room">Room 10</div>

the css:

.room {
   margin-top: 40px
}

I'm not sure why you used the top property, are they all positioned absolutely?

撕心裂肺的伤痛 2024-11-01 00:34:00

首先,我建议为“房间” div 提供一个通用 css 属性的类:

或者,如果它们都在一个公共父元素中,请使用它来分配公共 css 属性:

<div id="allrooms">
  <div id="room1">Room 1</div>
  <div id="room2">Room 2</div>
</div>

#allrooms div {
  ...
}

如果您不希望所有 div 都有该 css 规则列表,那么它可能值得考虑将 left 属性直接应用于 div

Room 1

< /代码>

First I'd suggest to give the "room" divs also a class for the common css properties: <div id="room1" class="room"></div> Or if all of them are in a common parent element use that to assign the common css properties:

<div id="allrooms">
  <div id="room1">Room 1</div>
  <div id="room2">Room 2</div>
</div>

#allrooms div {
  ...
}

If you don't want that list of css rules for all divs, it may be worth considering applying the left properties directly to the divs: <div style="left: 20px">Room 1</div>

梦忆晨望 2024-11-01 00:34:00

最好使用 div 来包装整行,然后...所以你有一个
<代码>

房间 1
在此输入时间划分元素

CSS


.schedulerRow, room { height: 40px;}

那么你就可以与同一行的时分元素进行交互,而不会影响其他房间行。

it might be better to use a div to wrap the entire row instead then...so you have a

<div class="schedulerRow">
<div id="room1" class="room">room 1</div>
<div id="scheduler1">Enter Time division elements in here</div>
</div>

Css
.schedulerRow, room { height: 40px;}

then you can interact with the time division elements on the same row without affecting the other room rows.

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