div 中的 CSS 自动换行

发布于 2024-07-13 05:33:01 字数 904 浏览 6 评论 0原文

我有一个宽度为 250px 的 div。 当内部文本比它宽时,我希望它分解。 div 是 float: left 并且现在有溢出。 我希望通过使用自动换行来消失滚动条。 我怎样才能做到这一点?

<div id="Treeview">
<div id="HandboekBox">
    <div id="HandboekTitel">
        <asp:Label ID="lblManual" runat="server"></asp:Label>
    </div>
    <div id="HandboekClose">
        <asp:ImageButton ID="btnCloseManual" runat="server" 
            ImageUrl="Graphics/close.png" onclick="btnCloseManual_Click" 
            BorderWidth="0" ToolTip="Sluit handboek" />
    </div>
</div>
<asp:TreeView ID="tvManual" runat="server" RootNodeStyle-CssClass="RootNode">
    <Nodes>

    </Nodes>
</asp:TreeView>
</div>

CSS:

#Treeview
{
padding-right: 5px;
width: 250px;
height: 100%;
float: left;
border-right: solid 1px black;
overflow-x: scroll;
}

I have a div with a width of 250px. When the innertext is wider than that i want it to break down. The div is float: left and now has an overflow. I want the scrollbar to go away by using word-wrapping. How can i achieve this?

<div id="Treeview">
<div id="HandboekBox">
    <div id="HandboekTitel">
        <asp:Label ID="lblManual" runat="server"></asp:Label>
    </div>
    <div id="HandboekClose">
        <asp:ImageButton ID="btnCloseManual" runat="server" 
            ImageUrl="Graphics/close.png" onclick="btnCloseManual_Click" 
            BorderWidth="0" ToolTip="Sluit handboek" />
    </div>
</div>
<asp:TreeView ID="tvManual" runat="server" RootNodeStyle-CssClass="RootNode">
    <Nodes>

    </Nodes>
</asp:TreeView>
</div>

CSS:

#Treeview
{
padding-right: 5px;
width: 250px;
height: 100%;
float: left;
border-right: solid 1px black;
overflow-x: scroll;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

迷你仙 2024-07-20 05:33:01

正如安德鲁所说,你的文字应该就是这样做的。

我可以想到一个实例,它将按照您建议的方式运行,那就是如果您设置了空白属性。

看看你的 CSS 中是否没有以下内容:

white-space: nowrap

这将导致文本在同一行上继续,直到被换行符中断。

好的,我很抱歉,不确定之后是否编辑或添加了标记(一开始没有看到)。

Overflow-x 属性是导致滚动条出现的原因。 删除它,div 将调整到包含所有文本所需的高度。

As Andrew said, your text should be doing just that.

There is one instance that I can think of that will behave in the manner you suggest, and that is if you have the whitespace property set.

See if you don't have the following in your CSS somewhere:

white-space: nowrap

That will cause text to continue on the same line until interrupted by a line break.

OK, my apologies, not sure if edited or added the mark-up afterwards (didn't see it at first).

The overflow-x property is what's causing the scroll bar to appear. Remove that and the div will adjust to as high as it needs to be to contain all your text.

平安喜乐 2024-07-20 05:33:01

或者直接使用

word-wrap: break-word;

IE 5.5+、Firefox 3.5+ 和 WebKit 浏览器(例如 Chrome 和 Safari)支持的浏览器。

Or simply use

word-wrap: break-word;

supported in IE 5.5+, Firefox 3.5+, and WebKit browsers such as Chrome and Safari.

过气美图社 2024-07-20 05:33:01

尝试 white-space:normal; 这将覆盖继承 white-space:nowrap;

try white-space:normal; This will override inheriting white-space:nowrap;

漆黑的白昼 2024-07-20 05:33:01

在没有看到渲染的 html 是什么样子以及树视图 div 中的元素应用了哪些样式的情况下,很难明确地说,但我立即想到的是

overflow-x: scroll;

如果删除它会发生什么?

It's pretty hard to say definitively without seeing what the rendered html looks like and what styles are being applied to the elements within the treeview div, but the thing that jumps out at me right away is the

overflow-x: scroll;

What happens if you remove that?

2024-07-20 05:33:01

你可以使用:

overflow-x: auto; 

如果在overflow-x中设置“auto”,则仅当内部尺寸最大为DIV区域时才会出现滚动

you can use:

overflow-x: auto; 

If you set 'auto' in overflow-x, scroll will appear only when inner size is biggest that DIV area

沩ん囻菔务 2024-07-20 05:33:01

我有点惊讶它不只是这样做。 div 内是否还有另一个宽度设置为大于 250 的元素?

I'm a little surprised it doesn't just do that. Could there another element inside the div that has a width set to something greater than 250?

离鸿 2024-07-20 05:33:01

仅设置 width 和 float css 属性将得到一个环绕面板。
下面的示例工作得很好:

<div style="float:left; width: 250px">
Pellentesque feugiat tempor elit. Ut mollis lacinia quam. 
Sed pharetra, augue aliquam   ornare vestibulum, metus massa
laoreet tellus, eget iaculis lacus ipsum et diam. 
</div>

也许还有其他样式可以修改外观?

Setting just the width and float css properties would get a wrapping panel.
The folowing example work just fine:

<div style="float:left; width: 250px">
Pellentesque feugiat tempor elit. Ut mollis lacinia quam. 
Sed pharetra, augue aliquam   ornare vestibulum, metus massa
laoreet tellus, eget iaculis lacus ipsum et diam. 
</div>

Maybe there are other styles in place that modify the appearance?

﹏雨一样淡蓝的深情 2024-07-20 05:33:01

我发现 word-wrap: Anywhere 有效(与另一个答案中提到的 word-wrap: break-word 相反)。

另请参阅:

“自动换行”中的“任何地方”是什么意思“CSS属性?

I found that word-wrap: anywhere worked (as opposed to word-wrap: break-word mentioned in another answer).

See also:

What does "anywhere" mean in "word-wrap" css property?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文