CSS如何在li中换行文本

14
我试图在我的幻灯片菜单中换行文字。我有一个最大宽度为200px的ul,但当我输入较长的文本时,文本就看不见了(现在我添加了overflow: visible来展示你我想表达的意思)。如何换行?
```html

进入图片描述

我的代码看起来像这样:

```
<nav class="navbar navbar-default" role="navigation">

<div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
        Dropdown
        <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" style="max-width: 200px" role="menu" aria-labelledby="dropdownMenu1">
        <li role="presentation" class="dropdown-header">Dropdown header</li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
        <li role="presentation" class="disabled"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
        <li role="presentation" class="divider"></li>
        <li role="presentation" class="dropdown-header">Dropdown header</li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
    </ul>
</div>

</nav>

现在是CSS:

<style>

    .dropdown .dropdown-menu {
        -webkit-transition: all 0.3s;
        -moz-transition: all 0.3s;
        -ms-transition: all 0.3s;
        -o-transition: all 0.3s;
        transition: all 0.3s;

        max-height: 0;
        display: block;
        overflow: hidden; <-- here i was trying to add  word-wrap: break-word; but it doesn'twork   
        opacity: 0;
    }

    .dropdown.open .dropdown-menu {
        max-height: 200px;
        opacity: 1;
    }
</style>

也为单词换行添加宽度 - chirag satapara
你是如何为那个项目分隔符设置样式的? - Jinghui Niu
3个回答

29

您可以在 li 上使用 word-wrap: break-word;。请查看下面更新的片段。

.dropdown-menu li, .dropdown-menu li a {
    white-space: normal;
    float: left;
    width: 100%;
    height: auto;
    word-wrap: break-word;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<nav class="navbar navbar-default" role="navigation">

<div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
        Dropdown
        <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" style="max-width: 200px" role="menu" aria-labelledby="dropdownMenu1">
        <li role="presentation" class="dropdown-header">Dropdown header trying to write something</li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
        <li role="presentation" class="disabled"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
        <li role="presentation" class="divider"></li>
        <li role="presentation" class="dropdown-header">Dropdown header</li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
    </ul>
</div>

</nav>


1
这个可以在没有 word-wrap: break-word; 的情况下工作,但是如果没有 white-space: normal; 就无法工作。 - Kari Kääriäinen

7

试试这个:

.dropdown-header {
  overflow-wrap: break-word;
  word-wrap: break-word;
  white-space: normal !important;
}

你可能想要查看 何时使用 important - Jon P
我只在Wenus不改变他的代码时使用!important,没有其他原因,但还是感谢你提供的信息。 - ABlack
将行高设置为normal对我很有帮助,还有上面的几行代码。此外,不需要使用!important。没有!important也可以正常工作。 - Amey079

-1
请尝试使用这段代码。
.dropdown-menu li, .dropdown-menu li a {
    word-break: break-word;
    white-space: normal;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接