html - 删除边距底部的第 N 个子表达式在响应式中不起作用

标签 html css responsive-design css-selectors

我想做的是创建一个 nth-child 表达式,以在桌面 View 中删除最后一行的边距底部,并在响应 View 中删除最后一项的边距底部,这里是一个示例,或者您可以检查<强> live demo here

HTML

<div id="wrapper">
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="box">
            <p>Test goes here...</p>
        </div>
    </div>
</div>  <!-- end wrapper -->

CSS

.box{
    border: 1px solid  #DFDFDF;
    padding: 20px;
    margin-bottom: 30px;
    text-align: center;
}

所以我尝试下面的代码:

.col-md-3:nth-last-child(-n+4) .box {
     margin-bottom: 0;
}

它在大 View 中完美运行,但在响应式 View 中显示时它不起作用。希望有人能给我建议。

最佳答案

您需要 @media 查询来执行此操作。

通常使用 .col-md-3:not(:nth-last-child(-n+4)) .box 选择器来设置 margin-bottom。它将选择除最后四个 div 之外的所有内容。

对于响应式 View ,使用 .col-md-3:not(:last-of-type) .box 选择器来设置 margin-bottom。这将选择除最后一个 div 之外的所有内容。

.col-md-3宽度是使用JavaScript找到的,是991px

关于 CodePen 的工作演示

.box{
  border: 1px solid  #DFDFDF;
  padding: 20px;
  text-align: center;
}
.col-md-3:not(:nth-last-child(-n+4)) .box {
  margin-bottom: 30px;
}

@media only screen and (max-width: 991px){
  .col-md-3:not(:last-of-type) .box {
    margin-bottom: 30px;
  }
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div id="wrapper">
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
  <div class="col-md-3">
    <div class="box">
      <p>Test goes here...</p>
    </div>
  </div>
</div>  <!-- end wrapper -->

关于html - 删除边距底部的第 N 个子表达式在响应式中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652725/

相关文章:

css - 如何在响应式布局上添加页脚?

html - 在之后添加带有伪元素的 'before'

css - DT Shiny 不同的自定义列标题按列

javascript - 如何更改动态 AngularJS ng 类中字形的颜色?

CSS:四个边上的框阴影具有模糊效果

html - 我的网站没有响应,它在视口(viewport)集上显示水平滚动

html - 响应式 CSS 链接不起作用

php - Drupal 7 - 向页面添加 block

javascript - 使用 javascript 将 HTML 代码添加到 div 中

javascript - 将 <div> 叠加在另一个(动态定位的)<div> 上