Je pense, compte tenu de la multitude de possibilités offertes par les CSS, qu'il s'agit d'une solution assez élégante et polyvalente avec un code minimal.
L'extrait ci-dessous contient toutes les CSS nécessaires et est accompagné de nombreux commentaires. Il est préférable de l'exécuter en mode 'pleine page'. Redimensionnez d'abord votre navigateur jusqu'à ce que ' rcb 1 a 6 colonnes . Ensuite, commencez à faire défiler et à redimensionner pour voir les différents effets.
À première vue, vous ne vous en rendez peut-être pas compte, mais ce code simple a beaucoup de potentiel en tant que galerie, bande de film, scrollbox (à la fois verticale et horizontale), etc.
Même si ce n'est peut-être pas une solution à 100 %, je suis assez satisfait du résultat !
Jetez-y un coup d'œil et donnez-nous votre avis !
-Addendum- Cette solution a été testée pour fonctionner :
- Android 5+ : navigateur par défaut
- Windows7-x32 : Google Chrome (46+), Firefox (41+), Firefox DE (43+) et IE11+
MISE À JOUR 11/20/15 Quelques modifications de code, voir les commentaires.
/* A few initial globals I always use */
html, body { box-sizing: border-box; height: 100%; width: 100%;
margin: 0; padding: 0; border: 0; cursor: default }
*, *:before, *:after { box-sizing: inherit }
body { max-width: 100%; margin: 0 auto }
/*******************************/
/* The important code */
/*******************************/
.rcb { /* [MANDATORY] Main Responsive Component Box container */
overflow-x: hidden; /* Content wraps vertically, no horizontal scollbar needed */
overflow-y: auto; /* Vertical scrollbar when needed */
width: 100%; /* Maximum width within parent element */
height: auto; /* Adjust height to child element needs */
padding: 15px /* Needed to get from under autohide IE scrollbar, check difference with FF/Ch */
}
.rcb-cmp-list { /* [MANDATORY] Component list, direct child of RCB, controls the flexbox behaviour */
display: flex; /* make element a flexible layout container */
flex-wrap: wrap; /* a row of N columns, wrapping within frame*/
}
.rcb-cmp-item { /* [RECOMMENDED] RCB component list item, default RCB influences flexbox behaviour of parent list */
flex: 1 1; /* add auto or 0, or add some min required width (fiddle away!) */
min-width: 165px; /* best value is a multiple or division of 330px for all types of devices */
max-width: 100%; /* don't make this too small, leave as is or make it a multiple of min-width */
max-height: 100%; /* ditto */
min-height: auto; /* override if you want to set minimum; interacts with flex-basis! */
overflow: hidden; /* Chop off outside content */
margin: 8px /* some space between the boxes */
}
.rcb-cmp-item img {
display: block } /* -Add 11/20/15 - remove the gap below image elements
(by default, IMG is an inline element and causes bottom space) */
.rcb-cmp-item-cnt > * { /* [OPTIONAL] RCB component list content container, fully occupies parent */
min-width: 100%; /* min/max 100% makes sizes fixed to parent */
max-width: 100%
}
/* [OPTIONAL] Media Queries to keep box sizes within reasonable limits (corrected for 8px margin) */
@media all and (min-width: 721px) { .rcb-cmp-item { max-width: calc(33% - 16px) } }
@media all and (min-width: 991px) { .rcb-cmp-item { max-width: calc(25% - 16px) } }
@media all and (min-width: 1321px) { .rcb-cmp-item { max-width: calc(20% - 16px) } }
/*******************************/
/* That's it, the rest is demo */
/*******************************/
/* which you can replace with your own */
body { font-family: Lato; background-color: rgba(96,125,139,1); /* bluegrey 500*/
color: rgba(255,255,255,.87); font-size: 1vmax; line-height: 1.3vmax; }
h3,
.rcb-cmp-list h4 { text-align: center }
.rcb-cmp-item { background-color: #ffc107;
color: rgba(0,0,0,.87) /* amber 500 */;
padding: 4px; /* -update 11/20/15 - see add of ".rcb-cmp-item img" */
border-radius: 2px }
.rcb-cmp-item,
.rcb-cmp-item p img { box-shadow: 0px 2px 2px 0px rgba(0, 0, 0, 0.14),
0px 3px 1px -2px rgba(0, 0, 0, 0.20),
0px 1px 5px 0px rgba(0, 0, 0, 0.12) }
.rcb-cmp-item p img { vertical-align: middle }
.rcb-cmp-item p { font-weight: 400; letter-spacing: 0 }
.rcb-cmp-item iframe { border: none }
<h3>rcb 1</h3>
<div id="rcb-control-1" class="rcb">
<div class="rcb-cmp-list">
<h4 class="rcb-cmp-item">200x200 rectangles</h4>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<h4 class="rcb-cmp-item">odd sized rectangles</h4>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/100"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/400"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/500"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/100"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/400"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/500"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/100"></div></div>
</div>
</div>
<h3>rcb 2</h3>
<div id="rcb-control-2" class="rcb">
<div class="rcb-cmp-list">
<h4 class="rcb-cmp-item">300x150 oblongs</h4>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300x150"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300x150"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300x150"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300x150"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300x150"></div></div>
<h4 class="rcb-cmp-item">odd sized oblongs</h4>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/100x50"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200x100"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300x150"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/400x200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/500x250"></div></div>
</div>
</div>
<h3>rcb 3</h3>
<div id="rcb-control-3" class="rcb">
<div class="rcb-cmp-list">
<h4 class="rcb-cmp-item">portrait 9:16</h4>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<h4 class="rcb-cmp-item">landscape 16:9</h4>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x180"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x180"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x180"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x180"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x180"></div></div>
<h4 class="rcb-cmp-item">mixed 16:9 - 9:16</h4>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x180"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/180x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x180"></div></div>
</div>
</div>
<h3>rcb 4</h3>
<div id="rcb-control-4" class="rcb">
<div class="rcb-cmp-list">
<h4 class="rcb-cmp-item">odd ones out</h4>
<div class="rcb-cmp-item">empty rcb-cmp</div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt">empty rcb-cmp-item-cnt</div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><p>While I used a few oddly shaped images in this demo, on average most images will have ratio 3:2, 4:3 or 16:9</p></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/100x200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><iframe src="http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=0"></iframe><h4>live media</h4></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200x300"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt">this is some text div with a <a href="javascript:void(0)">clickable anchor</a></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/320x200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/400x300"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><p>this is some paragraph with a <a href="javascript:void(0)">clickable anchor</a> and a tiny image <img src="http://dummyimage.com/35"> somewhere inside</p></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/100x150"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200x100"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/300x320"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/200"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/400x100"></div></div>
<div class="rcb-cmp-item"><div class="rcb-cmp-item-cnt"><img src="http://dummyimage.com/257x133"></div></div>
</div>
</div>