低于768像素的视口上侧面的不同填充

时间:2018-07-13 06:30:04

标签: html css twitter-bootstrap

我正在制作一个包含块元素的网站。当我低于768像素时,元素周围将带有边框。块元素每一侧的填充都不同。我需要网站在5px上具有相同的填充。我试图通过添加代码来解决这个问题:

@media only screen and (max-width: 768px) {
    .row {
        margin-left: -5px;
        margin-right: -5px;
    }

    .row>[class*="col-"] {
        padding-left: 5px;
        padding-right: 5px;
    }
}

但是不能解决问题。有人知道我该如何解决吗? (当我将我的HTML代码粘贴到Stackoverflow编辑器中时,div标签会跳来跳去。对此很抱歉,我不知道为什么。所以html代码看起来很杂乱

/* Card and Button */

.index-content a:hover {
  color: black;
  text-decoration: none;
}

.index-content {
  /*margin-bottom: 20px;*/
  /*padding: 50px 0px;*/
}

.index-content .row {
  margin-top: 20px;
}

.index-content a {
  color: black;
}

.index-content .card {
  background-color: #FFFFFF;
  padding: 0;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}

.index-content .card:hover {
  box-shadow: 0 16px 24px 2px rgba(0, 0, 0, 0.14), 0 6px 30px 5px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(0, 0, 0, 0.3);
  color: black;
}

.index-content .card img {
  width: 100%;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  height: 350px;
}

.index-content .card h4 {
  margin: 20px;
}

.index-content .card p {
  margin: 20px;
  opacity: 0.65;
}

.index-content .blue-button {
  width: 100px;
  -webkit-transition: background-color 1s, color 1s;
  /* For Safari 3.1 to 6.0 */
  transition: background-color 1s, color 1s;
  min-height: 20px;
  background-color: #002E5B;
  color: #ffffff;
  border-radius: 4px;
  text-align: center;
  font-weight: lighter;
  margin: 0px 20px 15px 20px;
  padding: 5px 0px;
  display: inline-block;
}

.index-content .blue-button:hover {
  background-color: #dadada;
  color: #002E5B;
}

@media (max-width: 768px) {
  .index-content .col-lg-4 {
    margin-top: 20px;
  }
}

@media (min-width: 768px) {
  .card {
    position: relative;
  }
  .card-content {
    position: absolute;
    bottom: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .card-content h4,
  .card-content p {
    color: white;
    width: 100%;
    float: left;
    margin: 0 0 5px;
  }
  .card-content a {
    float: right;
  }
  .index-content .card h4,
  .index-content .card p {
    padding: 15px 20px;
    margin: 0;
  }
  .index-content .card p {
    padding: 0 20px 15px;
    margin: 0;
  }
}


/* GRID ELEMENTS */


/* Set width between block elements */

.small-padding.top {
  padding-top: 10px;
}

.small-padding.bottom {
  padding-bottom: 10px;
}

.small-padding.left {
  padding-left: 5px;
}

.small-padding.right {
  padding-right: 5px;
}


/* Media quries */

@media only screen and (max-width: 768px) {
  .small-padding.bottom {
    padding-bottom: 10px;
  }
}

@media only screen and (max-width: 768px) {
  .row {
    margin-left: -7.5px;
    margin-right: -7.5px;
  }
  .row>[class*="col-"] {
    padding-left: 7.5px;
    padding-right: 7.5px;
  }
}
<div class="row">
  <div class="index-content">
    <div class="container">
      <a href="#">
        <div class="col-sm-12 small-padding bottom">
          <div class="card">
            <img src="https://image.ibb.co/nJ97Go/bmw.jpg"></img>
            <div class="card-content">
              <h4>BMW NEW MODELS</h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
              <a href="#" class="blue-button">Read More</a>
            </div>
          </div>
        </div>
      </a>
    </div>
  </div>
</div>
<div class="row">
  <div class="index-content">
    <div class="container">
      <div class="col-sm-4 small-padding right bottom">
        <div class="card">
          <img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
          <div class="card-content">
            <h4>BMW NEW MODELS</h4>
            <a href="#" class="blue-button">Read More</a>
          </div>
        </div>
      </div>
      <div class="col-sm-8 small-padding left">
        <div class="card">
          <img src="https://image.ibb.co/nJ97Go/bmw.jpg"></img>
          <div class="card-content">
            <h4>BMW NEW MODELS</h4>
            <!-- <p>Brødtekst</p> -->
            <a href="#" class="blue-button">Read More</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

4 个答案:

答案 0 :(得分:2)

只需添加 !important(如果要应用相同的填充)。 还要从您的CSS中删除>

@media only screen and (max-width: 768px) {
        .row {
          margin-left: -5px !important;
          margin-right: -5px !important;
        }
        .row [class*="col-"] {
          padding-left: 5px !important;
          padding-right: 5px !important;
        }

    }

答案 1 :(得分:1)

您为什么不使用Bootstrap grid类?然后就不需要自定义CSS。

<div class="col-sm-12 col-md-12 col-xs-12">
</div>

答案 2 :(得分:1)

如果仅将class="col-sm-8"用于中等屏幕,则必须对所有屏幕尺寸都使用引导程序类,正确的用法是:

<div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
</div>

答案 3 :(得分:0)

.row>[class*="col-"]class="col-的直接子元素中寻找任何.row,您的HTML并非如此。因此,删除>

 .row [class*="col-"] {
      padding-left: 5px;
      padding-right: 5px;
  }

或者您也可以设置.container [class*="col-"]