Text doesn't wrap properly between elements having white-space: nowrap

孤者浪人 提交于 2019-12-07 07:43:22

问题


If I give all the children of an element white-space: nowrap, white space doesn't break between the elements where it should in webkit (and blink):

jsfiddle.net/VJyn2

.pairs {
    width: 180px;
    overflow: hidden;
}
.pairs > span {
    white-space: nowrap;
}
<div class="pairs">
    <span>
        <strong>bread:</strong>
        <em>crust</em>
    </span>
    <span>
        <strong>watermelon:</strong>
        <em>rind</em>
    </span>
    ...
</div>

The intention of the CSS is to keep the word pairs together, but allow the text to break between the <span> elements. This works as expected in IE and FireFox.

But, in Webkit based browsers (safari, chrome, opera), rather than pushing a too-long span to the next line, the span gets clipped.

This is a bug in webkit (and blink), right? Is there a workaround?


回答1:


As of today (Chrome v42) this bug is no longer an issue. Chrome has fixed the rendering bug, so the below work around is no longer necessary.

Nothing to see here, move along.


There are a few ways to work around this bug. Here are three options:

CSS Technique

Use float: left. Besides making it wrap correctly, this will also collapse the whitespace between the spans, so add a margin-right as well.

.pairs > span {
    white-space: nowrap;
    float: left;
    margin-right: 0.5em;
}

jsfiddle.net/VJyn2/3

HTML Technique

Add a zero-width space (U+200b) between each <span>:

<div class="pairs">
    <span>
        <strong>bread:</strong>
        <em>crust</em>
    </span>
    &#x200b;
    <span>
        <strong>watermelon:</strong>
        <em>rind</em>
    </span>
    &#x200b;
    <span>
        <strong>banana:</strong>
        <em>peel</em>
    </span>
    ...
</div>

jsfiddle.net/VJyn2/2

A Better HTML Technique

It turns out, all that is required to get this to work properly is to put the span elements on the same line in the HTML:

<div class="pairs">
    <span><strong>bread:</strong> <em>crust</em></span>
    <span><strong>watermelon:</strong> <em>rind</em></span>
    <span><strong>banana:</strong> <em>peel</em></span>
    ...
</div>

jsfiddle.net/VJyn2/7




回答2:


try this:

.pairs {
width: 180px;
overflow: hidden;
}
.pairs > span {
display:block;
}



回答3:


Adding display: inline-block; on adjacent span elements should help:

<span style='display: inline-block;'>



回答4:


just remove white-space property and use word-wrap.

copy paste css below its working:

.pairs {
width: 180px;
overflow: hidden;
}

.pairs > span {
word-wrap:break-word;
}


来源:https://stackoverflow.com/questions/21198456/text-doesnt-wrap-properly-between-elements-having-white-space-nowrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!