简体   繁体   English

如何将Label,Button或TextArea保持为一行?

[英]How do I keep a Label, Button, or TextArea as a single line?

Lets say I have a Label, Button, or TextArea object, that contains some amount of text. 可以说我有一个Label,Button或TextArea对象,其中包含一些文本。 The way that things work by default is that text put in these objects will automatically word wrap around to the next line. 默认情况下,工作方式是将这些对象中的文本自动自动换行到下一行。 Is there a way to disable this? 有没有办法禁用此功能? I am aware that the CSS attribute 我知道CSS属性

overflow : hidden ;

will stop the scrollbar from showing up. 将停止滚动条的显示。 But is there a way to stop the text from going to the next line? 但是,有没有一种方法可以阻止文本进入下一行?

I wish it to be the case that if I have a string that is "wider" than the object it is placed within, it will simply write out the string to the limit of what the object can contain, without wrapping it to the next line? 我希望情况是这样的:如果我的字符串比放置在其中的对象“宽”,它将简单地将字符串写成对象可以容纳的限制,而不必将其包装到下一行? Anyone have a way of doing this? 有人有办法吗?

Thank you. 谢谢。

You can use the following css definition to achieve this: 您可以使用以下css定义来实现此目的:

<style type="text/css">

.element {
        width:200px;
        overflow: hidden;
        text-overflow: ellipsis;
        -o-text-overflow: ellipsis;
        white-space: nowrap;        
}

</style>

<div class="element">
This text will not wrap.  Hamina hamina hamina hamina hamina.
</div>

This should prevent any text from wrapping to the next line. 这样可以防止任何文本换行到下一行。 If the text exceeds the width of the element, it cuts off. 如果文本超过元素的宽度,它将被截断。 If you are using webkit / explorer you will get a nifty ellipsis effect where the text cuts off (to suggest that there is more text than is visible). 如果您使用的是webkit / explorer,则会在文字被截断的地方得到一个漂亮的省略号效果(暗示文字多于可见文字)。

Unfortunately firefox does not support ellipsis. 不幸的是,Firefox不支持省略号。 But the text will still cut off and will not wrap. 但是文本仍然会被截断并且不会自动换行。

I haven't tested this defintion with button or textarea elements - only with divs. 我还没有使用button或textarea元素测试此定义-仅使用divs。 But I see no reason it should not work. 但我认为没有理由不起作用。 I leave it to you to experiment. 我把它留给你去试验。

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

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