0

我使用<p>标签创建了一个 HTML 段落。段落块的尺寸为:

  • X 尺寸:300 像素。
  • Y 尺寸:400 像素。

如果我插入一长串连续的字符,我希望显示一个水平滚动条。我试图设置 CSS 溢出属性,但我的字符行已损坏,其余字符显示在新行上。

如何显示水平滚动条而不是打断长行字符?

4

4 回答 4

6

如果我正确地看到了您的需求,我相信您需要更改 CSSwhite-space属性:

http://jsfiddle.net/kSUS8/

在我上面的小提琴中,这是我的 HTML。

<p>
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.
</p>​

这是我的CSS:

p {
max-width: 300px;
max-height: 400px;
white-space: nowrap;
overflow: auto;
}​

设置white-spacenowrap取消文本换行。当块级元素的内容超出其尺寸时,设置overflow为生成滚动条。auto

您也可以更改该word-wrap属性,但该属性是 CSS3 属性,white-space而是 CSS1 属性。后者得到更广泛的支持。

于 2012-03-26T21:05:57.170 回答
2
#box {
width:300px;
height:400px;
word-wrap:normal;
overflow:scroll;
}​

​&lt;p id="box">
 It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

​​​​​</p>

您可以在此处查看 jsfiddle

于 2012-03-26T21:06:43.447 回答
0

把它放在你的 CSS 中:

p {
   max-width: 300px;
   max-height: 400px;
   white-space: nowrap;
   overflow-x: scroll;
   }  ​
于 2012-12-18T04:05:34.807 回答
0

尝试overflow: scroll在 CSS 中使用。这应该可以使您的段落自动具有滚动条。

于 2021-01-31T15:20:44.603 回答