无法为水平 html 滚动框输入长行文本

发布于 2024-11-26 17:12:49 字数 810 浏览 1 评论 0原文

我想在我的博客上有一个滚动框,以便我可以向读者显示代码。但是,当实际在开头和结尾“div”之间粘贴代码时,实际编写代码内容的输入区域的文本换行意味着我无法输入足够长的文本行,以致水平滚动条无法显示才会真正投入使用。发布帖子时,文本换行会弄乱代码行。应该像这样的滚动条

<div style="width:500px;height:300px;overflow:scroll;
border-width:2px;border-color:000000;border-style:solid;">
12345678910111213141516171819202122232425262728299999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 
</div>

包裹文本以产生类似这样的内容(无水平滚动)

<div style="width:500px;height:300px;overflow:scroll;
border-width:2px;border-color:000000;border-style:solid;">
123456789101
112131415161718192021222324252
6272829999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999
999999 
</div>

I want to have a scroll box on my blog so that I can show code for my readers. However, when actually pasting the code between the opening and ending "divs" the text wrapping of the entry area where you actually write the content of the code means that I can't enter lines of text that are sufficiently long that the horizontal scroll bar will actually be called into use. The text wrapping mucks up the lines of code when the post is published. A scroll bar that should act like this

<div style="width:500px;height:300px;overflow:scroll;
border-width:2px;border-color:000000;border-style:solid;">
12345678910111213141516171819202122232425262728299999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 
</div>

wraps the text to produce something like this ( no horizontal scrolling )

<div style="width:500px;height:300px;overflow:scroll;
border-width:2px;border-color:000000;border-style:solid;">
123456789101
112131415161718192021222324252
6272829999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999
999999 
</div>

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

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

发布评论

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

评论(1

若水微香 2024-12-03 17:12:49

这只需使用 CSS 即可实现,但从语义上讲,您在代码框中使用了错误的元素。您应该使用

 而不是 
,这样会自动对换行进行排序。然后您应该将代码包装在 标记中。实例: http://jsfiddle.net/ddEPB/2/

<pre style="width:500px;height:300px;overflow:auto;border-width:2px;border-color:000000;border-style:solid;">
<code>12345678910111213141516171819202122232425262728299999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999</code> 
</pre>

我也将其更改为 overflow:auto 以便滚动条仅在需要时出现。

This can be achieved with just CSS, but semantically you are using the wrong element for a code box. You should use <pre> instead of <div> and that will automatically sort out the wrapping. Then you should be wrapping the code in <code> tags. Live example: http://jsfiddle.net/ddEPB/2/

<pre style="width:500px;height:300px;overflow:auto;border-width:2px;border-color:000000;border-style:solid;">
<code>12345678910111213141516171819202122232425262728299999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999</code> 
</pre>

I also changed it to overflow:auto so that scrollbars only appear when needed.

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