简体   繁体   English

无法在 html 和 css 中正确设置 ul 和 li

[英]Trouble setting ul and li properly in html and css

I have a listview using JQuery Mobile.我有一个使用 JQuery Mobile 的列表视图。

This is what it looks like on mobile .这就是它在移动设备上的样子。

I want the height of each li of the listview to grow automatically to accomodate the information and I also need the listview to be scrollable.我希望列表视图的每个 li 的高度自动增长以容纳信息,并且我还需要列表视图是可滚动的。

I found that if I give a fixed height to the listview, it becomes scrollable in both directions.我发现如果我给列表视图一个固定的高度,它就会在两个方向上滚动。 But I want it to be only vertically scrollable and not horizontally.但我希望它只能垂直滚动而不是水平滚动。

I tried increasing the height of each li in the listview in my css, but the text would still only be displayed horizontally and it wont be contained within the li height and width.我尝试在我的 css 中增加列表视图中每个 li 的高度,但文本仍然只能水平显示,并且不会包含在 li 的高度和宽度内。

If i change the overflow property, then the scrollable behaviour disappears.如果我更改溢出属性,那么可滚动行为就会消失。

What do I need to do?我需要做什么?

Below is my css for the listview:以下是我用于列表视图的 css:

.listview-container
{
  margin-left: 25px;
  margin-right: 25px;
  max-height: 300px;
  overflow: scroll;
  width: 100%;
}

.logsView-scroll
{

  -webkit-overflow-scrolling: touch;
}

.listview-li
{
  display: inline-block !important;
}

The html bit for the listview:列表视图的 html 位:

<div class="listview-container">
    <ul id="chickenLog" data-role="listview" class="logsView-scroll">
    </ul>
</div>

PS the li elements are programmatically appended through jquery by cloning the below template: PS li 元素通过克隆以下模板通过 jquery 以编程方式附加:

<ul id="chickenLog" data-role="listview" class="logsView-scroll">
    <li class="listview-li" id="aLog" data-theme="a"><span id="text"></span> 
    </li>
</ul>

li after it is appended附加后的 li

Conclusion:结论:

The li elements are display: inline-block; li元素是display: inline-block; , this will cause them to go next to each other and overflow. ,这将导致它们彼此相邻并溢出。 You can get them to stack on top of one another by changing the display style to block .您可以通过将display样式更改为block来让它们堆叠在一起。

Secondly, the li elements themselves have a span with the text inside.其次, li元素本身与里面的文本有一个span This will also overflow, so you need to give either your li the style white-space: normal;这也会溢出,所以你需要给你的li样式white-space: normal; so the span can inherit this property or directly add it to your span .所以 span 可以继承这个属性或者直接把它添加到你的span

( Note: When using libraries, they can often include their own CSS . So if you've put some styles on an element and they don't seem to be showing, this could be due to the fact that the library is overriding it. To combat this, you can put !important on the end of a style.) 注意:使用库时,它们通常可以包含自己的CSS 。因此,如果您在元素上放置了一些样式,但它们似乎没有显示,这可能是由于库覆盖了它。为了解决这个问题,您可以将!important放在样式的末尾。)

Example:例子:

.listview-li {
    display: block;
    white-space: normal !important;
}

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

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