简体   繁体   English

如何防止背景色/图像出现在上一行(引导程序)

[英]How do I prevent my background colour/image from appearing in the previous row (bootstrap)

I am trying to apply a background image/colour to the row entitled: <div class="row-fluid promotion"> . 我正在尝试将背景图像/颜色应用于标题为<div class="row-fluid promotion"> However, when I do set such a background, the background appears in the row before, entitled <div class="row-fluid products-row"> I have seen a few articles, on here with similar issues, but their solutions do not seem to answer my problem. 但是,当我设置了这样的背景时,背景出现在标题为<div class="row-fluid products-row">我在此处看到了几篇类似问题的文章,但是他们的解决方案却没有似乎可以回答我的问题。

HTML/PHP HTML / PHP

<div class="row-fluid products-row">
        <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4 product-image-links" id="product-image-a">
            <a href="https://www.example.com/product-a/"><img src="http://www.example.com/wp-content/uploads/2017/03/product-a.jpg" height="200" width="200"></a>
            <h3 class="button"><a href="https://www.example.com/product-a/"><i class="fa fa-circle" aria-hidden="true"></i><span class="product-title">Product A</span><i class="fa fa-circle" aria-hidden="true"></i></a></h3>
        </div>

        <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4" id="product-image-b">
            <a href="https://www.example.com/product-b/"><img src="http://www.example.com/wp-content/uploads/2017/03/product-b.jpg" height="200" width="200"></a>
            <h3 class="button"><a href="https://www.example.com/product-b/"><i class="fa fa-circle" aria-hidden="true"></i><span class="product-title">Product B</span><i class="fa fa-circle" aria-hidden="true"></i></a></h3>
        </div>

        <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4" id="product-c">
            <a href="https://www.example.com/product-c/"><img src="http://www.example.com/wp-content/uploads/2017/03/product-c.jpg" height="200" width="200"></a>
            <h3 class="button"><a href="https://www.example.com/product-c/"><i class="fa fa-circle" aria-hidden="true"></i><span class="product-title">Product C</span><i class="fa fa-cirlce" aria-hidden="true"></i></a></h3>
        </div>
    </div>

    <div class="row-fluid promotion">
        <div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
            <p>Text Text Text</p>
        </div>
        <div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
            <p>Text Text Text</p>  
        </div>
        <div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
            <p>Text Text Text</p>
        </div>
    </div>

CSS 的CSS

.products-row{
    text-align: center;
}
#product-image-a{
    width: 29.3%;
    border-radius: 25px;
    list-style-type: none;
    background-color: green;
    background-image: url("http://www.example.com/wp-content/uploads/2017/03/pattern.png"); 
    padding-top: 25px;
    padding-bottom: 10px;
    -webkit-box-shadow:0 0 20px rgba(0,0,0,0.8);
    -moz-box-shadow:0 0 20px rgba(0,0,0,0.8);
    box-shadow:0 0 20px rgba(0,0,0,0.8);
    margin-left: 2%;
    margin-right: 4%;
    margin-bottom: 50px;
}
#product-image-b{
    width: 29.3%;
    border-radius: 25px;
    list-style-type: none;
    background-color: green;
    background-image: url("http://www.example.com/wp-content/uploads/2017/03/pattern.png"); 
    padding-top: 25px;
    padding-bottom: 10px;
    -webkit-box-shadow:0 0 20px rgba(0,0,0,0.8);
    -moz-box-shadow:0 0 20px rgba(0,0,0,0.8);
    box-shadow:0 0 20px rgba(0,0,0,0.8);
    margin-left: 2%;
    margin-right: 4%;
    margin-bottom: 50px;
}
#product-image-c{
    width: 29.3%;
    border-radius: 25px;
    list-style-type: none;
    background-color: green;
    background-image: url("http://www.example.com/wp-content/uploads/2017/03/pattern.png"); 
    padding-top: 25px;
    padding-bottom: 10px;
    -webkit-box-shadow:0 0 20px rgba(0,0,0,0.8);
    -moz-box-shadow:0 0 20px rgba(0,0,0,0.8);
    box-shadow:0 0 20px rgba(0,0,0,0.8);
    margin-right: 2%;
    margin-bottom: 50px;
}
.button{
    background-color: green;
    font-weight: bold;
    margin-left: 30px;
    margin-right: 30px;
    padding-top: 10px;
    padding-bottom: 10px;
    border-top-left-radius: 15px;
    border-top-right-radius: 40px;
    border-bottom-right-radius: 15px;
    border-bottom-left-radius: 40px;
    -moz-box-shadow:    inset 0 0 10px #000000;
    -webkit-box-shadow: inset 0 0 10px #000000;
    box-shadow:         inset 0 0 10px #000000; 
}
.product-title{
    padding-left: 20px;
    padding-right: 20px;    
}
.promotion{
    background: silver;
    height: 30px;
    background-image: url(http://www.example.com/wp-content/uploads/2017/03/pattern.png);
}

You can target the div by chaining both of the class selectors: 您可以通过链接两个类选择器来定位div:

.row-fluid.promotion {
  // your style
}

This ensures that the rule only applies to the div with both of those classes. 这样可以确保该规则仅适用于具有这两个类的div。

You need to change .row-fluid to .row . 您需要将.row-fluid更改为.row

The only Grid System class that has -fluid in it is .container-fluid . 唯一具有-fluid Grid System类是.container-fluid Since .row-fluid doesn't exist and .row does, .row-fluid wasn't able to clear the floats of your column elements (which .row does). 由于.row-fluid不存在, .row存在, .row .row-fluid无法清除列元素的浮点数( .row做到)。

What's happening and how does one normally fix it? 发生了什么,通常如何解决?

The reason the background for one row moves up behind the previous row is because your rows have floated child elements. 一行的背景向上移到前一行之后的原因是,您的行具有浮动的子元素。 When this happens, the parent collapses and has no height. 发生这种情况时,父对象会崩溃并且没有高度。 The parent collapses because floated elements are taken out of the normal document flow and don't take up space. 父级折叠是因为浮动元素已从常规文档流中删除,并且不占用空间。

The only reason you saw a gray background at all is because you set a height of 30px on .row-fluid via .promotion . 根本看不到灰色背景的唯一原因是因为您通过.promotion.row-fluid上设置了30px的高度。 Otherwise that row would have collapsed also, hiding the gray background. 否则,该行也将崩溃,隐藏灰色背景。

In the below snippet you cannot see the gold background of the container .row element because its children are floated and do not take up space. 在下面的代码段中,您看不到container .row元素的金色背景,因为它的子元素是浮动的并且不占用空间。

 * { box-sizing: border-box; } body { margin: 0; } .row { background-color: gold; } .row > div { float: left; width: 33.333%; min-height: 35px; border: 1px solid #444; } 
 <div class="row"> <div></div> <div></div> <div></div> </div> 

What you need to rectify this is a clearfix. 您需要纠正的是一个明确的解决方案。 Normally I would recommend the Micro Clearfix but you are using Bootstrap 3 and its row classes have a clearfix built in. A clearfix creates a new block formatting context so the parent element doesn't collapse and things like backgrounds and borders can be applied as intended. 通常,我会建议使用Micro Clearfix,但您使用的是Bootstrap 3,并且其行类内置有clearfix。clearfix创建新的块格式化上下文,因此父元素不会折叠,并且可以按预期应用背景和边框之类的东西。

In the following snippet we have cleared the floated child elements from the first example so that the parent element isn't collapsed and you can see the gold background. 在以下代码段中,我们从第一个示例中清除了浮动的子元素,以便父元素不会折叠,并且您可以看到金色背景。 This is what .row does. 这是.row所做的。

 * { box-sizing: border-box; } body { margin: 0; } .row { background-color: gold; } .row > div { float: left; width: 33.333%; min-height: 35px; border: 1px solid #444; } .cf:before, .cf:after { content: " "; /* 1 */ display: table; /* 2 */ } .cf:after { clear: both; } 
 <div class="row cf"> <div></div> <div></div> <div></div> </div> 

After browsing much of the internet, and various trials and errors, I have managed to figure out the solution. 在浏览了很多互联网以及各种试验和错误之后,我设法找到了解决方案。

For anyone, experiencing a similar problem, try inserting clear: both into the affected element, via your CSS file. 对于遇到类似问题的任何人,请尝试通过CSS文件将clear: both插入到受影响的元素中。

Read more: W3Schools 阅读更多: W3Schools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM