【问题标题】:indenting a generated html div class with the help of css在 css 的帮助下缩进生成的 html div 类
【发布时间】:2013-07-08 04:19:26
【问题描述】:

我正在用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 它确实显示缩进。我想我知道这里出了什么问题,但是显示它们缩进的优雅解决方案是什么?

谢谢

【问题讨论】:

    标签: css css-selectors sass


    【解决方案1】:

    您可以嵌套您的 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;
    }
    

    【讨论】:

      【解决方案2】:

      只需添加这些 CSS 规则:

      .sub-product {
          border: 1px solid gray;
      }
      
      .post-1 {
          margin-left: 30px;
      }
      
      .post-2 {
          margin-left: 60px;
      }
      
      .post-3 {
          margin-left: 90px;
      }
      
      .post-4 {
          margin-left: 120px;
      }
      

      这是DEMO

      【讨论】:

      • 我想对不同的帖子类型应用不同的边距。 Post-1 从一开始就缩进了 30px。后 2 30 更多,依此类推,直到后 4。
      • 是的,它确实有帮助。还有其他方法吗(更简洁的方法)?
      • 否 =( 这种方式只能引用具有特定样式的元素(在这种情况下)。
      【解决方案3】:

      这可以通过 jQuery 巧妙地完成。只需将以下代码添加到您的 HTML 文档中,您就可以对任意数量的 div 执行此操作。

      <script>
      
      //iterate through all divs that have .sub-product class
      $('.sub-product').each(function(){
      
          //save the class of each div in a variable
          var num = $(this).attr('class');
      
          //fetch just the number from each classname
          num = num.replace('sub-product post-', '');
      
          //change the margin-left of each div based on its class number
          $(this).css({
              marginLeft: num*30+'px'
          });
      });
      </script>
      

      工作示例:http://jsfiddle.net/Tv347/11/

      否则,如果你严格地想使用 CSS,你最好的办法是像这样将样式应用于单个 div:

      .post-1{
          margin-left:30px
      }
      
      .post-2{
          margin-left:60px
      }
      
      .post-3{
          margin-left:90px
      }
      
      .post-4{
          margin-left:120px
      }
      

      【讨论】:

      • 我想对不同的帖子类型应用不同的边距。 Post-1 从一开始就缩进了 30px。后 2 30 更多,依此类推,直到后 4。
      • 我提供了一个 javascript 解决方案,它可以普遍处理任何数量的 div :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 2011-06-05
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多