css - 在 css 的帮助下缩进生成的 html div 类

标签 css css-selectors sass

我正在用 rails 编写一个应用程序。 我对 css 还不是很熟悉。虽然,我正在尝试。还有一个问题,我生成的html标签是

<div class="sub-product post-1">
 // content goes here
</div>

<div class="sub-product post-2">
 // content goes here
</div>

<div class="sub-product post-3">
 // content goes here
</div>

<div class="sub-product post-4">
 // content goes here
</div>

现在,如您所见,有不同的数字排列到 post。我想做的是让他们缩进,这是我的 CSS

.sub-product.post-1 {
margin:30px
}

它有效,但如果我将其设为 .sub-product.post-1.post-2.post-3.post-4,它会显示缩进。我想我知道这里出了什么问题,但显示缩进的优雅解决方案是什么?

谢谢

最佳答案

您可以嵌套您的 post-* div 以更清楚地表明它们具有父子关系(至少这是我根据您的帖子假设的):

<div class="sub-product post-1">
// content goes here
    <div class="sub-product post-2">
    // content goes here
        <div class="sub-product post-3">
        // content goes here
           <div class="sub-product post-4">
               // content goes here
           </div>
        </div>
    </div>
</div>

这样你只需要下面的CSS:

.sub-product[class^=post-] { // class starts with post-
    margin-left: 30px;
}

关于css - 在 css 的帮助下缩进生成的 html div 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17515279/

相关文章:

javascript - 滚动新闻提要与 div 重叠

css - SASS(SCSS、Bourbon、Neat)网格不工作

css - 响应式地在 div 内移动文本

javascript - 动态多列布局

html - 如果内部 div 为空,我想隐藏外部 div

html - 基础 CSS 选择器

css - 有没有办法缩短 css 选择器?

html - 跨设备响应文本

html - 可以将 SVG 剪辑路径相对于其容器居中吗?

css - 我需要在容器内将多个图像居中,每个图像都有自己的 div 标签