2017-12-10 21 views
0

私は反応の良いグリッドで遊んでいますが、ネストされた要素に背景色を表示するのに問題があります。css背景色の子供が表示されない

私のHTMLとCSSのページ:
https://jsfiddle.net/ro196bjr/9/

<body> 
    <div class="grid-container"> 
     <div class="row"> 
      <div class="col-4 col-m-6 responsive-margin-bottom"> 
      <div class="box primary"> 
       <h1>Primary Box</h1> 
       <p>This is a paragraph</p> 
      </div> 
      </div> <!-- 25% --> 
      <div class="col-8 col-m-6"> 
      <div class="box secondary"> 
       <h1>Secondary Box</h1> 
       <p>This is a paragraph</p> 
      </div> 
      </div> <!-- 75% --> 
     </div> 
    </div> 
</body> 


* { 
    box-sizing: border-box; 
} 

body { 
} 

/*-------------Layout------------- */ 


#container { 
} 

/*----Grid---- */ 
.grid-container { 
    width: 100%; 
    max-width: 1200px; 
    margin: 0 auto; 
} 

/* For mobile phones: */ 
[class*="col-"] { 
    width: 100%; 
    float: left; 
} 

@media only screen and (min-width: 700px) { 
    /* For tablets: */ 
    .col-m-1 {width: 8.33%;} 
    .col-m-2 {width: 16.66%;} 
    .col-m-3 {width: 25%;} 
    .col-m-4 {width: 33.33%;} 
    .col-m-5 {width: 41.66%;} 
    .col-m-6 {width: 50%;} 
    .col-m-7 {width: 58.33%;} 
    .col-m-8 {width: 66.66%;} 
    .col-m-9 {width: 75%;} 
    .col-m-10 {width: 83.33%;} 
    .col-m-11 {width: 91.66%;} 
    .col-m-12 {width: 100%;} 
} 

@media only screen and (min-width: 950px) { 
    /* For desktop: */ 
    .col-1 {width: 8.33%;} 
    .col-2 {width: 16.66%;} 
    .col-3 {width: 25%;} 
    .col-4 {width: 33.33%;} 
    .col-5 {width: 41.66%;} 
    .col-6 {width: 50%;} 
    .col-7 {width: 58.33%;} 
    .col-8 {width: 66.66%;} 
    .col-9 {width: 75%;} 
    .col-10 {width: 83.33%;} 
    .col-11 {width: 91.66%;} 
    .col-12 {width: 100%;} 
} 

/*---- Box---- */ 

.box{ 
    padding: 25px; 
    margin: 0 0 15px; 
} 

.box .primary{ 
    color: #fff; 
    background-color: #74a57f; 
} 

.box .secondary{ 
    color: #fff; 
    background-color: #077187; 
} 

私はオーバーフローに追加しようとしました:自動;グリッドコンテナには、しかし、それは助けていないようです。誰でも私が間違っていることを知っている?

乾杯

答えて

0

.boxと.primaryの間にはスペースを入れないでください。間にスペースがあると、primary classbox classの子孫ですが、そうではありません。彼らは両方とも同じコンテナクラスを参照するので、それらの間のスペースを削除し、.box.primary or just .primaryと二次同様の基準を利用することができます。

.box.primaryは、ボックスクラスとプライマリクラスの両方を持つコンテナを指します。

.primaryは少なくとも1次クラスを持つコンテナを指します。

* { 
 
    box-sizing: border-box; 
 
} 
 

 
body { 
 
} 
 

 
/*-------------Layout------------- */ 
 

 

 
#container { 
 
} 
 

 
/*----Grid---- */ 
 
.grid-container { 
 
\t width: 100%; 
 
\t max-width: 1200px; 
 
\t margin: 0 auto; 
 
} 
 

 
/* For mobile phones: */ 
 
[class*="col-"] { 
 
    width: 100%; 
 
\t float: left; 
 
} 
 

 
@media only screen and (min-width: 700px) { 
 
    /* For tablets: */ 
 
    .col-m-1 {width: 8.33%;} 
 
    .col-m-2 {width: 16.66%;} 
 
    .col-m-3 {width: 25%;} 
 
    .col-m-4 {width: 33.33%;} 
 
    .col-m-5 {width: 41.66%;} 
 
    .col-m-6 {width: 50%;} 
 
    .col-m-7 {width: 58.33%;} 
 
    .col-m-8 {width: 66.66%;} 
 
    .col-m-9 {width: 75%;} 
 
    .col-m-10 {width: 83.33%;} 
 
    .col-m-11 {width: 91.66%;} 
 
    .col-m-12 {width: 100%;} 
 
} 
 

 
@media only screen and (min-width: 950px) { 
 
    /* For desktop: */ 
 
    .col-1 {width: 8.33%;} 
 
    .col-2 {width: 16.66%;} 
 
    .col-3 {width: 25%;} 
 
    .col-4 {width: 33.33%;} 
 
    .col-5 {width: 41.66%;} 
 
    .col-6 {width: 50%;} 
 
    .col-7 {width: 58.33%;} 
 
    .col-8 {width: 66.66%;} 
 
    .col-9 {width: 75%;} 
 
    .col-10 {width: 83.33%;} 
 
    .col-11 {width: 91.66%;} 
 
    .col-12 {width: 100%;} 
 
} 
 

 
/*---- Box---- */ 
 

 
.box{ 
 
    padding: 25px; 
 
    margin: 0 0 15px; 
 
} 
 

 
.box.primary{ 
 
\t color: #fff; 
 
\t background-color: #74a57f; 
 
} 
 

 
.box.secondary{ 
 
\t color: #fff; 
 
\t background-color: #077187; 
 
}
<body> \t 
 
\t <div class="grid-container"> 
 
\t \t <div class="row"> 
 
\t \t <div class="col-4 col-m-6 responsive-margin-bottom"> 
 
\t \t \t <div class="box primary"> 
 
\t \t \t \t <h1>Primary Box</h1> 
 
\t \t \t \t <p>This is a paragraph</p> 
 
\t \t \t </div> 
 
\t \t </div> <!-- 25% --> 
 
\t \t <div class="col-8 col-m-6"> 
 
\t \t \t <div class="box secondary"> 
 
\t \t \t \t <h1>Secondary Box</h1> 
 
\t \t \t \t <p>This is a paragraph</p> 
 
\t \t \t </div> 
 
\t \t </div> <!-- 75% --> 
 
\t \t </div> 
 
\t </div> 
 
</body>

0

あなたは.box.primary/.secondryスペースで.box .primaryなど.box .secondryを書いているので、あなたは、あなたが来る要素をターゲットにするように、これは、適切な方法でそれをやっていません.boxの子として、実際には、スタイルを設定する要素には両方のクラスが含まれています。

したがって、2つのクラスの間のスペースを削除して、両方のクラスを持つ要素をスタイルすることをブラウザに指示するだけです。

* { 
 
    box-sizing: border-box; 
 
} 
 

 
body { 
 
} 
 

 
/*-------------Layout------------- */ 
 

 

 
#container { 
 
} 
 

 
/*----Grid---- */ 
 
.grid-container { 
 
\t width: 100%; 
 
\t max-width: 1200px; 
 
\t margin: 0 auto; 
 
} 
 

 
/* For mobile phones: */ 
 
[class*="col-"] { 
 
    width: 100%; 
 
\t float: left; 
 
} 
 

 
@media only screen and (min-width: 700px) { 
 
    /* For tablets: */ 
 
    .col-m-1 {width: 8.33%;} 
 
    .col-m-2 {width: 16.66%;} 
 
    .col-m-3 {width: 25%;} 
 
    .col-m-4 {width: 33.33%;} 
 
    .col-m-5 {width: 41.66%;} 
 
    .col-m-6 {width: 50%;} 
 
    .col-m-7 {width: 58.33%;} 
 
    .col-m-8 {width: 66.66%;} 
 
    .col-m-9 {width: 75%;} 
 
    .col-m-10 {width: 83.33%;} 
 
    .col-m-11 {width: 91.66%;} 
 
    .col-m-12 {width: 100%;} 
 
} 
 

 
@media only screen and (min-width: 950px) { 
 
    /* For desktop: */ 
 
    .col-1 {width: 8.33%;} 
 
    .col-2 {width: 16.66%;} 
 
    .col-3 {width: 25%;} 
 
    .col-4 {width: 33.33%;} 
 
    .col-5 {width: 41.66%;} 
 
    .col-6 {width: 50%;} 
 
    .col-7 {width: 58.33%;} 
 
    .col-8 {width: 66.66%;} 
 
    .col-9 {width: 75%;} 
 
    .col-10 {width: 83.33%;} 
 
    .col-11 {width: 91.66%;} 
 
    .col-12 {width: 100%;} 
 
} 
 

 
/*---- Box---- */ 
 

 
.box{ 
 
    padding: 25px; 
 
    margin: 0 0 15px; 
 
} 
 

 
.box.primary{ /*no spaces between the two classes*/ 
 
\t color: #fff; 
 
\t background-color: #74a57f; 
 
} 
 

 
.box.secondary{ /*no spaces between the two classes*/ 
 
\t color: #fff; 
 
\t background-color: #077187; 
 
}
<body> \t 
 
\t <div class="grid-container"> 
 
\t \t <div class="row"> 
 
\t \t <div class="col-4 col-m-6 responsive-margin-bottom"> 
 
\t \t \t <div class="box primary"> 
 
\t \t \t \t <h1>Primary Box</h1> 
 
\t \t \t \t <p>This is a paragraph</p> 
 
\t \t \t </div> 
 
\t \t </div> <!-- 25% --> 
 
\t \t <div class="col-8 col-m-6"> 
 
\t \t \t <div class="box secondary"> 
 
\t \t \t \t <h1>Secondary Box</h1> 
 
\t \t \t \t <p>This is a paragraph</p> 
 
\t \t \t </div> 
 
\t \t </div> <!-- 75% --> 
 
\t \t </div> 
 
\t </div> 
 
</body>

更新JSfiddleを確認してください。

関連する問題