html - div中的嵌套设计

标签 html css web

请原谅,因为我在 Web 编程方面的能力非常有限,这使得即使在 google 上搜索主题也很困难,因为我不知道正确的术语..

无论如何,我的问题(我确信我的措​​辞会很奇怪,因为我真的不知道如何谈论这个问题)是:

有什么办法可以用html + css实现这个伪代码吗?

CSS文件:

#ropeimage {
    background-repeat:repeat-x;
    background:url(images/rope.png);
}

#ropetext {
    <div id="ropeimage">
   /* some text properties */
}

然后在html文件中

<div id="ropetext">hello</div>
<div id="ropetext">there</div>

我问的原因是,即使我知道我可以做类似的事情

<div id="ropetext"><div id="ropeimage">hello</div></div>
<div id="ropetext"><div id="ropeimage">there</div></div>

如果我知道我总是需要这个,为什么不把它藏在 css 文件中。

如果有人好奇,我想做的是构建一个 div,它将重复 x 一个“绳索”图像,然后垂直打印一些文本。我通常不能将所有这些都放在一个 div 中,因为绳子的背景图像会在文本下方重复,所以我需要两个 div,以便图像在碰到文本 div 时立即停止重复。

a crude picture explaining what im trying to do

最佳答案

我相信你不想使用 id 而想使用 class

所以你的 CSS 看起来像这样:

.ropetext {
    /* properties */
}

然后在您的 HTML 中:

<div class="ropetext"></div>

类和 ID 之间的区别在于您只想将 ID 用于一个 HTML 标记。一个类可以应用于多个标签。

此外,您可以在 CSS 中进行嵌套样式定义:

.rope {
    /* properties defining any rope div in general */
    position: relative;
}

.rope .top {
    /* properties defining the top of the div */
    /* background image stuff goes here */
}

.rope .bottom {
    /* properties defining the bottom of the div */
    position: absolute;
    bottom: 0px;
}

在您的 HTML 中:

<div class="rope">
    <div class="top">
    </div>
    <div class="bottom">
        Hello
    </div>
</div>

我还没有对此进行测试,但我相信这符合您的要求。

关于html - div中的嵌套设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15937886/

相关文章:

html - 将水平制表符从 Bootstrap 3 转换为 Bootstrap 4

html - 表列的动态溢出x?

html - 如何在电脑和手机上显示相同的字体粗细?

具有不同标题的 HTML 打印网页

html - IE7显示父子背景图片问题

javascript - 避免 AngularJS 栏脚重叠内容

html - 导航栏不会改变颜色。更正了 html 和 css 标记。没有效果

javascript - 如何获取谷歌地图 Angular 落的坐标?

javascript - 三.js 加载器

java - 如何使用具有三个继承级别的节