CSS Flexbox: 8 Divs Per Row with Margin, Last Item No Margin
To display 8 div elements in each row with a left margin of 10px, and no margin for the last item in each row, you can use the `flexbox` layout in CSS. Here's an example:\n\nHTML:\n\nhtml\n<div class="container">\n <div class="item">1</div>\n <div class="item">2</div>\n <div class="item">3</div>\n <div class="item">4</div>\n <div class="item">5</div>\n <div class="item">6</div>\n <div class="item">7</div>\n <div class="item">8</div>\n <div class="item">9</div>\n <div class="item">10</div>\n <div class="item">11</div>\n <div class="item">12</div>\n <!-- Add more div elements here -->\n</div>\n\n\nCSS:\n\ncss\n.container {\n display: flex;\n flex-wrap: wrap;\n}\n\n.item {\n flex-basis: calc(12.5% - 10px); /* 12.5% to fit 8 items in each row with margin */\n margin-left: 10px;\n margin-bottom: 10px;\n}\n\n.item:nth-child(8n) {\n margin-left: 0;\n}\n\n\nExplanation:\n- The container element is set to `display: flex` with `flex-wrap: wrap` to allow the items to wrap to the next row.\n- Each item is set to `flex-basis: calc(12.5% - 10px)`, which means each item should take up 12.5% of the container width minus 10px for the left margin.\n- The left margin is set to `margin-left: 10px` for all items.\n- The `:nth-child(8n)` selector is used to target the last item in each row (8th, 16th, 24th, etc.) and override the left margin to `margin-left: 0`. \n\nRemember to adjust the `flex-basis` value if you want a different number of items per row or different margin sizes.
原文地址: http://www.cveoy.top/t/topic/pNOO 著作权归作者所有。请勿转载和采集!