【问题标题】:CSS - Wrapping very last text to another line if it doesn't fitCSS - 如果最后一个文本不合适,则将其包装到另一行
【发布时间】:2013-03-31 17:33:51
【问题描述】:

我在 4 个段落的末尾有一个链接。比如:

Here is my text, and it's just an example of text to show
And I am wanting to wrap the last bit of text, which happens
to be a link, but I need the link to either stay on the line
that is the available width and not break to another line,
unless it can't fit left-aligned on that line like this
http://mylink.com

因此,HTML 中的实际代码如下:

<p>Here is my text, and it's just an example of text to show
And I am wanting to wrap the last bit of text, which happens
to be a link, but I need the link to either stay on the line
that is the available width and not break to another line,
unless it can't fit left-aligned on that line like this<br />
<a href="http://mylink.com">http://mylink.com</a></p>

我在这里使用&lt;br /&gt;,但我不想使用&lt;br /&gt;,因为屏幕会捕获实际显示的文本量,有时它可能看起来像这样:

Here is my text, and it's just an example of text to show And I am wanting to wrap
the last bit of text, which happens to be a link, but I need the link to either stay
on the line that is the available width and not break to another line, unless it can't 
fit left-aligned on that line like this
http://mylink.com

因此,在上述情况下,我需要它不要使用 &lt;br /&gt; 标记来分解它,而是应该像这样呈现:

Here is my text, and it's just an example of text to show And I am wanting to wrap
the last bit of text, which happens to be a link, but I need the link to either stay
on the line that is the available width and not break to another line, unless it can't 
fit left-aligned on that line like this http://mylink.com

这可以通过 CSS 实现吗?可能是一些text-align 值,或者为了在不使用&lt;br /&gt; 标记的情况下完成此任务?但是,如果链接可以自然地放在行上,那么它不应该向下移动到下一行。相反,它应该保持在那条线上。

谢谢!

【问题讨论】:

    标签: css line-breaks word-wrap


    【解决方案1】:

    一些浏览器将您的 URL 中的斜杠解释为允许打破 单词 的字符,而其他浏览器则不会。您需要做的就是调整链接的空白属性

    http://jsfiddle.net/Msc4S/

    a {
        white-space: pre;
    }
    

    【讨论】:

      【解决方案2】:

      根据需要使用此 html:

      <p>Here is my text, and it's just an example of text to show
      And I am wanting to wrap the last bit of text, which happens
      to be a link, but I need the link to either stay on the line
      that is the available width and not break to another line,
      unless it can't fit left-aligned on that line like this
      <a href="http://mylink.com">http://mylink.com</a></p>
      

      但是将它添加到 CSS 中。

      a {
          display: block;
      }
      

      但是,这会使a 在任何地方都出现在新行上,因此您必须使用此 CSS 更多地指定它

      p a {
          display: block;
      }
      

      这是 jsfiddle 示例 http://jsfiddle.net/vzmtB/(不使用 p a)和 http://jsfiddle.net/vzmtB/1/(使用 p a)。

      【讨论】:

      • 我想你误解了我想要做什么。从您的 jsfiddle 示例的末尾删除几句话,您会注意到链接保留在下一行。这不是我要的。如果整个链接可以放在该行上,我需要它来填充该行,否则,它将需要换行。
      • 那么在您的问题中添加更多示例。
      • 请花点时间阅读整个问题。我实际上提供了一个例子。
      • 不,你不能用 CSS 做,用 JS 也很难做。
      • @DanielImms - 哦,真臭! :( :( 我想知道这是否可以通过计算最后一行文本的宽度和链接的宽度来用 jQuery 完成...?
      【解决方案3】:

      我认为没有中断的默认自动换行已经这样做了。当某些东西不适合行时它会移动到下一行,但如果它可以适合则不会移动到下一行。您需要定义父元素的宽度,其中文本将适合。 check this fiddle.

      nothing you need to do in css.
      

      【讨论】:

      • 不,如果就这么简单,我不会问这个问题。在您的 jsfiddle 中,链接将转到下一行。我需要的是,如果整个链接自然不适合该行,则将其移至新行,否则将其保留在该行上。这听起来非常简单,好像不知何故,这应该是某处 CSS 的一部分,但出了点问题,从未在其中实现过!
      • 嗯,我没听懂你。整个链接是什么意思?如果一个词被链接,那么这个词就不会中断。它将进入同一行,如果可以是 ift,但如果不是,那么它将进入下一行。在第二段中,链接不符合行,所以它出现在下一行。 check this fiddle
      • 但是我并没有真正使用http://mylink.com 作为链接,我使用的链接是这样的:&lt;a href="http://mylink.com"&gt;This is my link&lt;/a&gt; 但无论如何,您的示例仍然显示链接换行(中链接之间的方式)到下一行。所以它的一部分在第 2 到最后一行,其余的链接在最后一行(即使它是 1 个字......实际链接)。
      • 如果链接文本中有多个单词,那么我认为您需要设置空白;锚定标签 check this fiddle已编辑:- 哦,伙计,我没有看到上面的答案。它已经回答了。
      猜你喜欢
      • 2013-04-28
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      相关资源
      最近更新 更多