css – 数学帮助我的网格:第n个孩子(一个b)

前端之家收集整理的这篇文章主要介绍了css – 数学帮助我的网格:第n个孩子(一个b)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使一个不依赖于预设列数的网格.我创建了一个小样本来显示情况:
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta http-equiv="content-type" content="text/html; charset=utf-8">
  5. <title>Grid in HTML5 and CSS3</title>
  6. <style>
  7.  
  8. * {margin:0;padding:0;}
  9. .row {display:block;position:relative;clear:both;}
  10. .row>* {display:block;position:relative;clear:both;float:left;clear:none;width:100%;}
  11. .row>*:empty {width:0px;}
  12.  
  13. /* one column in the row */
  14. .row>:nth-last-child(1):nth-child(1) {width:100%;}
  15.  
  16. /* two columns in the row */
  17. .row>:nth-last-child(2):nth-child(1) {width:50%;}
  18. .row>:nth-last-child(1):nth-child(2) {width:50%;}
  19.  
  20. /* three columns in the row */
  21. .row>:nth-last-child(3):nth-child(1) {width:33.33%;}
  22. .row>:nth-last-child(2):nth-child(2) {width:33.33%;}
  23. .row>:nth-last-child(1):nth-child(3) {width:33.34%;}
  24. .row>:empty:nth-last-child(3):nth-child(1)+:not(:empty) {width:66.66%;}
  25. .row>:empty:nth-last-child(2):nth-child(2)+:not(:empty) {width:66.67%;}
  26.  
  27. article {margin:.5em;border:1px solid green;border-radius:.3em;padding:.5em; }
  28. </style>
  29.  
  30. </head>
  31. <body>
  32.  
  33. <section class="row">
  34. <div>
  35. <article>
  36. <p>This row has only one child.</p>
  37. </article>
  38. </div>
  39. </section>
  40.  
  41. <section class="row">
  42. <div>
  43. <article>
  44. <p>This row has two children</p>
  45. </article>
  46. </div>
  47. <div>
  48. <article>
  49. <p>This is the second child</p>
  50. </article>
  51. </div>
  52. </section>
  53.  
  54. <section class="row">
  55. <div>
  56. <article>
  57. <p>
  58. This row has three children
  59. </p>
  60. </article>
  61. </div>
  62. <div>
  63. <article>
  64. <p>So this is col 2 of 3</p>
  65. </article>
  66. </div>
  67. <div>
  68. <article>
  69. <p>And this is col 3 of 3.</p>
  70. </article>
  71. </div>
  72. </section>
  73.  
  74. <section class="row">
  75. <div></div>
  76. <div>
  77. <article>
  78. <p>The first child of this row is empty so spanned with the second</p>
  79. </article>
  80. </div>
  81. <div>
  82. <article>
  83. <p>This is the second column</p>
  84. </article>
  85. </div>
  86. </section>
  87.  
  88. <section class="row">
  89. <div>
  90. <article>
  91. <p>This is the first column</p>
  92. </article>
  93. </div>
  94. <div></div>
  95. <div>
  96. <article>
  97. <p>The second and third column are spanned</p>
  98. </article>
  99. </div>
  100. </section>
  101.  
  102. </body>
  103. </html>

我在jsfiddle上放了一个更大的样本 – 更详细地描述了这些问题

http://jsfiddle.net/jordenvanforeest/MDv32/

我现在的问题是,如果您希望这个网格可以容纳3列以上,则CSS大小将呈指数级增长.所以我正在寻找一个其他的解决方案.我试图做类似的事情

  1. .row>:nth-last-child(an+b):nth-child(cn+d) {}

但我的微积分技能有点生锈,我无法让它正常工作.
帮助将不胜感激.

更新

thirtydot提供了一个使CSS更小的答案.这个fiddle是他提出的改进版本.

任何其他建议仍然受到欢迎.我的12个并列网格仍然需要30K的跨度.

解决方法

你可以做一些类似的显示:table combined with table-layout: fixed.

浏览器支持http://caniuse.com/css-table(但限制因素为:not() and :empty)

参见:http://jsfiddle.net/thirtydot/MDv32/3/

你可以看到,它看起来几乎相同.有一些聪明才智,您应该可以使用我的技术复制演示中的大部分功能.我在我停下来的演示中注释出了HTML.

CSS:

  1. .row {
  2. display: table;
  3. table-layout: fixed;
  4. width: 100%;
  5. }
  6. .row > * {
  7. display: table-cell;
  8. }
  9. .row > :empty {
  10. display: none;
  11. }
  12. /* for example: */
  13. .row > :empty + :not(:empty) + :last-child:not(:empty) {
  14. width: 33.33%;
  15. }

猜你在找的CSS相关文章