如何在不使用
的情况下从 css 换行?
如何在没有
的情况下实现相同的输出?
<p>hello <br> How are you </p>
输出:
hello How are you
How to achieve same output without <br>
?
<p>hello <br> How are you </p>
output:
hello How are you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
您可以使用
white-space: pre;
使元素的行为类似于You can use
white-space: pre;
to make elements act like<pre>
, which preserves newlines. Example:不可能使用相同的 HTML 结构,您必须有一些东西来区分
Hello
和How are you
。我建议使用
span
,然后将其显示为块(实际上就像一样)。
Impossible with the same HTML structure, you must have something to distinguish between
Hello
andHow are you
.I suggest using
span
s that you will then display as blocks (just like a<div>
actually).正常使用
,但用display: none
隐藏它我希望大多数发现这个问题的人都希望使用响应式设计来决定是否使用换行符出现在特定位置。
虽然不是很明显,但您可以将
display:none
应用于
以隐藏它。这意味着您可以像使用任何其他 CSS 一样使用媒体查询。
如果您需要根据屏幕宽度在不同位置强制换行,这会很有用。当然,如果您想始终防止特定位置发生换行,您也可以使用
。
jsfiddle 示例
Use
<br/>
as normal, but hide it withdisplay: none
I would expect most people finding this question are looking to use responsive design to decide whether or not a line-break appears in a specific place.
While not immediately obvious, you can apply
display:none
to a<br/>
in order to hide it.This means you can use media queries just as you would for any other css.
This can be useful if you need to force a line break in a different place based on the screen width. And of course you can also use
if you want to always prevent a line break from happening in a specific place.
jsfiddle example
有多个选项可用于定义空格和换行符的处理。
如果可以将内容放入
标签中,那么就很容易获得想要的任何内容。
为了保留换行符而不是空格,请使用
pre-line
(而不是pre
),如下所示:如果需要另一种行为,请选择其中之一 (WS=WhiteSpace, LB= LineBreak):
来源:W3 学校
There are several options for defining the handling of white spaces and line breaks.
If one can put the content in e.g. a
<p>
tag it is pretty easy to get whatever one wants.For preserving line breaks but not white spaces use
pre-line
(notpre
) like in:If another behavior is wanted choose among one of these (WS=WhiteSpace, LB=LineBreak):
SOURCE: W3 Schools
CSS 中的"\a" 命令生成回车符。这是 CSS,而不是 HTML,因此它应该更接近您想要的:没有额外的标记。
在
blockquote
中,下面的示例显示标题和源链接,并用回车符 ("\a"
) 分隔两者:The "\a" command in CSS generates a carriage return. This is CSS, not HTML, so it shall be closer to what you want: no extra markup.
In a
blockquote
, the example below displays both the title and the source link and separate the two with a carriage return ("\a"
):在 CSS 中使用代码
使用此 CSS,
P
标记内的每个输入都将成为 HTML 中的断线。In the CSS use the code
With this CSS every enter inside the
P
tag will be a break-line at the HTML.基于之前所说的,这是一个可行的纯 CSS 解决方案。
Building on what has been said before, this is a pure CSS solution that works.
要使元素在后面有换行符,请指定它:
display:block;
块级元素之后的非浮动元素将出现在下一行。许多元素,例如
和
虽然知道这一点很好,但这实际上更多地取决于您的内容的上下文。在您的示例中,您不想使用 CSS 强制换行。
>是合适的,因为从语义上来说,p 标签最适合您正在显示的文本。仅仅为了挂起 CSS 而添加更多标记是不必要的。从技术上讲,它不完全是一个段落,但是没有<问候语>。标签,所以使用你拥有的。使用 HTMl 很好地描述您的内容更为重要 - 在您完成这些之后,然后想办法让它看起来更漂亮。
To make an element have a line break afterwards, assign it:
display:block;
Non-floated elements after a block level element will appear on the next line. Many elements, such as <p> and <div> are already block level elements so you can just use those.
But while this is good to know, this really depends more on the context of your content. In your example, you would not want to use CSS to force a line break. The <br /> is appropriate because semantically the p tag is the the most appropriate for the text you are displaying. More markup just to hang CSS off it is unnecessary. Technically it's not exactly a paragraph, but there is no <greeting> tag, so use what you have. Describing your content well with HTMl is way more important - after you have that then figure out how to make it look pretty.
或
来源:https://stackoverflow.com/a/36191199/2377343
OR
source: https://stackoverflow.com/a/36191199/2377343
这是一个糟糕问题的糟糕解决方案,但确实符合要点:
Here's a bad solution to a bad question, but one that literally meets the brief:
使用
overflow-wrap:break-word;
如下:Use
overflow-wrap: break-word;
like:也许有人会遇到和我一样的问题:
我在一个带有
display: flex
的元素中,所以我必须使用flex-direction: column
。Maybe someone will have the same issue as me:
I was in a element with
display: flex
so I had to useflex-direction: column
.对于链接列表
其他答案提供了一些根据情况添加换行符的好方法。但应该注意的是,
:after
选择器是实现 CSS 对链接列表的控制(以及类似事物)的更好方法之一,原因如下。这是一个例子,假设有一个目录:
这是 Simon_Weaver 的技术,它更简单、更兼容。它没有将样式和内容分开,需要更多代码,并且在某些情况下您可能想要在事后添加中断。不过,这仍然是一个很好的解决方案,特别是对于旧版 IE。
请注意上述解决方案的优点:
pre
相比)display:block
注释)float
或clear
样式影响周围的内容
或带有硬编码中断的pre
)a.toc:after
和For a List of Links
The other answers provide some good ways of adding line breaks, depending on the situation. But it should be noted that the
:after
selector is one of the better ways to do this for CSS control over lists of links (and similar things), for reasons noted below.Here's an example, assuming a table of contents:
And here's Simon_Weaver's technique, which is simpler and more compatible. It doesn't separate style and content as much, requires more code, and there may be cases where you want to add breaks after the fact. Still a great solution though, especially for older IE.
Note the advantages of the above solutions:
pre
)display:block
comments)float
orclear
styles affecting surrounding content<br/>
, orpre
with hard-coded breaks)a.toc:after
and<a class="toc">
我喜欢非常简单的解决方案,这里还有一个:
和 CSS:
I like very simple solutions, here is one more:
and CSS:
将
br
标记设置为display: none
很有帮助,但最终您可能会得到 WordsRunTogether。我发现用空格字符替换它更有帮助,如下所示:HTML:
CSS:
Setting a
br
tag todisplay: none
is helpful, but then you can end up with WordsRunTogether. I've found it more helpful to instead replace it with a space character, like so:HTML:
CSS:
来源:http://www.w3schools.com/tags/tag_pre.asp
How about
<pre>
tag?source: http://www.w3schools.com/tags/tag_pre.asp
代码可以是:
CSS 可以是:
The code can be:
CSS would be:
您需要在
中声明内容。之后线路将被打破。
\A
表示换行符。You need to declare the content within
<span class="class_name"></span>
. After it the line will be break.\A
means line feed character.您可以添加大量填充并强制文本拆分为新行,例如,
在响应式设计的情况下对我来说工作得很好,只有在一定的宽度范围内才需要拆分文本。
You can add a lot of padding and force text to be split to new line, for example
Worked fine for me in a situation with responsive design, where only within a certain width range it was needed for text to be split.
一个现代而简单的解决方案可以设置宽度,如下所示:
width: min-content;
此 CSS 规则对文本内容最有用,但不仅限于:
https://developer.mozilla.org/en-US/文档/Web/CSS/min-content
A modern and simple solution could be setting the width like that:
width: min-content;
This CSS rule is mostly useful for text content, but not only:
https://developer.mozilla.org/en-US/docs/Web/CSS/min-content
使用
空白
对于没有空格的长句子不起作用,例如HiHowAreYouHopeYouAreDoingGood...etc
来解决此问题使用word-wrap:break-word;
来代替它是为了允许长单词能够断行并换行到下一行。,它由 Facebook 使用,Instagram 和我
Using
white-space
will not work for long sentences without spaces likeHiHowAreYouHopeYouAreDoingGood...etc
to fix this consider usingword-wrap: break-word;
insteadit's made to allow long words to be able to break and wrap onto the next line., its used by Facebook, Instagram and me ????
Example
在 CSS-tricks 上,
Chris Coyier
进行了很多测试选项,最后一个非常简洁的选项是使用display:table
,每个选项都有自己的问题,当您在跨度上使用background-color
时,您会发现这些问题!在这里您可以看到
codepen
上的所有其他选项:注入换行符
On CSS-tricks,
Chris Coyier
have tested lots of options and the final and pretty neat one was usingdisplay:table
, Each one have their own problems which you will find out when you usebackground-color
on the span!Here You can see all other options on
codepen
:Injecting a Line Break
文森特·罗伯特和乔伊·亚当斯的答案都是有效的。但是,如果您不想更改标记,则只需使用 JavaScript 插入
即可。在 CSS 中没有办法在不改变标记的情况下做到这一点。
Both Vincent Robert and Joey Adams answers are valid. If you don't want, however, change the markup, you can just insert a
<br />
using javascript.There is no way to do it in CSS without changing the markup.
就我而言,我需要一个输入按钮在其前面有一个换行符。
我将以下样式应用于按钮并且它起作用了:
In my case, I needed an input button to have a line break before it.
I applied the following style to the button and it worked:
如果这对某人有帮助......
你可以这样做:
使用这个CSS:
In case this helps someone...
You could do this:
With this css:
使用
代替空格可以防止中断。
Using
instead of spaces will prevent a break.
我猜您不想使用断点,因为它总是会断线。这是正确的吗?如果是这样,如何在文本中添加断点
,然后给它一个类似
的类,然后使用媒体查询位于您希望其中断的大小的正上方以隐藏
,因此它会在特定宽度处中断,但保持内联于该宽度之上。HTML:
CSS:
https://jsfiddle.net/517Design/o71yw5vd/
I'm guessing you did not want to use a breakpoint because it will always break the line. Is that correct? If so how about adding a breakpoint
<br />
in your text, then giving it a class like<br class="hidebreak"/>
then using media query right above the size you want it to break to hide the<br />
so it breaks at a specific width but stays inline above that width.HTML:
CSS:
https://jsfiddle.net/517Design/o71yw5vd/
这在 Chrome 中有效:
This works in Chrome:
简单的解决方案。
将外部段落类命名为“父级”,将内部段落命名为“子级”。
设置 css 属性
父级: margin-bottom: 0;
child: margin-top: 0;
一切顺利。
Simple easy solution.
Name th external paragraph class as 'parent' and internal paragraph as 'child'.
Set css property
parent: margin-bottom: 0;
child: margin-top: 0;
You are good to go.