简体   繁体   English

ul li 元素超过 DIV 高度

[英]Ul li elements exceeding DIV height

I have a div with fixed height and it has Ul, li elements, so if the contents of the ul/li elements exceed the div height, it should align to the right side and not overflow outside the div.我有一个固定高度的 div,它有 Ul、li 元素,所以如果 ul/li 元素的内容超过 div 高度,它应该对齐到右侧并且不会溢出到 div 之外。 I have tried everything but not able to achieve it, below is my code:我已经尝试了一切但无法实现,下面是我的代码:

 div { max-height: 200px; background: red; }
 <div> <h3>ItemsA</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> <h3>ItemsB</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> <h3>ItemsC</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div>

If you want place a row of li elements.如果你想放置一行 li 元素。 You can give the div display: flex feature.你可以给 div display: flex功能。 If the height of the lis exceeds the height of the div, you can use overflow-y: auto .如果 lis 的高度超过了 div 的高度,可以使用overflow-y: auto This puts the scroll bar to parent div这会将滚动条放到父 div

 .parent { max-height: 200px; background: red; overflow-y: auto; display: flex; flex-wrap: wrap; gap: 20px; }
 <div class="parent"> <div> <h3>ItemsA</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div> <h3>ItemsB</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div> <h3>ItemsC</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div> <h3>ItemsD</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> </div>

First of all I'd highly suggest wrapping each <ul> and its associated <h3> into a container, so you can control the layout much better.首先,我强烈建议将每个<ul>及其关联的<h3>包装到一个容器中,这样您就可以更好地控制布局。 In whatever layout you're using, it would directly affect both the <ul> and the <h3> -elements individually.在您使用的任何布局中,它都会分别直接影响<ul><h3>元素。 By wrapping the two associated elements into separate containers, the layout properties are affecting those containers only.通过将两个关联元素包装到单独的容器中,布局属性仅影响这些容器。

One way to solve this is using flex-wrap .解决这个问题的一种方法是使用flex-wrap By applying flex-direction: column , the flex-flow will try to put the elements in a single column, and if they can't, they will wrap into a new column.通过应用flex-direction: column ,flex-flow 将尝试将元素放在单个列中,如果不能,它们将换行到新列中。

 /* Simple reset */ * { margin: 0; box-sizing: border-box; } .wrapper { max-height: 200px; background: red; display: flex; flex-direction: column; flex-wrap: wrap; }
 <div class="wrapper"> <div class="container"> <h3>ItemsA</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div class="container"> <h3>ItemsB</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div class="container"> <h3>ItemsC</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div class="container"> <h3>ItemsD</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> </div>

 .item-list { display: flex; flex-direction: column; flex-wrap: wrap; height: 100px; overflow-y: scroll; background: red; }
 <div class="item-list"> <div> <h3>ItemsA</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div> <h3>ItemsB</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> <div> <h3>ItemsC</h3> <ul> <li>Item1</li> <li>Item2</li> <li>Item3</li> <li>Item4</li> </ul> </div> </div>

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

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