css - Container with max-width won't wrap inline-block children when resized -


given html :

 <section class="wrapper clearfix">     <section class="col">         <img src="http://placehold.it/200x120" alt="">     </section>     <section class="col">         <img src="http://placehold.it/200x120" alt="">     </section> </section> 

...and css

@media screen , (max-width: 600px) {     .col {         max-width: 150px;     } }   .wrapper {     max-width: 500px;     margin: 0 auto;     background: grey;     font-size: 0; }  .col {     width: 200px;     margin-right: 100px;     display: inline-block; }  .col:last-child {     margin-right: 0px; }  img {     max-width: 100%; } 

demo - http://jsfiddle.net/j77cd856/4/

the container won't wrap around elements when media query gets activated. same thing happens floated children (which normal, guess?)

i managed fix following clearfix, still don't know why happens or whether there better way fix it.

.clearfix {    content: "";    display: table;    clear: both; 

}

demo - http://jsfiddle.net/j77cd856/6/

demo

one option add "inner" wrapper; wrapper within wrapper. can make display inline-block , set text-align center on parent wrapper. finally, remove grey background wrapper , apply inner wrapper.

just 1 drawback when make window small .col divs not lined on left. not sure if that's issue you

html

<section class="wrapper clearfix">      <div class='inner-wrap'>         <section class="col">             <img src="http://placehold.it/200x120" alt="">         </section>         <section class="col">             <img src="http://placehold.it/200x120" alt="">         </section>             </div>     </section> 

css

@media screen , (max-width: 600px) {     .col {         max-width: 150px;     } }  .inner-wrap {     display: inline-block;     background: grey; } .wrapper {     text-align: center;     max-width: 500px;     margin: 0 auto;     font-size: 0; }  .col {     width: 200px;     margin-right: 100px;     display: inline-block; }  .col:last-child {     margin-right: 0px; }  img {     max-width: 100%; } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -