Original: the When And How the To the Use CSS Multi-Column Layout
Author: Rachel Andrew
Translator: Bo Hin
When we put the attention on CSS Grid
the layout and CSS Flexbox
layout, they often ignore another layout method. In this article, I will introduce multi-column layout - commonly referred to as multicol
or "the Columns CSS" . Through this article, you learned to use columns
best practices, as well as the use of some columns
precautions.
What is Multicol?
multicol
The basic idea is that you can put a large piece of content to the multiple columns, like a newspaper. You can use one of two properties to accomplish this. You can use the cloumn-count
property to specify the number of columns content. You can also use column-width
properties to specify the desired column width, so the browser to determine the appropriate number of columns.
No matter what your content contains elements, when you convert it to a multi-column layout, everything will work from the content stream, but the column will show the form. This makes the multicol
layout and now common in browsers other layouts will be different. For example Flexbox
and Grid
acquire container elements child elements, so that these sub-elements involved flex
and grid
layout in the past. Use multicol
, within each column, you also get a normal content stream.
In the following example, we are using column-width
, the minimum column width 14em
. Multicol
Will allocate as much as the width 14em
of the column, then, let each column share the remaining space. At least the width of each column 14em
, unless the container is smaller than the width 14em
, it will only show one. multicol
Also CSS
in an act for the first time, you can create a column, but also in line with the default response type rules. You do not increase the CSS query ( Media Queries
), modify the number of columns for each breakpoint, but to develop an optimum width, so that the browser itself for processing.
// 核心代码摘录
.container {
max-width: 800px;
margin: 0 auto;
column-width: 14em;
}复制代码
Column style
When you use the column properties ( columns
) to create the layout when cloumns box
the content could not be located. You can not use JavaScript
to locate, you can not specify a unique style for a single column, such as background color, margins, padding, and so on. All column boxes
will remain the same size. The only thing you can do is to use the column-rule
property to add rules between columns, similar to the role of the property border
. You can also use the column-gap
property to control the gaps between the columns, the default value of the property 1em
, but you can also be changed to any valid length units.
// 核心代码
.container {
max-width: 800px;
margin: 0 auto;
column-width: 14em;
column-gap: 2em;
column-rule: 1px solid #ccc;
}复制代码
This is a multicol
basic function. You can split a large content column. Content will turn to fill each column, including the direction and with the creation of these columns. You can control the gap between the columns and the rules, use the border
same values can be. So far, all of the above in your browser to get good support, and has been around for a long time, making the specification backwards compatibility is very safe.
You may want to consider some other things the use of columns, as well as some potential issues to be aware of when using the column on the Web.
Hurdle
Sometimes, you may want to break down some of the content is also listed, the use of an element across these columns. The columns-span
property used to multicol
be to achieve this.
The following example, I use a <blockquote>
element to span columns. Note that, when performing this operation, the content is divided into two regions, each is positioned at the column-span
upper and lower parts of the elements. Content will not skip a set column-span
of elements.
column-span
Property has been implemented in Firefox, and backward compatibility.
// 核心代码
blockquote {
font: 2em 'Berkshire Swash', cursive;
margin: 0;
column-span: all;
text-align: center;
}
.container {
max-width: 800px;
margin: 0 auto;
column-width: 14em;
column-gap: 2em;
column-rule: 1px solid #ccc;
}复制代码
You know, the current norms, column-span
values only all
, or none
. If you want to achieve in newspaper style, can be used in conjunction with other multi-column layout layout to achieve. In the following example, we achieve a container with two column grid rails (Grid). The width of the track is on the left 2fr
, the width of the right side of the track is 1fr
. We left the track into a two-column with a multicol
container. It also contains an element of hurdles.
On the right, we will put the contents of the second Grid
column of the track. By trying a variety of layout method, we can figure out exactly fit the layout method for the task at hand - do not be afraid to mix and match!
// 核心代码
.container {
max-width: 800px;
margin: 0 auto;
display: grid;
grid-gap: 1em;
grid-template-columns: 2fr 1fr;
align-items: start;
}
.container article {
column-count: 2;
column-gap: 2em;
column-rule: 1px solid #ccc;
}
.container aside {
border-left: 1px solid #ccc;
padding: 0 1em;
}复制代码
A cutoff control
If your content is included in the title, you might want to avoid title appears at the end of the column, and the content has appeared in the next column. As another example, your content with a picture of some text, then Ideally, pictures and his text, will remain a whole show without being split across columns.
When you split column content, this behavior is known as CSS fragments module (Fragmentation) . The content is divided into two on display, just like a printer printing mode select two columns of the same. Thus, other methods on the layout of the Web compared to multicol
the closest Paged Media
layout method. Therefore, we can use the property belonging to the CSS2.1 page-break
to achieve this.
page-break-before
page-break-after
page-break-inside
Recently, the CSS fragments module specification defines attributes fragmentation module fragments the context of how the various design specifications include Paged Media
, multicol
and a region style (Region Styling) at a standstill; will support display region is decomposed (fragmented) continuous content. By using these common attributes, they may be applied to any future CSS fragments module context, as Flexbox
the property is moved to align Box Alignment
the specification, like, so that they can Grid
and Block
use the layout.
break-before
break-after
break-inside
In the following example, I <figure>
use the element break-inside avoid
properties that prevent separation of the image and title. Support the property browser, we should see the effect of the joint together, even if that results in the contents of the column looks unbalanced.
// 核心代码
figure {
margin: 0;
break-inside: avoid;
}
.container {
max-width: 800px;
margin: 0 auto;
column-width: 14em;
}复制代码
Unfortunately, in multicol
support of these attributes are very incomplete. Even in the case of support, they should be seen as a suggestion, because in actual use will be made a lot of requirements to control whether content should be separated, but in fact the browser and does not really let them separate from the content. In the above example, we define the priority show, but when you use or be careful and use only where it is most needed.
Some problems Columns Layout
When we browse the Web, and did not see multicol
being widely used, the reason is reading habits of readers, usually top-down scrolling to read. For people to use English, or other vertical writing, the horizontal layout is not a good reading experience.
If you fixed height of the container, for example, viewport
the unit vh
, when the content is too large, it will inline direction of the overflow, while, you will get a horizontal scroll bar.
These cases are not very satisfactory. So, when using a multi-column layout, we need to carefully consider the amount of content for each column.
Columns overflow block
In the Level 2
specification which, we consider how to use a method, the contents of the column does not overflow, generated lateral scroll bar, but in the column direction to generate a new block above. This means that you can have a high degree of having multicol
a container, and once the content exceeds the width of the container, it will create a new set are listed below. It looks a bit like crossing our example above, however, not to make a set column-span
of elements results in a new column box to start, but by the block size limit of the width of the container, so as to solve the problem of overflow.
This feature will enable multicol
to the Web more useful. Although we are still some distance, but you can focus on the issue of the CSS Working Group repurchase . If you have other use cases for this feature, please post them, it is very useful when designing new features.
Today, how do we use Multicol?
Using the current standard, it is not recommended to split all the contents of the column regardless of the rolling problem. However, in some cases, multicol
to web
be ideal. When viewing the design mode, there are enough examples of k for your reference.
UI text element or folded small
multicol
Can be used anywhere you need to take up less space in the list of projects. For example, a simple list or a list of names check box. Usually in these cases, the visitor will not read one and then return to the top of the next column, but scanning the content to click to select the check box or item to be selected. Even if you do make scrolling experience, the problem still exists.
You can DonarMuseum website to see Sander de Jong used in this way of multicol
examples.
Confirm the contents of a small amount
Sometimes, we design a website, we know that some of the content area is relatively small and fit most screen display, and does not require scrolling. I Notist demo page used multicol
for introduction speech.
Andy Clarke is Equfund website design a lovely example.
In order to avoid the very small screen leading to scroll the page appears, just as the width, you can use media queries to check the height. For content beyond the minimum height, you can set a breakpoint min-height
to avoid the use of smaller user equipment brings poor experience.
SUMMARY cascade (MASONRY-LIKE) Show
Another local multi-column layouts work beautifully if you want to create content waterfall displays. Multicol
It is currently the only, you can create highly uncertain layout method. Grid
Either arrangement will leave a gap or stretch the project to form a strict two-dimensional grid.
Veerle Pieters has a good example of that is her inspiration pages used in this manner multicol
.
Grid and alternatives Flexbox
These column-
properties can also be used Grid
and Flex
layout options. If you specify a property on which the container is used display: flex
or display: grid
to remove any column behavior, it is converted to the container Flex
or Grid
layout. For example, if you have to use a Grid
card layout layout, if it instead of running in the column on the page, then the layout is readable, you can use multicol
as a simple backup. Does not support the Grid
browser will multicol
show support for Grid
the browser will get Grid Layout
.
Do not forget Multicol!
I always answer Grid
and Flexbox
question, the answer is not to use Grid
or Flexbox
instead try Multicol
. You may not use it on each site, but when you encounter scenes mentioned in this article, it can be very useful. On MDN about multicol and CSS fragments module to helpful resources.
If you have a project in multicol
other usage scenarios, also welcome message to share!
This article has contacted the original author and to authorize the translation, reprint please retain the original link
Reproduced in: https: //juejin.im/post/5cfc2ff76fb9a07f0a2dd290