简体   繁体   English

div中的文本:换行时在中间分裂

[英]Text in a div: splitting in middle when it word wraps

I've got a centered text phrase that fits on one line in some devices, but wraps into two lines on others.我有一个居中的文本短语,在某些设备上适合一行,但在其他设备上会换成两行。 The text is in a div .文本位于div中。 On some devices it's just barely too long, so only the last word wraps.在某些设备上,它几乎不会太长,所以只有最后一个单词换行。 Aesthetically, this looks bad.从美学上讲,这看起来很糟糕。 What I wish I could do is split the phrase in the middle, so each line is centered at a similar length.我希望我能做的是将短语从中间分开,所以每行都以相似的长度居中。

I've tried this CSS:我试过这个 CSS:

text-align: center;
margin: 0 auto;
width: 100%;

but that only gets me to the problem I described.但这只会让我遇到我描述的问题。 After googling for how to proceed, I haven't found any viable solutions.在谷歌搜索如何进行之后,我还没有找到任何可行的解决方案。 Actually, I'm not finding anyone trying to ask the same question, so I suspect there's something off about my search terms.实际上,我没有找到任何人试图问同样的问题,所以我怀疑我的搜索词有问题。 How do you word wrap in the middle of the text instead of on the right side?你如何在文本中间而不是在右侧换行?

This is the intrinsic nature of how HTML and web dev works.这是 HTML 和 web 开发工作的内在本质。 It is naturally "responsive" and text will wrap at the width of its parent element.它自然是“响应式”的,文本将以其父元素的宽度换行。 What I am hearing from you is you would like to keep from getting "widows" in your text.我从您那里听到的是,您希望避免在您的文本中出现“寡妇”。 This is not an easy problem to fix in web-dev, but there are a few techniques I have found, most of them require a bit of manual attention which in a way is appropriate considering every instance can be unique.这在 web-dev 中不是一个容易解决的问题,但是我发现了一些技术,其中大多数需要一些手动注意,考虑到每个实例都可以是唯一的,这在某种程度上是合适的。

If you have a precise layout you can set a width or max-width on the parent element, and use media queries to change it when needed.如果您有精确的布局,您可以在父元素上设置widthmax-width ,并在需要时使用媒体查询来更改它。

HTML HTML

<div class="parent">
    <p>Curabitur blandit tempus porttitor. Nulla vitae elit libero, a pharetra augue.</p>
</div>

CSS CSS

.parent {
    max-width: 288px;
}

@media (min-width: 600px) {
    .parent {
        max-width: 568px;
    }
} 

Another technique is to add a non-breaking space &nbsp;另一种技巧是添加不间断空格&nbsp; between the last two words, effectively making them one word that will not wrap to a new line without the other.在最后两个单词之间,有效地使它们成为一个单词,没有另一个单词就不会换行。 This looks a little bit better and can work well especially if they are short words.这看起来更好一些,并且可以很好地工作,特别是如果它们是简短的单词。

HTML HTML

<div class="parent">
    <p>Curabitur blandit tempus porttitor. Nulla vitae elit libero, a pharetra&nbsp;augue.</p>
</div>

Another technique is similar to the &nbsp;另一种技术类似于&nbsp; but instead us a span and styles to achieve the same thing.但是我们使用span和 styles 来实现相同的目标。 This could be useful in a CMS editor or where you need to style the grouping or somewhere a non-breaking space won't work.这在 CMS 编辑器或您需要为分组设置样式的地方或非中断空间不起作用的地方可能很有用。

HTML HTML

<div class="parent">
    <p>Curabitur blandit tempus porttitor. Nulla vitae elit libero, a <span class="nowrap">pharetra augue.</span></p>
</div>

CSS CSS

.nowrap {
    white-space: nowrap;
}

Can you wrap each part of the phrase in its own div and center the divs using flex?您可以将短语的每个部分包装在自己的 div 中并使用 flex 将 div 居中吗?

 #container { border: 1px solid; width: 100%; margin: 0 auto; display: flex; flex-direction: column; align-items: center; }
 <div id="container"> <div>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</div> <div>when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div> </div>

You could set a max-width for the div which is proportionate to where you want the word to be split您可以为 div 设置一个最大宽度,该宽度与您希望单词被拆分的位置成比例

div{
    max-width: 25px;
}

Ps.附言。 25px is just a random value you can check with your code how much you need. 25px 只是一个随机值,你可以用你的代码检查你需要多少。

If you have a good idea of where you would like the break to be then CSS and/or alterations to the HTML work.如果您很清楚希望休息的位置,那么 CSS 和/或对 HTML 工作的更改。

But for a general solution I think you have to resort to Javascript.但对于一般解决方案,我认为您必须求助于 Javascript。

This snippet senses whether more than one line has been used, and if so iterates the width to find the minimum width that keeps the original height.此代码段检测是否使用了多条线,如果是,则迭代宽度以找到保持原始高度的最小宽度。 This spreads the words over the lines as evenly as possible, and it works for multiple lines, not just 2.这会将单词尽可能均匀地分布在各行中,并且它适用于多行,而不仅仅是 2 行。

 function resize() { const fitme = document.querySelector('.fitme'); fitme.style.maxWidth = '100%'; const origH = fitme.offsetHeight; const str = fitme.innerHTML; let arr = str.split(' '); fitme.innerHTML = arr[0]; if (origH > fitme.offsetHeight) { //the line is split fitme.innerHTML = str; //iterate the width to find the smallest that will keep the original height for (w = 55; w <= 100; w+=5) { fitme.style.maxWidth = w + '%'; if (origH >= fitme.offsetHeight) { break; } } } fitme.innerHTML = str; } window.onresize = resize; resize();
 .fitmecontainer { margin: 0 auto; width: 100%; }.fitme { font-size: 70px; text-align: center; max-width: 100%; margin: 0 auto; padding: 0; }
 <div class="fitmecontainer"> <div class="fitme">This text is sometimes on more than one line</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM