简体   繁体   English

如何在固定高度的div中垂直换行

[英]How to wrap text vertically within a fixed-height div

Note: I don't think that the I have worded the question the best possibly way. 注意:我认为我没有以最好的方式表达这个问题。 Feel free to edit the title if someone has a better wording for the question that I am trying to ask. 如果有人对我要提出的问题有更好的措词,请随时编辑标题。


Lets say we have a div that has some fixed height (and width). 假设我们有一个具有固定高度(和宽度)的div。

Once the divs' height increases, a scroll bar will appear. div的高度增加后,将出现一个滚动条。 Instead of the scrollbar, I want text to go into the next 'column' of sorts. 我希望文本而不是滚动条进入下一个“列”。

Example: 例:

A1          A1 A6
A2          A2 A7
A3          A3 A8
A4    =>    A4 A9 
A5          A5
- - - - - - - - - - - <-- where our fixed height is set.
A6          
A7          
A8          
A9          

Thankfully for us (i think), the text is actually an unordered list like below. 值得感谢的是(我认为),该文本实际上是如下所示的无序列表。

<ul>
 <li><a href="">A1</a></li>
 <li><a href="">A2</a></li>
 ...
</ul>

So I am basically making a carousel with text which would have about 16rows in a column. 因此,我基本上是在制作带有文字的轮播,该文字每列大约有16行。 Is there a direct way to make the text just go to the right once its finished with its available height or will I have to parse the list using javascript, break it into divs and then float them all? 有没有一种直接的方法可以使文本在其可用高度结束后才向右移动,还是我必须使用javascript解析列表,将其拆分为div,然后全部浮动? I am going to be using anchors based on the first letter of the text (A, B, C..) to scroll left/right. 我将基于文本的第一个字母(A,B,C ..)使用锚点向左/向右滚动。

Any help will be appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

EDIT: Is there something that would work for IE8+ (possibly degrade gracefully for IE6/7), Safari, Chrome and Firefox? 编辑:是否有适用于IE8 +(可能对IE6 / 7降级的功能),Safari,Chrome和Firefox的工具? I always forget to add various browsers to the question. 我总是忘记为问题添加各种浏览器。

This can be done using CSS3 columns quite easily. 使用CSS3列可以很容易地做到这一点。 Here's an example, HTML: 这是一个示例,HTML:

<ul id = "limheight">
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
 <li><a href="">Glee is great</a></li>
</ul>

CSS: CSS:

#limheight {
    height: 300px; /*your fixed height*/
    -webkit-column-count: 3;
       -moz-column-count: 3;
            column-count: 3; /*3 in those rules is just placeholder -- can be anything*/
}
#limheight li {
    display: inline-block; /*necessary*/
}

And a little demo: little link . 还有一点演示: 一点链接

Hope that helped! 希望能有所帮助!

Not sure if I understood you correctly, but CSS has a column-property you might find useful. 不知道我是否理解正确,但是CSS具有列属性,您可能会觉得有用。 Read more about it here: 在此处了解更多信息:

http://www.w3schools.com/css3/css3_multiple_columns.asp http://www.w3schools.com/css3/css3_multiple_columns.asp

Note: Does not work in <= IE9, so beware. 注意:在<= IE9中不起作用,请当心。

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

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