Home >Web Front-end >CSS Tutorial >CSS multi-column layout properties: column-count and column-gap

CSS multi-column layout properties: column-count and column-gap

PHPz
PHPzOriginal
2023-10-28 08:35:331711browse

CSS 多列布局属性:column-count 和 column-gap

CSS multi-column layout properties: column-count and column-gap, specific code examples are required

In front-end development, implementing multi-column layout is a very common requirement . In CSS, there are two properties that can help us easily implement multi-column layout, they are column-count and column-gap.

  1. column-count attribute

column-count attribute is used to specify how many columns the content of an element is divided into. It accepts a positive integer value indicating how many columns to divide the content into. It is worth noting that when the column-count attribute is set, the browser will automatically help us calculate and layout the number of columns.

The following is an example:

HTML code:

<div class="columns">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <p>Morbi sit amet urna leo. Suspendisse potenti.</p>
  <p>Nunc dapibus vitae libero a tristique. Donec vitae commodo sem, eu tristique erat.</p>
  <p>Fusce tristique lorem sed ipsum consectetur, nec laoreet dui euismod.</p>
  <p>Maecenas eget sem id ex feugiat placerat. Sed nec enim placerat, tristique diam in, sollicitudin sapien.</p>
</div>

CSS code:

.columns {
  column-count: 3;
}

The above code will wrap the dc6dce4a544fdca2df29d5ac0ea9906b element Paragraph text is displayed in 3 columns. The browser will automatically layout the content based on the length and number of columns to achieve multi-column display.

  1. column-gap attribute

column-gap attribute is used to specify the distance between columns. It accepts a length value that represents the spacing between columns. We can use pixel values, percentages or keywords to set the corresponding distance.

The following is an example:

HTML code:

<div class="columns">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <p>Morbi sit amet urna leo. Suspendisse potenti.</p>
  <p>Nunc dapibus vitae libero a tristique. Donec vitae commodo sem, eu tristique erat.</p>
  <p>Fusce tristique lorem sed ipsum consectetur, nec laoreet dui euismod.</p>
  <p>Maecenas eget sem id ex feugiat placerat. Sed nec enim placerat, tristique diam in, sollicitudin sapien.</p>
</div>

CSS code:

.columns {
  column-count: 3;
  column-gap: 20px;
}

The above code will wrap the dc6dce4a544fdca2df29d5ac0ea9906b element Paragraph text is displayed in 3 columns, with a 20-pixel spacing between each column.

Summary:

By using the column-count and column-gap attributes, we can easily implement multi-column layout. column-count is used to specify how many columns the content is divided into, and column-gap is used to specify the distance between columns. These two attributes can quickly help us achieve multi-column layout effects and have good controllability.

The above is an introduction to the CSS multi-column layout properties column-count and column-gap. I hope it will be helpful to you. Everyone is welcome to try to use these properties in actual projects to achieve better layout effects.

The above is the detailed content of CSS multi-column layout properties: column-count and column-gap. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn