简体   繁体   English

带有CSS3的智能列

[英]Smart columns with CSS3

I have some text in a <pre> that is almost doing what I want with column-count and media queries. 我的<pre>中有一些文本几乎可以完成我想要的column-count和媒体查询。

http://jsfiddle.net/tbhtrL0f/11/ (change the size of the result pane to see 1/2/3 columns) http://jsfiddle.net/tbhtrL0f/11/ (更改结果窗格的大小以查看1/2/3列)

There are a couple of things I would like to change about this but don't know how: 关于此,我有两件事想更改,但不知道如何做:

  1. Instead of doing the number of columns based on a media query, I would rather add a new column when there is sufficient horizontal space to do so (without overflow). 与其根据媒体查询来计算列数,不如在有足够水平空间的情况下添加一个新列(而不溢出)。
  2. I would like to preferentially break where there are two returns in a row. 我想优先打破连续有两个退货的地方。

Are either/both of these changes possible with pure CSS? 使用纯CSS是否可以同时进行这两项更改?

Your first question is doable, but the second would require some markup. 您的第一个问题是可行的,但是第二个问题需要一些标记。

  1. You can set the column width instead: column-width: 20em (or which ever width that suits your design). 您可以改为设置列宽: column-width: 20em (或哪种宽度适合您的设计)。 Doing so means that the column will be at least that wide (unless the containing block is even narrower), and new columns will be added as needed as long as they fit at that minimum measure. 这样做意味着该列将至少具有该宽度(除非包含块更窄),并且将根据需要添加新列,只要它们适合该最小尺寸即可。 So no media queries needed. 因此,无需媒体查询。 You could also combine column-width and column-count , which means that the column count is the maximum number of allowed columns. 您还可以结合使用column-widthcolumn-count ,这意味着列数是允许的最大列数。 Finally, there's the columns shorthand, that allows you to set both at the same time: columns: 20em 4; 最后,还有一些columns简写,允许您同时设置两个columns: 20em 4;

  2. There's the break-before / break-after and break-inside properties, and they can be applied to elements within a column layout, but not to whitespace. break-before / break-afterbreak-inside属性,它们可以应用于列布局中的元素 ,但不能应用于空格。 I'd use JS to detect some certain kind of whitespace and inject something to break on. 我将使用JS来检测某种特定的空格并注入一些中断的内容。 Also, if I remember correctly, the column break properties aren't that widely supported, but I can't find a link on that right now.. 另外,如果我没记错的话,分栏符属性并没有得到广泛支持,但是现在我找不到该链接。

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

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