CSS 可以在元素中的每个单词后强制换行吗?
我正在建立一个多语言网站,网站所有者帮助我进行一些翻译。一些显示的短语需要换行以保持网站的风格。
不幸的是,所有者不是计算机专家,因此如果他看到 foo
,他就有可能在翻译时以某种方式修改数据。
gt;bar
是否有 CSS 解决方案(除了更改宽度)可应用于每个单词后都会中断的元素?
(我知道我可以在 PHP 中做到这一点,但我想知道在 CSS 中是否有一个我不知道的巧妙技巧来完成同样的事情,也许在 CJK 功能中。)
编辑
我我会尝试用图表来说明正在发生的事情:
---------------- ----------------
| Short Word | | Gargantuan |
| | | Word |
---------------- ----------------
长词会自动中断,而短词则不会。我希望它看起来像这样:
---------------- ----------------
| Short | | Gargantuan |
| Word | | Word |
---------------- ----------------
I'm building a multilingual site, with the owner helping me with some translations. Some of the displayed phrases need line breaks to maintain the style of the site.
Unfortunately, the owner isn't a computer guy, so if he sees foo<br />bar
there's the chance he'll modify the data somehow as he's translating.
Is there a CSS solution (besides changing the width) to apply to an element which would break after every word?
(I know I can do this in PHP, but I'm wondering if there's a nifty trick I don't know about in CSS to accomplish the same thing, perhaps in the CJK features.)
EDIT
I'll attempt to diagram what's happening:
---------------- ----------------
| Short Word | | Gargantuan |
| | | Word |
---------------- ----------------
The long word breaks automatically, the short word doesn't. I want it to look like this:
---------------- ----------------
| Short | | Gargantuan |
| Word | | Word |
---------------- ----------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您无法针对 CSS 中的每个单词。不过,只要使用一点 jQuery,您也许就可以做到。
使用 jQuery,您可以将每个单词包装在
中,然后 CSS 将 span 设置为
display:block
,这会将其放在自己的行上。当然是理论上的:P
You can't target each word in CSS. However, with a bit of jQuery you probably could.
With jQuery you can wrap each word in a
<span>
and then CSS set span todisplay:block
which would put it on its own line.In theory of course :P
我在一个项目中这样做了,客户希望将 3 个词的标题放在不同的行上。基本上,您可以使用 CSS 增加空格,并使用空格来分隔行。
I did this on a project where the client wanted the 3 word title on a different line. Basically your increase the spaces with CSS the use the white-space to separate the lines.
https://jsfiddle.net/bm3Lfcod/1/
对于那些寻求有效解决方案的人一个灵活的父容器,其子容器在两个维度上都是灵活的。例如。导航栏按钮。
https://jsfiddle.net/bm3Lfcod/1/
For those seeking for a solution that works within a flexible parent container with a children that is flexible in both dimensions. eg. navbar buttons.
我遇到了同样的问题,这里的选项都没有帮助我。某些邮件服务不支持指定的样式。
这是我的版本,它解决了问题并且在我检查的任何地方都有效:
或使用 CSS:
I faced the same problem, and none of the options here helped me. Some mail services do not support specified styles.
Here is my version, which solved the problem and works everywhere I checked:
OR using CSS:
如果您可以在每个单词周围添加标记(例如
span
),那么您可以使用 CSS 在每个单词后面插入换行符。请注意,我们插入的“内容”是
\a
,它是十进制 10 的十六进制,即换行符。If you can add markup (e.g. a
span
) around each word then you can use CSS to insert a line break after each.Note that the "content" we are inserting is
\a
which is hex for decimal 10 which is the newline character.TL;DR:拆分单词并使用
标签将它们连接起来。注1:这是一个非CSS替代解决方案。
注2:这是针对我自己的具体问题的,它工作得很好,我也接受缺点;即需要更多的计算能力,添加 JavaScript 并在每个单词之间添加额外的
标签。同样,这在我的 React.js 项目中运行良好,我想我会分享它,以防有人发现它有用。
TL;DR: Split the words and join them with a
<br>
tag.Note 1: This is a non-CSS alternative solution.
Note 2: This is specific to my own specific problem and it works fine and I'm okay with the cons; i.e. more compute power needed, the addition of JavaScript and adding extra
tags between each word. Again, this works fine in my React.js project and I thought I'd share it in case someone finds it useful.
就我而言,
效果很好,希望它能帮助像我这样的其他新手。
In my case,
worked perfecly, hope it helps any other newcomer like me.
使用
其中
是父元素的宽度(或不适合一行的任意高值)。这样您就可以确保单个字母后甚至有换行符。适用于 Chrome/FF/Opera/IE7+(甚至可能适用于 IE6,因为它也支持字间距)。Use
where
<parent-width>
is the width of the parent element (or an arbitrary high value that doesn't fit into one line). That way you can be sure that there is even a line-break after a single letter. Works with Chrome/FF/Opera/IE7+ (and probably even IE6 since it's supporting word-spacing as well).@HursVanBloob 给出的答案仅适用于固定宽度的父容器,但在流体宽度容器的情况下失败。
我尝试了很多属性,但都没有达到预期的效果。最后我得出的结论是,给
word-spacing
一个非常大的值效果非常好。或者,对于现代浏览器,您可以使用 CSS
vw
单位(视觉宽度,以屏幕尺寸的百分比表示)。The answer given by @HursVanBloob works only with fixed width parent container, but fails in case of fluid-width containers.
I tried a lot of properties, but nothing worked as expected. Finally I came to a conclusion that giving
word-spacing
a very huge value works perfectly fine.or, for the modern browsers you can use the CSS
vw
unit (visual width in % of the screen size).尝试使用
white-space: pre-line;
。它会在代码中出现换行符的地方创建换行符,但忽略额外的空白(制表符和空格等)。首先,将您的单词写在代码中的单独行上:
然后将样式应用到包含单词的元素。
请小心,元素内代码中的每个换行符都会创建一个换行符。因此,编写以下内容将导致第一个单词之前和最后一个单词之后出现额外的换行符:
有一篇关于 CSS 技巧 解释其他空白属性。
Try using
white-space: pre-line;
. It creates a line-break wherever a line-break appears in the code, but ignores the extra whitespace (tabs and spaces etc.).First, write your words on separate lines in your code:
Then apply the style to the element containing the words.
Be careful though, every line break in the code inside the element will create a line break. So writing the following will result in an extra line break before the first word and after the last word:
There's a great article on CSS Tricks explaining the other white-space attributes.
将句子每行一个单词中描述了一种替代解决方案,通过将
display:table-caption;
应用于元素An alternative solution is described on Separate sentence to one word per line, by applying
display:table-caption;
to the element如果您希望能够从不同的解决方案中进行选择,除了给定的答案之外...
另一种方法是将容器的宽度设置为 0 并确保溢出可见。然后每个单词都会从其中溢出并独占一行。
或者,您可以使用新提议的
width
值之一,前提是在您阅读本文时这些值仍然存在。If you want to be able to choose from different solutions, in addition to the given answers...
An alternative method is to give the container a width of 0 and to make sure overflow is visible. Then each word will overflow out of it and will be on its own line.
Or you can use one of those newly proposed
width
values, provided those still exist by the time you read this.是一个内联元素,我添加一个
display: inline-block
来为元素提供宽度max-width: min-content ;
,min-content
是文本/句子中最小单词的值/宽度。如果您使用
min-content
,“宽度”将是最长的单词。在这种情况下,Example
是您的较长单词。但是,如果您有不同的单词,例如和 if
或几个 2/3 字符的单词,那么这些单词将适合在同一行上。如果您想保持文字行为,您可以指定固定宽度,例如
5px
。在 CodePen 中查看更多示例。
<span>
is an inline element and I'm adding andisplay: inline-block
to give a width to the elementmax-width: min-content;
,min-content
is the value/width of the smallest word in your text/sentance.If you use
min-content
, the "width" will be your longest word. In this caseExample
is your longer word. But if you have different words likeand if
or few 2/3 char words then this words will fit on the same line.If you want to keep the on word behavior you can give a fixed width, for example
5px
.Check more examples in CodePen.
最好的解决方案是
word-spacing
属性。将
添加到具有特定大小(例如
300px
)的容器中,然后必须将该大小添加为字间距中的值。HTML
CSS
这样,你的句子将始终被分解并设置在一个列中,但段落的 with 将会是动态的。
这里是一个示例 Codepen
The best solution is the
word-spacing
property.Add the
<p>
in a container with a specific size (example300px
) and after you have to add that size as the value in the word-spacing.HTML
CSS
In this way, your sentence will be always broken and set in a column, but the with of the paragraph will be dynamic.
Here an example Codepen