简体   繁体   English

如何在每个列框之间添加空格

[英]How to add a white space between each column boxes

I just wanna ask how to add a white space between each column boxes.我只是想问如何在每个列框之间添加一个空格。 I've been trying to figure it out for the past hour but I was not able to find an answer.过去一个小时我一直在试图弄清楚,但我找不到答案。 Here is the code:这是代码:

HTML: HTML:

<div class="row">
    <div class="column" style="background-color:#aaa;">
        <h2>Column 1</h2>
        <p>Some text..</p>
    </div>
    <div class="column" style="background-color:#bbb;">
        <h2>Column 2</h2>
        <p>Some text..</p>
    </div>
    <div class="column" style="background-color:#ccc;">
        <h2>Column 3</h2>
        <p>Some text..</p>
    </div>
    <div class="column" style="background-color:#bbb;">
        <h2>Column 4</h2>
        <p>Some text..</p>
    </div>

CSS CSS

.column {
    float: left;
    width: 25%;
    padding: 15px;
    height: 300px;
}

.row:after {
    content: "";
    display: table;
    clear: both;
}

Consider using either CSS Grid or CSS Flex-box for your layout;考虑使用 CSS Grid 或 CSS Flex-box 进行布局; either of those options will allow you to specify the gap (formerly grid-gap , but since changed to work with both Grid and Flex-box) between items;这两个选项中的任何一个都允许您指定项目之间的gap (以前是grid-gap ,但后来改为同时使用 Grid 和 Flex-box); for example with Grid:例如与网格:

 document.querySelector('button').addEventListener('click', (e) => { let source = document.querySelector('.row'); [...source.cloneNode(true).children].forEach( (el) => source.append(el) ); });
 *, ::before, ::after { box-sizing: border-box; margin: 0; padding: 0; } .row { /* triggers the use of CSS grid layout: */ display: grid; /* defines four columns each of which is 1fr - one fractional unit of the available space: */ grid-template-columns: repeat(4, 1fr); /* defines a gap of 0.2em above and below each grid-row, and of 1em between adjacent grid- columns: */ gap: 0.2em 1em; counter-reset: columnCount; } .column { padding: 15px; height: 300px; counter-increment: columnCount; } h2::after { content: ' ' counter(columnCount); }
 <button>Add more content to the row</button> <div class="row"> <div class="column" style="background-color:#aaa;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#bbb;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#ccc;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#bbb;"> <h2>Column</h2> <p>Some text..</p> </div>

Or, with Flex-box;或者,使用 Flex-box; note that with flex-box the gap property doesn't take effect until the contents are sufficiently closely-packed that the space-between declaration of justify is insufficient to keep them separated:请注意,使用 flex-box 时, gap属性不会生效,直到内容足够紧密以至于justify声明space-betweenspace-between不足以将它们分开:

 document.querySelector('button').addEventListener('click', (e) => { let source = document.querySelector('.row'); [...source.cloneNode(true).children].forEach( (el) => source.append(el) ); });
 *, ::before, ::after { box-sizing: border-box; margin: 0; padding: 0; } .row { /* triggers the use of css flex-box layout: */ display: flex; /* allows the content of the .row element(s) to wrap to new lines as required: */ flex-wrap: wrap; /* evenly spaces the child elements of .row across the available space (so, by default, the items are separated visually from each other): */ justify-content: space-between; /* when the children of the .row element are too numerous to be spread apart by the use of 'justify: space-between' the gap forces the gap between horizontal 'rows' of 0.2em and between 'columns' of 1em: */ gap: 0.2em 1em; counter-reset: columnCount; } .column { padding: 15px; height: 300px; counter-increment: columnCount; } h2::after { content: ' ' counter(columnCount); }
 <button>Add more content to the row</button> <div class="row"> <div class="column" style="background-color:#aaa;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#bbb;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#ccc;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#bbb;"> <h2>Column</h2> <p>Some text..</p> </div> </div>

CSS Multi-column is a little different and, currently, less easily used and styled for layout; CSS Multi-column 有点不同,目前不太容易使用和样式化布局; but it does allow some appropriate customisation:但它确实允许一些适当的定制:

 document.querySelector('button').addEventListener('click', (e) => { let source = document.querySelector('.row'); [...source.cloneNode(true).children].forEach( (el) => source.append(el) ); });
 *, ::before, ::after { box-sizing: border-box; margin: 0; padding: 0; } .row { /* setting a column-count property causes the browser to use a multi-column layout for the element, and the child elements of that element will be placed into the appropriate columns (though some fine- tuning may be required): */ column-count: 4; /* sets a 1em gap between adjacent columns: */ column-gap: 1em; counter-reset: columnCount; } .column { padding: 15px; height: 300px; counter-increment: columnCount; } h2::after { content: ' ' counter(columnCount); }
 <button>Add more content to the row</button> <div class="row"> <div class="column" style="background-color:#aaa;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#bbb;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#ccc;"> <h2>Column</h2> <p>Some text..</p> </div> <div class="column" style="background-color:#bbb;"> <h2>Column</h2> <p>Some text..</p> </div> </div>

References:参考:

Bibliography:参考书目:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM