简体   繁体   English

div容器:限制文本向右一行

[英]div container: limit to text going on one line to right

I tried adjusting the width in several places of my CSS, but the text keeps on flowing on one line and doesn't wrap within a "leftsidebar". 我尝试在CSS的多个位置调整宽度,但是文本一直沿一行显示,并且没有包含在“左侧栏”中。 There seems to be no limit as to how far my text goes to the right, and I want there to be a limit. 我的文字向右走的距离似乎没有限制,我希望有一个限制。 How do I set that in CSS? 如何在CSS中进行设置?

<h3> JKHJKHJKHKJHJKHJKHJKHKJHJKHJKHKJH</h3> 


#leftsidebar {
  position:fixed; 
  width: 160px; 
  top:150px; 
  margin:0px;
  line-height:165%; 
  white-space:nowrap; 
  z-index:50; 
  padding: 0px 35px;
}

http://jsfiddle.net/4uxcN/ http://jsfiddle.net/4uxcN/

Remove white-space:nowrap; 删除white-space:nowrap; (and eventually add word-wrap: break-word; to respect your div boundaries with your no-spaced long word) to send the text to new lines; (并最终添加word-wrap: break-word;用无间隔的长单词尊重div边界)以将文本发送到新行;

add overflow-x: scroll; 添加overflow-x: scroll; if you instead want the text in one single line but want to have an horizontal scrollbar inside your fixed width div: http://jsfiddle.net/4uxcN/10/ 如果您希望将文本放在一行中,但又想在固定宽度div中使用水平滚动条,则请执行以下操作: http : //jsfiddle.net/4uxcN/10/

如果您在谈论word-wrap: break-word属性,它将在到达父级宽度的末尾时中断单词。

Remove {white-space:nowrap;}, which forces your text to one line and add {word-wrap: break-word} to deal with your very long word. 删除{white-space:nowrap;},将您的文本强制为一行,然后添加{word-wrap:break-word}以处理您很长的单词。

http://jsfiddle.net/4uxcN/6/ http://jsfiddle.net/4uxcN/6/

#leftsidebar {
    word-wrap: break-word;
}

Basically, there are 3 ways you can do it: 基本上,有3种方法可以做到:

  1. Just remove your white-space property. 只需删除您white-space属性。 LINK 链接
  2. Remove white-space property, and add word-wrap: break-word; 删除white-space属性,并添加word-wrap: break-word; property. 属性。 LINK 链接
  3. Set you white-space to pre-wrap instead of nowrap . 将您的white-space设置为pre-wrap而不是nowrap LINK 链接

If you remove 如果删除

white-space:nowrap; 

All text will break at the spaces to fit within the box width. 所有文本都会在空格处中断以适合框的宽度。

Super long strings like the one in you example will not break. 例子中的超长字符串不会中断。 To force stupid long strings to break you can use. 要强制愚蠢的长字符串断开,可以使用。

word-wrap:break-word;

also you could use an overflow property Like 你也可以使用溢出属性

overflow:scroll

It just really depends on the layout you are going for 这实际上取决于您要使用的布局

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

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